How to reduce Multiple buy signals when working on hourly breakout

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.

dc50Amibroker

hello

Maybe can solve you problem by reading this?

Limit number of trades per day in a backtest

also ExRem function removes excessive signals:

// example

Buy=ExRem(Buy,Sell); Sell=ExRem(Sell,Buy); 
Short=ExRem(Short,Cover); Cover=ExRem(Cover,Short);

hi Panos, thanks for your response, i went through the articles, they are referring about intraday signals but i want to carry it positional, until it hits my trailing SL or target.

also, my sell condition here is set to 0, so i am finding it hard to, removing the signals.

The code is incorrect.
Just another rubbish. Just one of a few issues... there is not done any expansion of longer time frame after compression in shorter time frame. Read how to do multi time frame code properly.

Also must read How do I debug my formula?

Besides why are users here supposed to fix code made by other websites?
Simply go to the guy who created that one (if he is actually the one) and ask him to study AB manual before selling code or making Ads for.

2 Likes

I am sorry for posting the name of the website, from where i took this afl.. It's not the original one, i was trying to reuse and did the changes myself. I shall look into the details mentioned by you. Thanks for your response.

_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);
NextCandleClose = Ref(C,1);
NextCandleHigh = Ref(H,1);

TimeFrameRestore();

//Get details of the daily ATR 
TimeFrameSet(inDaily);

atrValue = Ref(ATR(14),-1);

TimeFrameRestore();

//Need to Expand the array details set in hourly and daily 
DonchianUpperExpanded = TimeFrameExpand(DonchianUpper,inHourly);
DonchianLowerExpanded = TimeFrameExpand(DonchianLower,inHourly);
NextCandleCloseExpanded = TimeFrameExpand(NextCandleClose,inHourly);
NextCandleHighExpanded = TimeFrameExpand(NextCandleHigh,inHourly);
atrValueExpanded = TimeFrameExpand(atrValue,inDaily);

orb = ParamList("ORB Range", "5min|10min|15min|30min|60min",4);

newday = Day() != Ref(Day(),-1);  //check if new day or not

starttime = ValueWhen(newday,TimeNum());

//15min ORB
if(orb == "15min")
{
IBendtime = starttime+1500;
minh = ValueWhen(newday,TimeFrameGetPrice("H",in5Minute*3));
minl = ValueWhen(newday,TimeFrameGetPrice("L",in5Minute*3));
}

//60min ORB
if(orb == "60min")
{
IBendtime = starttime+6000;
minh = ValueWhen(newday,TimeFrameGetPrice("H",inhourly));
minl = ValueWhen(newday,TimeFrameGetPrice("L",inhourly));
}


ORBH = IIf(TimeNum() < IBendtime, Null ,minh);
ORBL = IIf(TimeNum() < IBendtime, Null, minl);

conditionForEntry = ORBH+ORBH*0.001;

 DCconditionForEntry = ValueWhen(newday,DonchianUpperExpanded);
 //check if during opening range is greater than dc50 for the day and if next candle high is greater than conditionForEntry
Buy= ORBH >= DCconditionForEntry AND  NextCandleHighExpanded >= conditionForEntry;// Cross(fifteenMinuteCloseExpanded,conditionForEntry);//fifteenMinuteClose>=conditionForBuy;
Sell=0;
BuyPrice=NextCandleHighExpanded;

atrTrailingStop = IIf(Buy AND (conditionForEntry-DCconditionForEntry)>atrValueExpanded, atrValueExpanded,0.25*atrValueExpanded);

printf("DonchianUpperExpanded : " + DCconditionForEntry );  
printf("\nORBH :" + ORBH);
printf("\nTriggerPrice : " + conditionForEntry);
printf("\nNextCandleHighExpanded :" + NextCandleHighExpanded); 
printf("\nNextCandleCloseExpanded: " + NextCandleCloseExpanded);
printf("\natrDiffValue: " + (conditionForEntry-DCconditionForEntry));
printf("\n atr value : " + ValueWhen(Buy,atrValueExpanded));
printf("\n atrTrailingStop: " + ValueWhen(Buy,atrTrailingStop));
printf("\n atrProfitTarget: " + ValueWhen(Buy,1*atrValueExpanded));
printf("\n TargetPrice: " + (conditionForEntry+ValueWhen(Buy,1*atrValueExpanded)));

Plot(DonchianUpper,"DU",colorBlue,styleLine);
Plot(DonchianLower,"DL",colorRed,styleLine);

ApplyStop(stopTypeTrailing, stopModePoint, atrTrailingStop, True, True );
ApplyStop(stopTypeProfit,stopModePoint, 1*atrValueExpanded,True,True);


if ( Status( "Action" ) == actionIndicator ) {
	///evaluate stops
	eq = Equity(1, 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);

}
if(Status("Action") == actionExplore ){

Filter = 1; 
AddColumn(ORBH,"ORBH");
AddColumn(DCconditionForEntry,"DC50");
//AddColumn((conditionForEntry+ValueWhen(Buy,1*atrValueExpanded)),"Target");
AddColumn(NextCandleHighExpanded,"nextCandleHigh");
AddColumn(conditionForEntry,"BuyPrice");
AddColumn(Buy,"Buy");
//AddColumn( ValueWhen(Buy,atrTrailingStop), "stoploss");
}


_SECTION_END();

@Fxshrat Thanks for your help in guiding me in right direction. Now i understood, how to work on multi time frame.