Hi,
The follg. code generates buy and sell signal perfectly in real time while exploring. But when back testing, it create 2 problems:
1. It misses the signal in which the SL hits in the same bar after the signal is generated.
2. It takes the high of the trigger bar as trigger price in case of buy instead of the low of the prv bar as per strategy.
Pls. help. Also tell me if I have written the Apply stop function correctly for sl and tgt as 1% of equity.
Here is the code:
_SECTION_BEGIN("simple reversal");
// to plot price
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) Vol " +WriteVal( V, 1.0 ) +" {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 )) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
//buy and short conditions
Cond1 = Ref ( Low, -1 ) < Ref ( Low, -2 );
Cond2 = Ref ( Close, -1 ) > Ref ( Close, -2 );
Cond3 = Ref ( Close, -1 ) > Ref ( Open , -1);
Cond4 = Ref ( High , -1 ) < Ref ( High , -2 );
Cond5 = Ref (RSI (14), -2) < 30;
Cond6 = Ref (RSI (14) , -1 ) > 30 ;
Cond7 = Ref (Low, -1 ) < Ref (LLV (Low, 8) , -1 );
Cond8 = High > Ref ( High, -1 );
Cond9 = Ref ( High, -1) > Ref (High, -2 );
Cond10 = Ref ( Close, -1 ) < Ref ( Close, -2 );
Cond11 = Ref ( Close, -1 ) < Ref ( Open , -1);
Cond12 = Ref ( Low , -1 ) > Ref ( Low, -2);
Cond13 = Ref ( RSI (14) , -2 ) > 70 ;
Cond14 = Ref ( RSI (14) , -1 ) < 70 ;
Cond15 = Low < Ref (Low, -1) ;
Cond16 = Ref (High, -1 ) > Ref ( HHV (High ,8) , -1 );
Cond17 = TimeNum() < 143100 ;//signals after 2.30 r ignored
Cond18 = TimeNum() > 093100 ; // signals bfor 9.30 r ignored
Buy = Cond1 AND Cond2 AND Cond3 AND Cond4 AND Cond5 AND Cond6 AND Cond7 AND Cond8 AND Cond17 AND Cond18 ;
Short = Cond9 AND Cond10 AND Cond11 AND Cond12 AND Cond13 AND Cond14 AND Cond15 AND Cond16 AND Cond17 AND Cond18 ;
R = Ref (High ,-1 ) - Ref ( Low , -1);
if(Status("action") == actionExplore);
Filter = Buy | Short;
SetOption ("NoDefaultColumns", True );
AddTextColumn(Name(), "SYMBOL");
AddColumn(DateTime(), "DATE", formatDateTime);
AddColumn(IIf(Buy, 66, 83), "TRIGGER", formatChar, colorWhite, IIf(Buy, colorGreen, colorRed));
AddColumn(IIf(Buy, Ref ( High , -1), Ref ( Low, -1 )), "TRIGGER PRICE", 1.2);
AddColumn(IIf(Buy, Ref(Low ,-1), Ref(High , -1)) , "Stop Loss", 1.2);
AddColumn(IIf(Buy, Ref (High , -1) + R , Ref (Low , -1) - R ), "Target", 1.2);
AddColumn(IIf(Buy, 420/R ,420/R ),"shares",1.2 );
//ApplyStop(stopTypeLoss, stopModePercent, 1, 1);
//ApplyStop(stopTypeProfit , stopModePercent, 1, 1);
//to get alert via speakers and in alert output window with the text put in aphostrophies
AlertIf( Buy, "SOUND C:\\WINDOWS\\Media\\Raga.wav", "REVBUY", 1 );
AlertIf( Short, "SOUND C:\\Windows\\Media\\Raga.wav", "REVSELL", 3 );
//to plot up arrow and down arrow in chart for buy and short signal
PlotShapes(shapeUpArrow*Buy,colorBrightGreen,0,L);
PlotShapes (shapeDownArrow*Short, colorRed, 0, H);
_SECTION_END() ;
cond8 and cond15 is the trigger to buy and sell. i.e if it crosses prv bar high/low. rest of the conditions indicate buy/sell set up.
Pls. help.
Regards,