I now use this amazingly short and simple code from Member RConner7:
##ADD AT TOP OF CODE##
def CloseAllCondition = SecondstillTime(1558) == 0;
def EOD = if SecondsTillTime(1558) == 0 and SecondsFromTime(1558) == 0 then 1 else 0;
## ADD TOWARDS BOTTOM WITH OTHER AddOrder calls##
AddOrder(OrderType.Sell_TO_CLOSE, EOD or closeAllCondition, price = close, tickcolor = Color.ORANGE, arrowcolor = Color.ORANGE, name = "SELL EOD");
AddOrder(OrderType.BUY_TO_CLOSE, EOD or closeAllCondition, price = close, tickcolor = Color.ORANGE, arrowcolor = Color.ORANGE, name = "BUY EOD");
I stumbled across this thread again while considering using conditional orders for EOD so I don't have to be awake at almost 5am in my local time to close day trades that haven't stopped out or reached their targets. I noticed this question was never answered and I believe I can provide a better solution.Hey! Just wanted to add that I’m trying this code in one of my strategies and if doesnt seem to be triggering for the “show report” or plot the sell order on the chart... so not sure if you have any tips why it wouldn’t fire.
I am backtesting against a 15 minute chart so my understanding is that if the buy to open fires multiples times per day or multiple days, each individual EOD would chose a 0 position target right?
Thanks!
Chad
def EOD = if SecondsTillTime(1558) == 0 and SecondsFromTime(1558) == 0 then 1 else 0;
def isIntraday = if GetAggregationPeriod() < AggregationPeriod.DAY then 1 else 0;
def EOD = RegularTradingEnd(GetYYYYMMDD()) - GetTime() <= GetAggregationPeriod() - 1;
input barsFromEnd = 2;
def EOD = RegularTradingEnd(GetYYYYMMDD()) - GetTime() <= GetAggregationPeriod() * barsFromEnd - 1;
input barsFromEnd = 1;
def EOD = RegularTradingEnd(GetYYYYMMDD()) - GetTime() <= GetAggregationPeriod() * barsFromEnd - 1;
AddOrder(OrderType.SELL_TO_CLOSE, EOD[-1], close[-1], 0, Color.ORANGE, Color.ORANGE, "EOD LX");
AddOrder(OrderType.BUY_TO_CLOSE, EOD[-1], close[-1], 0, Color.ORANGE, Color.ORANGE, "EOD SX");
Amazingly simple and effective code Slippage! Works great (I am using 1 minute charts).I stumbled across this thread again while considering using conditional orders for EOD so I don't have to be awake at almost 5am in my local time to close day trades that haven't stopped out or reached their targets. I noticed this question was never answered and I believe I can provide a better solution.
I used some ideas from this thread when I needed an EOD exit for a strategy some months ago. The example code:
only works on 1 minute charts. The start time of the last bar of the day is different on each timeframe and we need the time to be earlier than the start of that bar.Ruby:def EOD = if SecondsTillTime(1558) == 0 and SecondsFromTime(1558) == 0 then 1 else 0;
At the time I played with this I ended up using an if else block to set the correct time for the few timeframes I cared about. However, a better solution is possible. Typically in enumerations programmers use a zero-based index, or sometimes, a hexadecimal index which allows a single value to represent multiple list items selected. However, I recently noticed in the AggregationPeriod enumeration the TOS programmers used the number of milliseconds represented by each timeframe as its index. This means we can do time math based on AggregationPeriod.
Please forgive any typos or errors in my code blocks. I'm just typing this stuff here.
For instance, and this one would work even if it was a zero-based incremented index, you can use less than and greater than with timeframes, not only equals.
Ruby:def isIntraday = if GetAggregationPeriod() < AggregationPeriod.DAY then 1 else 0;
For the end of day exit, we can do this:
Ruby:def EOD = RegularTradingEnd(GetYYYYMMDD()) - GetTime() <= GetAggregationPeriod() - 1;
Remember that the value returned by GetAggregationPeriod() is the number of milliseconds duration of the period so we can multiply if we want to end at the second last bar instead of the last.
Ruby:input barsFromEnd = 2; def EOD = RegularTradingEnd(GetYYYYMMDD()) - GetTime() <= GetAggregationPeriod() * barsFromEnd - 1;
We still need the look-ahead -1 index.
If you're looking for a solution to copy/paste, this is the code block you want.
Ruby:input barsFromEnd = 1; def EOD = RegularTradingEnd(GetYYYYMMDD()) - GetTime() <= GetAggregationPeriod() * barsFromEnd - 1; AddOrder(OrderType.SELL_TO_CLOSE, EOD[-1], close[-1], 0, Color.ORANGE, Color.ORANGE, "EOD LX"); AddOrder(OrderType.BUY_TO_CLOSE, EOD[-1], close[-1], 0, Color.ORANGE, Color.ORANGE, "EOD SX");
So if I want to only limit buy_to_open orders on the 1 min chart to less then 12 pm I just need to calculate the bars from 12 pm to close and enter a barsfromend into the buy_to_open code correct?I stumbled across this thread again while considering using conditional orders for EOD so I don't have to be awake at almost 5am in my local time to close day trades that haven't stopped out or reached their targets. I noticed this question was never answered and I believe I can provide a better solution.
I used some ideas from this thread when I needed an EOD exit for a strategy some months ago. The example code:
only works on 1 minute charts. The start time of the last bar of the day is different on each timeframe and we need the time to be earlier than the start of that bar.Ruby:def EOD = if SecondsTillTime(1558) == 0 and SecondsFromTime(1558) == 0 then 1 else 0;
At the time I played with this I ended up using an if else block to set the correct time for the few timeframes I cared about. However, a better solution is possible. Typically in enumerations programmers use a zero-based index, or sometimes, a hexadecimal index which allows a single value to represent multiple list items selected. However, I recently noticed in the AggregationPeriod enumeration the TOS programmers used the number of milliseconds represented by each timeframe as its index. This means we can do time math based on AggregationPeriod.
Please forgive any typos or errors in my code blocks. I'm just typing this stuff here.
For instance, and this one would work even if it was a zero-based incremented index, you can use less than and greater than with timeframes, not only equals.
Ruby:def isIntraday = if GetAggregationPeriod() < AggregationPeriod.DAY then 1 else 0;
For the end of day exit, we can do this:
Ruby:def EOD = RegularTradingEnd(GetYYYYMMDD()) - GetTime() <= GetAggregationPeriod() - 1;
Remember that the value returned by GetAggregationPeriod() is the number of milliseconds duration of the period so we can multiply if we want to end at the second last bar instead of the last.
Ruby:input barsFromEnd = 2; def EOD = RegularTradingEnd(GetYYYYMMDD()) - GetTime() <= GetAggregationPeriod() * barsFromEnd - 1;
We still need the look-ahead -1 index.
If you're looking for a solution to copy/paste, this is the code block you want.
Ruby:input barsFromEnd = 1; def EOD = RegularTradingEnd(GetYYYYMMDD()) - GetTime() <= GetAggregationPeriod() * barsFromEnd - 1; AddOrder(OrderType.SELL_TO_CLOSE, EOD[-1], close[-1], 0, Color.ORANGE, Color.ORANGE, "EOD LX"); AddOrder(OrderType.BUY_TO_CLOSE, EOD[-1], close[-1], 0, Color.ORANGE, Color.ORANGE, "EOD SX");
So if I want to only limit buy_to_open orders on the 1 min chart to less then 12 pm I just need to calculate the bars from 12 pm to close and enter a barsfromend into the buy_to_open code correct?
def trigger = [whatever] and SecondsTillTime(1200) > 0;
AddOrder(OrderType.BUY_TO_OPEN, trigger);
I tried adding this to the script, however it does not seem to open a buy order. Is it possible to send the script to you directly?For that case, I wouldn't approach it that way. Instead, I would add a new time-based condition to whatever triggers a buy order. For instance:
Ruby:def trigger = [whatever] and SecondsTillTime(1200) > 0; AddOrder(OrderType.BUY_TO_OPEN, trigger);
Well I am an ***** had to change the settings to stop the old orders because my global settings were for only 1 trade.For that case, I wouldn't approach it that way. Instead, I would add a new time-based condition to whatever triggers a buy order. For instance:
Ruby:def trigger = [whatever] and SecondsTillTime(1200) > 0; AddOrder(OrderType.BUY_TO_OPEN, trigger);
Well I am an ***** had to change the settings to stop the old orders because my global settings were for only 1 trade.
Thanks!
Join useThinkScript to post your question to a community of 21,000+ developers and traders.
Thread starter | Similar threads | Forum | Replies | Date |
---|---|---|---|---|
T | Close position based on Stop Loss and Elapsed Time (or number of candles) | Questions | 3 | |
N | 1st 5 minute candle open and close | Questions | 2 | |
Close above the high of lowest low | Questions | 1 | ||
P | calculate weekdays stats based on open vs close | Questions | 1 | |
T | MTF Last Month's High, Low, Close | Questions | 0 |
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.