|
本帖最後由 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[i]>H[i+cnt])
- {
- ScoreUp[i] += 1;
- }
- // special case, same value when cnt<0, replicate previous bar's ScoreUp
- else if(H[i]==H[i+cnt])
- {
- if(cnt<0)
- {
- ScoreUp[i] = ScoreUp[i-1];
- }
- }
- }
- }
- 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[i]<L[i+cnt])
- {
- ScoreDn[i] += 1;
- }
- // special case, same value when cnt<0, replicate previous bar's ScoreDn
- else if(L[i]==L[i+cnt])
- {
- if(cnt<0)
- {
- ScoreDn[i] = ScoreDn[i-1];
- }
- }
- }
- }
- // 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[i]==n_bar*2 && ScoreDn[i]==n_bar*2)
- {
- if(curr_state==1)
- {
- if(H[i]>prev_up_y)
- {
- LA = LineArray(prev_dn_x, prev_dn_y, i, H[i]);
- LA_update = true;
- prev_up_x = i;
- prev_up_y = H[i];
- }
- }
- else if(curr_state==-1)
- {
- if(L[i]<prev_dn_y)
- {
- LA = LineArray(prev_up_x, prev_up_y, i, L[i]);
- LA_update = true;
- prev_dn_x = i;
- prev_dn_y = L[i];
- }
- }
- else // 0: init, (I choose to) change curr_state to 1
- {
- curr_state = 1;
- prev_up_x = i;
- prev_up_y = H[i];
- // also give down pivot value, this init value is not a big deal
- prev_dn_x = i;
- prev_dn_y = L[i];
- }
- }
- else
- {
- if(ScoreUp[i]==n_bar*2) // up pivot
- {
- if(curr_state==1) // state extend
- {
- if(H[i]>prev_up_y)
- {
- LA = LineArray(prev_dn_x, prev_dn_y, i, H[i]);
- LA_update = true;
- prev_up_x = i;
- prev_up_y = H[i];
- }
- }
- else if(curr_state==-1) // state change
- {
- curr_state = 1;
- LA = LineArray(prev_dn_x, prev_dn_y, i, H[i]);
- LA_update = true;
- prev_up_x = i;
- prev_up_y = H[i];
- }
- else // 0: init
- {
- curr_state = 1;
- prev_up_x = i;
- prev_up_y = H[i];
- // also give down pivot value, this init value is not a big deal
- prev_dn_x = i;
- prev_dn_y = L[i];
- }
- }
- else if(ScoreDn[i]==n_bar*2) // down pivot
- {
- if(curr_state==-1) // state extend
- {
- if(L[i]<prev_dn_y)
- {
- LA = LineArray(prev_up_x, prev_up_y, i, L[i]);
- LA_update = true;
- prev_dn_x = i;
- prev_dn_y = L[i];
- }
- }
- else if(curr_state==1) // state change
- {
- curr_state = -1;
- LA = LineArray(prev_up_x, prev_up_y, i, L[i]);
- LA_update = true;
- prev_dn_x = i;
- prev_dn_y = L[i];
- }
- else // 0: init
- {
- curr_state = -1;
- prev_dn_x = i;
- prev_dn_y = L[i];
- // also give up pivot value, this init value is not a big deal
- prev_up_x = i;
- prev_up_y = H[i];
- }
- }
- }
- 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);
複製代碼 |
-
評分
-
查看全部評分
|