Problem with optimize order flow  [SOLVED]

Questions about MultiCharts and user contributed studies.
User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Problem with optimize order flow

Postby JoshM » 27 Feb 2013

Description of problem
With the optimize order flow setting turned on, both buy and sell stops are not submitted (only the buy order). With optimize order flow turned off, both buy and sell orders are submitted, but only once per bar.

Data
MultiCharts Version 8.5 Release (Build 6743)
MB Trading demo account
No errors in Order and Position Tracker log
60-minute chart

These settings:
Image
Image

Code example

Code: Select all

[IntrabarOrderGeneration = True];

Inputs:
TickOffSet(20),
PosSize(1000);

Variables:
IntraBarPersist TradeEntryTime(0),
IntraBarPersist TradeExitTime(0),
IntraBarPersist PrevMP(0),
IntraBarPersist CalcPrices(True),
IntraBarPersist LongStopPrice(0),
IntraBarPersist ShortStopPrice(0),
OneMinute(ELTimeToDateTime(1)),
TwoMinutes(ELTimeToDateTime(2)),
currentDT(0);

if (GetAppInfo(aiStrategyAuto) = 0) or (LastBarOnChart_s = False) then
#return;

currentDT = ComputerDateTime;

// Submit orders for opening a position
if (MarketPosition(0) = 0) and (currentDT < TradeExitTime + TwoMinutes) then begin

// Calculate order prices
if (CalcPrices = True) then begin
LongStopPrice = Close + (TickOffSet * (MinMove / PriceScale));
ShortStopPrice = Close - (TickOffSet * (MinMove / PriceScale));
CalcPrices = False;
end;

Buy ("EL STP") PosSize contracts next bar at LongStopPrice stop;
SellShort ("ES STP") PosSize contracts next bar at ShortStopPrice stop;

end;

// Open market positions
if (MarketPosition(0) <> 0) then begin

// Time stop
if (currentDT > TradeEntryTime + OneMinute) then begin
Sell ("XL Time") all contracts next bar at market;
BuyToCover ("XS Time") all contracts next bar at market;
end;
end;

// Workaround for trade entry time
if (GetAppInfo(aiStrategyAuto) = 1) and (MarketPosition(0) = 0) then
TradeEntryTime = ComputerDateTime;

// Reset variables
if (PrevMP <> MarketPosition(0)) or (currentDT > TradeExitTime + TwoMinutes) then begin
LongStopPrice = 0;
ShortStopPrice = 0;
CalcPrices = True;
TradeExitTime = currentDT;
end;

PrevMP = MarketPosition(0);
With optimize order flow turned ON
With the setting turned on, the following happens:

Image

In other words, only the long stop orders are submitted. If a position is opened, the long time stop is triggered after the time interval, closing the position. Subsequently a new buy stop is submitted. Short sell stop are never submitted.

With optimize order flow turned OFF
With the option turned off, the following happens:

Image

Here both orders are submitted, like the code says it should, but after a position is closed here with the time stop, new buy and sell stops are not submitted.

Summary
Optimize order flow turned on: only long orders are submitted, multiple times per bar as the code specifies.

Optimize order flow turned off: both long and short orders are submitted (as the code specifies), but only once.

Conclusion: In both cases, optimize order flow on or off, the code is not followed as programmed.

What am I doing wrong? Or, in other words, what is happening here?

******************************
Edit: Testing with the LMAX demo shows similar behaviour:
* Optimize order flow turned ON: only buy stops are submitted (and resubmitted intra-bar),
* Optimize order flow turned OFF: both buy and sell stops are submitted and, in contradiction with MB Trading, are resubmitted intra-bar.

--> If the behaviour described above with MB Trading only happens with that broker, then this is not an issue for me and then, as far as I'm concerned, no further research is needed.

