How would you write "skip the first 5 opening bars (or 5 bars after the open)"?

rottentrade

Member
That is, skip the first 5 bars before making a trade. I'm thinking it's something like "if IsNaN(close[5]) then begin calculating" but not sure.
 
Last edited:
Solution
That is, skip the first 5 bars before making a trade. I'm thinking it's something like "if IsNaN(close[5]) then begin calculating" but not sure.

See if this helps. I made it so you can see what the code is doing, but it can be shortened to use in your code.

Capture.jpg
Ruby:
def rth = Between(GetTime(), RegularTradingStart(GetYYYYMMDD()), RegularTradingEnd(GetYYYYMMDD()));

def x   = if GetTime() crosses above RegularTradingStart(GetYYYYMMDD())
          then 1
          else x[1] + 1;

plot notradezone = if rth and Between(x, 1, 5) then 1 else 0;
notradezone.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);

plot tradezone   = rth and !notradezone;
tradezone.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
That is, skip the first 5 bars before making a trade. I'm thinking it's something like "if IsNaN(close[5]) then begin calculating" but not sure.

See if this helps. I made it so you can see what the code is doing, but it can be shortened to use in your code.

Capture.jpg
Ruby:
def rth = Between(GetTime(), RegularTradingStart(GetYYYYMMDD()), RegularTradingEnd(GetYYYYMMDD()));

def x   = if GetTime() crosses above RegularTradingStart(GetYYYYMMDD())
          then 1
          else x[1] + 1;

plot notradezone = if rth and Between(x, 1, 5) then 1 else 0;
notradezone.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);

plot tradezone   = rth and !notradezone;
tradezone.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
 
Solution
@SleepyZ

I would also like to use this for ETH session but I don't see a Date and Time function for afterhours. Is there a way around the RegularTradingStart(GetYYYYMMDD()) ?

BTW this code prevents me from entering trades during ETH or pre/after market. I just want to skip the first 5 bars when the market opens for RTH or reopens for ETH but otherwise trade nonstop day and night.

Thx in advance.
 
Last edited:
@SleepyZ

I would also like to use this for ETH session but I don't see a Date and Time function for afterhours. Is there a way around the RegularTradingStart(GetYYYYMMDD()) ?

BTW this code prevents me from entering trades during ETH or pre/after market. I just want to skip the first 5 bars when the market opens for RTH or reopens for ETH but otherwise trade nonstop day and night.

Thx in advance.

Try this

Capture.jpg
Ruby:
input start = 0930;
input end   = 1600;
def rth = secondsfromTime(start)>=0 and secondstillTime(end)>0;

def x   = if secondsfromTime(start) == 0
          then 1
          else x[1] + 1;

plot notradezone = if rth and Between(x, 1, 5) then 1 else 0;
#notradezone.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);

plot tradezone   = !rth or (rth and !notradezone);
#tradezone.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
assignpriceColor(if tradezone then color.green else color.white);
 
Thanks, much appreciate your help. There's one problem though. I would like to skip the first 5 bars at the start of both the RTH and the ETH.

I tried modifying your code with no success. Here's the code that I used:

Ruby:
input startRTH = 0930;
input endRTH   = 1700;
input startETH = 1800;
input endETH   = 0930;

def rth = secondsfromTime(startRTH)>=0 and secondstillTime(endRTH)>0;
def eth = secondsfromTime(startETH)>=0 and secondstillTime(endETH)>0;

def x   = if secondsfromTime(startRTH or startETH) == 0
          then 1
          else x[1] + 1;

plot notradezone = if (rth or eth) and Between(x, 1, 5) then 1 else 0;
notradezone.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);

plot tradezone   = (rth or eth) and !notradezone;
tradezone.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
assignpriceColor(if tradezone then color.green else color.white);


Also while tinkering with the inputs, I've noticed a bug when the start time is lower than the end time. For example, futures ETH begins at 1800 EST and runs until 0930 the next day. But when I change the input settings to 1800 (start) and 0930 (end), it doesn't show anything on the chart.
 
Thanks, much appreciate your help. There's one problem though. I would like to skip the first 5 bars at the start of both the RTH and the ETH.

I tried modifying your code with no success. Here's the code that I used:

