Overlapping buy and short signals

Hi,

I have strategy that will generate both buy and short signals, but I found when buy and short signals are overlapped, the first trade will be forced out. That means, only one trade at a time, is there a way to work around it?
Here is my simplified code:

PointValue = 50;
MarginDeposit = 20000;

SetPositionSize(2, spsShares);  // two contracts
SetOption( "MaxOpenPositions", 2 );
SetOption("MaxOpenLong", 1 );
SetOption("MaxOpenShort", 1 );

Buy = Cross(TimeNum(), 092900);
Sell = Cross(TimeNum(), 160000);

Short = Cross(TimeNum(), 102900);
Cover = Cross(TimeNum(), 162900);

pt
From screenshot above, you can see long entry is always forced to exit before short entry. And the number of contract is 2 even if I set MaxOpenLong and MaxOpenShort to 1, why?

Add

SetOption( "ReverseSignalForcesExit", False);

It is part of analysis settngs.
https://www.amibroker.com/guide/w_settings.html

4 Likes

Thanks. But there are only long entries this time, since the number of contract always be 2, there is no capital for short entries. Setting MaxOpenLong and MaxOpenShort to 1 seems does not work.
image

You apply backtestRegular.

Check out SetBacktestMode

1 Like

I've tried backtestRegular and backtestRegularRawMulti but no luck. Here is my code:

PointValue = 50;
MarginDeposit = 40000;

SetBacktestMode(backtestRegular);
SetOption( "ReverseSignalForcesExit", False);

SetPositionSize(2, spsShares);
SetOption( "MaxOpenPositions", 2 );
SetOption("MaxOpenLong", 1 );
SetOption("MaxOpenShort", 1 );

Buy = Cross(TimeNum(), 092900);
Sell = Cross(TimeNum(), 160000);

Short = Cross(TimeNum(), 102900);
Cover = cross(TimeNum(), 162900);

I guess the problem is MaxOpenLong and MaxOpenShort not working.

Why don't you save the whole Analysis project as APX and upload it. You also need to mention the AB Version.

This is just guessing games because the settings in your AFL doesn't cover everything about the backtester.

And elaborate in more detail what you are trying to do.

You have to read the manual.
Number of contracts has nothing to do with MaxOpenLong

I can have 1 Long with 10 contracts.

And you have set SetPositionSize(2, spsShares); which clearly tells BackTester to take 2 contracts per Trade.

What is the initial Equity, what is the margin reqd, total how many contracts can be purchased?
Someone will fill in the blanks ?

We had a similar discussion here a few days ago, kindly read it.

3 Likes

No, they do work. Position size is not number of positions.
If you want to trade 1 contract then set position size to 1 but not to 2.
And if you want to keep long and short positions open at same time then apply backtestRegularRawMulti.

PointValue = 50;
MarginDeposit = 40000;

SetBacktestMode(backtestRegularRawMulti);
SetOption( "ReverseSignalForcesExit", False);

SetPositionSize(1, spsShares);
SetOption( "MaxOpenPositions", 2 );
SetOption("MaxOpenLong", 1 );
SetOption("MaxOpenShort", 1 );

Buy = Cross(TimeNum(), 092900);
Sell = Cross(TimeNum(), 160000);

Short = Cross(TimeNum(), 102900);
Cover = cross(TimeNum(), 162900);
2 Likes

I've tried two long positions, it works as expected.

PointValue = 50;
MarginDeposit = 20000;

SetPositionSize(1, spsShares);
SetOption( "MaxOpenPositions", 2 );
SetOption("MaxOpenLong", 2 );
SetBacktestMode(backtestRegularRawMulti);

Short = False;
Cover = False;

Buy1 = Cross(TimeNum(), 092900);
Sell1 = Cross(TimeNum(), 160000);

Buy2 = Cross(TimeNum(), 102900);
sell2 = Cross(TimeNum(), 162900);

Buy = Buy1 || Buy2;
Sell = Sell1 || Sell2;

@travick thanks for kind advice, I will follow your advice in my next topic.
@codejunkie it works as expected now. Many thanks for your kind help.