Debugging pivot trading strategy

Hello , i am writing a code for below logic with regards to floor pivots.
Buy if yesterday high is higher then R2 and todays 5 min close crosses yesterday high.
Sell if yesterday low is lower then S2 and todays 5 min close crosses below yesterday low.
stops will be upper bound and lower bounds of central pivot range respectively.
trade is closed after 3:15 our local market ends at 3 30 and this is for intraday only.

However i am not getting any signals plotted on the chart . I got the code ready made for the CPR levels and i am trying to code buy/sell signal myself , please check what is wrong in the below code.

_SECTION_BEGIN( "bar chart" );
Plot( C, _DEFAULT_NAME(), IIf( C > Ref(C, -1), colorBlue, IIf(C < Ref(C, -1), colorRed, colorGrey40)), styleBar | styleThick );
_SECTION_END();

_SECTION_BEGIN( "CPR" );
	 dtDayStartComp = TimeFrameCompress( DateTime(), inDaily, compressOpen );
	 dtDayStartExp = TimeFrameExpand( dtDayStartComp, inDaily, expandFirst );
	 dtDayEndComp = TimeFrameCompress( DateTime(), inDaily, compressLast );
	 dtDayEndExp = TimeFrameExpand( dtDayEndComp, inDaily, expandFirst );
	 DayCond = DateTime() >= dtDayStartExp AND DateTime() < dtDayEndExp;	 
	 
	 // Previous Days High Low Close
	 PDH =  TimeFrameGetPrice( "H", inDaily, -1 );
	 PDL =  TimeFrameGetPrice( "L", inDaily, -1 );
	 PDC =  TimeFrameGetPrice( "C", inDaily, -1 );
	 
	 PV = ( PDH + PDL + PDC ) / 3;
	 LB = ( PDH + PDL ) / 2;
	 UB = 2 * PV - LB;
	 
	 Plot( IIf( DayCond, UB, Null ), "UB", colorAqua, styleLine, Null, Null, 0 );
	 Plot( IIf( DayCond, UB, Null ), "UB", colorAqua, styleLine, Null, Null, 2 );
	 
	 Plot( IIf( DayCond, PV, Null), "Pivot", colorAqua, styleLine, Null, Null, 0 );
	 Plot( IIf( DayCond, PV, Null), "Pivot", colorAqua, styleLine, Null, Null, 2 );
	 
	 Plot( IIf( DayCond, LB, Null), "LB", colorAqua, styleLine, Null, Null, 0 );
	 Plot( IIf( DayCond, LB, Null), "LB", colorAqua, styleLine, Null, Null, 2 );
_SECTION_END();

_SECTION_BEGIN( "Pivot Levels" );
	 //PDH =  TimeFrameGetPrice( "H", inDaily, -1 );
	 //PDL =  TimeFrameGetPrice( "L", inDaily, -1 );
	 //PDC =  TimeFrameGetPrice( "C", inDaily, -1 );

	 //PV = ( PDH + PDL + PDC ) / 3;
	 R1 = 2 * PV - PDL;
	 R2 = PV + PDH - PDL;
	 S1 = 2 * PV - PDH;
	 S2 = PV - PDH + PDL;

	 Plot( IIf( DayCond, R1, Null ), "R1", colorRed, styleLine, Null, Null, 0 );
	 Plot( IIf( DayCond, R1, Null ), "R1", colorRed, styleLine, Null, Null, 2 );
	 
	 Plot( IIf( DayCond, R2, Null ), "R2", colorOrange, styleLine, Null, Null, 0 );
	 Plot( IIf( DayCond, R2, Null ), "R2", colorOrange, styleLine, Null, Null, 2 );
	 
	 Plot( IIf( DayCond, S1, Null ), "S1", colorGreen, styleLine, Null, Null, 0 );
	 Plot( IIf( DayCond, S1, Null ), "S1", colorGreen, styleLine, Null, Null, 2 );
	 
	 Plot( IIf( DayCond, S2, Null ), "S2", colorLime, styleLine, Null, Null, 0 );
	 Plot( IIf( DayCond, S2, Null ), "S2", colorLime, styleLine, Null, Null, 2 );
_SECTION_END();
Market_Start_Time=091500;
Market_end_time=151500;
Exit_Time = iif(TimeNum() > Market_end_time,True,False);

//trading logic
uyt = TimeFrameGetPrice("H",inDaily, -1 );
Buy = Cross(Close,uyt) AND uyt > Ref(R2,-1);
Sell = Cross(Low,UB) OR Exit_Time == 1;

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

poi = TimeFrameGetPrice("L",inDaily, -1 );

Short = Cross(poi,Low) == 1 AND poi < Ref(S2,-1);
Cover = Cross(LB,High) OR Exit_Time == 1;

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

//signal drawings 

PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);                      
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45); 

PlotShapes(IIf(Sell, shapeStar, shapeNone),colorGold, 0, L, Offset=-15);

PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);                      
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);

PlotShapes(IIf(Cover, shapeStar, shapeNone),colorGold, 0,L, Offset=-15); 

Please use debug methods described here. It is not difficult to come to conclusion yourself.

Position of R2 is just more away from intraday price action than R1 or PV.
So it is less likely to happen that yesterday High is higher than R2 contrary to it being higher than PV or R1.

So simply modify this line

Buy = Cross(Close,uyt) AND uyt > Ref(R2,-1);

to this one

Buy = Cross(Close,uyt) AND uyt > Ref(PV,-1);

Or just use this one

Buy = Cross(Close,uyt);

to enable "Ahhhhh..." moment inside brain.

In addition it does not hurt anyone to output e.g. uyt variable.

uyt = TimeFrameGetPrice("H",inDaily, -1 );
Plot( uyt, "uyt", colorGreen, styleDashed);

and disable output (plot) of any pivot point related variable except for R2.

Do you see the light now?

8

5 Likes

Thank you for your swift response, i read the article written by Tomasz and it solved everything for me.