Hi everyone,
I could really do with some help on understanding why the applystops function is not behaving as I would expect. It’s probably something simple, but I have spent hours now trying to figure it out and am stuck. Help to resolve is much appreciated.
The Problem
- The system uses a EMA cross-over to generate the buy signal.
- It is a long only system.
- It uses daily bars only.
- It buys the next day at the Open price.
- It uses a trailing volatility stop. The points being calculated using 5x the ATR(30)
- The stop should trail from the Highest High since the Buy was executed.
- When the close price is lower than the trailing volatility stop the system should exit the next day at the Open Price.
Please see below for the code I have produced. Please also look at the chart.
As you can see the bar with the vertical blue line running through it clear closed below the trailing volatility stop (the dark blue line on the chart is the plot of the trailing volatility stop level).
I would expect to see a hollow red triangle above this bar indicating the trailing volatility stop has been hit. In addition, the back test would show the trade as closed. Instead, I am seeing what I have posted.
Thanking you in advance for help.
_SECTION_BEGIN ("Crypto Trading Strategy");
// System Variables
EMA21 = EMA(Close,21);
EMA55 = EMA(Close,55);
EMA144= EMA(Close,144);
StopLossTF = ATR(30) * 5 ;
// System Parameters
SetOption( "InitialEquity", 4000);
SetTradeDelays( 1, 1, 1, 1 );
SetPositionSize(100,spsPercentOfEquity);
//Entry and Exit Criteria
LongEntry1 = Cross (EMA(Close,21), EMA(Close,55)) ;
///////////// Back-test ////////////////////////////////////
//Back-test signals
Buy = LongEntry1;
Sell = 0 ;
Short = 0 ;
Cover = 0;
//Back-test Prices
BuyPrice = Open ;
SellPrice = Open ;
ShortPrice = 0 ;
CoverPrice = 0 ;
//STOP METHOD - USES EOD CLOSE
ApplyStop (stopTypeTrailing,stopModePoint, StopLossTF, ExitAtStop = 0, Volatile = True, 0 ); // Trailing Points stop loss based on 5X ATR(30) days - check close prices and exit NEXT day (BAR) on SellPrice = Open.
/////////////Plotting Chart Title, EMA's and Stop Loss /////////
//Plot Chart and Title
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} - {{DATE}} - Open %g, Hi %g, Lo %g, Close %g (%.2f%%) {{VALUES}}", O, H, L, C, SelectedValue((Close-Ref(C,-1))/Ref(C,-1)*100) ));
//Plot the trailing stop Long
Equity( 1, 0); //evaluate stops, all quotes
intradeLong = Flip(Buy, Sell) ; //Returns True when Buy, False when Sell
SetOption("EveryBarNullCheck", True); //Checks for null bars
StoplineLong = IIf(intradeLong, HighestSince(Buy, H - StopLossTF), NULL);
Plot( stoplineLong, "5X ATR", colorBlue, styleLine,0,0,0,0);
//Plot the Indicators
Plot(Close,"Close",colorBlack,styleCandle);
Plot(EMA21, "EMA 21",colorRed, styleLine);
Plot(EMA55, "EMA 55",colorGreen, styleLine);
Plot(EMA144,"EMA 144",colorAqua, styleLine);
//Plot Buy and Sell Arrows
PlotShapes(shapeUpArrow * Buy, colorGreen, 0, Low, -12);
PlotShapes(shapeDownArrow * Sell , colorRed, 0, High, -12);
_SECTION_END();