Help with a Price/Volume Study

PocketsofTime

New member
I'm new here and have done some basic think script coding but nothing to this extent and this is just a bit (read A LOT) over my head.

I am looking to write a study that works in any time frame (if one has to be chosen - 2M) and will put an arrow under a green candle (when it closes) if it exceeds the height of the previous red candle (no matter how far back it is) and the green candle has a volume higher than the previous volume bar.

Would also like to include the premarket and aftermarket but not essential and repaint.

Hope this is clear and thanks for any help you can provide.
 

mashume

Well-known member
VIP
Perhaps something like this:
Code:
declare upper;

def red_height = if close[1] < open[1]
                then (high[1] - low[1])
                else red_height[1];
def volume_signal = if VOLUME > VOLUME[1] then 1 else double.nan;

plot GO = if (high - low) > red_height and close > open and volume_signal == 1 then low else double.nan;

GO.SetPaintingStrategy(PaintingStrategy.ARROW_UP);

Happy Trading,
Mashume
 

PocketsofTime

New member
Mashume,

Thank you.

It seems to work perfectly, just one thing I did not think of in the beginning, eliminating the wicks of the green bar, just measuring the body.

Trying to learn so here is what I tried:

plot GO = if BodyHeight () > red_height and close > open and volume_signal == 1 then low else double.nan

I assume this will measure the body height of the current candle but not sure if this is right because how does it know if it was green.
 

mashume

Well-known member
VIP
Code:
declare upper;

def red_height = if close[1] < open[1]
                then (high[1] - low[1])
                else red_height[1];
                
def green_body = if close > open then close - open else 0;

def volume_signal = if VOLUME > VOLUME[1] then 1 else double.nan;

plot GO = if green_body > red_height and close > open and volume_signal == 1 then low else double.nan;

GO.SetPaintingStrategy(PaintingStrategy.ARROW_UP);

Maybe.
 

QUIKTDR1

Member
VIP
I added
Plot NO = if red_body >green_height and close < open and volume_signal ==-1 then high else double.nan;
NO.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);

but doesn't work any ideas what I'm doing wrong when trying to add down arrows?
 

halcyonguy

Active member
VIP
I added
Plot NO = if red_body >green_height and close < open and volume_signal ==-1 then high else double.nan;
NO.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);

but doesn't work any ideas what I'm doing wrong when trying to add down arrows?

did you define these variables in your study? they don't exist in the above studies.
red_body , green_height
 

QUIKTDR1

Member
VIP
plot NO = if green_body < red_height and close < open and volume_signal ==-1 then high else double.nan;

NO.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN); I thought that it was defined earlier
 

QUIKTDR1

Member
VIP
Doesn't work hmmm what is wrong?

declare upper;

def red_height = if close[1] < open[1]
then (high[1] - low[1])
else red_height[1];

def green_body = if close > open then close - open else 0;

def green_height = if close[1] > open[1] then (high[1] + low[1] else green_height[1];

def red_body = if close < open then close + open else 0;

def volume_signal = if VOLUME > VOLUME[1] then 1 else double.nan;

plot GO = if green_body > red_height and close > open and volume_signal == 1 then low else double.nan;

GO.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
Plot NO = if red_body >green_height and close < open and volume_signal ==-1 then high else double.nan;
NO.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);

plot NO = if green_body < red_height and close < open and volume_signal ==-1 then high else double.nan;

NO.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
 

mashume

Well-known member
VIP
Doesn't work hmmm what is wrong?

declare upper;

def red_height = if close[1] < open[1]
then (high[1] - low[1])
else red_height[1];

def green_body = if close > open then close - open else 0;

def green_height = if close[1] > open[1] then (high[1] + low[1] else green_height[1];

def red_body = if close < open then close + open else 0;

def volume_signal = if VOLUME > VOLUME[1] then 1 else double.nan;

plot GO = if green_body > red_height and close > open and volume_signal == 1 then low else double.nan;

GO.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
Plot NO = if red_body >green_height and close < open and volume_signal ==-1 then high else double.nan;
NO.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);

plot NO = if green_body < red_height and close < open and volume_signal ==-1 then high else double.nan;

