Coding help needed for Trading with weekly open in higher time frame

Hi guys

I have tried the following scenario in the below AFL code.

Long should be entered when

  1. Buy price touches the Donchain Upper line and
  2. If in HigherTimeframe, Buy price should be greater than weekly open.

Sell at DonchainLower .

Short should be entered when

  1. Buy price touches the Donchain Lower line and
  2. If in HigherTimeframe, Buy price should be less than weekly open.

Cover at DonchainUpper.


_SECTION_BEGIN("Donchian Channel trading System");
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));

//Initial Parameters
SetTradeDelays( 0,0,0, 0 );

Plot( Close, "Price", colorWhite, styleCandle );

FirstTradeTime = 093000;  //Earliest time to take a trades
LastTradeTime =  153000; //Latest time to take new trades

FirstTradeTime  = ParamTime( "Start Time", "09:30" );
LastTradeTime = ParamTime( "End Time", "15:30" );


//3 minutes
DonchianUpper =HHV(Ref(H,-1),pds);
DonchianLower = LLV(Ref(L,-1),pds);
DonchianMiddle = (DonchianUpper+DonchianLower)/2;

WeekOpen =TimeFrameGetPrice( "O", inWeekly, 0 ); 


Buy=Cross(DonchianUpper,WeekOpen) AND (TimeNum() >= FirstTradeTime AND TimeNum() <= LastTradeTime );
Short=Cross(WeekOpen,DonchianLower) AND (TimeNum() >= FirstTradeTime AND TimeNum() <= LastTradeTime );
Sell= DonchianLower;
Cover= DonchianUpper;


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

printf("\nBuy : " + Buy );  
printf("\nSell : " + Sell );  
printf("\nShort : " + Short );  
printf("\nCover : " + Cover );  

/* 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);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=25);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=35);
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-30);
PlotShapes(IIf(Sell, shapeStar, shapeNone),colorGold, 0, L, Offset=-15);
PlotShapes(IIf(Cover, shapeStar, shapeNone),colorGold, 0,L, Offset=-15);

Issue: The signals are not right ,Could you help please.

As much as I could infer from your requirements, your signal logic is flawed which is why your signals are not being plotted correctly. Replace your signal logic with the below code and check that these signals match your requirements.

time_filter = TimeNum() >= FirstTradeTime AND TimeNum() <= LastTradeTime;
Buy = H >= DonchianUpper AND DonchianUpper > WeekOpen AND time_filter;
Short =	L <= DonchianLower AND DonchianLower < WeekOpen AND time_filter;
Sell = L < DonchianLower;
Cover = H > DonchianUpper;