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.

Code:

_SECTION_BEGIN("Donchian Channel trading System");
SetPositionSize(1,spsShares);
SetChartOptions(0,chartShowArrows|chartShowDates);
_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" );

pds=Param("DonchianPeriods",4,4,15,1);
iHPds=Param("hourlyPeriods",6,6,10,1);

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

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

BuyPrice=DonchianUpper;

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

BuyPrice=DonchianUpper;
SellPrice=Close;
ShortPrice=DonchianLower;
CoverPrice=Close;

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

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;