Overlapping signals / same bar conflicts

I am trying to solve an issue relating to overlapping signals and same bar conflicts. The primary issue is shown below as an image:

Buy after Sell

As highlighted, there are both Buy and Sell signals on 1/12/2021. Amibroker first exits the position from the previous entry (on 1/11/2021), and then enters into another position on the same day. The issue is that I am looking to Buy at the Open and Sell at the Close, thus there is a future leak I am trying to eliminate. To be clear, I do not want the trade highlighted in red to take place.

My code is below (and I have also attached the full analysis file for reference):

//Backtester settings 
//General settings  
SetOption( "InitialEquity", 10000 );   
SetTradeDelays( 0, 0, 0, 0 );    
SetBacktestMode( backtestRegularRaw );   
SetOption( "AllowPositionShrinking", True );
RoundLotSize = 1;   

//Buy signals precede sell signals
SetOption("AllowSameBarExit", True );   
SetOption( "HoldMinBars", 0 );  

//Settlement delay to avoid same bar conflicts
SetOption("SettlementDelay", 1 ); 

//Position sizing
SetPositionSize( 100, spsPercentOfEquity );	  
SetOption("MaxOpenPositions", 1 ); 	

//Trading signals
//Buy signals
Buy = 		1; 

BuyPrice = 	O;

//Sell signals
Sell = 		C > Ref( C, -1 );

SellPrice = C;

I have read through the following articles in the knowledge base:


https://www.amibroker.com/guide/h_portfolio.html (resolving same bar / same symbol conflicts)

As per the first article, I have changed my settings to include a SettlementDelay of 1 and changed the backtest mode to backtestRegularRaw.

As per the second article, Scenario 3 in resolving same bar / same symbol conflicts above addresses part of my issue, however, I would like to retain the ability to exit intraday. I thus do not want to apply SetOption("HoldMinBars", 1 ).

Is there a way to address my issue without using CBT? My apologies in advance if this issue has been covered elsewhere, I have read through the forum and the knowledge base and have not found the answer to my problem.

Link to APX analysis file: https://www.dropbox.com/s/y13h962d5ah6fc0/Overlapping%20signals.apx?dl=0

As a possible solution, try using the Exrem() function by adding it to the code after the buy and sell signals.

Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );
Short = ExRem( Short, Cover );
Cover = ExRem( Cover, Short );
2 Likes

No, ExRem() should NOT be used.

Update: I should rather say "ExRem should not be used always (as a "magic bullet" in every case), but only in specific cases)". In this specific case it is OK.

I was a too quick with this one as I am a bit "allergic" with ExRems placed everywhere in large amounts.

All supported scenarios are listed already in the Users' Guide:

2 Likes

Thank you for your response. I read through Scenario 3 and I do not believe my scenario is supported as the "trade that was just open can not be closed the same bar". To be clear, I would like trades to exit on the same bar so this does not apply to me.

Looking at Scenario 2, I run into the issue that an entry signal is processed following an exit (as shown in the image above).

In this case, should I rely on CBT in order to address my specific situation?

1 Like

@deb_s I certainly don't want to contradict @Tomasz but after trying various scenarios it appears to me that

Buy = ExRem( Buy, Sell );

is both logical in your scenario (ie you have a buy signal every day but obviously do not want to act on it until the day after a Sell signal) and it appears to work.
image

I would suggest reviewing a larger number of trades and testing all coding scenario's listed in the user guide. Compare the results.

3 Likes

Thank you for your advice - I have implemented your idea and it does appear to remove the unwanted Buy signals. Thanks again.

@svaztej and @portfoliobuilder were right.

I should rather say "ExRem should not be used always (as a "magic bullet" in every case), but only in specific cases)". In this particular case it is OK.

I was too quick with my previous reply, I am sorry.

5 Likes

As you indicated in another post about the use of the Exrem() function and SetBacktestMode( backtestRegular ) it was clear to me that using Exrem() was redundant, since AB were already doing it internally with the SetBacktestMode( backtestRegular ) option.

In my case, I use the Exrem(buy, sell) solution tied to the code I use to mark the signals on the chart.

// DELETING EXTRASIGNAL
Buy = ExRem(Buy, Sell OR Short);
Short = ExRem(Short, Cover OR Buy);
Sell = ExRem(Sell, Buy);
Cover = ExRem(Cover, Short);

// PLOTTING ACTIVATION SIGNALS
distancia = 3*ATR(14);
for(i=0; i<BarCount; i++)
{
if(Buy[i]) PlotText("buy\n" + BuyPrice[i], i, H[i]-distancia[i], colorGreen);
if(Sell[i] AND !Short[i]) PlotText("sell\n" + SellPrice[i], i, L[i]+distancia[i], colorRed);
if(Short[i]) PlotText("short\n" + ShortPrice[i], i,  L[i]+distancia[i], colorRed);
if(Cover[i] AND !Buy[i]) PlotText("cover\n" + CoverPrice[i], i, H[i]-distancia[i], colorGreen);
}