If someone can confirm that this behaviour (i.e., the not resubmitting of buy and sell stops intrabar) is limited to MB Trading, that would be great. :)
Attachments
scr.27-02-2013 12.58.39.png
(2.23 KiB) Downloaded 751 times
scr.27-02-2013 12.41.24.png
(2.83 KiB) Downloaded 753 times
scr.27-02-2013 12.50.39.png
(15.59 KiB) Downloaded 750 times
scr.27-02-2013 12.50.36.png
(20.74 KiB) Downloaded 742 times

SUPER
Posts: 646
Joined: 03 Mar 2007
Has thanked: 106 times
Been thanked: 84 times

Re: Problem with optimize order flow

Postby SUPER » 27 Feb 2013

JoshM,

I tried your code with IB-Broker and found that with Optimize Order Flow turned on it just behaved like your experience ( view image file j-2).
[img]j-2.png[/img]
j-2.png
(220.1 KiB) Downloaded 622 times
However, with Optimize Order Flow turned off it seem to have followed expected behaviour of your code. (view image file j-1).
[img]j-1.png[/img]
j-1.png
(311.15 KiB) Downloaded 612 times
Sure something is not right here.

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

Re: Problem with optimize order flow

Postby Andrew MultiCharts » 27 Feb 2013

Hello,

When Optimize Order Flow box is checked it is expected that instead of 2 stop orders MC places only 1 stop order. This is how Optimize Order Flow designed.

As for multiple order replacement within the bar when the box is checked, we will test this case.

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: Problem with optimize order flow

Postby JoshM » 27 Feb 2013

I tried your code with IB-Broker and found that with Optimize Order Flow turned on it just behaved like your experience
Thanks Super for checking that, really appreciate that. :)

------

There also seems to be another problem with intra-bar order submittance.

Description
After one of the stop has triggered and opened a position, and subsequently this position is closed, new stop orders are generated by the code but not send by MultiCharts.

Data
MultiCharts Version 8.5 Release (Build 6743)
LMAX demo account
No errors in Order and Position Tracker log (see below)
60-minute chart
The same settings as above; Optimize Order Flow turned OFF.

Code example
Same code as above, only now with Print statements:

Code: Select all

[IntrabarOrderGeneration = True];

Inputs:
TickOffSet(20),
PosSize(1000);

Variables:
IntraBarPersist TradeEntryTime(0),
IntraBarPersist TradeExitTime(0),
IntraBarPersist PrevMP(0),
IntraBarPersist CalcPrices(True),
IntraBarPersist LongStopPrice(0),
IntraBarPersist ShortStopPrice(0),
OneMinute(ELTimeToDateTime(1)),
TwoMinutes(ELTimeToDateTime(2)),
currentDT(0);

if (GetAppInfo(aiStrategyAuto) = 0) or (LastBarOnChart_s = False) then
#return;

once cleardebug;

currentDT = ComputerDateTime;

// Submit orders for opening a position
if (MarketPosition(0) = 0) and (currentDT < TradeExitTime + TwoMinutes * 3) then begin

// Calculate order prices
if (CalcPrices = True) then begin
Print(FormatTime("HH:mm:ss|", ComputerDateTime), "Resetting prices and sending new orders..");
LongStopPrice = Close + (TickOffSet * (MinMove / PriceScale));
ShortStopPrice = Close - (TickOffSet * (MinMove / PriceScale));
CalcPrices = False;
end;

Buy ("EL STP") PosSize contracts next bar at LongStopPrice stop;
SellShort ("ES STP") PosSize contracts next bar at ShortStopPrice stop;

Print(FormatTime("HH:mm:ss|", ComputerDateTime), "Submitting orders. Close: ",
NumToStr(Close, 5), ", Buy @ ", NumToStr(LongStopPrice, 5),
", Sell @ ", NumToStr(ShortStopPrice, 5));
end;

// Open market positions
if (MarketPosition(0) <> 0) then begin

Print(FormatTime("HH:mm:ss|", ComputerDateTime), "Open position: ",
NumToStr(MarketPosition(0) * CurrentContracts, 0));

// Time stop
if (currentDT > TradeEntryTime + OneMinute) then begin

