A buytocover question

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

A buytocover question

Postby cnbiz850 » 23 Nov 2013

The following sample code does some entries and some exits on the 1-minute chart. The problem I have is that the exits at the end of day are NOT executed with the specified size during backtesting. Note the total size at the end is 15, but on my system it only exits 12. A screenshot is attached.

Would anyone please help figuring out what is wrong?

Code: Select all

vars: ChangePos(0), BEFlag(1);

ChangePos=0;

if (time=901) then
ChangePos=-3;
if (time=916) then
ChangePos=-2;
if (time=1040) then
ChangePos=-6;
if (time=1340) then
ChangePos=6;
if (time=1348) then
ChangePos=-6;
if (time=1403) then
ChangePos=2;
if (time=1440) then
ChangePos=-6;
if (time=1456) then
ChangePos=15 {currentcontracts};

if (ChangePos<>0) then
print(date, " time=", time, " Position before order: ", currentcontracts);

if (ChangePos<0) then
sellshort -ChangePos contracts next bar at market;
if (ChangePos>0) then begin
if (BEFlag=1) then
buytocover ("BC") ChangePos contracts total next bar at market
else
buytocover ("BC1") ChangePos contracts total next bar at market;
BEFlag=-BEFlag;
end;
Attachments
Screenshot - 2013年11月24日 - 06时41分28秒.png
(25.9 KiB) Downloaded 474 times

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

Re: Is this a buytocover bug?

Postby JoshM » 24 Nov 2013

The following sample code does some entries and some exits on the 1-minute chart. The problem I have is that the exits at the end of day are NOT executed with the specified size during backtesting. Note the total size at the end is 15, but on my system it only exits 12. A screenshot is attached.

Would anyone please help figuring out what is wrong?

Code: Select all

...
if (time=901) then
ChangePos=-3;
if (time=916) then
ChangePos=-2;
if (time=1040) then
ChangePos=-6;
if (time=1340) then
ChangePos=6;
if (time=1348) then
ChangePos=-6;
if (time=1403) then
ChangePos=2;
if (time=1440) then
ChangePos=-6;
if (time=1456) then
ChangePos=15 {currentcontracts};
...
The drawback of specifying the exact time for submitting an order (e.g., 14:56) is that when there is no trade during that minute, the order will not get triggered. Have you used trying a time range? For example (untested),

Code: Select all

if (time >= 1456 and time <= 1500 and onlyOnce = true) then begin
ChangePos=15 {currentcontracts};
onlyOnce = false;
end;
Otherwise, you could try it with the Bar Magnifier set to a very low time frame (e.g., 1 tick) and see if that gives better results.

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

Re: Is this a buytocover bug?

Postby cnbiz850 » 24 Nov 2013

Thank you JoshM.

Using time for entry and exit is only to demostrate the problem. The issue occurred during backtesting, so it has nothing to do with liquidity. There must be something else affecting buytocover's execution.
The drawback of specifying the exact time for submitting an order (e.g., 14:56) is that when there is no trade during that minute, the order will not get triggered. Have you used trying a time range? For example (untested),

Code: Select all

if (time >= 1456 and time <= 1500 and onlyOnce = true) then begin
ChangePos=15 {currentcontracts};
onlyOnce = false;
end;
Otherwise, you could try it with the Bar Magnifier set to a very low time frame (e.g., 1 tick) and see if that gives better results.

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

Re: Is this a buytocover bug?

Postby JoshM » 25 Nov 2013

Using time for entry and exit is only to demostrate the problem. The issue occurred during backtesting, so it has nothing to do with liquidity. There must be something else affecting buytocover's execution.
If you don't have historical trades during a specific time (e.g., 14:56:00 till 14:56:59) in your backtest period, the order would not trigger during backtesting. So, whether or not there are trades at that time is relevant (generally speaking).

What are your Strategy Properties when this behaviour occurs?

Have you tried not using the Total reserved word?

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

Re: Is this a buytocover bug?

Postby cnbiz850 » 25 Nov 2013

Hi JoshM,

If you check the screenshot I attached, you will find that trades are not missed. The only problem is that size is not as specified. Also, this problem does not just happen on a specific day - everyday is the same with the demo code.

I don't see anything special with settings at Strategy Properties. I allowed multiple entries. I did not activate IOG, so every trade happen at the open of the next bar.

Without using word Total is totally a different behavior. The reference on Total describes its function.
If you don't have historical trades during a specific time (e.g., 14:56:00 till 14:56:59) in your backtest period, the order would not trigger during backtesting. So, whether or not there are trades at that time is relevant (generally speaking).

What are your Strategy Properties when this behaviour occurs?

Have you tried not using the Total reserved word?

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Is this a buytocover bug?

Postby Henry MultiСharts » 25 Nov 2013

cnbiz850, please add the following attribute to your code:

Code: Select all

[SameExitFromOneEntryOnce=False]
Here is what it does.

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

Re: Is this a buytocover bug?

Postby cnbiz850 » 25 Nov 2013

Thank you. That only applies to 8.5 and above. I will try it when the agent releases 8.5, supposedly next month.
cnbiz850, please add the following attribute to your code:

Code: Select all

[SameExitFromOneEntryOnce=False]
Here is what it does.


Return to “MultiCharts”