//PLOTTIN SHAPES//   
PlotShapes(IIf(Buy,shapeUpArrow,shapeNone),colorGreen,0,L,-15);   
PlotShapes(IIf(Buy,shapeHollowCircle,shapeNone),colorGreen,0,BuyPrice,0);   
PlotShapes(IIf(Sell AND !Short,shapeDownArrow,shapeNone),colorRed,0,H,-15);   
PlotShapes(IIf(Sell AND !Short ,shapeHollowCircle,shapeNone),colorRed,0,SellPrice,0);   
PlotShapes(IIf(Short,shapeDownArrow,shapeNone),colorBrown,0,H,-15);   
PlotShapes(IIf(Short,shapeHollowCircle,shapeNone),colorBrown,0,ShortPrice,0);   
PlotShapes(IIf(Cover AND !Buy,shapeUpArrow,shapeNone),colorDarkGreen,0,L,-15);   
PlotShapes(IIf(Cover AND !Buy,shapeHollowCircle,shapeNone),colorDarkGreen,0,CoverPrice,0);   

If I did not do this, it would display marks when the buy/sell conditions are met (which the backtest does not take into account when using SetBacktestMode( backtestRegular )).

For example, using this simple trading system

_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
_SECTION_END();

// EXAMPLE TRADING SYSTEMA AMIBROKER 6.39.0

_SECTION_BEGIN( "GENERAL SETTINGS" );
//SETTINGS
SetChartOptions( 0, chartShowDates );

//OTHERS
SetOption( "FuturesMode", True );
SetOption( "Allowsamebarexit", True );
SetOption( "ReverseSignalForcesExit", false );
SetOption( "PriceBoundChecking", True );

SetBacktestMode( backtestRegular );// https://www.amibroker.com/guide/afl/setbacktestmode.html
_SECTION_END();

_SECTION_BEGIN( "MONEY MANAGMENT SETTINGS" );
//INITIAL EQUITY, ,
Ini = Param( "Initial Equity", 100000, 5000, 100000, 5000 );
SetOption( "initialequity", Ini ); // starting capital

//RISK AND POSITION
maxpositions = Param( "Simultaneous Positions", 1, 1, 100 );
SetOption( "MaxOpenPositions", maxpositions );

SetOption( "UsePrevBarEquityForPosSizing", 0 );

PointValue=50;
PositionSize = MarginDeposit = 1;

//COMISSIONS AND SLIPPAGE
Com = Param( "Comisiones y deslizamiento", 0, 0, 150, 10 );
SetOption( "CommissionMode", 2 );
SetOption( "commissionamount", Com );
_SECTION_END();

_SECTION_BEGIN("OSCILATOR");
// OSCILATOR CONDITION
OverSold = param("Oversold", 30, 5, 45, 5);
Overbought = param("Overbought", 70, 55, 95, 5);
lengthRSI = param("Length RSI", 14, 5, 20, 5);

RSIwork = RSI(lengthRSI);

//PLOTTING RSI 
pintarRSI = ParamToggle("Plot RSI","No|Yes", 0);

if(pintarRSI==1)
{
Plot(OverSold, "Oversold", colorgreen, styleLine + styleThick +  styleLeftAxisScale + styledashed);
Plot(Overbought, "Overbougth", colorred, styleLine + styleThick +  styleLeftAxisScale + styledashed);
Plot(RSIwork, "RSI", colororange, styleLine + styleThick +  styleLeftAxisScale);
}
_SECTION_END();

// TRADING SYSTEM
Buy = Cross(RSIwork, OverSold);
Sell = Cross(Overbought, RSIwork);

Short = Cover = 0;


// DELETING EXTRASIGNAL
Buy = ExRem(Buy, Sell OR Short);
Short = ExRem(Short, Cover OR Buy);
Sell = ExRem(Sell, Buy);
Cover = ExRem(Cover, Short);


// PLOTTING ACTIVATION SIGNALS
distancia = 3*ATR(14);
for(i=0; i<BarCount; i++)
{
if(Buy[i]) PlotText("buy\n" + BuyPrice[i], i, H[i]-distancia[i], colorGreen);
if(Sell[i] AND !Short[i]) PlotText("sell\n" + SellPrice[i], i, L[i]+distancia[i], colorRed);
if(Short[i]) PlotText("short\n" + ShortPrice[i], i,  L[i]+distancia[i], colorRed);
if(Cover[i] AND !Buy[i]) PlotText("cover\n" + CoverPrice[i], i, H[i]-distancia[i], colorGreen);
}

//PLOTTIN SHAPES//   
PlotShapes(IIf(Buy,shapeUpArrow,shapeNone),colorGreen,0,L,-15);   
PlotShapes(IIf(Buy,shapeHollowCircle,shapeNone),colorGreen,0,BuyPrice,0);   
PlotShapes(IIf(Sell AND !Short,shapeDownArrow,shapeNone),colorRed,0,H,-15);   
PlotShapes(IIf(Sell AND !Short ,shapeHollowCircle,shapeNone),colorRed,0,SellPrice,0);   
PlotShapes(IIf(Short,shapeDownArrow,shapeNone),colorBrown,0,H,-15);   
PlotShapes(IIf(Short,shapeHollowCircle,shapeNone),colorBrown,0,ShortPrice,0);   
PlotShapes(IIf(Cover AND !Buy,shapeUpArrow,shapeNone),colorDarkGreen,0,L,-15);   
PlotShapes(IIf(Cover AND !Buy,shapeHollowCircle,shapeNone),colorDarkGreen,0,CoverPrice,0);   

I Signals actually taken by the backtest are displayed

image

image

However, if I remove the Exrem() functions from the code, despite the fact that the backtest does not take into account the redundant signals, since it is working with SetBacktestMode( backtestRegular ), they are painted on the graph.

image

Am I doing it right or should I take some other alternative?

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