# Pivot breakout Buy condition - delay signal problem

Hi guys,

I am trying to create a simple pivot breakout buying condition for my trading system. The rule to buy is simple:

1. Buy when there is a break of the most recent pivot.
2. Buy when stock is creating higher low higher high.

My BuyCondition as you can see in the code was written in a way that it can give accurate backtest result as I wanted, while also able to use in real time.
The big problem now for me is that sometimes, my real-time signal is delayed by 1 bar. I have found out that the problem only happen when the signal candle Close is equal to the signal candle high (a Marubozu candle).
It will be clear as you can see in the 2 pictures below:

Pic 1: No signal on 11/17/2020

Pic 2: New signal on 11/18/2020

Can you guys please look into my code and help me to fix this delay in the signal? I would like the signal to appear on that day so I can buy at the end of the trading session at ATC.
This is my first post ever. It would be amazing if you guys can help me with this. I have been really struggling with it

Thank you so much!
Here is my code:

``````/**********************************************************************************
************************* Pivot Breakout System************************
***********************************************************************************/

_SECTION_BEGIN("Pivot breakout");
SV_LP=4;//Param("Lookback Period for pivot?",4,2,50,1);

midhigh = (C+H)/2;
midlow = (C+L)/2;

vcl = 0.75;//Optimize("vcl",0.7,0,1,0.01); //Best is 0.76 with cmae
midday = L+(H-L)*vcl; //a High-Close is a better breakout (C>75% of candle body)

PH = H > Ref(HHV(H,SV_LP),-1) AND Ref(HHV(H,SV_LP),SV_LP)<=H; //Auto correct because future has not occurred
PHP = ValueWhen(PH,midhigh); //Value of the last Pivot high
PHP2 = ValueWhen(PH,midhigh,2); //Value of pivot high on the 2nd most recent occurence
PHP3 = ValueWhen(PH,midhigh,3);

HHP = PHP >  PHP2; //higher high pivot
LHP = PHP <  PHP2; //lower high pivot

PL = L < Ref(LLV(L,SV_LP),-1) AND Ref(LLV(L,SV_LP),SV_LP)>=L; //similar to pivot high for pivot low
PLP = ValueWhen(PL,midlow); //Value of the last Pivot low
PLP2 = ValueWhen(PL,midlow,2); //Value of pivot low on the 2nd most recent occurence
LLP = PLP <  PLP2;  //lower low pivot
HLP = PLP >= PLP2; //higher low pivot

SV_SC = C<PLP AND HHP AND HLP; //Reverse uptrend to downtrend - go short
SV_BC = C>PHP AND LLP AND LHP; //Reverse downtrend to uptrend - go long

m = 0.005;
cond0 = C<PHP;
cond0_true  = IIf(PH,IIf(C+m>PHP2,H+m>PHP2 AND C>midday,C+m>PHP3),C+m>PHP2);
cond0_false = IIf(PH,IIf(C+m>PHP2,H+m>PHP2 AND C>midday,C+m>PHP3),H+m>PHP AND C>midday);
brk1 = IIf(cond0,cond0_true,cond0_false);

breakout = brk1 AND HLP; //Up we go!

PlotShapes(IIf(SV_SC,shapeDownTriangle,shapeNone),colorRed,0,H,-25);
PlotShapes(IIf(SV_BC,shapeUpTriangle,shapeNone),colorGreen,0,L,-25);
if(ParamToggle("Plot Pivots?","No,Yes",1)){
PlotShapes(IIf(PH,shapeSmallCircle,shapeNone),colorGreen,0,H,12);
PlotShapes(IIf(PL,shapeSmallCircle,shapeNone),colorRed,0,L,-12);}

_SECTION_END();

``````

Guys, I think I found the problem.

The reason was that in my trading system, I use another indicator that is look forward into the future. It was the Accumulation Distribution modified indicator.
The code looks like this:

``````_SECTION_BEGIN("AccDist");
if (Nz(VarGet("runonce_AccDist")) == 0) {
VarSet("runonce_AccDist",1);
RequestTimedRefresh( 1 );
PC = StaticVarGet("PC");
PV = StaticVarGet("PV");

Plot( FA_AccDist(), _DEFAULT_NAME(), ParamColor("Color", colorRed ), ParamStyle("Style", styleLine) );
}
_SECTION_END();
``````

I don't know why it looks into the future, but once I delete this indicator from my system, the problem i mentioned in the previous comment no longer prevail. I suppose that is the solution.

However, it would be great still if you guys take a look at my pivot breakout buy condition above and point out any possible problem? Thank you

in my opinion 1 of the reasons is that your last pivot is not completed. So you could add these 2 lines to your code:

``````bi = BarIndex();

PH = H > Ref( HHV( H, SV_LP ), -1 ) AND Ref( HHV( H, SV_LP ), SV_LP ) <= H; //Auto correct because future has not occurred
PH = PH AND LastValue( bi ) - ValueWhen( PH, bi ) > SV_LP;

PL = L < Ref( LLV( L, SV_LP ), -1 ) AND Ref( LLV( L, SV_LP ), SV_LP ) >= L; //similar to pivot high for pivot low
PL = PL AND LastValue( bi ) - ValueWhen( PL, bi ) > SV_LP;
``````

then you will see the pivots will be only drawn once they are fully developed and will no longer change their position. But then there is another problem which has to do with you buy and sell rule itself.

Best to run the playback function in Amibroker and see step by step what is going on. Then you will figure it out

2 Likes

Thank you so much! I think that definitely solve my problem with the pivot!
This community is amazing!

not sure if I solved it but with these pivots you need to take into account that they need SV_LP points on both sides of the pivot. So you can only use the pivot once it is fully developed. And therefore also the lines derived from this pivot.

You for instance use PLP which is defined as:

``````PLP = ValueWhen( PL, midlow ); //Value of the last Pivot low
``````

but because the pivot PL is only known to exist SV_LP bars after you see the pivot located in the chart this level PLP also only can be used from that point. So you should actually use:

``````PLP = Ref( ValueWhen( PL, midlow ), -SV_LP ); //Value of the last Pivot low
``````

I did not test it all but those are some things to think about. What is helpfull is to also plot these levels in the chart while you are developing the system like:

``````Plot( PLP, "", colorRed, styleLine | styleNoRescale, Null, Null, 0, 0, 1 );
Plot( PLP2, "", colorRed, styleDashed | styleNoRescale, Null, Null, 0, 0, 1 );
Plot( PHP, "", colorGreen, styleLine | styleNoRescale, Null, Null, 0, 0, 1 );
Plot( PHP2, "", colorGreen, styleDashed | styleNoRescale, Null, Null, 0, 0, 1 );
``````

these pivots are always tricky. I am still confused by them often. But when you use the playback tool and the signals do not suddenly disappear or appear late then you can trust you have done it correctly

1 Like

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.