# Add this into the condition?

#### Trigun1127

##### Member
This isnt plotting right. Im trying to a get a dot to plot when 3 bull bars or 3 bears bars occur and there is a gap (bull open>close[1]) or (bear open<close[1] anywhere in those 3 bars.
Code:
def legup    = Sum(open < close, 3) == 3 and open [0] > close[1] and close > open and close[1] > open[1];
def legdn    = Sum(open > close, 3) == 3 and open [0] < close[1] and close < open and close[1] < open[1];

plot LegupStart = if !legup and !legup[-1] and legup[-2] then low else Double.NaN;
LegupStart.SetPaintingStrategy(PaintingStrategy.POINTS);
LegupStart.SetLineWeight(5);
LegupStart .SetDefaultColor(Color.WHITE);

plot LegdnStart = if !legdn and !legdn[-1] and legdn[-2] then high else Double.NaN;
LegdnStart.SetPaintingStrategy(PaintingStrategy.POINTS);
LegdnStart.SetLineWeight(5);
LegdnStart .SetDefaultColor(Color.YELLOW);

Solution
You've got the >'s and <'s flipped, try this:

def legup = Sum(Close > Open,3) == 3 and Sum(Open > Close[1],2);
def legdn = Sum(Close < Open,3) == 3 and Sum(Open < Close[1],2);

plot LegupStart = if !legup and !legup[-1] and legup[-2] then low else Double.NaN;
LegupStart.SetPaintingStrategy(PaintingStrategy.POINTS);
LegupStart.SetLineWeight(5);
LegupStart .SetDefaultColor(Color.WHITE);

plot LegdnStart = if !legdn and !legdn[-1] and legdn[-2] then high else Double.NaN;
LegdnStart.SetPaintingStrategy(PaintingStrategy.POINTS);
LegdnStart.SetLineWeight(5);
LegdnStart .SetDefaultColor(Color.YELLOW);

That would go something like this

Def Direction = If LegUp then 1 else if LegDown then -1 else Direction[1];

If Direction == 1 then "Up" else if Direction == -1 then "Down" else "No Legs",
If Direction == 1 then Color.Green else if Direction == -1 then Color.Red else Color.Yellow
)

That would go something like this

Def Direction = If LegUp then 1 else if LegDown then -1 else Direction[1];

If Direction == 1 then "Up" else if Direction == -1 then "Down" else "No Legs",
If Direction == 1 then Color.Green else if Direction == -1 then Color.Red else Color.Yellow
)
Oh very nice thank you. Im trying to make the labels MTF whats wrong here. Im guessing im doing the aggregation period incorrectly.
def data15m = AggregationPeriod.FIFTEEN_MIN;
def LegUp15m = LegUp(data15m);
def LegDn15m = LegDn(data15m);
def Direction15m = If LegUp15m then 1 else if LegDn15m then -1 else Direction[1];
If Direction15m == 1 then "Up" else if Direction15m == -1 then "Down" else "No Legs",
If Direction15m == 1 then Color.Green else if Direction15m == -1 then Color.Red else Color.Yellow,
"15m"
);

def data1h = AggregationPeriod.ONE_HOUR;
def LegUp1h = LegUp(data1h);
def LegDn1h = LegDn(data1h);
def Direction1h = If LegUp1h then 1 else if LegDn1h then -1 else Direction[1];
If Direction1h == 1 then "Up" else if Direction1h == -1 then "Down" else "No Legs",
If Direction1h == 1 then Color.Green else if Direction1h == -1 then Color.Red else Color.Yellow,
"1h"
);

User defined variables do not have a period parameter or accept aggregation constants. The secondary aggregation must be referenced from the basic OHLC price fundamentals. Just for example:

def Per = AggregationPeriod.ONE_HOUR;
def legup = Sum(Close(Period = Per) > Open(Period = Per),3) == 3 and Sum(Open(Period = Per) > Close(Period = Per)[1],2);

User defined variables do not have a period parameter or accept aggregation constants. The secondary aggregation must be referenced from the basic OHLC price fundamentals. Just for example:

def Per = AggregationPeriod.ONE_HOUR;
def legup = Sum(Close(Period = Per) > Open(Period = Per),3) == 3 and Sum(Open(Period = Per) > Close(Period = Per)[1],2);
I dont think im understanding. Is this on the right track?

def data15m = AggregationPeriod.FIFTEEN_MIN;
def o15m = open(data15m);
def h15m = high(data15m);
def l15m = low(data15m);
def c15m = close(data15m);

def LegUp15m = LegUp(o15m, h15m, l15m, c15m);
def LegDn15m = LegDn(o15m, h15m, l15m, c15m);
def Direction15m = If LegUp15m then 1 else if LegDn15m then -1 else Direction[1];
If Direction15m == 1 then "Up" else if Direction15m == -1 then "Down" else "No Legs",
If Direction15m == 1 then Color.Green else if Direction15m == -1 then Color.Red else Color.Yellow,
"15m"
);

def data1h = AggregationPeriod.ONE_HOUR;
def o1h = open(data1h);
def h1h = high(data1h);
def l1h = low(data1h);
def c1h = close(data1h);