Print(FormatTime("HH:mm:ss|", ComputerDateTime), "Submitting time stop.. ");

Sell ("XL Time") all contracts next bar at market;
BuyToCover ("XS Time") all contracts next bar at market;
end;
end;

// Workaround for trade entry time
if (GetAppInfo(aiStrategyAuto) = 1) and (MarketPosition(0) = 0) then
TradeEntryTime = ComputerDateTime;

// Reset variables
if (PrevMP <> MarketPosition(0)) or (currentDT > TradeExitTime + TwoMinutes * 3) then begin
LongStopPrice = 0;
ShortStopPrice = 0;
CalcPrices = True;
TradeExitTime = currentDT;
end;

PrevMP = MarketPosition(0);
Output
The above code gives the following output:

Code: Select all

18:19:05|Resetting prices and sending new orders..
18:19:05|Submitting orders. Close: 1.51320, Buy @ 1.51340, Sell @ 1.51300
18:19:08|Submitting orders. Close: 1.51320, Buy @ 1.51340, Sell @ 1.51300
18:19:09|Submitting orders. Close: 1.51320, Buy @ 1.51340, Sell @ 1.51300
18:19:10|Submitting orders. Close: 1.51321, Buy @ 1.51340, Sell @ 1.51300
18:19:11|Submitting orders. Close: 1.51321, Buy @ 1.51340, Sell @ 1.51300
18:19:11|Submitting orders. Close: 1.51321, Buy @ 1.51340, Sell @ 1.51300
18:19:13|Submitting orders. Close: 1.51326, Buy @ 1.51340, Sell @ 1.51300
18:19:23|Submitting orders. Close: 1.51326, Buy @ 1.51340, Sell @ 1.51300
18:19:28|Submitting orders. Close: 1.51326, Buy @ 1.51340, Sell @ 1.51300
18:19:30|Open position: 1
18:19:30|Open position: 1
18:19:30|Open position: 1
18:19:30|Open position: 1
18:19:30|Open position: 1
18:19:30|Open position: 1
18:19:30|Open position: 1
18:19:30|Open position: 1
18:19:30|Open position: 1
18:19:30|Open position: 1
18:19:31|Open position: 1
18:19:31|Open position: 1
18:19:33|Open position: 1
18:19:37|Open position: 1
18:19:40|Open position: 1
18:19:59|Open position: 1
18:20:00|Open position: 1
18:20:00|Open position: 1
18:20:01|Open position: 1
18:20:01|Open position: 1
18:20:06|Open position: 1
18:20:06|Open position: 1
18:20:09|Open position: 1
18:20:13|Open position: 1
18:20:21|Open position: 1
18:20:21|Open position: 1
18:20:26|Open position: 1
18:20:27|Open position: 1
18:20:27|Open position: 1
18:20:29|Open position: 1
18:20:29|Submitting time stop..
18:20:31|Open position: 1
18:20:31|Submitting time stop..
18:20:31|Open position: 1
18:20:31|Submitting time stop..
18:20:31|Open position: 1
18:20:31|Submitting time stop..
18:20:31|Open position: 1
18:20:31|Submitting time stop..
18:20:31|Open position: 1
18:20:31|Submitting time stop..
18:20:31|Resetting prices and sending new orders..
18:20:31|Submitting orders. Close: 1.51348, Buy @ 1.51368, Sell @ 1.51328
18:20:31|Resetting prices and sending new orders..
18:20:31|Submitting orders. Close: 1.51348, Buy @ 1.51368, Sell @ 1.51328
18:20:31|Submitting orders. Close: 1.51348, Buy @ 1.51368, Sell @ 1.51328
18:20:31|Submitting orders. Close: 1.51348, Buy @ 1.51368, Sell @ 1.51328
18:20:31|Submitting orders. Close: 1.51348, Buy @ 1.51368, Sell @ 1.51328
18:20:37|Submitting orders. Close: 1.51349, Buy @ 1.51368, Sell @ 1.51328
18:20:37|Submitting orders. Close: 1.51347, Buy @ 1.51368, Sell @ 1.51328
18:20:41|Submitting orders. Close: 1.51351, Buy @ 1.51368, Sell @ 1.51328
18:20:41|Submitting orders. Close: 1.51351, Buy @ 1.51368, Sell @ 1.51328
18:20:42|Submitting orders. Close: 1.51352, Buy @ 1.51368, Sell @ 1.51328
18:20:50|Submitting orders. Close: 1.51351, Buy @ 1.51368, Sell @ 1.51328
18:20:50|Submitting orders. Close: 1.51352, Buy @ 1.51368, Sell @ 1.51328
18:20:51|Submitting orders. Close: 1.51352, Buy @ 1.51368, Sell @ 1.51328
18:20:52|Submitting orders. Close: 1.51351, Buy @ 1.51368, Sell @ 1.51328
18:20:53|Submitting orders. Close: 1.51352, Buy @ 1.51368, Sell @ 1.51328
18:20:53|Submitting orders. Close: 1.51352, Buy @ 1.51368, Sell @ 1.51328
18:20:55|Submitting orders. Close: 1.51352, Buy @ 1.51368, Sell @ 1.51328
18:20:55|Submitting orders. Close: 1.51352, Buy @ 1.51368, Sell @ 1.51328
18:20:55|Submitting orders. Close: 1.51352, Buy @ 1.51368, Sell @ 1.51328
As you can see in this output, the following happens:
* On 18:19:05, a buy stop was submitted for 1.51340 and a sell stop for 1.51300.
* The buy stop was triggered at 18:19:30, leading to a market position of 1.
* The time stop was triggered at 18:20:29, which closed the position.
* New stop orders were submitted at 18:20:31, for a buy stop of 1.51368 and a sell stop of 1.51328.

