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);

cXhTsds.png
I am not really sure if any of your code is correct or not. Other than actual syntax errors, like when the line turns red in the editor, I have no way of knowing if your code doing the thing that you actually want it to do. Let me give you an example.

plot ReqStrngCloLong = sum(close > open, 3)>=1 and sum(close > low + (.85 * (high-low)),3)>=1;

A close has to be greater than an open, at least once out of three bars.
And, a close has to be in the upper %85 of a bar at least once too.
But these don't have to happen on the same bar.

I think you probably mean this:

Sum(Close > Open AND Close > Low + (0.85 * (High-Low)) , 3) >= 1;

This means both conditions have to happen on the same bar, at least once out of three bars.
But I am not sure.
Your code wont "crash" of course, but I don't know its correct relative to your intentions.

The code is one thing, the development process is another. Here is what I think you should do:

First, pick a bias, either long or short, and get rid of the rest, at least for now.
Also get rid of everything where you're adjusting the plot back to the start of the leg, etc.
Forget about dots and numbers, again, only do this in the final stage.
Most of this is cosmetic, do this type of thing later.

Change the study to a lower study; either just move it down, or declare lower;

Plot each of the conditions one at a time, the line will be 1 for Yes and 0 for No.

Compare this plot to the price data to ensure its behaving the way you would like it to.
Check it thoroughly against several samples and charts.

If its correct, change the Plot back to Def, and then plot the next condition, until you've checked them all.

If you have trouble with any individual line or condition, we can work on them one at a time.

Once the base code is in working order, then we go back and add all the extras.
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Yes this was very helpful.
let make sure this is correct
Def Mchannelup = sum(low > low[1],3)==3;
Def Mchanneldn = sum (High < high[1],3)==3;

Some of legups and legdowns were getting hung up on the fact the low of the first bar of the sequence was not greater then low of prior bar out of the sequence. What im trying to say is out of the 3 bars the latter 2 need to have lows greater then the prior. so 1st bar doesnt matter as long as Close>open , 2nd bar Low>low[1], 3rdbar low>low[1].
I changed it to this but im not sure that solves my issue
Def Mchannelup = sum(low > low[1],3)>=2;
"The low is greater then prior bar in 3 bars where the condition must be true twice."
The problem is i need it make sure it understands it must be consecutively correct for the 2 latter bars.
 
Last edited:
In this case it should be:

Sum(Low > Low[1],2) == 2

Low > Low[1] AND Low[1] > Low[2]

A lot of these can be easily written in the second form.

The reason you would want to use sum is for inputs.

Input Length = 3;

#checks against one bar outside of the sequence.
def x = Sum(Low > Low[1],Length) == Length;

#only checks between the bars inside of the sequence.
def y = Sum(Low > Low[1],Length - 1) == Length - 1;

Making this determination is only required when using an offset inside of sum.
In this case Low[1]
 
In this case it should be:

Sum(Low > Low[1],2) == 2

Low > Low[1] AND Low[1] > Low[2]

A lot of these can be easily written in the second form.

The reason you would want to use sum is for inputs.

Input Length = 3;

#checks against one bar outside of the sequence.
def x = Sum(Low > Low[1],Length) == Length;

#only checks between the bars inside of the sequence.
def y = Sum(Low > Low[1],Length - 1) == Length - 1;

Making this determination is only required when using an offset inside of sum.
In this case Low[1]
Very Helpful! I'll continue to work on this with this new bit of information.

1. How would you code bar uniformity? I'm needing it to look at a 3 bar sequence of Close>open where it looks at the size each bar and makes sure that they are within a % range of each other like 75% of the tick Height of the bars.
2. How could I print a different value then the set one above each bar that just tells me how many conditions are true? Value could be any text.

3. would you recommend a better way of doing this code?

Def BullCloseTail = sum(close > open and close >= low + (.94 * (high-low)),3)>=2;
Def BearCloseTail = sum(close < open and close <= low + (.94 * (high-low)),3)>=2;
Def NoBullOpenTail = sum(open==low,3)>=2;
Def NoBearOpenTail = sum(open==High,3)>=2;

BullCloseTail is the top wick on a green bar and bearclosetail would be bottom wick on a red bar. I'm trying to make it where conditions is set when the there is either no wick or a very small one.
NoBullOpenTail is that there should be no wick on the bottom of a green bar vice versa for bear bars.

So here for bear closetail it says that the condition is true, my observing shows this to be false. The tail has to be small or not there at all.
 
Last edited:
For very small wicks on the top of green candles, I would probably do something like this:

Def X = Close > Open and Close > High - ((High - Low) * 0.1);
Plot Y = X;
Y.setpaintingStrategy(paintingStrategy.BOOLEAN_ARROW_DOWN);

It just marks them with an arrow so you can check it out. This will naturally also include candles with no top wick. I assume that what you're going for is a very strong close on the candle. I wouldn't necessarily separate tiny wicks and no wicks into two separate conditions, rather I would just chalk the difference up to minor jitters.

Are you planning on using this intraday, or just daily?

