COCO研究院

 找回密碼
 註冊
搜索
查看: 3775|回復: 2

ShenbaKumar pivots

[複製鏈接]
發表於 10-6-11 17:53 | 顯示全部樓層 |閱讀模式
  1. _SECTION_BEGIN("ShenbaKumar pivots");
  2. /* **********************************
  3. Code to automatically identify pivots
  4. By Kumaresan Selvaraj
  5. ********************************** */
  6. // -- what will be our lookback range for the hh and ll?
  7. farback = Param("How Far back to go", 100, 50, 5000, 10);
  8. nBars = Param("Number of bars", 12, 5, 40);
  9. // -- Title.

  10. Title = Name() + " (" + StrLeft(FullName(), 15) + ") O: " + Open + ", H: "
  11. + High + ", L: " + Low + ", C: " + Close;
  12. // -- Plot basic candle chart
  13. PlotOHLC(Open, High, Low, Close,
  14. "BIdx = " + BarIndex() +
  15. "\n" + "O = " + O + "\n" + "H = " + H + "\n" + "L = " + L
  16. + "\n" + "C ", colorBlack, styleCandle);

  17. GraphXSpace = 7;
  18. // -- Create 0-initialized arrays the size of barcount
  19. aHPivs = H - H;
  20. aLPivs = L - L;
  21. // -- More for future use, not necessary for basic plotting
  22. aHPivHighs = H - H;
  23. aLPivLows = L - L;
  24. aHPivIdxs = H - H;
  25. aLPivIdxs = L - L;
  26. nHPivs = 0;
  27. nLPivs = 0;
  28. lastHPIdx = 0;
  29. lastLPIdx = 0;
  30. lastHPH = 0;
  31. lastLPL = 0;
  32. curPivBarIdx = 0;
  33. // -- looking back from the current bar, how many bars
  34. // back were the hhv and llv values of the previous
  35. // n bars, etc.?
  36. aHHVBars = HHVBars(H, nBars);
  37. aLLVBars = LLVBars(L, nBars);
  38. aHHV = HHV(H, nBars);
  39. aLLV = LLV(L, nBars);
  40. // -- Would like to set this up so pivots are calculated back from
  41. // last visible bar to make it easy to "go back" and see the pivots
  42. // this code would find. However, the first instance of
  43. // _Trace output will show a value of 0
  44. aVisBars = Status("barvisible");
  45. nLastVisBar = LastValue(Highest(IIf(aVisBars, BarIndex(), 0)));
  46. _TRACE("Last visible bar: " + nLastVisBar);
  47. // -- Initialize value of curTrend
  48. curBar = (BarCount - 1);
  49. curTrend = "";

  50. if (aLLVBars[curBar] < aHHVBars[curBar])
  51. {
  52. curTrend = "D";
  53. }
  54. else
  55. {
  56. curTrend = "U";
  57. }
  58. // -- Loop through bars. Search for
  59. // entirely array-based approach
  60. // in future version
  61. for (i = 0; i < farback; i++)
  62. {
  63. curBar = (BarCount - 1) - i;
  64. // -- Have we identified a pivot? If trend is down...
  65. if (aLLVBars[curBar] < aHHVBars[curBar])
  66. {
  67. // ... and had been up, this is a trend change
  68. if (curTrend == "U")
  69. {
  70. curTrend = "D";
  71. // -- Capture pivot information
  72. curPivBarIdx = curBar - aLLVBars[curBar];
  73. aLPivs[curPivBarIdx] = 1;
  74. aLPivLows[nLPivs] = L[curPivBarIdx];
  75. aLPivIdxs[nLPivs] = curPivBarIdx;
  76. nLPivs++;
  77. }
  78. // -- or current trend is up
  79. }
  80. else
  81. {
  82. if (curTrend == "D")
  83. {
  84. curTrend = "U";
  85. curPivBarIdx = curBar - aHHVBars[curBar];
  86. aHPivs[curPivBarIdx] = 1;
  87. aHPivHighs[nHPivs] = H[curPivBarIdx];
  88. aHPivIdxs[nHPivs] = curPivBarIdx;
  89. nHPivs++;
  90. }
  91. // -- If curTrend is up...else...
  92. }
  93. // -- loop through bars
  94. }
  95. // -- Basic attempt to add a pivot this logic may have missed
  96. // -- OK, now I want to look at last two pivots. If the most
  97. // recent low pivot is after the last high, I could
  98. // still have a high pivot that I didn't catch
  99. // -- Start at last bar
  100. curBar = (BarCount - 1);
  101. candIdx = 0;
  102. candPrc = 0;
  103. lastLPIdx = aLPivIdxs[0];
  104. lastLPL = aLPivLows[0];
  105. lastHPIdx = aHPivIdxs[0];
  106. lastHPH = aHPivHighs[0];

  107. if (lastLPIdx > lastHPIdx)
  108. {
  109. // -- Bar and price info for candidate pivot
  110. candIdx = curBar - aHHVBars[curBar];
  111. candPrc = aHHV[curBar];
  112. if (
  113. lastHPH < candPrc AND
  114. candIdx > lastLPIdx AND
  115. candIdx < curBar)
  116. {
  117. // -- OK, we'll add this as a pivot...
  118. aHPivs[candIdx] = 1;
  119. // ...and then rearrange elements in the
  120. // pivot information arrays
  121. for (j = 0; j < nHPivs; j++)
  122. {
  123. aHPivHighs[nHPivs - j] = aHPivHighs[nHPivs -
  124. (j + 1)];
  125. aHPivIdxs[nHPivs - j] = aHPivIdxs[nHPivs - (j + 1)];
  126. }
  127. aHPivHighs[0] = candPrc;
  128. aHPivIdxs[0] = candIdx;
  129. nHPivs++;
  130. }
  131. }
  132. else
  133. {
  134. // -- Bar and price info for candidate pivot
  135. candIdx = curBar - aLLVBars[curBar];
  136. candPrc = aLLV[curBar];
  137. if (lastLPL > candPrc AND candIdx > lastHPIdx AND candIdx < curBar)
  138. {
  139. // -- OK, we'll add this as a pivot...
  140. aLPivs[candIdx] = 1;
  141. // ...and then rearrange elements in the
  142. // pivot information arrays
  143. for (j = 0; j < nLPivs; j++)
  144. {
  145. aLPivLows[nLPivs - j] = aLPivLows[nLPivs - (j + 1)];
  146. aLPivIdxs[nLPivs - j] = aLPivIdxs[nLPivs - (j + 1)];
  147. }
  148. aLPivLows[0] = candPrc;
  149. aLPivIdxs[0] = candIdx;
  150. nLPivs++;
  151. }
  152. }
  153. // -- Dump inventory of high pivots for debugging
  154. /*
  155. for (k=0; k<nHPivs; k++) {
  156. _TRACE("High pivot no. " + k
  157. + " at barindex: " + aHPivIdxs[k] + ", "
  158. + WriteVal(ValueWhen(BarIndex()==aHPivIdxs[k],
  159. DateTime(), 1), formatDateTime)
  160. + ", " + aHPivHighs[k]);
  161. }
  162. */
  163. // -- OK, let's plot the pivots using arrows
  164. PlotShapes(IIf(aHPivs == 1, shapeDownArrow, shapeNone), colorRed, 0, High, Offset = - 15);
  165. PlotShapes(IIf(aLPivs == 1, shapeUpArrow, shapeNone), colorGreen, 0, Low, Offset = - 15);
  166. _SECTION_END();
複製代碼
發表於 10-6-11 19:21 | 顯示全部樓層
發表於 11-3-29 11:17 | 顯示全部樓層
曾經想過要學習用程式交易....
但實在是沒時間.
您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

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

GMT+8, 24-11-22 17:45

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

快速回復 返回頂部 返回列表
理財討論網站 |