Below is the new code. But what about cancelling all other pending orders.
Pending orders are cancelled when you don't resubmit them in the code (see for instance the
buy wiki page).
This means you'll need to implement programming logic in your script that ensures that the limit orders aren't submitted after 14:45 hours.
Code: Select all
if TIME >= 2:45 then
Begin
if MarketPosition =-1 then BUYTOCOVER ("EoD Exit") All contracts Next Bar At Market;
if MarketPosition = 1 then SELL ("Eod Exit") All contracts Next Bar At Market;
End ;
I don't mean to criticise but I see a few errors:
* The `Time` keyword returns the closing time of the bar in 24-hour
HHmm format. This means that 2:45 PM is not 245 but 1445.
* The value that's returned by `Time` doesn't include a colon; you used this in a previous post also, but 2:45 isn't valid: it has to be 245. Likewise, 14:45 doesn't work; this needs to be 1445.
There's also room for improvement with this code snippet:
* This code snippet will only close the open position after a certain time; but it doesn't prevent the strategy from opening another position. From what I understand the market that you're trading is open till 15:00. This means that during 14:45 till 15:00 you can end up entering a position followed by closing it immediately afterwards. So your entry conditions also need to use time filtering.
* You might want to implement the
RecalcLastBarAfter() keyword to ensure that your script it calculated at least every n seconds. There probably will be enough trades between 14:45 and 15:00 for liquid instruments, but with instruments that don't trade that often (or instruments like options) you're quite dependent on enough trades to have the orders cancelled and the open positions closed. If you use `RecalcLastBarAfter()` you can start closing the orders (and cancelling the open orders) immediately after 14:45, which also ensures that there's enough time to execute everything properly at the broker's side.
* You might want to use
IntrabarOrderGeneration so that the strategy can submit (and cancel) orders independent from the bar's resolution. You didn't specify the chart resolution in this thread (but I might have overlooked it), but if you for example use hourly bars then on the close of trading at 15:00 the `time >= 1445` condition is true and the open position can be closed (from the code's standpoint). But at that point, the orders cannot be filled anymore (given that the market is closed). By using `IntrabarOrderGeneration` your signal can calculate even when a price bar hasn't closed, which allows the strategy to close the positions and open order sooner and that also gives enough time on the broker's side to flatten everything.