Backtesting order execution limited to once  [SOLVED]

Questions about MultiCharts and user contributed studies.
cnbiz850
Posts: 33
Joined: 15 Oct 2012
Has thanked: 1 time
Been thanked: 1 time

Backtesting order execution limited to once

Postby cnbiz850 » 02 Mar 2013

Trying to understand with backtesting how scaling in and fading out strategies work. It appears that MC only execute order once.

For instance, I opened a short position of 100 contracts at market open. Then at 9:11, an order to cover 20 contracts was generated, and it was shown on chart. Then at 9:16, the strategy has an order to cover another 20 contracts, but it was not shown on chart. A print statement in the code does show that the condition was met for the 9:16 order.

If I disable the 9:11 order in the code, then the order at 9:16 was shown on chart.

I work on 1 minute chart and does not use IOG.

I guess there might be a setting for accepting multiple orders, but what I found was only for openning orders, not for closing orders.

Any help please? I am using MC 7.4.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Backtesting order execution limited to once

Postby TJ » 02 Mar 2013

Trying to understand with backtesting how scaling in and fading out strategies work. It appears that MC only execute order once.
For instance, I opened a short position of 100 contracts at market open. Then at 9:11, an order to cover 20 contracts was generated, and it was shown on chart. Then at 9:16, the strategy has an order to cover another 20 contracts, but it was not shown on chart. A print statement in the code does show that the condition was met for the 9:16 order.
If I disable the 9:11 order in the code, then the order at 9:16 was shown on chart.
I guess there might be a setting for accepting multiple orders, but what I found was only for openning orders, not for closing orders.
Any help please? I am using MC 7.4.
1. Can't tell you much without the code.

2. "Scaling in/scaling out" is an advanced level programming exercise. You'd better start with a detailed flowchart and logic map, and prepared to spent time tracing the steps in each scenario. Simply talking about it in general will not get you the specific help you need.

3. Here are some articles for background knowledge:
viewtopic.php?f=16&t=10811

cnbiz850
Posts: 33
Joined: 15 Oct 2012
Has thanked: 1 time
Been thanked: 1 time

Re: Backtesting order execution limited to once

Postby cnbiz850 » 02 Mar 2013

Thanks very much for the reply.

I understand the complexity there. But the issue at hand is the second order not executed even when the condition was met.

For instance:

Code: Select all

if (MyCondition) then begin
print("BC20");
buytocover ("BC20") 20 contracts next bar at close-MinMove stop;
end;
print("BC20"); was executed, but buytocover was not. What is wrong?

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Backtesting order execution limited to once

Postby TJ » 02 Mar 2013

Thanks very much for the reply.

I understand the complexity there. But the issue at hand is the second order not executed even when the condition was met.

For instance:

Code: Select all

if (MyCondition) then begin
print("BC20");
buytocover ("BC20") 20 contracts next bar at close-MinMove stop;
end;
print("BC20"); was executed, but buytocover was not. What is wrong?
it's a stop order... you need to dig into more information.
is this a coding error?
is this an execution error?
did the market trade through the stop price?
did ALL the order not execute? or only a few that did not?
are you using Bar Magnifier?
did you set the autotrade on one trade per bar?
what is the chart resolution?
what bar type?
are you using IOG?
why not market order? If you are going to trade multiple scale in/out in one bar, it makes no sense to use stop order.

There are more questions; you will need to debug it systematically.

Autotrade is a complex system; you are talking about a specific situation, you will need to show the occurance at the specific time, specific bar, specific price. Otherwise we are just talking things in general.

cnbiz850
Posts: 33
Joined: 15 Oct 2012
Has thanked: 1 time
Been thanked: 1 time

Re: Backtesting order execution limited to once

Postby cnbiz850 » 03 Mar 2013

TJ,

Thanks very much again for the reply. I thought I described the situation clearly, but I am really sorry that perhaps my question was not clear enough.

It was not about autotrade - it was about backtesting.