Uniformity is more tricky, I don't have the time to work it out at the moment, maybe tomorrow.

For #2, I am not exactly sure what you're asking. Try just plotting ConditionsMet plainly.
I think this might be what you mean:

plot CondCount = ConditionsMetLong;
CondCount.setpaintingStrategy(paintingStrategy.VALUES_ABOVE);

You could also plot any of the values, but without anything after them.

Plot XYZ = Sum(Close>Open,3);
XYZ.setpaintingStrategy(paintingStrategy.VALUES_ABOVE);

instead of
Def XYZ = Sum(Close>Open,3) == 3;

This will show you the actual value of the sum function.
 
For very small wicks on the top of green candles, I would probably do something like this:

Def X = Close > Open and Close > High - ((High - Low) * 0.1);
Plot Y = X;
Y.setpaintingStrategy(paintingStrategy.BOOLEAN_ARROW_DOWN);

It just marks them with an arrow so you can check it out. This will naturally also include candles with no top wick. I assume that what you're going for is a very strong close on the candle. I wouldn't necessarily separate tiny wicks and no wicks into two separate conditions, rather I would just chalk the difference up to minor jitters.

Are you planning on using this intraday, or just daily?

Uniformity is more tricky, I don't have the time to work it out at the moment, maybe tomorrow.

For #2, I am not exactly sure what you're asking. Try just plotting ConditionsMet plainly.
I think this might be what you mean:

plot CondCount = ConditionsMetLong;
CondCount.setpaintingStrategy(paintingStrategy.VALUES_ABOVE);

You could also plot any of the values, but without anything after them.

Plot XYZ = Sum(Close>Open,3);
XYZ.setpaintingStrategy(paintingStrategy.VALUES_ABOVE);

instead of
Def XYZ = Sum(Close>Open,3) == 3;

This will show you the actual value of the sum function.
Def BullCloseTail = sum(close > open and close > high - ((high-low)*0.1),3)>=2;
Def BearCloseTail = sum(close < open and close < high - ((high-low)*0.1),3)>=2;

plot up = bullclosetail;
up.setpaintingStrategy(paintingStrategy.BOOLEAN_ARROW_up);

1. I don't think I'm incorporating the sum function into this properly because its definitely not capturing several examples of bars with no wicks or small wicks.
2. Is the bear version of this code correct? its picking up bull bars for me.
3. What does *0.1 actually refer to? How would I adjust what i think the appropriate wick length is?
4. In regards to the values thing. I actually want to have where if 4 conditions are met I could have custom text appear instead of just how many conditions are met. I'm thinking of when a certain amount of conditions are met I could display what I believe to be the probability of the setup.
 
Close < Open and Close < Low + ((high-low) * 0.1)

* 0.1 = The wick on the appropriate side has to be less than %10 of the length of the bar's range

Plots don't accept custom text unfortunately, they only accept numbers.

Your sum function is stating that at least two out of the last three bars have tiny/no wicks. This will still apply on the 3rd bar, even if the 3rd bar has a large wick, assuming that the 1st and 2nd out of the 3 have tiny/no wick. You may also need to adjust the percentage to your liking. This might help, with this you can put in a plain number for percent, and it will decimalize it for you:

Input Percent = 10;
def Pct = Percent * 0.01;

Then the conditions would be:

Close > Open and Close > High - ((High - Low) * Pct)
Close < Open and Close < Low + ((high-low) * Pct)
 
Close < Open and Close < Low + ((high-low) * 0.1)

* 0.1 = The wick on the appropriate side has to be less than %10 of the length of the bar's range

Plots don't accept custom text unfortunately, they only accept numbers.

Your sum function is stating that at least two out of the last three bars have tiny/no wicks. This will still apply on the 3rd bar, even if the 3rd bar has a large wick, assuming that the 1st and 2nd out of the 3 have tiny/no wick. You may also need to adjust the percentage to your liking. This might help, with this you can put in a plain number for percent, and it will decimalize it for you:

Input Percent = 10;
def Pct = Percent * 0.01;

Then the conditions would be:

Close > Open and Close > High - ((High - Low) * Pct)
Close < Open and Close < Low + ((high-low) * Pct)
Ok this works very well. How do I do the same thing but instead of the Tail at the Open of the bar? Vice Versa for Bear Bars?


I've tried this
input OpenPercentTail = 10;
def OpenPct = OpenPercentTail *0.01;

Def NoBullOpenTail = sum(close > open and close < low - ((high-low)*OpenPct),3)>=2;
Def NoBearOpenTail = sum(close < open and close >high - ((high-low)*OpenPct),3)>=2;
and
Def NoBullOpenTail = sum(open==low,3)>=2;
Def NoBearOpenTail = sum(open==High,3)>=2;

Both appear to be wrong.
I want the tail to be either 0 or very very small
 
Try this by itself, just to see how it looks, or make any adjustments.
If its right, integrate the conditions into the main code.

Code:
input Percent = 10;
def Pct = Percent * 0.01;

