Code on to cancel orders at session end before after hours

Questions about MultiCharts and user contributed studies.
Ming80
Posts: 24
Joined: 18 Jun 2012
Has thanked: 20 times
Been thanked: 1 time

Code on to cancel orders at session end before after hours

Postby Ming80 » 14 Apr 2014

Hi guys,

This isn't so much about MC but more of a coding issue which I was hoping to seek some help here. My strategy enters a stop order just before the close but I would like to cancel the stop order if it is not filled before the close and prevent it from being executed in the after hours.

1. In this specific case, my chart settings for Eurex symbols set the session end at 1730 exchange time.
2. I enter a stop order at 1715 just before the close (e.g 1 bar interval of 15 mins).
3. My chart stops displaying data after 1730 but the order is still held at the broker end (IB) until 2200hrs (post hours continue immediately after 1730 until 2200).

I would have thought with the code below it would cancel orders at the session close but it still remains held possibly due that my chart has already stopped. I was wondering if you guys have any suggestions to a quick workaround as to what I could do to cancel the orders without altering the chart settings. Thanks!

Code: Select all

var: EntryPeriod(0);

EntryPeriod = CalcTime(SessionEndTime(0,1), -1 * BarInterval); {Enters order before close}

{Order Entry}
If Time = EntryPeriod and Time <= SessionEndTime(0,1) {Enters order before close but does not remove order after hours}
then SellShort next bar at OpenD(0) + 1 point Stop;

{Error Check}
if LastBarOnChart then begin
ClearPrintLog;
print(
"Entry Period"," ",EntryPeriod,newline,
"Session Close"," ",SessionEndTime(0,1),newline,
"");
End;

User avatar
furytrader
Posts: 354
Joined: 30 Jul 2010
Location: Chicago, IL
Has thanked: 155 times
Been thanked: 217 times

Re: Code on to cancel orders at session end before after hou

Postby furytrader » 15 Apr 2014

Before proposing a solution, I have one question for you:

Are you using intra-bar order generation?

Ming80
Posts: 24
Joined: 18 Jun 2012
Has thanked: 20 times
Been thanked: 1 time

Re: Code on to cancel orders at session end before after hou

Postby Ming80 » 15 Apr 2014

Hi fury, thanks for the reply. I do not use IOG.

User avatar
furytrader
Posts: 354
Joined: 30 Jul 2010
Location: Chicago, IL
Has thanked: 155 times
Been thanked: 217 times

Re: Code on to cancel orders at session end before after hou

Postby furytrader » 15 Apr 2014

Unfortunately, the solution I was going to propose requires you to use IOG. The reason is that, with IOG on, you could work your order up until the last minute (let's say) of the last day-session bar and then take it out.

Getting back to the original description of your problem, why does your chart stop displaying data at 1730 while your broker holds the order until 2200? Is that due to your broker's data feed or does it depend on how your chart is setup?

Ming80
Posts: 24
Joined: 18 Jun 2012
Has thanked: 20 times
Been thanked: 1 time

Re: Code on to cancel orders at session end before after hou

Postby Ming80 » 15 Apr 2014

Thanks anyway. I've set my chart session end to match the cash hours although the market continues to trade continuously until much later where only then the order is purged.

User avatar
Andrew MultiCharts
Posts: 1587
Joined: 11 Oct 2011
Has thanked: 931 times
Been thanked: 559 times

Re: Code on to cancel orders at session end before after hou

Postby Andrew MultiCharts » 16 Apr 2014

Hello Ming80,

What you are trying to achieve (placing and cancelling orders after session is over according to session settings) con be done only if:
  1. IntrabarOrderGeneration = True,
  2. RecalcLastBarAfter
  3. AllowSendOrdersAlways = true (will be available MultiCharts9.0)

Ming80
Posts: 24
Joined: 18 Jun 2012
Has thanked: 20 times
Been thanked: 1 time

Re: Code on to cancel orders at session end before after hou

Postby Ming80 » 16 Apr 2014

Thanks Andrew,

For the RecalcLastBarAfter option, assuming if I followed the example and omitted the print statement blank, would my orders be cancelled if I have already qualified the time window for the order to be traded? New ticks however would continue trading but my chart has already stopped due to the session end time settings.

Alternatively, because it continues to the afterhour session immediately, I think I might just create a 2nd session but not sure how it would affect standard functions such as HighD etc.

Code: Select all

if (LastBarOnChart_s = True) then begin
RecalcLastBarAfter(5);
end;

User avatar
Andrew MultiCharts
Posts: 1587
Joined: 11 Oct 2011
Has thanked: 931 times
Been thanked: 559 times

Re: Code on to cancel orders at session end before after hou

Postby Andrew MultiCharts » 16 Apr 2014

In MC 9.0 it should work if the code looks like:

Code: Select all

[IntrabarOrderGeneration = True]
[AllowSendOrdersAlways = true]
if (LastBarOnChart_s = True) then begin
RecalcLastBarAfter(5);
//here the conditions for the order generation are not satisfied, so the order should be cancelled
end;


Return to “MultiCharts”