Order to exit at the market close


Category:
0
0

Good afternoon,

Hope everyone is staying safe!  I’ve been trying to build a new AddOrder so that I’m completely cash at the end of the day if my indicator is “in” open orders that need to be closed, no matter the side.  I’ve included the current code I have for my conditions, however I’m trying to figure out how I can include this time order so that at 4:30pm ET, everything closes:

AddOrder(OrderType.BUY_TO_OPEN, TrendFlipBullSig, open[-1], PositionSize, Color.GREEN, Color.WHITE,”CTS BTO”);
AddOrder(OrderType.SELL_TO_CLOSE, high >= TrendFlipBullGain, TrendFlipBullGain, PositionSize, Color.RED, Color.WHITE, “CTS STC”);
AddOrder(OrderType.SELL_TO_CLOSE, low <= TrendFlipBullStop, TrendFlipBullStop, PositionSize, Color.RED, Color.WHITE, “CTS STC”);

I have the following inputs for other components of the conditions above:

Input TimeStart = 0930;
Input TimeEnd = 1600;

Thank you for the help!!  – Zach

Marked as spam
Posted by (Questions: 2, Answers: 1)
Asked on April 18, 2020 11:11 am
87 views
0
Private answer

Before we try to solve this we need to understand how time stamps on the bars of the chart are marked. This changes depending on the time frame the chart is set to.

The time stamp for each bar is set to the opening time. So on a five minute chart the last bar during the regular trading hours is stamped with a time of 15:55. For a 15 min time frame that last bar of regular trading hours is stamped with a time of 15:45.

So we need to provide a user input and that user input must be adjusted to match whatever time stamp is on the last bar where you want your strategy to post an exit. You can automate this so the time is adjusted to match your chart time frame. But it requires a very complex piece of code that is far beyond the scope of anything I can provide here free of charge.

input hardExitTime = 1545;
input tradeSize = 100;
def hardExitBar = SecondsTillTime(hardExitTime) == 0;
AddOrder(OrderType.SELL_TO_CLOSE, hardExitBar[-1], close[-1], tradeSize, Color.RED, Color.WHITE, “Hard Exit”);

This piece of code can be used as a completely self contained chart strategy. Save it as a new custom chart strategy and add it to any chart where you want your strategy to be flat on the bar with the time stamp you have chosen through the user input.

Marked as spam
Posted by (Questions: 37, Answers: 4087)
Answered on April 18, 2020 1:27 pm
0
Thank you guys!
( at April 19, 2020 12:00 pm)