NO.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
I may be missing something, my brain is not quite all there at the moment... but there doesn't appear to be anywhere that you could possibly set VOLUME_SIGNAL == -1. I see how it can be either 1 or NaN, but not -1.

You've also got NO being plotted twice... that's a no go. No puns intended.


-mashume
 

QUIKTDR1

Member
VIP
Mashume TY for your response! This still doesn't work, I thought I had to plot once for green and once for red.

I posted your first post and was trying to create a post for down arrows along with your post . Any suggestions when "your brain starts working again" will be appreciated LOL
 

halcyonguy

Active member
VIP
Mashume TY for your response! This still doesn't work, I thought I had to plot once for green and once for red.

I posted your first post and was trying to create a post for down arrows along with your post . Any suggestions when "your brain starts working again" will be appreciated LOL
i don't think you have explained what you are trying to do. you just jumped in saying you changed something and it didn't work.
please explain what you want the study to do, and the code you have right now, and we will look it over.
 

QUIKTDR1

Member
VIP
I'm sorry if I didn't make myself clear. The original code only gives signals bullish signals. I am attempting to add to code with bearish signals ( down arrows) Thank you!
 

halcyonguy

Active member
VIP
here you go, i fixed a few things.

i added bubbles to display variable values. i thought seeing how to do this will help you debug code in the future.
go to edit studies and click on the gear to the right of the study name and change
show_test_bubbles = yes to no to turn them off. or edit the code. it's at the end.
the bubble color is determined by the volume_signal variable.

==================================

looking at the code in post #11

you have the same variable , no , used twice, in 2 different plots. variables can only be used once.

no is a reserved word, can't use it as a variable.
https://tlc.thinkorswim.com/center/reference/thinkScript/Reserved-Words/no
https://tlc.thinkorswim.com/center/reference/thinkScript/Reserved-Words

i changed the 2 no variables to , no1 and no2

this line has an extra (, before high
def green_height = if close[1] > open[1] then (high[1] + low[1] else green_height[1];

no color specified for arrows. i added green for up arrors. i don't see any down arrows, so i made them white, to make sure i see them if they are plotted.
i added a size of 4 for the arrows, (can be 1-5), to make sure i see them, when they show up.


what @mashume said, volume_signal will never be equal to -1
def volume_signal = if VOLUME > VOLUME[1] then 1 else double.nan;

so your no plots will never be true and won't plot
Plot NO1 = if red_body > green_height and close < open and volume_signal == -1 then high else double.nan;

i changed the last part, from double.nan to be else -1
def volume_signal = if VOLUME > VOLUME[1] then 1 else -1;

=========================
down arrows show up now.
i didn't follow the code logic to determine what it is doing.


Ruby:
# PriceVolumez_01

# Help with a Price/Volume Study
# https://usethinkscript.com/threads/help-with-a-price-volume-study.2759/#post-69050
# post #11
# QUIKTDR1

declare upper;

input arrow_size = 4;

def red_height = if close[1] < open[1] then (high[1] - low[1])
  else red_height[1];

def green_body = if close > open then close - open else 0;

def green_height = if close[1] > open[1] then high[1] + low[1] else green_height[1];

def red_body = if close < open then close + open else 0;

#def volume_signal = if VOLUME > VOLUME[1] then 1 else double.nan;
#  change last part to be   else -1
def volume_signal = if VOLUME > VOLUME[1] then 1 else -1;

plot GO = if green_body > red_height and close > open and volume_signal == 1 then low else double.nan;
GO.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
go.setdefaultcolor(color.green);
go.setlineweight(arrow_size);

Plot NO1 = if red_body > green_height and close < open and volume_signal == -1 then high else double.nan;
NO1.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
no1.setdefaultcolor(color.white);
no1.setlineweight(arrow_size);

plot NO2 = if green_body < red_height and close < open and volume_signal ==-1 then high else double.nan;
NO2.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
no1.setdefaultcolor(color.white);
no2.setlineweight(arrow_size);


input show_test_bubbles = yes;
addchartbubble(show_test_bubbles, low * 0.997 , "G " +  green_body + "\n" + "R " + red_height + "\n" + "V " + volume_signal , if volume_signal == 1 then color.green else if volume_signal == -1 then color.red else color.gray, no);
#

qDcH5mL.jpg
 

Similar threads

Top