Ruby:
input startRTH = 0930;
input endRTH   = 1700;
input startETH = 1800;
input endETH   = 0930;

def rth = secondsfromTime(startRTH)>=0 and secondstillTime(endRTH)>0;
def eth = secondsfromTime(startETH)>=0 and secondstillTime(endETH)>0;

def x   = if secondsfromTime(startRTH or startETH) == 0
          then 1
          else x[1] + 1;

plot notradezone = if (rth or eth) and Between(x, 1, 5) then 1 else 0;
notradezone.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);

plot tradezone   = (rth or eth) and !notradezone;
tradezone.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
assignpriceColor(if tradezone then color.green else color.white);


Also while tinkering with the inputs, I've noticed a bug when the start time is lower than the end time. For example, futures ETH begins at 1800 EST and runs until 0930 the next day. But when I change the input settings to 1800 (start) and 0930 (end), it doesn't show anything on the chart.

I do not see a bug in the my script. My understanding is you wanted to be able to trade every bar except the requested exclusion of 5 bars.

The tradezone in my script is displayed as the green bars and the white are the 5 excluded bars. There should be no need to do what you are doing in the code you posted, unless I am misunderstanding something.
 
I do not see a bug in the my script. My understanding is you wanted to be able to trade every bar except the requested exclusion of 5 bars.

The tradezone in my script is displayed as the green bars and the white are the 5 excluded bars. There should be no need to do what you are doing in the code you posted, unless I am misunderstanding something.
Sorry for the confusion. It's all good as is, but I would like to skip the first five bars for the ETH session as well (see the chart below). The circled areas (left is RTH and right is ETH) are the places where I want to exclude from trading. But as you can see, the 5 bars on the right all display
green bars.

bMMcqkJ.jpg



Regarding the bug, take a look at these pics. In the first pic, I changed the time to where the chart is shaded for ETH. But you can see in the second pic that all candles are included in trading, even the candles that were excluded before.

xnWZwC6.jpg


ao6s1r0.jpg

and obviously the first 5 candles for the ETH are not excluded as well.

But when I change the time from "low to high" like 930 to 1600 it works fine. It's only when I go from "high to low" it doesn't work.

DPcafrJ.jpg
 
Last edited:
Okay, here's an update.

Ruby:
def rth = Between(GetTime(), RegularTradingStart(GetYYYYMMDD()), RegularTradingEnd(GetYYYYMMDD()));

### This second line seems more intuitive to me than the third line since ETH is technically "between RTH end and RTH start", but it doesn't work.
#def eth = Between(GetTime(), RegularTradingEnd(GetYYYYMMDD()), RegularTradingStart(GetYYYYMMDD()));

def eth = GetTime() < RegularTradingStart(GetYYYYMMDD()) or GetTime() > RegularTradingEnd(GetYYYYMMDD());

def x   = if GetTime() crosses above RegularTradingStart(GetYYYYMMDD())
          then 1
          else x[1] + 1;

def y   = if GetTime() crosses above RegularTradingEnd(GetYYYYMMDD())
          then 1
          else y[1] + 1;

plot notradezone = if rth and Between(x, 1, 5) then 1 else if eth and Between(y, 1, 5) then 1 else 0;
notradezone.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);

plot tradezone   = (rth or eth) and !notradezone;
tradezone.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
assignpriceColor(if tradezone then color.green else color.white);

It seems to shows up correctly on the chart, however it's not visible in the first two session (2nd pic). Any suggestion?

RPDZYEg.jpg


g1sAyTq.jpg
 
Okay, here's an update.

Ruby:
def rth = Between(GetTime(), RegularTradingStart(GetYYYYMMDD()), RegularTradingEnd(GetYYYYMMDD()));

### This second line seems more intuitive to me than the third line since ETH is technically "between RTH end and RTH start", but it doesn't work.
#def eth = Between(GetTime(), RegularTradingEnd(GetYYYYMMDD()), RegularTradingStart(GetYYYYMMDD()));

def eth = GetTime() < RegularTradingStart(GetYYYYMMDD()) or GetTime() > RegularTradingEnd(GetYYYYMMDD());

def x   = if GetTime() crosses above RegularTradingStart(GetYYYYMMDD())
          then 1
          else x[1] + 1;

def y   = if GetTime() crosses above RegularTradingEnd(GetYYYYMMDD())
          then 1
          else y[1] + 1;