Def X = Close > Open and Open < Low + ((High - Low) * Pct);
Def Y = Close < Open and Open > High - ((high - low) * Pct);

Plot A = X;
A.setpaintingStrategy(paintingStrategy.BOOLEAN_ARROW_UP);
A.setdefaultColor(color.green);

Plot B = Y;
B.setpaintingStrategy(paintingStrategy.BOOLEAN_ARROW_DOWN);
B.setdefaultColor(color.red);
 
Try this by itself, just to see how it looks, or make any adjustments.
If its right, integrate the conditions into the main code.

Code:
input Percent = 10;
def Pct = Percent * 0.01;

Def X = Close > Open and Open < Low + ((High - Low) * Pct);
Def Y = Close < Open and Open > High - ((high - low) * Pct);

Plot A = X;
A.setpaintingStrategy(paintingStrategy.BOOLEAN_ARROW_UP);
A.setdefaultColor(color.green);

Plot B = Y;
B.setpaintingStrategy(paintingStrategy.BOOLEAN_ARROW_DOWN);
B.setdefaultColor(color.red);
Everything works perfectly now!
1. If you have any suggestions on how to set a condition for bar uniformity (I'm thinking tracking the tick size of 3 bars and then calculating if they are within a certain % of each other would probably do) that would be helpful. (not sure if I'm tracking body or Tail to tail yet)
2. Also is there an easy way to plot horizontal lines for measured moves? Not price lines that indefinitely go to the right maybe a line width of 2 bars?
3. Any suggestions on the best way to get started with backtesting strategies with thinkscript? I want to be at a decent level where I can start pulling data on a multitude of strategies.
 
Uniformity is easy to, do but not easy to do well. I would have to do a little experimentation. A few lines should be easy, but I would need more specific information on that. I have no idea how to back test or use strategies, now that you mention it, I don't use buy/sell indicators or strategies when I trade. It would probably only take me a few minutes to figure it out if need be. I'll take a look when I get some free time.
 
Really appreciate your time in all of this! Heres one thing I've noticed. Is there something in the code that stop it from evaluating every 3 bar sequence? This here seems to have many bars that fulfill the conditions but no print of a value above. Does it have to do with the fact that the dot plots on the first bar of the sequence? I think if one of the next 3 bars shows that the conditions are true or stronger as well it should plot on each valid bar.

 
Another thing I was interested in what how can I add other conditions to legup in regards to the moving average? For Instance If I want it to only trigger after a Certain amount of consecutive bull bars above the moving average but it cannot be before those consecutive bars. Separate Count begins after x consecutive bars.
 
This should help with uniformity.

Input Length = 3;
Input UniPct = 10;
def UPct = UniPct * 0.01;
def Range = high - low;
def Largest = GetValue(Range, GetMaxValueOffset(Range,Length));
def UniRange = Largest - (Largest * UPct);
def Uniform = Length == Fold i = 0 to Length with d = 0 do d + (GetValue(Range,i) <= Largest and GetValue(Range,i) >= Unirange);

Post the code you've got so far.
 
Hey sorry I haven't replied been so busy. Havent been able to play with the uniformity code too much. I tried to plot it on the chart but I think i was doing something wrong. Need to get it so condition is met twice in 3 close>open and 3 close<open.

Still trying to figure out this? It did fine plotting the first three bars but I believe the next three bars fulfill the conditions?
 
This will do 2 out of 3.

Input Length = 3;
Input Required = 2;
Input UniPct = 10;
def UPct = UniPct * 0.01;
def Range = high - low;
def Largest = GetValue(Range, GetMaxValueOffset(Range,Length));
def UniRange = Largest - (Largest * UPct);
def Uniform = Required <= Fold i = 0 to Length with d = 0 do d + (GetValue(Range,i) <= Largest and GetValue(Range,i) >= Unirange);

Its self contained, so do:

Uniform and Sum(Close > Open,3) == 3

Raising UniPct to a larger number makes it less strict btw.
 
Awesome I will take a look at this tomorrow morning!

edit: this seems to be working perfectly very well done

Could you also tell me how I could code x consecutive bull bars above the moving average before it’s plots?
 
Last edited:
Yeah, that's pretty simple, but its also more complicated that it might seem. Code is very explicit, there's no room for ambiguity. Which moving average? Does the close just need to be above it, or does the whole bar need to be above it? If a red bar appears, how should it be handled? Should a red bar reset the count to 0, or should it the count only be reset by crossing under the average with red bars above the average just not incrementing the count, and so on?

Slap this on a chart and tell me how it looks:

def SMA = SimpleMovingAvg(close,5);
def Above =
if close[1] < SMA and Close > SMA and close > Open then 1
else if Close > SMA and Close > Open then Above[1] + 1
else 0
;
plot x = sma;
plot y = above;
y.setpaintingStrategy(paintingStrategy.VALUES_ABOVE);
 
Everything has been working great. Is there a way that I can add a label to the top of the chart that tells me if Legup or Legdown was last true. If Legup label is green if Legdn label is red.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
279 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

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?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top