def LegUp1h = LegUp(o1h, h1h, l1h, c1h);
def LegDn1h = LegDn(o1h, h1h, l1h, c1h);
def Direction1h = If LegUp1h then 1 else if LegDn1h then -1 else Direction[1];
If Direction1h == 1 then "Up" else if Direction1h == -1 then "Down" else "No Legs",
If Direction1h == 1 then Color.Green else if Direction1h == -1 then Color.Red else Color.Yellow,
"1h"
);

LegUp isn't a function, it is a user defined variable.

The period parameter must be set from within each fundamental function that contributes to your LegUp or LegDown variables.

Hey @Joshua I was going thru the original code trying change it to get a boolean indicator any time there are 3 bear or bull bars in a row (5 minute chart) but i was unsuccessful. Know how to get that? i think it should be simple. Thanks

Hey @Joshua I was going thru the original code trying change it to get a boolean indicator any time there are 3 bear or bull bars in a row (5 minute chart) but i was unsuccessful. Know how to get that? i think it should be simple. Thanks
I mean should just be this

Def legup =sum(close>open,3)==3
Def legdn = sun(close<open,3)==3

plot LegupStart = if !legup and !legup[-1] and legup[-2] then low else Double.NaN;
LegupStart.SetPaintingStrategy(PaintingStrategy.POINTS);
LegupStart.SetLineWeight(5);
LegupStart .SetDefaultColor(Color.WHITE);

plot LegdnStart = if !legdn and !legdn[-1] and legdn[-2] then high else Double.NaN;
LegdnStart.SetPaintingStrategy(PaintingStrategy.POINTS);
LegdnStart.SetLineWeight(5);
LegdnStart .SetDefaultColor(Color.YELLOW);

I mean should just be this

Def legup =sum(close>open,3)==3
Def legdn = sun(close<open,3)==3

plot LegupStart = if !legup and !legup[-1] and legup[-2] then low else Double.NaN;
LegupStart.SetPaintingStrategy(PaintingStrategy.POINTS);
LegupStart.SetLineWeight(5);
LegupStart .SetDefaultColor(Color.WHITE);

plot LegdnStart = if !legdn and !legdn[-1] and legdn[-2] then high else Double.NaN;
LegdnStart.SetPaintingStrategy(PaintingStrategy.POINTS);
LegdnStart.SetLineWeight(5);
LegdnStart .SetDefaultColor(Color.YELLOW);
This seems to work now. Corrected legup and legdn for typo and missing semicolons.

Code:
Def legup =sum(close>open,3)==3;
Def legdn = sum(close<open,3)==3;

plot LegupStart = if !legup and !legup[-1] and legup[-2] then low else Double.NaN;
LegupStart.SetPaintingStrategy(PaintingStrategy.POINTS);
LegupStart.SetLineWeight(5);
LegupStart .SetDefaultColor(Color.WHITE);

plot LegdnStart = if !legdn and !legdn[-1] and legdn[-2] then high else Double.NaN;
LegdnStart.SetPaintingStrategy(PaintingStrategy.POINTS);
LegdnStart.SetLineWeight(5);
LegdnStart .SetDefaultColor(Color.YELLOW);

I mean should just be this

Def legup =sum(close>open,3)==3
Def legdn = sun(close<open,3)==3

plot LegupStart = if !legup and !legup[-1] and legup[-2] then low else Double.NaN;
LegupStart.SetPaintingStrategy(PaintingStrategy.POINTS);
LegupStart.SetLineWeight(5);
LegupStart .SetDefaultColor(Color.WHITE);

plot LegdnStart = if !legdn and !legdn[-1] and legdn[-2] then high else Double.NaN;
LegdnStart.SetPaintingStrategy(PaintingStrategy.POINTS);
LegdnStart.SetLineWeight(5);
LegdnStart .SetDefaultColor(Color.YELLOW);
Hi-i've been following your tweaks to this code; could you please post the finished code that works the way you like it ? Thank You

Ok great its working now and added a sound alert as well. one other thing, i was looking to add boolean indicator on the third consecutive bar as opposed to the first bar in the 3 bar series. I tried changing Legupstart to a few other words but nothing worked. What can i change in the code to get that indicator on the 3rd bar?

Ok great its working now and added a sound alert as well. one other thing, i was looking to add boolean indicator on the third consecutive bar as opposed to the first bar in the 3 bar series. I tried changing Legupstart to a few other words but nothing worked. What can i change in the code to get that indicator on the 3rd bar?

This seems to do what you want with an arrow or a bubble.

Ruby:
def legup = Sum(close > open, 3) == 3;
def legdn = Sum(close < open, 3) == 3;

plot LegupStart = if !legup and !legup[-1] and legup[-2] then low else Double.NaN;
LegupStart.SetPaintingStrategy(PaintingStrategy.POINTS);
LegupStart.SetLineWeight(5);
LegupStart .SetDefaultColor(Color.WHITE);

