Preventing orders from executing / emulating when session is closed?

Questions about MultiCharts and user contributed studies.
Phenydor
Posts: 8
Joined: 19 Jul 2016
Has thanked: 1 time

Preventing orders from executing / emulating when session is closed?

Postby Phenydor » 08 Jun 2023

I would like orders to not get executed when the session on my chart is not active (after last bar on session and before first bar of next session) even though the market is open.

I have read a suggestion on another post to add a time filter and not send any orders on the last bar of the session. I do want orders to get executed as the first bar of the next session is forming though. As far as I know, the only way to do that is to indeed send orders on the last bar of the prior suggestion which prevents me from using a time filter.

In TS, orders sent on the last bar of the session are in a "held" state until the next bar of the session begins.

Any ideas? I thought using order emulation (instead of sending orders directly to the market) would work ... however, MC seems to emulate the order execution when the session is closed (but market is open) so this approach does not work.

User avatar
Polly MultiCharts
Posts: 203
Joined: 20 Jul 2022
Has thanked: 1 time
Been thanked: 56 times

Re: Preventing orders from executing / emulating when session is closed?

Postby Polly MultiCharts » 13 Jun 2023

Hi Phenydor,

If the order is sent to the broker it is managed by the broker and MultiCharts cannot influence the order execution.
So the only reliable solution here is not to send an order before the session closes and before it opens.

There are two possible options on how to achieve that:

1) You can reduce session time in your session template settings (QuoteManager -> Tools -> Session Template) so that the session ends earlier than the real session in 1-2 bars of the basic resolution.
Also you can make sure your script doesn’t generate orders on the penultimate bar of the session (in that case the orders already generated will be canceled). And then make sure your script does generate ‘Next bar’ orders on the last bar of the session (then they will be placed on the Open bar of the next session).

2) You can use these keywords in your script:
  • [AllowSendOrdersAlways = true]
  • [IntrabarOrderGeneration = true]
  • recalclastbarafter(1)
and analyze the time for sending orders in the code.
Please note that IntrabarOrderGeneration can significantly increase the CPU load and it is also not supported in Portfolio Trader.

Phenydor
Posts: 8
Joined: 19 Jul 2016
Has thanked: 1 time

Re: Preventing orders from executing / emulating when session is closed?

Postby Phenydor » 15 Jun 2023

Thanks for your insight and creative solution Polly! Before reading your post, I ran some tests on what would happen to my strategy performances if I accepted no trades on the first bar of the session and surprisingly across the board, performance either maintained or slightly improved. So now, I am happy with being able to cancel all the orders between session end and not having them back on till after the first bar of the new session completes.

To do this, would you be willing to comment on if my approach below would work. I would like to avoid using IntrabarOrderGeneration if I can (to keep my backtest and live executions in sync):
a) New variable: orders_allowed = t <> sessionendtime(0,1);
b) Wrap all orders, setstoploss, setprofittarget with the orders_allowed condition
c) [AllowSendOrdersAlways=true]
d) if not orders_allowed then recalclastbarafter(1);

Thoughts?

Also, I did briefly try this (not till the session close so I didn't properly test it :P) but I noticed that having "if orders_allowed then setstoploss" vs just "setstoploss" had the side effect of sending the stop loss order only after the entry bar completed (as opposed to right after the entry). orders_allowed was true for both teh entry bar and the bar preceding it. Any thoughts on this would be greatly appreciated as well.

Thanks!

Phenydor
Posts: 8
Joined: 19 Jul 2016
Has thanked: 1 time

Re: Preventing orders from executing / emulating when session is closed?

Postby Phenydor » 15 Jun 2023

Adding another comment as well. If this matters, I am emulating all my orders in MC (so it sends market orders when the price for my stop is reached). So resting orders aren't being sent to my broker (interactive).

Phenydor
Posts: 8
Joined: 19 Jul 2016
Has thanked: 1 time

Re: Preventing orders from executing / emulating when session is closed?

Postby Phenydor » 16 Jun 2023

Thanks for your insight and creative solution Polly! Before reading your post, I ran some tests on what would happen to my strategy performances if I accepted no trades on the first bar of the session and surprisingly across the board, performance either maintained or slightly improved. So now, I am happy with being able to cancel all the orders between session end and not having them back on till after the first bar of the new session completes.

To do this, would you be willing to comment on if my approach below would work. I would like to avoid using IntrabarOrderGeneration if I can (to keep my backtest and live executions in sync):
a) New variable: orders_allowed = t <> sessionendtime(0,1);
b) Wrap all orders, setstoploss, setprofittarget with the orders_allowed condition
c) [AllowSendOrdersAlways=true]
d) if not orders_allowed then recalclastbarafter(1);

Thoughts?

Also, I did briefly try this (not till the session close so I didn't properly test it :P) but I noticed that having "if orders_allowed then setstoploss" vs just "setstoploss" had the side effect of sending the stop loss order only after the entry bar completed (as opposed to right after the entry). orders_allowed was true for both teh entry bar and the bar preceding it. Any thoughts on this would be greatly appreciated as well.

Thanks!
hmm it seems from testing it out today the solution I proposed above does not work.

I'm getting the impression that the only way to cancel orders on session close is to use IOG, which could limit strategy development due to its impact on backtesting speed and impact strategies already developed without IOG. If this is true (I'm really hoping its not!), how do I adapt a strategy backtested without IOG for live execution? Would it involve wrapping every order with BarStatus(1) = 2, unless it's the last bar on the session? I'm concerned about the potential CPU load increase since all strategies (I run up to 50) would have to compute on every tick. To avoid this, should I wrap all my calculations with BarStatus(1) = 2? I hope there's an alternative to using IOG.

As usual, thanks for your time!

User avatar
Polly MultiCharts
Posts: 203
Joined: 20 Jul 2022
Has thanked: 1 time
Been thanked: 56 times

Re: Preventing orders from executing / emulating when session is closed?

Postby Polly MultiCharts » 20 Jun 2023

Phenydor,

I’m afraid the idea you’ve suggested will not work without IOG. But I have forwarded your suggestion to the dev team so that they can evaluate the possibility of adding this feature in one of the future releases.
If the condition for ‘stoploss’ becomes ‘False’ by the beginning of the entry bar due to ‘recalclastbarafter’, then the behavior corresponds to the disabled IOG option.
You can check if the condition is ‘True’ if you add traces in your code for example Print (barstatus, orders_allowed).

Phenydor
Posts: 8
Joined: 19 Jul 2016
Has thanked: 1 time

Re: Preventing orders from executing / emulating when session is closed?

Postby Phenydor » 25 Jun 2023

Hi Polly ... thanks again for your help! I ended up going into the IOG rabbit hole and it wasn't as hard as I thought to convert my non-IOG strategy into an IOG variant that produced the same backtested results as the non-IOG variant while retaining the live flexibility of IOG. With that and the steps you suggested (IOG on, allowsendbarsalways, recalcafterlastbar enabled when appropriate), I was able to achieve all of my criteria (this also fixed my stop loss issue).


Cheers


Return to “MultiCharts”