Pivot跟ZigZag比較
本帖最後由 zaqimon 於 17-1-30 15:39 編輯我不確定這東西是不是叫做Pivot
反正就是N bar H/L Peak轉折點的連線
(Pivot好像也可以指轉折價位的意思)
不確定能用來幹麼
這張圖是黃色PivotLine(3 bar)跟紫色ZigZag(1%)的比較
以下是程式碼
SetBarsRequired(100); // may be needed
n_bar = Param("N bar", 7, 3, 39, 2);
n_bar = (n_bar-1)/2;
// use loop scoring method instead of look-around at each bar
ScoreUp = 0;
for(cnt=-n_bar; cnt<=n_bar; cnt++)
{
if(cnt==0) continue; // no need compare same bar
for(i=n_bar; i<BarCount-n_bar; i++)
{
if(H>H)
{
ScoreUp += 1;
}
// special case, same value when cnt<0, replicate previous bar's ScoreUp
else if(H==H)
{
if(cnt<0)
{
ScoreUp = ScoreUp;
}
}
}
}
ScoreDn = 0;
for(cnt=-n_bar; cnt<=n_bar; cnt++)
{
if(cnt==0) continue; // no need compare same bar
for(i=n_bar; i<BarCount-n_bar; i++)
{
if(L<L)
{
ScoreDn += 1;
}
// special case, same value when cnt<0, replicate previous bar's ScoreDn
else if(L==L)
{
if(cnt<0)
{
ScoreDn = ScoreDn;
}
}
}
}
// filter pivot point and connect with LineArray()
prev_up_x = Null;
prev_up_y = Null;
prev_dn_x = Null;
prev_dn_y = Null;
curr_state = 0; // 0: init, 1: up, -1: down
PivotLine = Null;
for(i=n_bar; i<BarCount-n_bar; i++)
{
LA_update = false;
// rare case, single bar Up/Dn pivot, (I prioritize to) extend curr_state direction
if(ScoreUp==n_bar*2 && ScoreDn==n_bar*2)
{
if(curr_state==1)
{
if(H>prev_up_y)
{
LA = LineArray(prev_dn_x, prev_dn_y, i, H);
LA_update = true;
prev_up_x = i;
prev_up_y = H;
}
}
else if(curr_state==-1)
{
if(L<prev_dn_y)
{
LA = LineArray(prev_up_x, prev_up_y, i, L);
LA_update = true;
prev_dn_x = i;
prev_dn_y = L;
}
}
else // 0: init, (I choose to) change curr_state to 1
{
curr_state = 1;
prev_up_x = i;
prev_up_y = H;
// also give down pivot value, this init value is not a big deal
prev_dn_x = i;
prev_dn_y = L;
}
}
else
{
if(ScoreUp==n_bar*2) // up pivot
{
if(curr_state==1) // state extend
{
if(H>prev_up_y)
{
LA = LineArray(prev_dn_x, prev_dn_y, i, H);
LA_update = true;
prev_up_x = i;
prev_up_y = H;
}
}
else if(curr_state==-1) // state change
{
curr_state = 1;
LA = LineArray(prev_dn_x, prev_dn_y, i, H);
LA_update = true;
prev_up_x = i;
prev_up_y = H;
}
else // 0: init
{
curr_state = 1;
prev_up_x = i;
prev_up_y = H;
// also give down pivot value, this init value is not a big deal
prev_dn_x = i;
prev_dn_y = L;
}
}
else if(ScoreDn==n_bar*2) // down pivot
{
if(curr_state==-1) // state extend
{
if(L<prev_dn_y)
{
LA = LineArray(prev_up_x, prev_up_y, i, L);
LA_update = true;
prev_dn_x = i;
prev_dn_y = L;
}
}
else if(curr_state==1) // state change
{
curr_state = -1;
LA = LineArray(prev_up_x, prev_up_y, i, L);
LA_update = true;
prev_dn_x = i;
prev_dn_y = L;
}
else // 0: init
{
curr_state = -1;
prev_dn_x = i;
prev_dn_y = L;
// also give up pivot value, this init value is not a big deal
prev_up_x = i;
prev_up_y = H;
}
}
}
if(LA_update) // update PivotLine array when needed
{
PivotLine = IIf(IsNull(LA), PivotLine, LA);
}
} // END for
// show pivot point, ScoreXx == n_bar*2
if(ParamToggle("Show Pivot?","No|Yes",1))
{
PlotShapes(IIf(ScoreUp==n_bar*2, shapeSmallCircle, shapeNone), colorBrightGreen, 0, H, 12);
PlotShapes(IIf(ScoreDn==n_bar*2, shapeSmallCircle, shapeNone), colorRed, 0, L, -12);
}
Plot( PivotLine, "", ParamColor("Color", colorGold), ParamStyle("Style") | styleNoLabel | styleNoTitle);
頁:
[1]