Backtesting - long short entry and exit

Hi, I have written simple code for long short and backtesting the same. But the problem i'm facing in the backtesting results is whenever the after buy entry the exit result is not correctly coming what I mentioned in the code.

After buy entry sell condition of Sell= L < EMA(C, 9); is perfectly working. But other two conditions of sell is not exiting properly. I'm trying to exit after my buy whenever the price cross or low below the previous day low. But in the backtest results exit condition is not executing properly with my written code. It's exiting after some days even though previous day low breached, it's not exiting the trade.
Same with cover condition also.

Can anyone please help and suggest where I'm wrong in this code. As per my reading the //Cross// function in the site, generally the cross function array1 crosses array2 it should have to exit the trade. I'm unable to find out where I'm putting the wrong code? Same logic of Sell= L < EMA(C, 9); is exiting the trade perfectly, but not in other cases of Cover = C > Ref(High, -1); also.

In backtesting settings I have disabled all stops.

Buy= C > open AND C > BBandTop(C, 20, 1); 
Sell = Cross(Low, Ref(Low, -1));
//Sell = L < Ref(Low, -1);
//Sell= L < EMA(C, 9); 
Short= C < Open AND C < BBandBot(C, 20, 1); 
Cover = C > Ref(High, -1);
//Cover= C > EMA(C, 9);

Buy=ExRem(Buy,Sell);
Sell=ExRem(Sell,Buy);
Short=ExRem(Short,Cover);
Cover=ExRem(Cover,Short);
1 Like

Did you enable "Long & short" positions in the Analysis Settings?

Also don't use ExRem()

1 Like

@Tomasz, yes enabled in the settings. Removed ExRem(), still not getting desired results.

Actual Data :

Date/Time	Open	high	         low	        close	
08-04-2021	1435.50	1458.85	1425.20	1444.70
09-04-2021	1459.85	1459.85	1436.75	1448.10
12-04-2021	1472.25	1479.00	1421.70	1432.60
13-04-2021	1435.05	1437.95	1374.10	1401.50
15-04-2021	1335.00	1366.45	1317.40	1362.30
16-04-2021	1364.90	1372.20	1347.60	1354.00

backtest results :
Long 08-04-2021 1444.7 16-04-2021 1354 -6.28% -196093.40 -6.28% 2162 3123441.29 -72518.95 6 -32682.23 -8.81% 2.37% 0/0

As per my requirement and code it should have exit the trade on 12th April, but showing exit in the backtest results on 16th April, also exiting the trade at closing price on 16th April.

@Dragon, it seems to me that to get the same result (on the crossing bar) for the 2 above sell conditions (that below I renamed Cross1 and Cross2) you should invert the arguments in the Cross call:

From the docs:

To find out when ARRAY1 crosses below ARRAY2, use the formula cross(ARRAY2, ARRAY1)

To verify what your code is doing, explorations, IMO, are a great way to dissipate doubts (apply it to a single - i.e., "current" instrument - for a range of dates):

Cross1 = Cross( Low, Ref( Low, -1 ) );
Cross2 = Cross( Ref( Low, -1 ), Low ); // Reverse order of arguments
Lower =  L < Ref( Low, -1 );

// Exploration 
// Utility function to display empty cells instead of 0 (zero)
function nil( a )
{
    return ( Iif( a, a, Null ) );
}
Filter = 1;
AddColumn( Low, "Low", 1.5 );
AddColumn( Ref( Low, -1 ), "Low-1", 1.5 );
AddColumn( Nil( Cross1 ), "Cross 1", 1 );
AddColumn( Nil( Cross2 ), "Cross 2", 1 );
AddColumn( Nil( Lower ), "Lower Low", 1 );
// Check the "Lower" condition at the crossing
AddColumn( nil( Cross1 AND( Lower != Cross1 ) ), "Discrepancy 1", 1 ); 
AddColumn( nil( Cross2 AND( Lower != Cross2 ) ), "Discrepancy 2", 1 ); // Should be all 0 (displayed as an empty column)
5 Likes

@beppe, fantastic. Cross2 function working great. Thank you so much.

In backtesting results, exit price is always at close only. Is there any way to write exit should be at the low of the previous day low price, if breached. If I change in settings > Trades > Sell price > to LOW, it is exiting the trade at current day's low price, not previous day low price. So, it is not relevant. Can we achieve this with the help of formula?

You may use code level for assigning to trade prices

SellPrice = Min(Open, Ref(L,-1));
4 Likes

@fxshrat, thanks. Will incorporate the code.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.