/// This sets up the bars on the chart
_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();
// This sets up the styles for the indicators
HHighPrice = ParamField("HHighPrice",1);
HHfastcolor = ParamColor("HHfastcolor", colorAqua );
FastStyle = ParamStyle("FastStyle", styleDashed | styleNoLabel);
//Default Settings
HH_lookback = 252;
HH_FilterNumDays = 3;
HH_TicksAbove = 1;
ATR_lookback = 5;
ATR_StopMulti = 1;
//To Optimize use these
//HH_lookback = optimize( "HH_lookback", 252, 252, 252, 2 );
//HH_FilterNumDays = optimize( "HH_FilterNumDays", 3, 3, 3, 1 );
//HH_TicksAbove = optimize( "HH_TicksAbove", 1, 1, 1, 1 );
//ATR_lookback = optimize( "ATR_lookback", 5, 5, 5, 1 );
//ATR_StopMulti = optimize( "ATR_StopMulti", 2, 2, 2, 1 );
//Plot the HighestHigh of HH_lookback bars back
HH = hhv( High, HH_lookback);
Plot(HH,"HH",HHfastcolor,FastStyle);
printf( "ref(HH,-1): " + NumToStr( ref(HH,-1) ) + "\n" );
HHbarsAgo = BarsSince(Ref(HH,0) > Ref(HH,-1));
printf( "HHbarsAgo: " + NumToStr( HHbarsAgo ) + "\n" );
myATR = ATR(ATR_lookback);
printf( "myATR: " + NumToStr( myATR ) + "\n" );
//// Strategy buy/sell signals //////////////////////////
SetBacktestMode(backtestRegular);
//SetOption("InitialEquity",100000 );
SetOption("AllowSameBarExit",true);
SetTradeDelays(0,0,0,0);
//SetOption("MaxOpenPositions",100000);
EntryQty = 1000 / Close;
SetPositionSize( EntryQty, spsShares );
BuyPrice = Max(Ref(HH,-1) + .01 * HH_TicksAbove, Open);
SellPrice = Open ;
Buy = High >= Ref(HH,-1) + .01 * HH_TicksAbove AND Ref(HHbarsAgo,-1) >= HH_FilterNumDays;
Sell = open;
buy = ExRem( buy, sell );
sell = ExRem( sell, buy );
ApplyStop(stopTypeLoss,stopModePoint,ATR_StopMulti * myATR,0,0,0);
//ApplyStop(stopTypeNBar,stopModeBars,nBars,0,True);
//ApplyStop(stopTypeProfit,stopModePoint,Ref(PSL,-1),1,False,0);
//ApplyStop(stopTypeTrailing,stopModePoint,Ref(TSL,-1),1,False,0);
Equity(1); // THIS EVALUATES STOPS
Plot(Sell==4,"ApplyStop Sell",colorRed,1|styleOwnScale);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorBlue, 0, BuyPrice, Offset=-15);
PlotShapes(IIf(sell, shapeDownArrow, shapeNone),colorWhite, 0, SellPrice, Offset=-15);
No matter what I do, I cannot get the stop to exit on the same day as the entry. See this snapshot:
See how the stop is always one day later. I've read about:
SetOption("AllowSameBarExit",true);
SetTradeDelays(0,0,0,0);
ApplyStop(stopTypeLoss,stopModePoint,ATR_StopMulti * myATR,0,0,0);
but no matter what combinations of these I try it always exits on the next day.