Backtesting order execution limited to once [SOLVED]
Backtesting order execution limited to once
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.
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.
- TJ
- Posts: 7752
- Joined: 29 Aug 2006
- Location: Global Citizen
- Has thanked: 1034 times
- Been thanked: 2228 times
Re: Backtesting order execution limited to once
1. Can't tell you much without the code.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.
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
Re: Backtesting order execution limited to once
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:
print("BC20"); was executed, but buytocover was not. What is wrong?
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;
- TJ
- Posts: 7752
- Joined: 29 Aug 2006
- Location: Global Citizen
- Has thanked: 1034 times
- Been thanked: 2228 times
Re: Backtesting order execution limited to once
it's a stop order... you need to dig into more information.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:print("BC20"); was executed, but buytocover was not. What is wrong?Code: Select all
if (MyCondition) then begin
print("BC20");
buytocover ("BC20") 20 contracts next bar at close-MinMove stop;
end;
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.
Re: Backtesting order execution limited to once
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.
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.
Re: Backtesting order execution limited to once
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.
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.
Re: Backtesting order execution limited to once
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.
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;
-
- Posts: 401
- Joined: 19 Jan 2011
- Location: Netherlands
- Has thanked: 85 times
- Been thanked: 125 times
Re: Backtesting order execution limited to once
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]?
what if you change date<>date[1] to date > date[1]?
- TJ
- Posts: 7752
- Joined: 29 Aug 2006
- Location: Global Citizen
- Has thanked: 1034 times
- Been thanked: 2228 times
Re: Backtesting order execution limited to once
Backtesting is for autotrade.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.
...
- TJ
- Posts: 7752
- Joined: 29 Aug 2006
- Location: Global Citizen
- Has thanked: 1034 times
- Been thanked: 2228 times
Re: Backtesting order execution limited to once
It is not possible to break up the lot like this.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;
Re: Backtesting order execution limited to once
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.
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;
- TJ
- Posts: 7752
- Joined: 29 Aug 2006
- Location: Global Citizen
- Has thanked: 1034 times
- Been thanked: 2228 times
Re: Backtesting order execution limited to once
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.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.
...
Re: Backtesting order execution limited to once
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.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.
Scaling in/out is advanced and essential, but it shouldn't be tedious.
- TJ
- Posts: 7752
- Joined: 29 Aug 2006
- Location: Global Citizen
- Has thanked: 1034 times
- Been thanked: 2228 times
Re: Backtesting order execution limited to once
How do you reconcile your orders if something does not work right?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.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.
Scaling in/out is advanced and essential, but it shouldn't be tedious.
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.
- TJ
- Posts: 7752
- Joined: 29 Aug 2006
- Location: Global Citizen
- Has thanked: 1034 times
- Been thanked: 2228 times
- Andrew MultiCharts
- Posts: 1587
- Joined: 11 Oct 2011
- Has thanked: 931 times
- Been thanked: 559 times
Re: Backtesting order execution limited to once [SOLVED]
You can devide your script into 3 separate exit lines: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.
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;
You can use
Code: Select all
[SameExitFromOneEntryOnce = false]
Re: Backtesting order execution limited to once
Thanks Andrew for the response.
Even though this works in the demo code, this is not a solution for most strategies.
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;
This sounds great. I don't know what version of MC this is for. It doesn't work in 7.4.OR
You can usein the beginning of the script to use only 1 line for exiting multiple times from the same entry.Code: Select all
[SameExitFromOneEntryOnce = false]
Re: Backtesting order execution limited to once
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
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
- Andrew MultiCharts
- Posts: 1587
- Joined: 11 Oct 2011
- Has thanked: 931 times
- Been thanked: 559 times
Re: Backtesting order execution limited to once
Please install latest MC 8.5 and it should be working there.This sounds great. I don't know what version of MC this is for. It doesn't work in 7.4.