plot notradezone = if rth and Between(x, 1, 5) then 1 else if eth and Between(y, 1, 5) then 1 else 0;
notradezone.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);

plot tradezone   = (rth or eth) and !notradezone;
tradezone.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
assignpriceColor(if tradezone then color.green else color.white);

It seems to shows up correctly on the chart, however it's not visible in the first two session (2nd pic). Any suggestion?

RPDZYEg.jpg


g1sAyTq.jpg

Thank you for taking the time to thoroughly explain what you wanted.

TOS is not very friendly with time.

The following may work on most symbols to create a 5 bar no tradezone at the beginning of regular trading hrs and extended trading hrs. The 'between(bars,1,5)' was added to override the exclusion of those bars from trading, however, if you find that conflicts with your needs, you probably should remove that part of the code.

Capture.jpg
Ruby:
def bars = barnumber();

def x    = CompoundValue(1,
           if GetTime() crosses above RegularTradingStart(GetYYYYMMDD())
           then 1
           else x[1] + 1, 1);

def y    = CompoundValue(1,
           if GetTime() crosses above RegularTradingEnd(GetYYYYMMDD())
           then 1
           else y[1] + 1, 1);

plot notradezone = if Between(x, 1, 5) or
                      Between(y, 1, 5)
                   then 1
                   else 0;

plot tradezone   = between(bars,1,5) or !notradezone;

AssignPriceColor(if tradezone then Color.green else Color.white);
 
Last edited:
Thanks for the new code. Everything lined up perfectly. :)

Anyway, if you don't mind, can you tell me why CompoundValue used? It seems you can count the bars without it. What does CompoundValue do exactly?
 
Thanks for the new code. Everything lined up perfectly. :)

Anyway, if you don't mind, can you tell me why CompoundValue used? It seems you can count the bars without it. What does CompoundValue do exactly?

When dealing with recursive data, like x and y in the script, it helps to initialize the data, provide a value of 1 in this case. It helps prevent N/A values initially. It may not always appear to be necessary, but it helps prevent unreliable data. I especially use it with recursives involving the price: low.

Description​

Calculates a compound value according to following rule: if a bar number is greater than length then the visible data value is returned, otherwise the historical data value is returned. This function is used to initialize studies with recursion.
 
think of compoundvalue as another way to write an if then. it decides what to do, depending on the bar number used as the first parameter.
https://usethinkscript.com/threads/quarterly-value-area-tpo.9390/#post-85195
Thanks for the link. I would need time to absorb all that. Frankly, I'm spending too much time on learning thinkscript than trading, and I don't even know if this will all pay off in the end.

But before I go, can you explain the use cases when compoundvalue would be more appropriate to use?
 
Also either @SleepyZ or @halcyonguy

I have a little trouble with exiting out of the trade at the last bar (or actually 1 bar from the close since TOS makes the trade at the open of the next bar). This is the code that I came up with, but that last trade does not show up on the chart.

Ruby:
##### ENTRY/EXIT #####
 
def buy = close > close[1];
def sell = close < close[1];

def lastTrade = GetTime() == RegularTradingEnd(GetYYYYMMDD()) - 1; ## -1 is used to deduct 1 bar from the close

## note that all my trades are normally BUY_AUTO or SELL_AUTO, meaning they only reverse from long to short
AddOrder(OrderType.BUY_AUTO, Buy && !lastTrade or lastTrade, close[0], 1, tickcolor = GetColor(1), arrowcolor = Color.UPTICK, name = “Buy”);
AddOrder(OrderType.SELL_AUTO, Sell && !lastTrade or lastTrade, close[0], 1, tickcolor = GetColor(2), arrowcolor = Color.DOWNTICK, name = “Sell”);
 
Last edited:
Also either @SleepyZ or @halcyonguy

I have a little trouble with exiting out of the trade at the last bar (or actually 1 bar from the close since TOS makes the trade at the open of the next bar). This is the code that I came up with, but that last trade does not show up on the chart.

Ruby:
##### ENTRY/EXIT #####
 
def buy = close > close[1];
def sell = close < close[1];

def lastTrade = GetTime() == RegularTradingEnd(GetYYYYMMDD()) - 1; ## -1 is used to deduct 1 bar from the close

