COCO研究院

 找回密碼
 註冊
搜索
查看: 5169|回復: 4

Advanced Trend Lines

[複製鏈接]
發表於 10-6-11 16:31 | 顯示全部樓層 |閱讀模式
  1. _SECTION_BEGIN("Advanced Trend Lines");
  2. function GetXSupport(Lo, Percentage, Back)
  3. {
  4. return ((BarCount - 1) - LastValue(TroughBars(Lo, Percentage,Back)));
  5. }
  6. function GetYSupport(Lo, Percentage, Back)
  7. {
  8. return (LastValue(Trough(Lo, Percentage, back)));
  9. }

  10. function GetXResistance(Hi, Percentage, Back)
  11. {
  12. return ((BarCount - 1) -LastValue(PeakBars(Hi, Percentage, Back)));
  13. }
  14. function GetYResistance(Hi, Percentage, Back)
  15. {
  16. return (LastValue(Peak(Hi, Percentage, Back)));
  17. }
  18. function TD_Supply(P)
  19. {
  20. return ( P > Ref(P, 1) AND P > Ref(P, -1) AND P > Ref(C, -2));
  21. }
  22. function TD_Demand(P)
  23. {
  24. return ( P < Ref(P, 1) AND P < Ref(P, -1) AND P < Ref(C, -2));
  25. }
  26. ////////////////////////////////////////////////////////////////////////
  27. //Parameters
  28. Percentage = Param("Percentage", 1.5, 0.01, 100. ,0.01);
  29. DrawAllLines = ParamToggle("Draw All Lines?", "No|Yes");
  30. Lines = Param("Lines?", 1, 1, BarCount-2);
  31. DrawR = ParamList("Resistance Points", "Off|High to High|High to Low", 1);
  32. DrawS = ParamList("Support Points", "Off|Low to Low|Low to High", 1);
  33. ShowTDP = ParamToggle("Show TD Pionts", "No|Yes", 1);
  34. AllOrDownR = ParamToggle("Resistance Direction", "All|Down");
  35. AllOrUpS = ParamToggle("Support Direction", "All|Up");
  36. ShowSR = ParamToggle("Show Vert S/R","No|Yes", 1);
  37. SRPer = Param("S/R Percentage", 3, 1);
  38. SRBack = Param("S/R Back", 5, 1);
  39. str = "";
  40. Res = Sup = 0;
  41. Con = 1;
  42. ////////////////////////////////////////////////////////////////////////
  43. Main = C;
  44. Con = ConS = ConR = 1;
  45. if(DrawS=="Low to Low")
  46. {
  47. Support1 = L;
  48. Support2 = L;
  49. }
  50. else
  51. {
  52. Support1 = L;
  53. Support2 = H;
  54. }
  55. if(DrawR=="High to High")
  56. {
  57. Resistance1 = H;
  58. Resistance2 = H;
  59. }
  60. else
  61. {
  62. Resistance1 = H;
  63. Resistance2 = L;
  64. }
  65. ////////////////////////////////////////////////////////////////////////
  66. //Plotting Area
  67. Plot(Main, "", IIf(C>O,colorGreen, colorRed), styleBar);
  68. if(DrawAllLines)
  69. for(i = 2; i<=Lines+1; i++)
  70. {
  71. if(DrawS!="Off")
  72. {
  73. x0 = GetXSupport(Support1, Percentage, i);
  74. x1 = GetXSupport(Support2, Percentage, i-1);
  75. y0 = GetYSupport(Support1, Percentage, i);
  76. y1 = GetYSupport(Support2, Percentage, i-1);
  77. x = LineArray(x0, y0, x1, y1, 1);
  78. if(AllOrUpS) ConS = StrToNum(NumToStr(y0 < y1));
  79. if(Con AND ConS)
  80. Plot(x, "", IIf(LastValue(C) < LastValue(x), colorBlue,colorLightBlue), styleLine|styleThick);
  81. }
  82. if(DrawR!="Off")
  83. {
  84. x0 = GetXResistance(Resistance1, Percentage, i);
  85. x1 = GetXResistance(Resistance2, Percentage, i-1);
  86. y0 = GetYResistance(Resistance1, Percentage, i);
  87. y1 = GetYResistance(Resistance2, Percentage, i-1);
  88. x = LineArray(x0, y0, x1, y1, 1);
  89. if(AllOrDownR) ConR = y0 > y1;
  90. if(Con AND ConR)
  91. Plot(x, "", IIf(LastValue(C) < LastValue(x), colorBlue,colorLightBlue), styleLine|styleThick);
  92. }
  93. }
  94. else
  95. {
  96. if(DrawS!="Off")
  97. {
  98. x0 = GetXSupport(Support1, Percentage, Lines+1);
  99. x1 = GetXSupport(Support2, Percentage, Lines);
  100. y0 = GetYSupport(Support1, Percentage, Lines+1);
  101. y1 = GetYSupport(Support2, Percentage, Lines);
  102. x = LineArray(x0, y0, x1, y1, 1 );
  103. Sup = LastValue(LinRegSlope(x, Lines+1));
  104. if(AllOrUpS) ConS = y0 < y1;
  105. if(Con AND ConS)
  106. Plot(x, "", IIf(LastValue(C) < LastValue(x), colorBlue,colorLightBlue), styleLine|styleThick);
  107. }
  108. if(DrawR!="Off")
  109. {
  110. x0 = GetXResistance(Resistance1, Percentage, Lines+1);
  111. x1 = GetXResistance(Resistance2, Percentage, Lines);
  112. y0 = GetYResistance(Resistance1, Percentage, Lines+1);
  113. y1 = GetYResistance(Resistance2, Percentage, Lines);
  114. x = LineArray(x0, y0, x1, y1, 1 );
  115. Res = LastValue(LinRegSlope(x, Lines+1));
  116. if(AllOrDownR) ConR = y0 > y1;
  117. if(Con AND ConR)
  118. Plot(x, "", IIf(LastValue(C) < LastValue(x), colorBlue,colorLightBlue), styleLine|styleThick);
  119. }
  120. str = "\nR Slope=("+Res+"), S Slope=("+Sup+")";
  121. }

  122. if(ShowTDP)
  123. {
  124. PlotShapes(TD_Supply(H)*shapeSmallCircle, colorRed, 0, H, H*.001);
  125. PlotShapes(TD_Demand(L)*shapeSmallCircle, colorGreen, 0, L, -L*.001);
  126. }
  127. if(ShowSR)
  128. {
  129. for(i=1; i<=SRBack; i++)
  130. {
  131. x0 = GetXSupport(L, SRPer, i);
  132. x1 = BarCount-1;
  133. y0 = GetYSupport(L, SRPer, i);
  134. x = LineArray(x0, y0, x1, y0, 0);
  135. Plot(x, "", IIf(LastValue(C) > x, colorDarkGreen, colorDarkRed), styleLine|styleDashed|styleThick);
  136. x0 = GetXResistance(H, SRPer, i);
  137. y0 = GetYResistance(H, SRPer, i);
  138. x = LineArray(x0, y0, x1, y0, 0);
  139. Plot(x, "", IIf(LastValue(C) > x, colorDarkGreen, colorDarkRed), styleLine|styleDashed|styleThick);

  140. }
  141. }
  142. Title =FullName()+" ({{NAME}})\n{{DATE}}\n"+"Open: "+O+", Hi: "+H+", Lo: "+L+", Close: "+C+StrFormat(" (%.2f %.2f\%)", C-Ref(C, -1), SelectedValue(ROC(C, 1)))+str;

  143. _SECTION_END();

  144. _SECTION_BEGIN("lin Reg with channel");
  145. // Linear Regression Line with 2 Standard Deviation Channels Plotted Above and Below
  146. // Written by Patrick Hargus, with critical hints from Marcin Gorzynski, Amibroker.com Technical Support
  147. // Designed for use with AB 4.63 beta and above, using drag and drop feature.
  148. // Permits plotting a linear regression line of any price field available on the chart for a period determined by the user.
  149. // 2 Channels, based on a standard deviation each determined by the user, are plotted above and below the linear regression line.
  150. // A look back feature is also provided for examining how the indicator would have appeared on a chart X periods in the past.


  151. P = ParamField("Price field",-1);
  152. Daysback = Param("Period for Liner Regression Line",21,1,240,1);
  153. shift = Param("Look back period",0,0,240,1);


  154. // =============================== Math Formula =============================================================

  155. x = Cum(1);
  156. lastx = LastValue( x ) - shift;
  157. aa = LastValue( Ref(LinRegIntercept( p, Daysback), -shift) );
  158. bb = LastValue( Ref(LinRegSlope( p, Daysback ), -shift) );
  159. y = Aa + bb * ( x - (Lastx - DaysBack +1 ) );


  160. // ==================Plot the Linear Regression Line ==========================================================


  161. LRColor = ParamColor("LR Color", colorCycle );
  162. LRStyle = ParamStyle("LR Style");

  163. LRLine = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y, Null );
  164. Plot( LRLine , "LinReg", LRCOLOR, LRSTYLE ); // styleDots );

  165. // ========================== Plot 1st SD Channel ===============================================================

  166. SDP = Param("Standard Deviation", 1.5, 0, 6, 0.1);
  167. SD = SDP/2;

  168. width = LastValue( Ref(SD*StDev(p, Daysback),-shift) ); // THIS IS WHERE THE WIDTH OF THE CHANELS IS SET
  169. SDU = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y+width , Null ) ;
  170. SDL = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y-width , Null ) ;

  171. SDColor = ParamColor("SD Color", colorCycle );
  172. SDStyle = ParamStyle("SD Style");

  173. Plot( SDU , "Upper Lin Reg", SDColor,SDStyle );
  174. Plot( SDL , "Lower Lin Reg", SDColor,SDStyle );

  175. // ========================== Plot 2d SD Channel ===============================================================

  176. SDP2 = Param("2d Standard Deviation", 2.0, 0, 6, 0.1);
  177. SD2 = SDP2/2;

  178. width2 = LastValue( Ref(SD2*StDev(p, Daysback),-shift) ); // THIS IS WHERE THE WIDTH OF THE CHANELS IS SET
  179. SDU2 = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y+width2 , Null ) ;
  180. SDL2 = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y-width2 , Null ) ;

  181. SDColor2 = ParamColor("2 SD Color", colorCycle );
  182. SDStyle2 = ParamStyle("2 SD Style");

  183. Plot( SDU2 , "Upper Lin Reg", SDColor2,SDStyle2 );
  184. Plot( SDL2 , "Lower Lin Reg", SDColor2,SDStyle2 );

  185. // ============================ End Indicator Code ==============================================================
  186. _SECTION_END();
複製代碼


2.jpg
發表於 10-6-11 19:54 | 顯示全部樓層
功能真多~謝謝分享
發表於 10-6-16 20:52 | 顯示全部樓層
好東西  學習程式用  謝謝  分享 ~~~~~~~~~~
發表於 11-3-28 19:23 | 顯示全部樓層
這個問題我想了很久
沒有答案
只好與各位大大討論討論
謝謝
發表於 11-9-4 02:38 | 顯示全部樓層
很棒的東西, 感謝分享 !!
您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

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

GMT+8, 24-11-20 11:36

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

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