But these new stop orders were not submitted. Here is a screenshot of MultiCharts some six minutes after the strategy has apparently submitted these new stop orders:

Image

As you can see, the first stop orders were executed, but the new ones are nowhere to be found; not in the OPT, not on the chart, and there are no log messages:

Image

Why weren't these new stop orders triggered intra-bar? And why does the strategy clearly says it is submitting the orders, while in fact nothing is send?
Attachments
scr.27-02-2013 18.26.00.png
(45.32 KiB) Downloaded 723 times
scr.27-02-2013 18.28.30.png
(27.36 KiB) Downloaded 728 times

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: Problem with optimize order flow

Postby JoshM » 27 Feb 2013

When Optimize Order Flow box is checked it is expected that instead of 2 stop orders MC places only 1 stop order. This is how Optimize Order Flow designed.
Ah I see. Thanks Andrew, then I misunderstood what Optimize Order Flow is.
As for multiple order replacement within the bar when the box is checked, we will test this case.
That would be great. Could you also check how intra-bar order placement works with Optimize Order Flow turned off? (See my above post where these new orders were not generated). For easy replication, I've attached the workspace and PowerLanguage file to this post.
Attachments
_testSignal.pla
(6.66 KiB) Downloaded 471 times
LMAX_EURUSD.wsp
(122.45 KiB) Downloaded 493 times

SUPER
Posts: 646
Joined: 03 Mar 2007
Has thanked: 106 times
Been thanked: 84 times

Re: Problem with optimize order flow  [SOLVED]

Postby SUPER » 27 Feb 2013

JoshM,

Have you tried your code with " Allow unlimited entries and exits per bar" ?

It seems to work as expected with "Optimize order flow" turned off.
j-3.png
(10.36 KiB) Downloaded 633 times

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: Problem with optimize order flow

Postby JoshM » 28 Feb 2013

Have you tried your code with " Allow unlimited entries and exits per bar" ?

It seems to work as expected with "Optimize order flow" turned off.
Thanks Super, that indeed works. :)

I totally overlooked that option, I shamefully must admit. Sorry to bother you (Super) and Andrew with such a dummy topic. :)

Topic solved; thanks for your help!


Return to “MultiCharts”