請選擇 進入手機版 | 繼續訪問電腦版

COCO研究院

 找回密碼
 註冊
搜索
查看: 6195|回復: 0

[範例程式碼] Pivot跟ZigZag比較

[複製鏈接]
發表於 17-1-30 14:53 | 顯示全部樓層 |閱讀模式
本帖最後由 zaqimon 於 17-1-30 15:39 編輯

我不確定這東西是不是叫做Pivot
反正就是N bar H/L Peak轉折點的連線
(Pivot好像也可以指轉折價位的意思)
不確定能用來幹麼

這張圖是黃色PivotLine(3 bar)跟紫色ZigZag(1%)的比較
2017-01-30_153226.gif

以下是程式碼
  1. SetBarsRequired(100); // may be needed

  2. n_bar = Param("N bar", 7, 3, 39, 2);

  3. n_bar = (n_bar-1)/2;

  4. // use loop scoring method instead of look-around at each bar

  5. ScoreUp = 0;
  6. for(cnt=-n_bar; cnt<=n_bar; cnt++)
  7. {
  8.     if(cnt==0) continue; // no need compare same bar
  9.     for(i=n_bar; i<BarCount-n_bar; i++)
  10.     {
  11.         if(H[i]>H[i+cnt])
  12.         {
  13.             ScoreUp[i] += 1;
  14.         }
  15.         // special case, same value when cnt<0, replicate previous bar's ScoreUp
  16.         else if(H[i]==H[i+cnt])
  17.         {
  18.             if(cnt<0)
  19.             {
  20.                 ScoreUp[i] = ScoreUp[i-1];
  21.             }
  22.         }
  23.     }
  24. }
  25. ScoreDn = 0;
  26. for(cnt=-n_bar; cnt<=n_bar; cnt++)
  27. {
  28.     if(cnt==0) continue; // no need compare same bar
  29.     for(i=n_bar; i<BarCount-n_bar; i++)
  30.     {
  31.         if(L[i]<L[i+cnt])
  32.         {
  33.             ScoreDn[i] += 1;
  34.         }
  35.         // special case, same value when cnt<0, replicate previous bar's ScoreDn
  36.         else if(L[i]==L[i+cnt])
  37.         {
  38.             if(cnt<0)
  39.             {
  40.                 ScoreDn[i] = ScoreDn[i-1];
  41.             }
  42.         }
  43.     }
  44. }

  45. // filter pivot point and connect with LineArray()

  46. prev_up_x = Null;
  47. prev_up_y = Null;
  48. prev_dn_x = Null;
  49. prev_dn_y = Null;
  50. curr_state = 0; // 0: init, 1: up, -1: down
  51. PivotLine = Null;

  52. for(i=n_bar; i<BarCount-n_bar; i++)
  53. {
  54.     LA_update = false;
  55.     // rare case, single bar Up/Dn pivot, (I prioritize to) extend curr_state direction
  56.     if(ScoreUp[i]==n_bar*2 && ScoreDn[i]==n_bar*2)
  57.     {
  58.         if(curr_state==1)
  59.         {
  60.             if(H[i]>prev_up_y)
  61.             {
  62.                 LA = LineArray(prev_dn_x, prev_dn_y, i, H[i]);
  63.                 LA_update = true;
  64.                 prev_up_x = i;
  65.                 prev_up_y = H[i];
  66.             }
  67.         }
  68.         else if(curr_state==-1)
  69.         {
  70.             if(L[i]<prev_dn_y)
  71.             {
  72.                 LA = LineArray(prev_up_x, prev_up_y, i, L[i]);
  73.                 LA_update = true;
  74.                 prev_dn_x = i;
  75.                 prev_dn_y = L[i];
  76.             }
  77.         }
  78.         else // 0: init, (I choose to) change curr_state to 1
  79.         {
  80.             curr_state = 1;
  81.             prev_up_x = i;
  82.             prev_up_y = H[i];
  83.             // also give down pivot value, this init value is not a big deal
  84.             prev_dn_x = i;
  85.             prev_dn_y = L[i];
  86.         }
  87.     }
  88.     else
  89.     {
  90.         if(ScoreUp[i]==n_bar*2) // up pivot
  91.         {
  92.             if(curr_state==1) // state extend
  93.             {
  94.                 if(H[i]>prev_up_y)
  95.                 {
  96.                     LA = LineArray(prev_dn_x, prev_dn_y, i, H[i]);
  97.                     LA_update = true;
  98.                     prev_up_x = i;
  99.                     prev_up_y = H[i];
  100.                 }
  101.             }
  102.             else if(curr_state==-1) // state change
  103.             {
  104.                 curr_state = 1;
  105.                 LA = LineArray(prev_dn_x, prev_dn_y, i, H[i]);
  106.                 LA_update = true;
  107.                 prev_up_x = i;
  108.                 prev_up_y = H[i];
  109.             }
  110.             else // 0: init
  111.             {
  112.                 curr_state = 1;
  113.                 prev_up_x = i;
  114.                 prev_up_y = H[i];
  115.                 // also give down pivot value, this init value is not a big deal
  116.                 prev_dn_x = i;
  117.                 prev_dn_y = L[i];
  118.             }
  119.         }
  120.         else if(ScoreDn[i]==n_bar*2) // down pivot
  121.         {
  122.             if(curr_state==-1) // state extend
  123.             {
  124.                 if(L[i]<prev_dn_y)
  125.                 {
  126.                     LA = LineArray(prev_up_x, prev_up_y, i, L[i]);
  127.                     LA_update = true;
  128.                     prev_dn_x = i;
  129.                     prev_dn_y = L[i];
  130.                 }
  131.             }
  132.             else if(curr_state==1) // state change
  133.             {
  134.                 curr_state = -1;
  135.                 LA = LineArray(prev_up_x, prev_up_y, i, L[i]);
  136.                 LA_update = true;
  137.                 prev_dn_x = i;
  138.                 prev_dn_y = L[i];
  139.             }
  140.             else // 0: init
  141.             {
  142.                 curr_state = -1;
  143.                 prev_dn_x = i;
  144.                 prev_dn_y = L[i];
  145.                 // also give up pivot value, this init value is not a big deal
  146.                 prev_up_x = i;
  147.                 prev_up_y = H[i];
  148.             }
  149.         }
  150.     }
  151.     if(LA_update) // update PivotLine array when needed
  152.     {
  153.         PivotLine = IIf(IsNull(LA), PivotLine, LA);
  154.     }
  155. } // END for

  156. // show pivot point, ScoreXx == n_bar*2
  157. if(ParamToggle("Show Pivot?","No|Yes",1))
  158. {
  159.     PlotShapes(IIf(ScoreUp==n_bar*2, shapeSmallCircle, shapeNone), colorBrightGreen, 0, H, 12);
  160.     PlotShapes(IIf(ScoreDn==n_bar*2, shapeSmallCircle, shapeNone), colorRed, 0, L, -12);
  161. }

  162. Plot( PivotLine, "", ParamColor("Color", colorGold), ParamStyle("Style") | styleNoLabel | styleNoTitle);
複製代碼
2017-01-30_142844.gif

評分

參與人數 1金錢 +2 收起 理由
soumont + 2 感謝分享

查看全部評分

您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

手機版|Archiver|站長信箱|廣告洽詢|COCO研究院

GMT+8, 24-3-29 19:33

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

快速回復 返回頂部 返回列表
理財討論網站 | AI繪圖AI超擬真美女AI beauty AI Stable DiffusionAI正妹AI Lookbook