It was on 1-minute bar chart. I set one trade per bar, and did NOT use bar magnifier nor IOG.

As I said earlier with the example, the first trade was at 9:11, and the second trade should occur at 9:16 but didn't - these are on different bars - even though the section of the code was executed (verified with a print statement). When I disable the first trade, then the second trade occured as expected. The stop price is fine for it to be executed.

As I look through the chart, it appears that for every one set of trades (i.e. entry, exit, exit ...), where there are supposed to be multiple scaling-out trades, only one scaling-out trade executes (the final closeout order went through OK). So the issue is not about a single trade, it is all my scaling-out trades.
it's a stop order... you need to dig into more information.
is this a coding error?
is this an execution error?
did the market trade through the stop price?
did ALL the order not execute? or only a few that did not?
are you using Bar Magnifier?
did you set the autotrade on one trade per bar?
what is the chart resolution?
what bar type?
are you using IOG?
why not market order? If you are going to trade multiple scale in/out in one bar, it makes no sense to use stop order.

There are more questions; you will need to debug it systematically.

Autotrade is a complex system; you are talking about a specific situation, you will need to show the occurance at the specific time, specific bar, specific price. Otherwise we are just talking things in general.

cnbiz850
Posts: 33
Joined: 15 Oct 2012
Has thanked: 1 time
Been thanked: 1 time

Re: Backtesting order execution limited to once

Postby cnbiz850 » 03 Mar 2013

To clearly describe this problem, I made a simple test code. It should be added to 1-minute charts. In the code, I open a short position of 20 contracts at the market open on everyday, then cover 5 contracts each at 9:11, 9:16, and 10:02. Then just prior to 15:00, I close all out.

In my system (MC 7.4), only the 9:11 cover is executed. The other two at 9:16 and 10:02 are not. A screen shot is attached.

While this simple test code demonstrates the issue I am having with my bigger project, I hope someone can help me resolve it.

Code: Select all

if (date <> date[1]) then begin
// on the first bar of the day
sellshort ("S") 20 contracts next bar at close+MinMove stop;
end else begin
if MarketPosition <> 0 then begin
if (time=911 or time=916 or time = 1002) then
buytocover ("BC") 5 contracts next bar at close-MinMove stop;
end;

// close out all open positions
if (time>=1456 and time<1500) then begin
if (marketposition<0) then
buytocover ("E") currentcontracts contracts next bar at close-MinMove stop;
end;
end;
Attachments
选区_001.png
order execution
(55.48 KiB) Downloaded 1377 times
Last edited by cnbiz850 on 03 Mar 2013, edited 1 time in total.

cnbiz850
Posts: 33
Joined: 15 Oct 2012
Has thanked: 1 time
Been thanked: 1 time

Re: Backtesting order execution limited to once

Postby cnbiz850 » 03 Mar 2013

To further on that, I separate the three cover orders and they execute OK.

What am I missing here?

It is very unfortunate that one can not separate the orders like this in real projects as they can be due to the same condition that just occurs at different time.

Code: Select all

if (date <> date[1]) then begin
// on the first bar of the day
sellshort ("S") 20 contracts next bar at close+MinMove stop;
end else begin
if MarketPosition <> 0 then begin
if (time=911) then
buytocover ("BC") 5 contracts next bar at close-MinMove stop;
if (time=916) then
buytocover ("BC1") 5 contracts next bar at close-MinMove stop;
if (time=1002) then
buytocover ("BC2") 5 contracts next bar at close-MinMove stop;
end;
// close out all open positions
if (time>=1456 and time<1500) then begin
if (marketposition<0) then
buytocover ("E") currentcontracts contracts next bar at close-MinMove stop;
end;
end;

evdl
Posts: 401
Joined: 19 Jan 2011
Location: Netherlands
Has thanked: 85 times
Been thanked: 124 times

Re: Backtesting order execution limited to once

Postby evdl » 03 Mar 2013