## note that all my trades are normally BUY_AUTO or SELL_AUTO, meaning they only reverse from long to short
AddOrder(OrderType.BUY_AUTO, Buy && !lastTrade or lastTrade, close[0], 1, tickcolor = GetColor(1), arrowcolor = Color.UPTICK, name = “Buy”);
AddOrder(OrderType.SELL_AUTO, Sell && !lastTrade or lastTrade, close[0], 1, tickcolor = GetColor(2), arrowcolor = Color.DOWNTICK, name = “Sell”);

Try this, but it appears that with auto buy/sell, it will reenter in the opposite direction when lasttrade is activated.

Ruby:
def buy = close > close[1];
def sell = close < close[1];

#def lastTrade = GetTime() == RegularTradingEnd(GetYYYYMMDD()) - 1; ## -1 is used to deduct 1 bar from the close
def lastTrade = if GetTime() crosses above RegularTradingEnd(GetYYYYMMDD()) then 1 else 0;
## note that all my trades are normally BUY_AUTO or SELL_AUTO, meaning they only reverse from long to short
AddOrder(OrderType.BUY_AUTO, buy && !lastTrade or lastTrade[-2], close[0], 1, tickcolor = GetColor(1), arrowcolor = Color.UPTICK, name = “Buy”);
AddOrder(OrderType.SELL_AUTO, sell && !lastTrade or lastTrade[-2], close[0], 1, tickcolor = GetColor(2), arrowcolor = Color.DOWNTICK, name = “Sell”);

plot x = lastTrade[-2];
x.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
 
Try this, but it appears that with auto buy/sell, it will reenter in the opposite direction when lasttrade is activated.
Thanks again. Per your comment, I'm using separate orders for that (BUY_TO_CLOSE / SELL_TO_CLOSE).

But man, why is everything so easy for you? :) I haven't even thought of using the offset next to "lastTrade".
 
Here's one important find I like to share.

Use this for stocks
>> GetTime() crosses above RegularTradingEnd(GetYYYYMMDD())​


Use this for futures
>> GetTime() crosses below RegularTradingEnd(GetYYYYMMDD())​


PTZZhtj.jpg
 
Here's one important find I like to share.

Use this for stocks
>> GetTime() crosses above RegularTradingEnd(GetYYYYMMDD())​


Use this for futures
>> GetTime() crosses below RegularTradingEnd(GetYYYYMMDD())​


PTZZhtj.jpg

I believe the gettime() above 'end' works for stocks @1600 and futures (eg: @1615 /ES)

Here is something Mobius did awhille ago that may help

Code:
# GetTime() and Regular Trading Start / End Functions
# Mobius
# 4.27.2018

# TOS could write 4 functions or 2 and allow a different cross
# case to determine which to plot
#
# Here are the 3 RegularTradingStart() functions as you'd expect.
# Whether you use cross, cross above or cross below, it's all
# the same
#
# RegularTradingEnd() can be the RTH end or Extended Hours End
# depending on the cross selection. Cross by itself will get both

def RTHstart  = GetTime() crosses above RegularTradingStart(getYYYYMMDD());
def RTHEnd    = GetTime() crosses above RegularTradingEnd(getYYYYMMDD());
def ExtEnd    = GetTime() crosses below RegularTradingEnd(getYYYYMMDD());
plot x=rthstart;
x.setpaintingStrategy(paintingStrategy.BOOLEAN_ARROW_UP);
addVerticalLine(RTHStart, "RTH Hours Begin", color.cyan, curve.firm);
addVerticalLine(RTHEnd, "RTH Hours End", color.cyan, curve.firm);
addVerticalLine(ExtEnd, "Extended Hours End", color.cyan, curve.firm);
# End
 
I believe the gettime() above 'end' works for stocks @1600 and futures (eg: @1615 /ES)

Here is something Mobius did awhille ago that may help
It used to be the case that CME would shut down from 3:15 CST to 3:30 then resume for 30 minutes only to shut down again from 4:00 to 5:00. But that has all changed to 4:00. So TOS should make the change on their platform.

Mobius' code is great but the problem (TOS fault) is that you still must toggle on the "show Extended-Hours Trading session" from the options menu in order to see those lines.

BTW thanks again for all your help.
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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