Hi guys,
I am trying to create a simple pivot breakout buying condition for my trading system. The rule to buy is simple:
- Buy when there is a break of the most recent pivot.
- 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);
//AlertIf(SV_BC,"SOUND C:\\Windows\\Media\\tada.wav","Upward Breakout at :"+C,1);
//AlertIf(SV_SC,"SOUND C:\\Windows\\Media\\notify.wav","Downward Breakout at :"+C,2);
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();