Just looking at it quickly, the issue could be that you only let the script run at the first bar of the day and at that time, the time conditions are not met, so it will not trigger anything when the set time is met because it will only run on first bar of the day.

what if you change date<>date[1] to date > date[1]?

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Backtesting order execution limited to once

Postby TJ » 03 Mar 2013

TJ,
Thanks very much again for the reply. I thought I described the situation clearly, but I am really sorry that perhaps my question was not clear enough.

It was not about autotrade - it was about backtesting.
...
Backtesting is for autotrade.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Backtesting order execution limited to once

Postby TJ » 03 Mar 2013

To further on that, I separate the three cover orders and they execute OK.

What am I missing here?

It is very unfortunate that one can not separate the orders like this in real projects as they can be due to the same condition that just occurs at different time.

Code: Select all

if (date <> date[1]) then begin
// on the first bar of the day
sellshort ("S") 20 contracts next bar at close+MinMove stop;
end else begin
if MarketPosition <> 0 then begin
if (time=911) then
buytocover ("BC") 5 contracts next bar at close-MinMove stop;
if (time=916) then
buytocover ("BC1") 5 contracts next bar at close-MinMove stop;
if (time=1002) then
buytocover ("BC2") 5 contracts next bar at close-MinMove stop;
end;
// close out all open positions
if (time>=1456 and time<1500) then begin
if (marketposition<0) then
buytocover ("E") currentcontracts contracts next bar at close-MinMove stop;
end;
end;
It is not possible to break up the lot like this.

cnbiz850
Posts: 33
Joined: 15 Oct 2012
Has thanked: 1 time
Been thanked: 1 time

Re: Backtesting order execution limited to once

Postby cnbiz850 » 05 Mar 2013

Any help from MultiCharts please.

cnbiz850
Posts: 33
Joined: 15 Oct 2012
Has thanked: 1 time
Been thanked: 1 time

Re: Backtesting order execution limited to once

Postby cnbiz850 » 06 Mar 2013

Still no response from MultiCharts. I take it that this issue is a fundamental flaw of MC.

In the mean time, I cooked up a way to go around it - hard code enough cover statements in the program. I know it is not pretty.

Code: Select all

vars: MyCondition(false), Index(0);

if (date <> date[1]) then begin
// on the first bar of the day
sellshort ("S") 20 contracts next bar at close+MinMove stop;
index=0;
end else begin
if MarketPosition <> 0 then begin
MyCondition = time=911 or time=916 or time=1002;
if (MyCondition) then begin
if (index=0) then
buytocover ("BC") 5 contracts next bar at close-MinMove stop;
if (index=1) then
buytocover ("BC1") 5 contracts next bar at close-MinMove stop;
if (index=2) then
buytocover ("BC2") 5 contracts next bar at close-MinMove stop;
if (index=3) then
buytocover ("BC3") 5 contracts next bar at close-MinMove stop;
{one should hardcode enough of these to cover the possible occurence of the condition}
index = index+1;
end;
end;
// close out all open positions
if (time>=1456 and time<1500) then begin
if (marketposition<0) then
buytocover ("E") currentcontracts contracts next bar at close-MinMove stop;
end;
end;

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Backtesting order execution limited to once

Postby TJ » 06 Mar 2013

Still no response from MultiCharts. I take it that this issue is a fundamental flaw of MC.

In the mean time, I cooked up a way to go around it - hard code enough cover statements in the program. I know it is not pretty.
...
Easylanguage has been around for over 25 yrs, I am sure you are not the first person to attempt scaling in/out. This is advanced level programming; it seems tedious, but is essential.

cnbiz850
Posts: 33
Joined: 15 Oct 2012
Has thanked: 1 time
Been thanked: 1 time

Re: Backtesting order execution limited to once

Postby cnbiz850 » 06 Mar 2013

Easylanguage has been around for over 25 yrs, I am sure you are not the first person to attempt scaling in/out. This is an advanced level programming; it seems tedious, but is essential.
I don't agree that this is an issue with Easylanguage. It is an internal restriction within MC that prohibits (for whatever reasons) multiple executions of exit with the same name. It is the same as the default restriction on multiple entries. Multiple entries can be allowed with a setting on the property interface. I don't understand why such a setting is not there for exits.

