COCO研究院

 找回密碼
 註冊
搜索
查看: 6515|回復: 3

Heikin-Ashi(Koma-Ashi) with Moving Average Type

[複製鏈接]
發表於 10-6-11 17:52 | 顯示全部樓層 |閱讀模式
  1. /*
  2. Heikin-Ashi(Koma-Ashi) with Moving Average Type
  3. */
  4. SetChartOptions(2, chartWrapTitle);
  5. // Calculate Moving Average
  6. MAPeriod = Param("MA Period", 15, 1, 100);
  7. MAOpen = EMA(Open, MAPeriod);
  8. MAHigh = EMA(High, MAPeriod);
  9. MALow = EMA(Low, MAPeriod);
  10. MAClose = EMA(Close, MAPeriod);
  11. HaClose = (MAOpen + MAHigh + MALow + MAClose) / 4;
  12. HaOpen = AMA(Ref(HaClose, - 1), 0.5);
  13. // for graph collapse
  14. for (i = 0; i <= MAPeriod; i++)
  15. HaClose[i] = Null;
  16. /*
  17. // same
  18. // HaOpen = (Ref(HaOpen, -1) + Ref(HaClose, -1)) / 2;
  19. HaOpen[ 0 ] = HaClose[ 0 ];
  20. for(i = 1; i < BarCount; i++) {
  21. HaOpen[i] = (HaOpen[i - 1] + HaClose[i - 1]) / 2;
  22. }
  23. */
  24. HaHigh = Max(MAHigh, Max(HaClose, HaOpen));
  25. HaLow = Min(MALow, Min(HaClose, HaOpen));
  26. // outs comments
  27. "BarIndex = " + BarIndex();
  28. "Open = " + Open;
  29. "High = " + High;
  30. "Low = " + Low;
  31. "Close = " + Close;
  32. "HaOpen = " + HaOpen;
  33. "HaHigh = " + HaHigh;
  34. "HaLow = " + HaLow;
  35. "HaClose = " + HaClose;
  36. // Plot graphs
  37. _N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} HaOpen %g, HaHigh %g, HaLow %g, HaClose %g (%.1f%%) {{VALUES}}", HaOpen, HaHigh, HaLow, HaClose, SelectedValue(ROC(HaClose, 1))));
  38. PlotOHLC(HaOpen, HaHigh, HaLow, HaClose, _DEFAULT_NAME(), ParamColor("Color", colorBlack), styleCandle);
  39. /* **********************************
  40. Code to automatically identify pivots
  41. ********************************** */
  42. // -- what will be our lookback range for the hh and ll?
  43. farback = Param("How Far back to go", 100, 50, 5000, 10);
  44. nBars = Param("Number of bars", 12, 5, 40);
  45. // -- Title.
  46. Title = Name() + " (" + StrLeft(FullName(), 15) + ") O: " + Open + ", H: " + High + ", L: " + Low + ", C: " + Close;
  47. // -- Plot basic candle chart
  48. PlotOHLC(Open, High, Low, Close, "BIdx = " + BarIndex() + "\n" + "O = " + O + "\n" + "H = " + H + "\n" + "L = " + L + "\n" + "C ", colorYellow, styleLine | styleThick);
  49. GraphXSpace = 7;
  50. // -- Create 0-initialized arrays the size of barcount
  51. aHPivs = H - H;
  52. aLPivs = L - L;
  53. // -- More for future use, not necessary for basic plotting
  54. aHPivHighs = H - H;
  55. aLPivLows = L - L;
  56. aHPivIdxs = H - H;
  57. aLPivIdxs = L - L;
  58. nHPivs = 0;
  59. nLPivs = 0;
  60. lastHPIdx = 0;
  61. lastLPIdx = 0;
  62. lastHPH = 0;
  63. lastLPL = 0;
  64. curPivBarIdx = 0;
  65. // -- looking back from the current bar, how many bars
  66. // back were the hhv and llv values of the previous
  67. // n bars, etc.?
  68. aHHVBars = HHVBars(H, nBars);
  69. aLLVBars = LLVBars(L, nBars);
  70. aHHV = HHV(H, nBars);
  71. aLLV = LLV(L, nBars);
  72. // -- Would like to set this up so pivots are calculated back from
  73. // last visible bar to make it easy to "go back" and see the pivots
  74. // this code would find. However, the first instance of
  75. // _Trace output will show a value of 0
  76. aVisBars = Status("barvisible");
  77. nLastVisBar = LastValue(Highest(IIf(aVisBars, BarIndex(), 0)));
  78. _TRACE("Last visible bar: " + nLastVisBar);
  79. // -- Initialize value of curTrend
  80. curBar = (BarCount - 1);
  81. curTrend = "";
  82. if (aLLVBars[curBar] <
  83. aHHVBars[curBar])
  84. {
  85. curTrend = "D";
  86. }
  87. else
  88. {
  89. curTrend = "U";
  90. }
  91. // -- Loop through bars. Search for
  92. // entirely array-based approach
  93. // in future version
  94. for (i = 0; i < farback; i++)
  95. {
  96. curBar = (BarCount - 1) - i;
  97. // -- Have we identified a pivot? If trend is down...
  98. if (aLLVBars[curBar] < aHHVBars[curBar])
  99. {
  100. // ... and had been up, this is a trend change
  101. if (curTrend == "U")
  102. {
  103. curTrend = "D";
  104. // -- Capture pivot information
  105. curPivBarIdx = curBar - aLLVBars[curBar];
  106. aLPivs[curPivBarIdx] = 1;
  107. aLPivLows[nLPivs] = L[curPivBarIdx];
  108. aLPivIdxs[nLPivs] = curPivBarIdx;
  109. nLPivs++;
  110. }
  111. // -- or current trend is up
  112. }
  113. else
  114. {
  115. if (curTrend == "D")
  116. {
  117. curTrend = "U";
  118. curPivBarIdx = curBar - aHHVBars[curBar];
  119. aHPivs[curPivBarIdx] = 1;
  120. aHPivHighs[nHPivs] = H[curPivBarIdx];
  121. aHPivIdxs[nHPivs] = curPivBarIdx;
  122. nHPivs++;
  123. }
  124. // -- If curTrend is up...else...
  125. }
  126. // -- loop through bars
  127. }
  128. // -- Basic attempt to add a pivot this logic may have missed
  129. // -- OK, now I want to look at last two pivots. If the most
  130. // recent low pivot is after the last high, I could
  131. // still have a high pivot that I didn't catch
  132. // -- Start at last bar
  133. curBar = (BarCount - 1);
  134. candIdx = 0;
  135. candPrc = 0;
  136. lastLPIdx = aLPivIdxs[0];
  137. lastLPL = aLPivLows[0];
  138. lastHPIdx = aHPivIdxs[0];
  139. lastHPH = aHPivHighs[0];
  140. if (lastLPIdx > lastHPIdx)
  141. {
  142. // -- Bar and price info for candidate pivot
  143. candIdx = curBar - aHHVBars[curBar];
  144. candPrc = aHHV[curBar];
  145. if (
  146. lastHPH < candPrc AND
  147. candIdx > lastLPIdx AND
  148. candIdx < curBar)
  149. {
  150. // -- OK, we'll add this as a pivot...
  151. aHPivs[candIdx] = 1;
  152. // ...and then rearrange elements in the
  153. // pivot information arrays
  154. for (j = 0; j < nHPivs; j++)
  155. {
  156. aHPivHighs[nHPivs - j] = aHPivHighs[nHPivs -
  157. (j + 1)];
  158. aHPivIdxs[nHPivs - j] = aHPivIdxs[nHPivs - (j + 1)];
  159. }
  160. aHPivHighs[0] = candPrc;
  161. aHPivIdxs[0] = candIdx;
  162. nHPivs++;
  163. }
  164. }

  165. else
  166. {
  167. // -- Bar and price info for candidate pivot
  168. candIdx = curBar - aLLVBars[curBar];
  169. candPrc = aLLV[curBar];
  170. if (
  171. lastLPL > candPrc AND
  172. candIdx > lastHPIdx AND
  173. candIdx < curBar)
  174. {
  175. // -- OK, we'll add this as a pivot...
  176. aLPivs[candIdx] = 1;
  177. // ...and then rearrange elements in the
  178. // pivot information arrays
  179. for (j = 0; j < nLPivs; j++)
  180. {
  181. aLPivLows[nLPivs - j] = aLPivLows[nLPivs - (j + 1)];
  182. aLPivIdxs[nLPivs - j] = aLPivIdxs[nLPivs - (j + 1)];
  183. }
  184. aLPivLows[0] = candPrc;
  185. aLPivIdxs[0] = candIdx;
  186. nLPivs++;
  187. }
  188. }
  189. // -- Dump inventory of high pivots for debugging
  190. /*
  191. for (k=0; k<nHPivs; k++) {
  192. _TRACE("High pivot no. " + k
  193. + " at barindex: " + aHPivIdxs[k] + ", "
  194. + WriteVal(ValueWhen(BarIndex()==aHPivIdxs[k],
  195. DateTime(), 1), formatDateTime)
  196. + ", " + aHPivHighs[k]);
  197. }
  198. */
  199. // -- OK, let's plot the pivots using arrows
  200. PlotShapes(IIf(aHPivs == 1, shapeDownArrow, shapeNone), colorRed, 0, High, Offset = - 15);
  201. PlotShapes(IIf(aLPivs == 1, shapeUpArrow, shapeNone), colorBrightGreen, 0, Low, Offset = - 15);
複製代碼
發表於 10-6-11 19:23 | 顯示全部樓層
不錯謝謝
發表於 11-12-14 19:31 | 顯示全部樓層
回復 1# alexwang7777


    不錯謝謝
發表於 11-12-15 16:19 | 顯示全部樓層
look into the future?
您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

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

GMT+8, 24-11-22 23:13

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

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