lowest open of the last 3 up bars or down bars

thetahunter

New member
I am trying to write some thinkscript code that returns the lowest open of three bars. where it gets tricky (for me at least) is I want it to return the lowest open price of the last 3 bars, and all 3 bars should be the same type, meaning the last 3 up bars or down bars. I only want it to take into consideration if the bars are going in the same direction.

So in total I want to disregard the current actively trading bar. Then count back the last 3 up bars (close > open), and then return the lowest open of those 3 bars. The lowest open could come from any of the 3 bars I just want to know what it is. Obviously the code should be dynamic and adjust as like colored bars are added the lowest open is moved or stays the same.

If it is no sweat to reverse the situation. Using the last 3 down bars (close < open) and returning the highest open in that situation.

This seems simple I just cant seem to pull it off. I will include some screenshots to help illustrate my ask.

Thanks in advance. Longtime lurker. first question. Sorry if it has been brought up in past I searched and could not find it.

15827[/ATTACH]']
hl03vbq.png
15829[/ATTACH]']
sa0mHXD.png
15831[/ATTACH]']
6F7TN6w.png
 

Attachments

  • hl03vbq.png
    hl03vbq.png
    14.9 KB · Views: 103
  • 6F7TN6w.png
    6F7TN6w.png
    20.5 KB · Views: 103
  • sa0mHXD.png
    sa0mHXD.png
    17.4 KB · Views: 173
  • sa0mHXD.png
    sa0mHXD.png
    17.4 KB · Views: 103
  • hl03vbq.png
    hl03vbq.png
    14.9 KB · Views: 182
Last edited by a moderator:
Solution
Try this for the low open of three up bars.

Ruby:
def LO3 =
    absvalue(Fold Index = 1 to Max(1,BarNumber())
    with Data = 0 while Data >= 0 do
    if GetValue(close,index) > GetValue(open,index)
    then (if Data + 1 == 4 then -(Fold i = 1 to Max(1,Index)
    with D = Double.POSITIVE_INFINITY do
    if GetValue(close,i) > GetValue(open,i) and
    GetValue(open,i) < d then GetValue(open,i)
    else d) else Data + 1) else Data)
;
plot x = LO3;
x.setpaintingStrategy(paintingStrategy.HORIZONTAL);
I am trying to write some thinkscript code that returns the lowest open of three bars. where it gets tricky (for me at least) is I want it to return the lowest open price of the last 3 bars, and all 3 bars should be the same type, meaning the last 3 up bars or down bars. I only want it to take into consideration if the bars are going in the same direction.

So in total I want to disregard the current actively trading bar. Then count back the last 3 up bars (close > open), and then return the lowest open of those 3 bars. The lowest open could come from any of the 3 bars I just want to know what it is. Obviously the code should be dynamic and adjust as like colored bars are added the lowest open is moved or stays the same.

If it is no sweat to reverse the situation. Using the last 3 down bars (close < open) and returning the highest open in that situation.

This seems simple I just cant seem to pull it off. I will include some screenshots to help illustrate my ask.

Thanks in advance. Longtime lurker. first question. Sorry if it has been brought up in past I searched and could not find it.

https%3A//i.imgur.com/hl03vbq.png[/img]']
hl03vbq.png

https%3A//i.imgur.com/sa0mHXD.png[/img]']
sa0mHXD.png

https%3A//i.imgur.com/6F7TN6w.png[/img]']
6F7TN6w.png

This should help. It counts the close>=open and close<open, takes the highestall of each and subtracts 3 from those and starts comparing opens until the highestall count less 1.

Capture.jpg
Code:
def x  = if !IsNaN(close) and close >= open then x[1] + 1 else x[1];
def xx = HighestAll(x);

def lowopen = if x == xx - 3
              then open
              else if x <= xx - 1 and open < lowopen[1]
              then open 
              else lowopen[1];

AddLabel(1, "C>O - LO3: " + lowopen, Color.LIGHT_GREEN);

def y  = if !IsNaN(close) and close < open then y[1] + 1 else y[1];
def yy = HighestAll(y);
def highopen = if y == yy - 3
               then open
               else if y <= yy - 1 and open > highopen[1]
               then open
               else highopen[1];
AddLabel(1, "C<O - HO3 " + highopen, Color.LIGHT_RED);

#Testing
input test = no;
AddLabel(test, (xx - 3) + " " + (xx - 1), Color.GREEN);
AddLabel(test, (yy - 3) + " " + (yy - 1), Color.LIGHT_RED);
plot xxx = if !test then Double.NaN else x;
xxx.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
plot xxo = if !test then Double.NaN else lowopen;
plot yyy = if !test then Double.NaN else y;
yyy.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
plot yyo = if !test then Double.NaN else  highopen;
 

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

This should help. It counts the close>=open and close<open, takes the highestall of each and subtracts 3 from those and starts comparing opens until the highestall count less 1.