Scaling in/out is advanced and essential, but it shouldn't be tedious.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Backtesting order execution limited to once

Postby TJ » 06 Mar 2013

Easylanguage has been around for over 25 yrs, I am sure you are not the first person to attempt scaling in/out. This is an advanced level programming; it seems tedious, but is essential.
I don't agree that this is an issue with Easylanguage. It is an internal restriction within MC that prohibits (for whatever reasons) multiple executions of exit with the same name. It is the same as the default restriction on multiple entries. Multiple entries can be allowed with a setting on the property interface. I don't understand why such a setting is not there for exits.

Scaling in/out is advanced and essential, but it shouldn't be tedious.
How do you reconcile your orders if something does not work right?

The programming seems tedious, but that's just the nature of programming. You have to start with a flowchart, and code each step and each permutation according to your logic. If you want the strategy to perform all the detailed decisions, you have to program in the step-by-step details, there is no short cuts.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Backtesting order execution limited to once

Postby TJ » 06 Mar 2013

Have you drawn up a flowchart of your logic?

cnbiz850
Posts: 33
Joined: 15 Oct 2012
Has thanked: 1 time
Been thanked: 1 time

Re: Backtesting order execution limited to once

Postby cnbiz850 » 06 Mar 2013

Have you drawn up a flowchart of your logic?
Mine is not that complex. Just some simple rules. But that part works well.

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

Re: Backtesting order execution limited to once  [SOLVED]

Postby Andrew MultiCharts » 21 Mar 2013

I don't agree that this is an issue with Easylanguage. It is an internal restriction within MC that prohibits (for whatever reasons) multiple executions of exit with the same name. It is the same as the default restriction on multiple entries. Multiple entries can be allowed with a setting on the property interface. I don't understand why such a setting is not there for exits.
You can devide your script into 3 separate exit lines:

Code: Select all

if (time=430) then
buytocover ("BC1") 5 contracts next bar at close-MinMove stop;
if (time=431) then
buytocover ("BC2") 5 contracts next bar at close-MinMove stop;
if (time = 432) then
buytocover ("BC3") 5 contracts next bar at close-MinMove stop;
OR

You can use

Code: Select all

[SameExitFromOneEntryOnce = false]
in the beginning of the script to use only 1 line for exiting multiple times from the same entry.

cnbiz850
Posts: 33
Joined: 15 Oct 2012
Has thanked: 1 time
Been thanked: 1 time

Re: Backtesting order execution limited to once

Postby cnbiz850 » 21 Mar 2013

Thanks Andrew for the response.

You can devide your script into 3 separate exit lines:

Code: Select all

if (time=430) then
buytocover ("BC1") 5 contracts next bar at close-MinMove stop;
if (time=431) then
buytocover ("BC2") 5 contracts next bar at close-MinMove stop;
if (time = 432) then
buytocover ("BC3") 5 contracts next bar at close-MinMove stop;
Even though this works in the demo code, this is not a solution for most strategies.
OR

You can use

Code: Select all

[SameExitFromOneEntryOnce = false]
in the beginning of the script to use only 1 line for exiting multiple times from the same entry.
This sounds great. I don't know what version of MC this is for. It doesn't work in 7.4.

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

Re: Backtesting order execution limited to once

Postby SUPER » 22 Mar 2013

cnbiz850,

It works on current release 8.5 however, have not tried it on earlier versions.

forum search revealed one thread on this subject: viewtopic.php?f=1&t=11110&p=54914&hilit ... nce#p54914

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

Re: Backtesting order execution limited to once

Postby Andrew MultiCharts » 22 Mar 2013

This sounds great. I don't know what version of MC this is for. It doesn't work in 7.4.
Please install latest MC 8.5 and it should be working there.


Return to “MultiCharts”