Consecutive bars

mikewest

Member
VIP
I'm trying to make a startegy that will buy when current price crosses above close of last bar after a consecutive amount of bars were lower than the previous bar if someone can help. I know how to put
def buy = low[1] < low[2] and low[2] < low[3] and low[3] < low[4] and close > close[1];
def sell = close < close; But I'm try to have the inputs so I can just change them from a selection instead of changing the code for backtesting with other studies in the same code.

input ConsecutiveBarsDown = 3;
input ConsecutiveBarsUp = 1;

def cbd = ConsecutiveBarsDown;
def cbu = ConsecutiveBarsUp;

#def Buy = cbd and cbu;
#def sell = close < close;

AddOrder(OrderType.BUY_TO_OPEN, buy, name = "Buy",tickcolor = Color.BLUE, arrowcolor = Color.BlUE);
AddOrder(OrderType.SELL_TO_ClOSE, sell,name = "Sell",tickcolor = Color.RED, arrowcolor = Color.RED);

Ex: from 4 bars ago the close < close each time until current price crossed above close of last bar.
 
Last edited by a moderator:
I'm trying to make a startegy that will buy when current price crosses above close of last bar after a consecutive amount of bars were lower than the previous bar if someone can help. I know how to put
def buy = low[1] < low[2] and low[2] < low[3] and low[3] < low[4] and close > close[1];
def sell = close < close; But I'm try to have the inputs so I can just change them from a selection instead of changing the code for backtesting with other studies in the same code.

input ConsecutiveBarsDown = 3;
input ConsecutiveBarsUp = 1;

def cbd = ConsecutiveBarsDown;
def cbu = ConsecutiveBarsUp;

#def Buy = cbd and cbu;
#def sell = close < close;

AddOrder(OrderType.BUY_TO_OPEN, buy, name = "Buy",tickcolor = Color.BLUE, arrowcolor = Color.BlUE);
AddOrder(OrderType.SELL_TO_ClOSE, sell,name = "Sell",tickcolor = Color.RED, arrowcolor = Color.RED);

Ex: from 4 bars ago the close < close each time until current price crossed above close of last bar.
Is there anyone who can help me with figuring this out?
 
@mikewest had to change your condition of close > close[1] as it produces unrealistic results for live trading.
Your sell condition of close < close is, I'm assuming supposed to be close < close[1]. This too will produce unrealistic results for live trading.
I did not change it, though.


5gymGYe.png

Ruby:
input ConsecutiveBarsDown = 3;
input ConsecutiveBarsUp = 1;

def LowerLow = low < low[1];

def CBD = fold d = 0 to ConsecutiveBarsDown with dcount = 0 do if GetValue(LowerLow,d) <> 0 then dcount + 1 else 0;
plot CBDP = if CBD == ConsecutiveBarsDown then CBD else Double.NaN;
CBDP.setPaintingStrategy(PaintingStrategy.VALUES_BELOW);

def BuyLine;
if close[1] > BuyLine[1] {
BuyLine = Double.NaN;
} else if CBD == ConsecutiveBarsDown {
BuyLine = close;
} else {
BuyLine = BuyLine[1];}

plot BuyLinePlot = BuyLine;
BuyLinePlot.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
BuyLinePlot.AssignValueColor(Color.WHITE);
 
def Buy = IsNaN(BuyLine) and !IsNaN(BuyLine[1]);
plot BP = Buy;
BP.SetpaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
BP.AssignValueColor(Color.UPTICK);
BP.SetLineWeight(3);

def sell = close < close[1];

AddOrder(OrderType.BUY_TO_OPEN, condition = Buy, price = open, tradeSize = 1, tickcolor = Color.CYAN, arrowcolor = Color.BLUE, "Buy");
AddOrder(OrderType.SELL_TO_CLOSE, condition = Sell, price = close[1], tradeSize = 1, tickcolor = Color.DARK_ORANGE, arrowcolor = Color.RED, "Sell");
 
Last edited:
Im not sure Im using the correct words to explain what Im trying to do but maybe this pic can explain better. I do appreciate @Svanoy for his time and help in trying to figure this out for me, the sell doesnt really matter because this will be part of multiple combined studies. I hate asking because some think you havent searched the forums but if you do not know the correct words to use then you have to ask. Thanks and again I greatly appreciate your help
9s3ML4t.png
 
@mikewest
Your first post specified lows not highs and price crossing above close of last bar not high of last bar.
Also, putting a buy signal before a bar closes will give you results that are impossible to achieve in live trading.
.
 
@mikewest
Your first post specified lows not highs and price crossing above close of last bar not high of last bar.
Also, putting a buy signal before a bar closes will give you results that are impossible to achieve in live trading.
.
Thank you for your help, thats why I figured the pic could say more than me trying to explain it correctly. I will work on it and see if I can change it Thanks again
 
@mikewest rewrote to use high and simplified.
Set buy price level you requested.

RSVHCil.png


Ruby:
input ConsecutiveBarsDown = 3;
input Trigger_Line = high;

def Condition = high < high[1];

