I don't think I fully understand what Buy[i] = 0;
does in the context of the following two examples:
How to plot a trailing stop in the Price chart
The specific line in this example is:
else Buy[ i ] = 0; // remove excess buy signals
Example 4: partial exit (scaling out) on profit target stops
The specific line in this example is:
Buy[ i ] = 0;
I am seeing two issues in my attempted recreation of this code.
-
When I use
else Buy[ i ] = 0;
like the first example and test it with it there and with it commented out, I don't see any differences. So I'm not clear what it is doing. -
When I use
Buy[ i ] = 0;
in a similar context to the second example a huge problem occurs - if I use an unusually small stop level, the strategy will not take valid signals that would violate the intraday stop. As an example, you can change the stop level to 2 and watch all the entries disappear on the chart that have breakout candles of larger than 2%.
I originally thought that Buy[i] = 0;
was resetting the Buy
variable once the trailing stop or profit target executed so that the strategy could accept a new Buy entry when a new signal was given. But the two points above have me questioning this.
The code I was using to experiment with this is below:
// ==========================================================================================
// === TRADING SYSTEM PARAMETERS ===
// ==========================================================================================
SetOption("InitialEquity", 100000);
SetTradeDelays( 0, 0, 0, 0 ); // 1 - Delays the trades to the next day, 0 - Trades same day via stops
SetOption("AllowPositionShrinking", True);
SetOption("AccountMargin",100); // 100 = Cash account, 75 = Using half available margin
SetOption("MaxOpenPositions", 20);
RoundLotSize = 1; // Disallow fractional share purchasing
// ==========================================================================================
// === RISK MANAGEMENT & POSITION SIZING ===
// ==========================================================================================
// Initial (MAX) stop loss
SetOption("ActivateStopsImmediately", False);
stopLoss = 10; // Change this to a small number to experience the issue with trade entries being ignored
ApplyStop(stopTypeLoss, stopModePercent, stopLoss, ExitAtStop = 1);
// Position size based on risk and max stop loss
PositionRisk = 1;
PosSize = 100 * PositionRisk / stopLoss;
SetPositionSize(PosSize, spsPercentOfEquity);
// ==========================================================================================
// === PLOT CHART & INDICATORS ===
// ==========================================================================================
// Chart
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - " + FullName() + " | {{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() );
// Donchian Channel
pdsU=10; // High channel lookback
pdsL=5; // Low channel lookback
DonchianUpper =HHV(H,pdsU);
DonchianLower = LLV(L,pdsL);
Plot(DonchianUpper,"DU",color = colorBlueGrey,styleDashed);
Plot(DonchianLower,"DL",color = colorBlueGrey, styleDashed);
// ==========================================================================================
// === BUY & SELL LOGIC ===
// ==========================================================================================
Buy=Cross(High, Ref(DonchianUpper, -1));
//Buy= (High > Ref(DonchianUpper, -1));
BuyPrice=Max(Ref(DonchianUpper, -1), Open);
// === TRAILING STOP ===
// Initialize variables
Sell=0;
trailArray = Null;
trailStop = 0;
//Trailing stop logic
for( i = 0; i < BarCount; i++ )
{
if( trailstop == 0 AND Buy[ i ] )
{
trailstop = BuyPrice[i] * (1- stopLoss/100);
}
//else Buy[i] = 0; // remove excess buy signals
if( trailstop > 0 AND Low[ i ] < trailstop )
{
Sell[ i ] = 1;
SellPrice[ i ] = Min( Open[ i ], trailstop );
trailstop = 0;
Buy[i] = 0; // this is preventing entries when stop loss setting is smaller than candle range
}
if( trailstop > 0 )
{
trailStop = Max(DonchianLower[i], trailStop);
trailARRAY[ i ] = trailstop;
}
}
Plot(trailArray, "Trailing Stop Level", colorRed, styleThick);
// Hide buy and sell signals if in position so that it doesn't plot extra arrows
Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);
// Plot arrows indicating trades
PlotShapes(IIf(Buy, shapeUpTriangle, shapeNone),color = colorLime, 0, L, Offset=-50);
PlotShapes(IIf(Sell, shapeDownTriangle, shapeNone),color = colorRed, 0, H, Offset=-50);