Hello everyone,
Below is the backtesting that i want to do,
Condition: Price breach donchian 50 hourly high
Entry: Price starts trading above the high dc50 hourly candle by 0.01%.
Stoploss: 0.5 ATR 14 (daily)from DC50 level
Target: 1.5 ATR14(daily)
//Reusing the below code from Trading Tuitions website.
//------------------------------------------------------
//
// Formula Name: Donchian Channel trading System
// Author/Uploader: Trading Tuitions
// E-mail: support@tradingtuitions.com
// Website: www.tradingtuitions.com
//------------------------------------------------------
_SECTION_BEGIN("Donchian Channel trading System");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g {{VALUES}}", O, H, L, C ));
//Initial Parameters
SetTradeDelays( 0,0,0, 0 );
SetOption( "InitialEquity", 200000);
SetOption("FuturesMode" ,True);
SetOption("MinShares",1);
SetOption("CommissionMode",2);
SetOption("CommissionAmount",100);
SetOption("AccountMargin",10);
SetOption("RefreshWhenCompleted",True);
SetPositionSize(10,spsShares);
SetOption( "AllowPositionShrinking", True );
Plot( Close, "Price", colorWhite, styleCandle );
//Get details of the hourly candle
TimeFrameSet(inHourly);
DonchianUpper =HHV(Ref(H,0),50);
DonchianLower = LLV(Ref(L,0),50);
fifteenMinuteClose = Ref(C,1);
fiteenMinuteHigh = Ref(H,0);
TimeFrameRestore();
//Get details of the daily ATR
TimeFrameSet(inDaily);
atrValue = Ref(ATR(14),-1);
TimeFrameRestore();
newday = Day() != Ref(Day(),-1);
//get the DChigh during day open.
DCHighForDay = ValueWhen(newday,donchianUpper);
highForDay = DCHighForDay;
//Get the first time, when the DC50 high was breached.
highForDay = ValueWhen(fiteenMinuteHigh >= DCHighForDay AND newday,fiteenMinuteHigh+fiteenMinuteHigh*0.001);
// now check, if the close is greate the our buy condition.
Buy= Cross(fiteenMinuteHigh,highForDay);//fifteenMinuteClose>=conditionForBuy;
Sell=0;
BuyPrice=highForDay;
atrDiffValue = IIf(Buy AND (highForDay-DCHighForDay)>atrValue,True,False);
atrTrailingStop = IIf(Buy AND (highForDay-DCHighForDay)>atrValue, atrValue,0.5*atrValue);
printf("\nDCForDay: " + DCHighForDay );
printf("\nDonchianLower : " + DonchianLower );
printf("\nconditionForHIgh : " + highForDay);
printf("\nfifteenMinuteClose: " + fifteenMinuteClose);
printf("\natrDiffValue: " + (highForDay-DCHighForDay));
printf("\n atr value : " + ValueWhen(Buy,atrValue));
printf("\n atrTrailingStop: " + ValueWhen(Buy,atrTrailingStop));
printf("\n atrProfitTarget: " + ValueWhen(Buy,1*atrValue));
printf("\n TargetPrice: " + (highForDay+ValueWhen(Buy,1*atrValue)));
Plot(DonchianUpper,"DU",colorBlue,styleLine);
Plot(DonchianLower,"DL",colorRed,styleLine);
ApplyStop(stopTypeTrailing, stopModePoint, atrTrailingStop, True, True );
ApplyStop(stopTypeProfit,stopModePoint, 1*atrValue,True,True);
if ( Status( "Action" ) == actionIndicator ) {
///evaluate stops
eq = Equity(2, 0);
printf("\nBuy : " + Buy );
/* Plot Buy and Sell Signal Arrows */
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-25);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-35);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-30);
}
_SECTION_END();
The issue that i have with this code, is it generate multiple buy signals, I am not sure how to remove excessive Buy signals when Sell=0. Also is there an efficient way to do this ? Please see the attached picture to understand the issue that i am facing.