Hello Forum,
I have a strategy that works fine however at times I notice that during intraday scanner generates some signals but after trading hours that trade doesnt appear in backtest. Otherway round too some trades that appear in backtest do not appear during market hours running a scanner. I am not using any forward looking indicator or refer future quotes so this is not a typical repainting problem.
I run this strategy on a 15 minute timeframe and for one indicator I switch to 4 hourly time frame. I suspect I am not doing it right and that is causing the issue but not sure how to fix that. Below is my code. Please let me know what's wrong in it which is causing the issue.
Pardon me for some redudant code as I am still developing it. Thanks in advance for any help on this.
_SECTION_BEGIN("Ripple");
OUTcolor = ParamColor("Outer Panel Color",colorYellow);
SetBarsRequired(100000,0);
GraphXSpace = 15;
SetChartOptions(0,chartShowArrows|chartShowDates);
SetChartBkColor(ParamColor("bkcolor",ColorRGB(0,0, 0)));
GfxSetBkMode(0);
GfxSetOverlayMode(1);
SetBarFillColor(IIf(C>O,ParamColor("Candle UP Color", colorGreen),IIf(C<=O,ParamColor("Candle Down Color", colorRed),colorLightGrey)));
Plot(C,"\nPrice",IIf(C>O,ParamColor("Wick UP Color", colorDarkGreen),IIf(C<=O,ParamColor("Wick Down Color", colorDarkRed),colorLightGrey)),64,0,0,0,0);
//200 SMA
Plot(MA(C,200),"",52,4);
//SetTradeDelays(0,0,0,0);
SetTradeDelays(0,0,0,0);
_N(Title = StrFormat( " Ripple " + "{{Name}} - {{Interval}} {{Date}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
//Magfied Market Price
FS=Param("Font Size",30,11,100,1);
GfxSelectFont("Times New Roman", FS, 700, True );
GfxSetBkMode( colorWhite );
GfxSetTextColor( ParamColor("Color",colorGreen) );
Hor=Param("Horizontal Position",940,1,1200,1);
Ver=Param("Vertical Position",12,1,830,1);
GfxTextOut(""+C, Hor , Ver );
YC=TimeFrameGetPrice("C",inDaily,-1);
DD=Prec(C-YC,2);
xx=Prec((DD/YC)*100,2);
GfxSelectFont("Times New Roman", 11, 700, True );
GfxSetBkMode( colorBlack );
GfxSetTextColor(ParamColor("Color",colorYellow) );
GfxTextOut(""+DD+" ("+xx+"%)", Hor , Ver+45 );
_SECTION_END();
_SECTION_BEGIN("Dynamic Support Resistance Ripple");
BuyPrice=Close;
SellPrice=Close;
ShortPrice=Close;
CoverPrice=Close;
LongRippleperiod=Param("LongRippleperiod",5,1,12,1);
res=HHV(H,LongRippleperiod); //resistance
sup=LLV(L,LongRippleperiod); //support
DynamicPivotLong=IIf(ValueWhen(IIf(C>Ref(res,-1),1,IIf(C<Ref(sup,-1),-1,0))!=0,IIf(C>Ref(res,-1),1,IIf(C<Ref(sup,-1),-1,0)),1)==1,sup,res);
//If current close breaks the resistance then dynamic pivot level would be last support, if close breaks the support then dynamic pivot level will be last resistance
Plot(DynamicPivotLong, "DynamicPivotLong", colorBlue, styleStaircase);
pBuy=Cross(C,DynamicPivotLong) ;
ShortRippleperiod=Param("ShortRippleperiod",5,1,12,1);
res=HHV(H,ShortRippleperiod); //resistance
sup=LLV(L,ShortRippleperiod); //support
DynamicPivotShort=IIf(ValueWhen(IIf(C>Ref(res,-1),1,IIf(C<Ref(sup,-1),-1,0))!=0,IIf(C>Ref(res,-1),1,IIf(C<Ref(sup,-1),-1,0)),1)==1,sup,res);
//If current close breaks the resistance then dynamic pivot level would be last support, if close breaks the support then dynamic pivot level will be last resistance
Plot(DynamicPivotShort, "DynamicPivotShort", colorRed, styleStaircase);
pShort=Cross(DynamicPivotShort,C) ;
_SECTION_BEGIN("CCI CCIRipple");
LongCCIRippleperiods = Param( "LongCCIRippleperiods", 20, 2, 100, 1);//88
CCIRippleBuyLevel = Param( "CCIRippleBuyLevel", 0, 0, 100,20);//80
CBuy =IIf(CCI(LongCCIRippleperiods)>CCIRippleBuyLevel,1,0);
ShortCCIRippleperiods = Param( "ShortCCIRippleperiods", 2, 2, 30, 2);
CCIRippleSellLevel = Param( "CCIRippleSellLevel", -0, -100, -0,20);
CSell =IIf(CCI(ShortCCIRippleperiods)<CCIRippleSellLevel,1,0);
//Plot( CCI( periods ), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle("Style") );
_SECTION_END();
//sPrevRippleCall=Nz(StaticVarGet("PrevRippleCall"));
_SECTION_BEGIN("RSI");
RSIperiods = Param( "RSIPeriods", 14, 9, 21, 1 );
RSIBuyLevel = Param("RSIBuyLevel",50,30,60,2);
RSISellLevel = Param("RSISellLevel",50,40,70,2);
sBuy=IIf(RSI(RSIPeriods)>RSIBuyLevel,1,0);
sSell=IIf(RSI(RSIPeriods)<RSISellLevel,1,0);
_SECTION_END();
_SECTION_BEGIN("Stochastic %K Long Ripple");
LongRippleKperiods = Param( "LongRippleKperiods", 5, 2, 8, 1 );
LongRippleKsmooth = Param( "LongRippleKsmooth", 14, 3, 17,1 );
_SECTION_END();
_SECTION_BEGIN("Stochastic %D Long Ripple");
LongRippleDperiod = Param( "LongRippleDperiod", 5, 2,8, 1 );
LongRippleDsmooth = Param( "LongRippleDsmooth", 2, 2, 6,1);
_SECTION_END();
_SECTION_BEGIN("Stochastic %K Short Ripple");
ShortRippleKperiods = Param( "Shortrude3MinScalpKperiods", 6,3, 9, 1);
ShorRippleKsmooth = Param( "ShortRippleKsmooth", 8, 5,11,1 );
_SECTION_END();
_SECTION_BEGIN("Stochastic %D Short Ripple");
ShorRippleDperiod = Param( "ShortRippleDperiod", 4, 2,6, 1 );
ShortRippleDsmooth = Param( "ShortRippleDsmooth", 3, 2, 6,1);
_SECTION_END();
SCrossB=StochK(LongRippleKperiods,LongRippleKsmooth) > StochD(LongRippleDperiod,LongRippleKsmooth,LongRippleDsmooth);
SCrossS=StochK(ShortRippleKperiods,ShorRippleKsmooth) < StochD(ShorRippleDperiod,ShorRippleKsmooth,ShortRippleDsmooth);
RippleOverSold=Param( "RippleOverSold Level", 20, 10, 30, 10);
RippleOverBought=Param("RippleOverBought Level", 80, 70, 90, 10);
RippleShortThreshold=Param( "RippleShortThreshold", 20, 20, 80, 10 );
RippleLongThreshold=Param( "RippleLongThreshold", 60, 20, 80, 10 );
tBuy=IIf(StochK(LongRippleKperiods,LongRippleKsmooth)>RippleOverSold,IIf(StochK(LongRippleKperiods,LongRippleKsmooth)<RippleLongThreshold,1,0),0);
tSell=IIf(StochK(ShortRippleKperiods,ShorRippleKsmooth)<RippleOverBought,IIf(StochK(ShortRippleKperiods,ShorRippleKsmooth)>RippleShortThreshold,1,0),0);
/*
StochasticSlope = Param("StochasticSlope",10,2,20,1);
SlopeAngle = Param("Angle",0.2,0.1,1.1,0.1);
Slope = ROC( StochK(periods,Ksmooth), StochasticSlope);//measuring slope over 5 bars, adjust as needed
SlopeAbove = Slope > SlopeAngle;//adjust ROC limit as needed
SlopeBelow = Slope < - SlopeAngle;//adjust ROC limit as nee
*/
/*
fBuy=IIf(xSp0 > xRp0,1,0);
fShort=IIf(xSp0 > xRp0,0,1);
*/
_SECTION_BEGIN("VolumeHigh Long Ripple");
LongCurrentPeriodVolumeRipple = Param( "CurrentVolumeAverage Long Ripple", 3, 1, 20, 1 );
LongAverageVolumePeriodRipple =Param( "AverageVolumePeriod Long Ripple", 3, 2, 30, 1);
LongVolumeHighRipple = IIf((TEMA(V, LongAverageVolumePeriodRipple) < TEMA(V, LongCurrentPeriodVolumeRipple)),1,0);
_SECTION_END();
_SECTION_BEGIN("VolumeHigh Short Ripple");
ShortCurrentPeriodVolumeRipple = Param( "CurrentVolumeAverage Short Ripple", 14, 1, 15, 1 );
ShortAverageVolumePeriodRipple =Param( "AverageVolumePeriod Short Ripple", 10, 2, 30, 1);
ShortVolumeHighRipple = IIf((TEMA(V, ShortAverageVolumePeriodRipple) < TEMA(V, ShortCurrentPeriodVolumeRipple)),1,0);
_SECTION_END();
RippleTickSize=Param("RippleTickSize",1,0.4,1.5,0.05);
bigTick = IIf(O>C,IIf(O-C>RippleTickSize,0,1),IIf(C-O>RippleTickSize,0,1));
_SECTION_BEGIN("Exit Strategy Ripple");
ExitRSIPeriodRippleB = Param( "ExitRSIPeriod RippleB", 14,4, 24,1);
ExitMAPeriodRippleB =Param( "ExitMAPeriod RippleB", 21,1, 50,1);
ExitBuy= Cross(MA(RSI(ExitRSIPeriodRippleB),ExitMAPeriodRippleB),RSI(ExitRSIPeriodRippleB));
ExitRSIPeriodRippleS = Param( "ExitRSIPeriod RippleS", 14,14, 64,2);
ExitMAPeriodRippleS =Param( "ExitMAPeriod RippleS", 28,2, 34,1);
ExitShort=Cross(RSI(ExitRSIPeriodRippleS),MA(RSI(ExitRSIPeriodRippleS),ExitMAPeriodRippleS));
_SECTION_END();
TimeFrameSet(inHourly*4);
_SECTION_BEGIN("MACD");
r1 = Param( "Fast avg", 12, 2, 200, 1 );
r2 = Param( "Slow avg", 26, 2, 200, 1 );
r3 = Param( "Signal avg", 9, 2, 200, 1 );
Plot( ml = MACD(r1, r2), StrFormat(_SECTION_NAME()+"(%g,%g)", r1, r2), ParamColor("MACD color", colorRed ), ParamStyle("MACD style") );
Plot( sl = Signal(r1,r2,r3), "Signal" + _PARAM_VALUES(), ParamColor("Signal color", colorBlue ), ParamStyle("Signal style") );
Plot( ml-sl, "MACD Histogram", ParamColor("Histogram color", colorDefault ), styleNoTitle | ParamStyle("Histogram style", styleHistogram | styleNoLabel, maskHistogram ) );
_SECTION_END();
SlowMacdB=IIf(sl<ml,1,0);
SlowMacdS=IIf(sl>ml,1,0);
TimeFrameRestore();
/*
TimeFrameSet(inHourly);
_SECTION_BEGIN("MACD Hourly");
r1Hourly = Param( "Fast avg Hourly", 12, 2, 200, 1 );
r2Hourly = Param( "Slow avg Hourly", 26, 2, 200, 1 );
r3Hourly = Param( "Signal avg Hourly", 9, 2, 200, 1 );
Plot( mlHourly = MACD(r1Hourly, r2Hourly), StrFormat(_SECTION_NAME()+"(%g,%g)", r1Hourly, r2Hourly), ParamColor("MACD color", colorRed ), ParamStyle("MACD style") );
Plot( slHourly = Signal(r1Hourly,r2Hourly,r3Hourly), "Signal" + _PARAM_VALUES(), ParamColor("Signal color", colorBlue ), ParamStyle("Signal style") );
Plot( mlHourly-slHourly, "MACD Histogram", ParamColor("Histogram color", colorDefault ), styleNoTitle | ParamStyle("Histogram style", styleHistogram | styleNoLabel, maskHistogram ) );
_SECTION_END();
SlowMacdBHourly=IIf(slHourly>mlHourly,1,0);
SlowMacdSHourly=IIf(slHourly<mlHourly,1,0);
TimeFrameRestore();
*/
_SECTION_BEGIN("ADX");
range = Param("Periods", 14, 2, 200, 1 );
Plot( ADX(range), _DEFAULT_NAME(), ParamColor( "ADX color", colorBlue ), ParamStyle("ADX style", styleThick ) );
Plot( PDI(range), "+DI", ParamColor( "+DI color", colorGreen ), ParamStyle("+DI style") );
Plot( MDI(range), "-DI", ParamColor( "-DI color", colorRed ), ParamStyle("-DI style") );
ADXBetweenB= IIf(ADX(range)<PDI(range),IIf(ADX(range)>MDI(range),1,0),0);
ADXBetweenS= IIf(ADX(range)>PDI(range),IIf(ADX(range)<MDI(range),1,0),0);
ADXb=IIf(PDI(range)>MDI(range),1,0);
ADXs=IIf(PDI(range)<MDI(range),1,0);
ADXSlopeBPeriod = Param("ADXSlopePeriod", 9, 1, 15, 1 );
ADXSlopeBLevel = Param("ADXSlopeLevel", 1.5, 0.1, 2.5, 0.1);
ADXRocB= IIf(ROC(ADX(range),ADXSlopeBPeriod)>ADXSlopeBLevel,1,0);
ADXSlopeSPeriod = Param("ADXSlopeSPeriod", 9, 2, 14, 1 );
ADXSlopeSLevel = Param("ADXSlopeSLevel", 1.5, 0.3, 2.5, 0.1);
ADXRocS= IIf(ROC(ADX(range),ADXSlopeSPeriod)<ADXSlopeSLevel,1,0);
ADXLevelLower = Param("ADXLevelLower", 10, 4, 15, 1 );
ADXLevelUpper = Param("ADXLevelUpper", 57, 30, 70, 1 );
ADXRangeStrength= IIf(ADX(range)>ADXLevelLower AND ADX(range)<ADXLevelUpper,1,0);
_SECTION_END();
_SECTION_BEGIN("Bollinger Bands");
P = ParamField("Price field",-1);
Periods = Param("Periods", 20, 10, 25, 5 );
Width = Param("Width", 2, 1, 3, 0.5 );
Color = ParamColor("Color", colorCycle );
Style = ParamStyle("Style");
Plot( BBandTop( P, Periods, Width ), "BBTop" + _PARAM_VALUES(), Color, Style );
Plot( BBandBot( P, Periods, Width ), "BBBot" + _PARAM_VALUES(), Color, Style );
//BBb = IIf(C>BBandTop( P, Periods, Width ) ,1,0);
//BBs = IIf(C<BBandBot( P, Periods, Width ) ,1,0);
BBb = Cross(C,BBandTop( P, Periods, Width ));
BBs = Cross(BBandBot( P, Periods, Width ) ,C);
_SECTION_END();
/*
_SECTION_BEGIN("Move Percent Ripple");
MovePercentThresholdRippleB= Param( "MovePercentThresholdRippleB", 3.4,1.2, 3.8,0.1);
MoveOkRippleB = IIf(((100*(C- Ref(C,-1)))/Ref(C,-1))< MovePercentThresholdRippleB,1,0);
MovePercentThresholdRippleS= Param( "MovePercentThresholdRippleS", 0.7,0.1, 4,0.1);
MoveOkRippleS = IIf(((100*(Ref(C,-1)- C))/C)< MovePercentThresholdRippleS,1,0);
_SECTION_END();
*/
_SECTION_BEGIN("Move Percent");
MovePercentThresholdRippleB= Optimize( "ExitRSIPeriod RippleB", 0.9,0.5, 3,0.1);
MoveOkRippleB = IIf(((100*(C-Ref(C,-1)))/100)< MovePercentThresholdRippleB,1,0);
MovePercentThresholdRippleS= Optimize( "ExitRSIPeriod RippleS", 2.3,0.5, 3,0.1);
MoveOkRippleS = IIf(((100*(Ref(C,-1)- C))/100)< MovePercentThresholdRippleS,1,0);
_SECTION_END();
_SECTION_BEGIN("Tick Trend");
TickTrendB = IIf(C>O,1,0);
TickTrendS = IIf(C<O,1,0);
_SECTION_END();
maCrossBuy=maCrossSell=0;
LmaBRipplePeriod=Param("LmaBRipplePeriod",2,1,50,1);
LmaSRipplePeriod=Param("LmaSRipplePeriod",170,1,200,1);
maCrossBuy=IIf(C>MA(Close,LmaBRipplePeriod),1,0);
maCrossSell=IIf(C<MA(Close,LmaSRipplePeriod),1,0);
SetOption("InitialEquity", 10000000);
PosQty = 100;
SetOption("MaxOpenPositions", PosQty );
PositionSize = -100/PosQty;
tn = TimeNum();
RipplestartTime = Param("Ripple Start Time",90000,90000,110000,15); // start in HHMMSS format
RippleendTime = Param("Ripple End Time",144505,220000,234500,15); // end in HHMMSS format
timeOK = tn >= RipplestartTime AND tn <= RippleendTime;
Buy=Short=0;
//VolumeHighRipple &&
//Buy = tBuy && SCrossB && TimeOK && cBuy && bigTick && LongVolumeHighRipple ;// && pBuy;// // && fBuy;
//Short = tSell && SCrossS && bigTick && TimeOK && cSell && ShortVolumeHighRipple;// && pShort ;// && fShort;
Buy = TickTrendB && MoveOkRippleB && ADXBetweenB && ADXb && BBb && TimeOK && ADXRocB && sBuy && SlowMacdB;// && sCrossB && tBuy ;// && maCrossBuy && LongVolumeHighRipple ; //; // && // && pBuy;// // && fBuy;
Short = TickTrendS && MoveOkRippleS && ADXBetweenS && ADXs && BBs && TimeOK && ADXRocS && sSell && SlowMacds;// && sCrossS && tSell ;// && maCrossSell && ShortVolumeHighRipple;// ;// && pShort ;// && fShort;
DynamicEMASLLevelB=Param("DynamicEMASLLevelB",15,5,21,1);
DynamicEMASLLevelS=Param("DynamicEMASLLevelS",15,9,21,1);
//Cover = C>EMA(C,DynamicEMASLLevelS) OR Cross(tn,RippleendTime);// OR ExitShort;
//Sell= C<EMA(C,DynamicEMASLLevelB) OR Cross(tn,RippleendTime);// OR ExitBuy;
Cover = ExitShort OR Cross(tn,RippleendTime);// OR ExitShort;
Sell= ExitBuy OR Cross(tn,RippleendTime);// OR ExitBuy;
RippleSL= Param("RippleSL",2,1.5,4,0.1);
RipplePL= Param("RipplePL",0.6,0.5,4,0.1);
ApplyStop(Type=0,Mode=1,RippleSL,1);
ApplyStop(Type=1,Mode=1,RipplePL,1);
/* Plot Buy and Sell Signal Arrows */
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
/*
PlotShapes(IIf(Cover, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Cover, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Cover, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);
*/
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);
Title = EncodeColor(colorWhite)+ "Ripple Ripple Strategy" + " - " + Name() + " - " + EncodeColor(colorRed)+ Interval(2) + EncodeColor(colorWhite) +
" - " + Date() +" - "+"\n" +EncodeColor(colorRed) +"Op-"+O+" "+"Hi-"+H+" "+"Lo-"+L+" "+
"Cl-"+C+" "+ "Vol= "+ WriteVal(V)+"\n"+ EncodeColor(colorLime)+
WriteIf (Buy , " GO LONG / Reverse Signal at "+C+" ","")+
WriteIf (Short , " EXIT LONG / Reverse Signal at "+C+" ","")+"\n"+EncodeColor(colorYellow)+
WriteIf(Short , "Total Profit/Loss for the Last Trade Rs."+(C-BuyPrice)+"","")+
WriteIf(Buy , "Total Profit/Loss for the Last trade Rs."+(ShortPrice-C)+"","");
_SECTION_END();
Thanks,
Ganesh