def CBD = Sum(Condition,ConsecutiveBarsDown);
plot CBDP = if CBD == ConsecutiveBarsDown then CBD else Double.NaN;
CBDP.setPaintingStrategy(PaintingStrategy.VALUES_BELOW);

def BuyLine;
if close[1] > BuyLine[1] {
BuyLine = Double.NaN;
} else if CBD == ConsecutiveBarsDown {
BuyLine = Trigger_Line;
} else {
BuyLine = BuyLine[1];}

plot BuyLinePlot = BuyLine;
BuyLinePlot.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
BuyLinePlot.AssignValueColor(Color.WHITE);
 
def Buy = close>BuyLine;
plot BP = Buy;
BP.SetpaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
BP.AssignValueColor(Color.UPTICK);
BP.SetLineWeight(3);

def sell = close < close[1];

AddOrder(OrderType.BUY_TO_OPEN, condition = Buy, price = BuyLine[1], tradeSize = 1, tickcolor = Color.CYAN, arrowcolor = Color.BLUE, "Buy");
AddOrder(OrderType.SELL_TO_CLOSE, condition = Sell, price = open, tradeSize = 1, tickcolor = Color.DARK_ORANGE, arrowcolor = Color.RED, "Sell");
 
@mikewest rewrote to use high and simplified.
Set buy price level you requested.

RSVHCil.png


Ruby:
input ConsecutiveBarsDown = 3;
input Trigger_Line = high;

def Condition = high < high[1];

def CBD = Sum(Condition,ConsecutiveBarsDown);
plot CBDP = if CBD == ConsecutiveBarsDown then CBD else Double.NaN;
CBDP.setPaintingStrategy(PaintingStrategy.VALUES_BELOW);

def BuyLine;
if close[1] > BuyLine[1] {
BuyLine = Double.NaN;
} else if CBD == ConsecutiveBarsDown {
BuyLine = Trigger_Line;
} else {
BuyLine = BuyLine[1];}

plot BuyLinePlot = BuyLine;
BuyLinePlot.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
BuyLinePlot.AssignValueColor(Color.WHITE);
 
def Buy = close>BuyLine;
plot BP = Buy;
BP.SetpaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
BP.AssignValueColor(Color.UPTICK);
BP.SetLineWeight(3);

def sell = close < close[1];

AddOrder(OrderType.BUY_TO_OPEN, condition = Buy, price = BuyLine[1], tradeSize = 1, tickcolor = Color.CYAN, arrowcolor = Color.BLUE, "Buy");
AddOrder(OrderType.SELL_TO_CLOSE, condition = Sell, price = open, tradeSize = 1, tickcolor = Color.DARK_ORANGE, arrowcolor = Color.RED, "Sell");
 
@mikewest had to change your condition of close > close[1] as it produces unrealistic results for live trading.
Your sell condition of close < close is, I'm assuming supposed to be close < close[1]. This too will produce unrealistic results for live trading.
I did not change it, though.


5gymGYe.png

Ruby:
input ConsecutiveBarsDown = 3;
input ConsecutiveBarsUp = 1;

def LowerLow = low < low[1];

def CBD = fold d = 0 to ConsecutiveBarsDown with dcount = 0 do if GetValue(LowerLow,d) <> 0 then dcount + 1 else 0;
plot CBDP = if CBD == ConsecutiveBarsDown then CBD else Double.NaN;
CBDP.setPaintingStrategy(PaintingStrategy.VALUES_BELOW);

def BuyLine;
if close[1] > BuyLine[1] {
BuyLine = Double.NaN;
} else if CBD == ConsecutiveBarsDown {
BuyLine = close;
} else {
BuyLine = BuyLine[1];}

plot BuyLinePlot = BuyLine;
BuyLinePlot.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
BuyLinePlot.AssignValueColor(Color.WHITE);
 
def Buy = IsNaN(BuyLine) and !IsNaN(BuyLine[1]);
plot BP = Buy;
BP.SetpaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
BP.AssignValueColor(Color.UPTICK);
BP.SetLineWeight(3);

def sell = close < close[1];

AddOrder(OrderType.BUY_TO_OPEN, condition = Buy, price = open, tradeSize = 1, tickcolor = Color.CYAN, arrowcolor = Color.BLUE, "Buy");
AddOrder(OrderType.SELL_TO_CLOSE, condition = Sell, price = close[1], tradeSize = 1, tickcolor = Color.DARK_ORANGE, arrowcolor = Color.RED, "Sell");
Great Job with this system, thank for share it, a quick question I noticed the system is only for LONG entry, can you or someone alse add the short version, will be great. thank you in advance
 
# This is a work in progress to keep a clean chart for intraday trading on one minute chart so I can have multiple charts open on 1 screen and just see the labels if they red or green so and select the one thats green to open on main screen. I will continue to work on it with help from this group and share my progress here. Thanks to Svanoy for the coding help which I lack any experience in as I mainly copy and paste.

https://usethinkscript.com/threads/intraday-scalping-strategy-for-thinkorswim.10698/
 
Last edited by a moderator:

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

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
303 Online
Create Post

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