First of all thanks for the reply. I have been running your script on my charts this morning. It seems to be hit or miss. Sometimes it shows correct and other times it does not. I am trying to pay attention to see what is causing the issue. Just through observation it seems it is having trouble when a bar is completed and a new one starts. Sometimes it is registering the completed bar as an up bar when it is actually a down bar. Then it causes everything else to be off. Then sometimes it registers the first bar correctly and it all works perfectly. It also seems to have an issue if there is a significant time gap between the bars. There was an instance this morning where there were 10 straight down bars on 2 min chart and the "C>0 label" was returning the open of a down bars instead of going back 11 bars to find that 3rd up bar. I dont know if I am explaining any of this well enough. I will try to capture some stuff with screen shots if I get some time and can catch it. Thanks for the reply and thanks for the script it is so much closer then what I was trying to do.

Here are some screen shots to hopefully help illustrate where my limited vocabulary may have failed me.

15846[/ATTACH]']
YbR7REV.png
15848[/ATTACH]']
eEFzwMe.png


15850[/ATTACH]']
eh1EQ8i.png


And one more.

15852[/ATTACH]']
gbSgEvW.png
15854[/ATTACH]']
NbWJTX2.png
 

Attachments

  • eEFzwMe.png
    eEFzwMe.png
    86.3 KB · Views: 104
  • eh1EQ8i.png
    eh1EQ8i.png
    125 KB · Views: 106
  • gbSgEvW.png
    gbSgEvW.png
    81.3 KB · Views: 102
  • gbSgEvW.png
    gbSgEvW.png
    81.3 KB · Views: 172
  • NbWJTX2.png
    NbWJTX2.png
    81.3 KB · Views: 100
  • YbR7REV.png
    YbR7REV.png
    82.3 KB · Views: 170
  • YbR7REV.png
    YbR7REV.png
    82.3 KB · Views: 102
Last edited by a moderator:
Try this for the low open of three up bars.

Ruby:
def LO3 =
    absvalue(Fold Index = 1 to Max(1,BarNumber())
    with Data = 0 while Data >= 0 do
    if GetValue(close,index) > GetValue(open,index)
    then (if Data + 1 == 4 then -(Fold i = 1 to Max(1,Index)
    with D = Double.POSITIVE_INFINITY do
    if GetValue(close,i) > GetValue(open,i) and
    GetValue(open,i) < d then GetValue(open,i)
    else d) else Data + 1) else Data)
;
plot x = LO3;
x.setpaintingStrategy(paintingStrategy.HORIZONTAL);
 
Solution
Try this for the low open of three up bars.

Ruby:
def LO3 =
    absvalue(Fold Index = 1 to Max(1,BarNumber())
    with Data = 0 while Data >= 0 do
    if GetValue(close,index) > GetValue(open,index)
    then (if Data + 1 == 4 then -(Fold i = 1 to Max(1,Index)
    with D = Double.POSITIVE_INFINITY do
    if GetValue(close,i) > GetValue(open,i) and
    GetValue(open,i) < d then GetValue(open,i)
    else d) else Data + 1) else Data)
;
plot x = LO3;
x.setpaintingStrategy(paintingStrategy.HORIZONTAL);


Ran the code for several hours and it appears to be working perfectly. Thanks a lot. I have been stuck on this for a while, and judging by your code i was never going to figure it out. Again thanks. It is brilliant.

Any chance you can get it to go the other direction? High open for last three down bars. I assume you just reverse some of the code, but honestly I really dont know what any of that means.

Regardless Thanks for the hard work.
 
This should be the flip side. I just edited it here on the forum though, I haven't tested it.

I put an x in front of all the variables in case you need to use both in the same script.

Code:
def HO3 =
    absvalue(Fold xIndex = 1 to Max(1,BarNumber())
    with xData = 0 while xData >= 0 do
    if GetValue(close,xindex) < GetValue(open,xindex)
    then (if xData + 1 == 4 then -(Fold xi = 1 to Max(1,xIndex)
    with xD = Double.NEGATIVE_INFINITY do
    if GetValue(close,xi) < GetValue(open,xi) and
    GetValue(open,xi) > xD then GetValue(open,xi)
    else xD) else xData + 1) else xData)
;
plot xx = HO3;
xx.setpaintingStrategy(paintingStrategy.HORIZONTAL);
 
This should be the flip side. I just edited it here on the forum though, I haven't tested it.

I put an x in front of all the variables in case you need to use both in the same script.

Code:
def HO3 =
    absvalue(Fold xIndex = 1 to Max(1,BarNumber())
    with xData = 0 while xData >= 0 do
    if GetValue(close,xindex) < GetValue(open,xindex)
    then (if xData + 1 == 4 then -(Fold xi = 1 to Max(1,xIndex)
    with xD = Double.NEGATIVE_INFINITY do
    if GetValue(close,xi) < GetValue(open,xi) and
    GetValue(open,xi) > xD then GetValue(open,xi)
    else xD) else xData + 1) else xData)
;
plot xx = HO3;
xx.setpaintingStrategy(paintingStrategy.HORIZONTAL);

Works perfectly. Thanks again for all the hard work. I really appreciate it.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
374 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