Hello,
II should appreciate it if someone would help me to find the correct code to prevent buying after selling on the same bar.
Since there is no SetOption("AllowSameBarEntry", False), I tryed the following code after de buy & sell rules:
for( i = 0; i < BarCount; i++ )
{
if( Sell[ i ] )
{
Buy[ i ] = False;
}
}
However, I does not work as I supposed, as it is shown in this image (Besides, it makes the trailingstop not being shown after that purchase):
How I should code that? I paste the complete system code at the end of the message.
Thank you beforehand for your help.
Best regards.
// --- Inputs ---
ParamMainAsset = ParamStr("Main Asset", "SPY");
// --- Backtester settings ---
SetOption("CommissionMode" , 1);
SetOption("CommissionAmount", 0);
SetOption("InitialEquity", 10000);
SetOption("MaxOpenshort", 0);
SetOption("MaxOpenLong", 1);
SetOption("MaxOpenPositions", 1);
SetOption("AllowPositionShrinking", True);
SetOption("MinShares", 0);
SetOption("HoldMinBars", 1);
SetPositionSize(100, spsPercentOfEquity);
SetTradeDelays(0, 0, 0, 0);
SetBacktestMode(backtestRegular);
// --- Variables ---
PRD1 = 8;
PRD2 = 16;
STOP = 16;
// --- Conditions ---
MainClose = Foreign(ParamMainAsset, "Close");
BuyCondition = EMA(MainClose, PRD1) > EMA(MainClose, PRD2) AND MainClose > EMA(MainClose, PRD2);
SellCondition = EMA(MainClose, PRD1) < EMA(MainClose, PRD2);
Buy = BuyCondition ;
Sell = SellCondition;
BuyPrice = Close;
SellPrice = Close;
for( i = 0; i < BarCount; i++ )
{
if( Sell[ i ] )
{
Buy[ i ] = False;
}
}
ApplyStop(stopTypeTrailing, stopModePercent, STOP);
// --- Chart ---
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Close %g (%.1f%%) {{VALUES}}", C, SelectedValue(ROC(C,1))));
Plot(C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | GetPriceStyle());
Plot(EMA(Close, PRD1), "EMA 1", colorblue, styleLine + styleDashed + styleNoTitle, Null, Null, 0, 0, 2);
Plot(EMA(Close, PRD2), "EMA 2", colorred, styleLine + styledashed + styleNoTitle, Null, Null, 0, 0, 2);
Equity( 1, 0 );
InTrade = Flip( Buy, Sell );
SetOption("EveryBarNullCheck", True );
StopLine = IIf( InTrade, HighestSince( Buy, High ) * ( 1 - 0.01 * STOP ), Null );
PlotShapes(Buy*shapeUpArrow,colorGreen,0,Low);
PlotShapes(Sell*shapeDownArrow,colorRed,0,High);
Plot(Close,"Price",colorBlack,styleBar);
Plot(StopLine, "Trailing Stop", colorRed, styleLine, Null, Null, 0, 0, 2);