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:
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.
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.
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);
@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.
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);
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);
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.
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 displayI 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.
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);
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?
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);
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?
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.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
##### 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”);
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”);
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);
Thanks again. Per your comment, I'm using separate orders for that (BUY_TO_CLOSE / SELL_TO_CLOSE).Try this, but it appears that with auto buy/sell, it will reenter in the opposite direction when lasttrade is activated.
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())
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
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.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
Join useThinkScript to post your question to a community of 21,000+ developers and traders.
Thread starter | Similar threads | Forum | Replies | Date |
---|---|---|---|---|
M | Learn To Write ThinkScript? | Questions | 5 | |
N | Anyone willing to write a script for this?... The Fab 4 | Questions | 1 | |
R | Write scanner for Ichimoku cloud | Questions | 2 | |
Y | Beginners Strategy Q: How to write a Parabolic SAR SELL_TO_CLOSE ? | Questions | 1 | |
L | Write the study values to a file | Questions | 1 |
Start a new thread and receive assistance from our community.
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.
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.