plot LegdnStart = if !legdn and !legdn[-1] and legdn[-2] then high else Double.NaN;
LegdnStart.SetPaintingStrategy(PaintingStrategy.POINTS);
LegdnStart.SetLineWeight(5);
LegdnStart .SetDefaultColor(Color.YELLOW);

input showarrow = yes;

def up = if up[1] == 0 and close > open then 1 else if close > open then up[1] + 1 else if close < open then 0 else up[1];
plot xup = up == 3;
xup.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
xup.SetDefaultColor(Color.YELLOW);
xup.SetLineWeight(3);
xup.SetHiding(!showarrow);

def dn = if dn[1] == 0 and close < open then 1 else if close < open then dn[1] + 1 else if close > open then 0 else dn[1];
plot xdn = dn == 3;
xdn.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
xdn.SetDefaultColor(Color.YELLOW);
xdn.SetLineWeight(3);
xdn.SetHiding(!showarrow);

input showbubbles = yes;

AddChartBubble(showbubbles and up == 3, low * .9995, up, Color.YELLOW, no);
AddChartBubble(showbubbles and dn == 3, high * 1.0005, dn, Color.YELLOW, yes);

LegUp isn't a function, it is a user defined variable.

The period parameter must be set from within each fundamental function that contributes to your LegUp or LegDown variables.
Hey josh its been a while. I apologize but I still dont think i understand. Could you give me an example of how this would work?

Post the code you've got so far for LegUp, along with any code above it that it is dependent on.

Post the code you've got so far for LegUp, along with any code above it that it is dependent on.
Happy New Years! I'm currently out of state with family far away from my computer. Could you give me an example on how to this with just the bear bones legup and legdn? Trying to work on it when I have nothing to do here.

Code:
Input MTF = AggregationPeriod.DAY;
Plot mLegUp = sum(close(period = MTF) > open(period = MTF), 3) == 3;
mLegUp.setpaintingStrategy(paintingStrategy.BOOLEAN_ARROW_UP);

By the way, what have we been building this whole time, would you mind explaining what this is all supposed to do?

Code:
Input MTF = AggregationPeriod.DAY;
Plot mLegUp = sum(close(period = MTF) > open(period = MTF), 3) == 3;
mLegUp.setpaintingStrategy(paintingStrategy.BOOLEAN_ARROW_UP);

By the way, what have we been building this whole time, would you mind explaining what this is all supposed to do?
Im backtesting many variables to see what has the best chance of a 2nd legup in the direction im trading.

Code:
Input MTF = AggregationPeriod.DAY;
Plot mLegUp = sum(close(period = MTF) > open(period = MTF), 3) == 3;
mLegUp.setpaintingStrategy(paintingStrategy.BOOLEAN_ARROW_UP);

By the way, what have we been building this whole time, would you mind explaining what this is all supposed to do?
So mlegdn it would just be
Plot mlegdn = sum(close(period = MTF) < open (period = mtf),3) ==3;
mlegdn.setpaintingStrategy(paintingStrategy.boolean_ARROW_DOWN);

If i wanted labels for the MTF it would be
Def Direction = If mLegUp then 1 else if mLegup then -1 else Direction[1];
If Direction == 1 then "15Up" else if Direction == -1 then "15Down" else "No Legs",
If Direction == 1 then Color.Green else if Direction == -1 then Color.Red else Color.Yellow
);

Yup, just need to fix this line.

Def Direction = If mLegUp then 1 else if mLegup then -1 else Direction[1];

ok I wont know till I get home. But wrote this. Does this makes sense for adding more?

#15min Label
Input MTF15 = AggregationPeriod.fifteen_min;
Plot mLegUp15 = sum(close(period = MTF15) > open(period = MTF15), 3) == 3;
Plot mlegdn15 = sum(close(period = MTF15) < open (period = mtf15),3) ==3;

Def Direction15 = If mLegUp15 then 1 else if mLegup15 then -1 else Direction15[1];
If Direction15 == 1 then "15Up" else if Direction15 == -1 then "15Down" else "No Legs",
If Direction15 == 1 then Color.Green else if Direction15 == -1 then Color.Red else Color.Yellow
);

#1hour Label
Input MTF1hr = AggregationPeriod.hour;
Plot mLegUp1hr = sum(close(period = MTF1hr) > open(period = MTF1hr), 3) == 3;
Plot mlegdn1hr = sum(close(period = MTF1hr) < open (period = mtf1hr),3) ==3;

Def Direction1hr = If mLegUp1hr then 1 else if mLegup1hr then -1 else Direction1hr[1];
If Direction1hr == 1 then "1hrUp" else if Direction1hr == -1 then "1hrDn" else "No Legs",
If Direction1hr == 1 then Color.Green else if Direction1hr == -1 then Color.Red else Color.Yellow
);
Another question i have is about "no legs". I don't know what context where it would say "no legs" If its checking for the last thing to be true then it should always be legup or legdn correct?

Basically, yes, but not necessarily. There is no guarantee that either condition will be met, especially as the script begins loading in from the left edge of the chart. The else statement is also compulsory in thinkscript regardless.

87k+ Posts
349 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?