Price Persistency.afl
上传用户:shiqiang
上传日期:2009-06-12
资源大小:1289k
文件大小:9k
源码类别:

金融证券系统

开发平台:

Others

  1. //------------------------------------------------------------------------------
  2. //
  3. //  Formula Name:    Price Persistency
  4. //  Author/Uploader: Anthony Faragasso 
  5. //  E-mail:          ajf1111@epix.net
  6. //  Date/Time Added: 2002-12-29 18:20:39
  7. //  Origin:          
  8. //  Keywords:        
  9. //  Level:           basic
  10. //  Flags:           exploration
  11. //  Formula URL:     http://www.amibroker.com/library/formula.php?id=245
  12. //  Details URL:     http://www.amibroker.com/library/detail.php?id=245
  13. //
  14. //------------------------------------------------------------------------------
  15. //
  16. //  Interested in measuring a short-term Trend ?
  17. //
  18. //  Price persistency is the number of days that a market continues to close
  19. //  either up or down. It's another term for a market run. The usefulness of
  20. //  Price Persistency is based on the theory of runs..It is the idea that,
  21. //  given the market has moved in a particular direction for ( n ) days, the
  22. //  likelihood it will either continue..or not...can be estimated and used in a
  23. //  profitable trading system....
  24. //
  25. //------------------------------------------------------------------------------
  26. //Price Persistency Exploration
  27. //Interpreted from January,2002, TASC, page 44
  28. //article by Gordon Gustafson on Price Persistency
  29. //Coded by Anthony Faragasso, 12/31/01
  30. //Version 1.00
  31. /*****Bars in the test*****************/
  32. bars=Cum(1);
  33. /**************************************/
  34. Day1up=C > Ref(C,-1);
  35. Day1Dn=C < Ref(C,-1);
  36. /**************************************/
  37. Day2up=C > Ref(C,-1) AND Ref(C,-1) > Ref(C,-2);
  38. Day2Dn=C < Ref(C,-1) AND Ref(C,-1) < Ref(C,-2);
  39. /***********************************************/
  40. Day3up=C > Ref(C,-1) AND Ref(C,-1) > Ref(C,-2) AND Ref(C,-2) > Ref(C,-3);
  41. Day3Dn=C < Ref(C,-1) AND Ref(C,-1) < Ref(C,-2) AND Ref(C,-2) < Ref(C,-3);
  42. /**********************************************/
  43. Day4up=C > Ref(C,-1) AND Ref(C,-1)>Ref(C,-2)AND Ref(C,-2)>Ref(C,-3) AND Ref(C,-3) > Ref(C,-4);
  44. Day4Dn=C < Ref(C,-1) AND Ref(C,-1)<Ref(C,-2)AND Ref(C,-2)<Ref(C,-3) AND Ref(C,-3) < Ref(C,-4);
  45. /**********************************************/
  46. Day5up=C > Ref(C,-1) AND Ref(C,-1)>Ref(C,-2)AND Ref(C,-2)>Ref(C,-3)AND Ref(C,-3) > Ref(C,-4) AND Ref(C,-4) > Ref(C,-5);
  47. Day5Dn=C < Ref(C,-1) AND Ref(C,-1)<Ref(C,-2)AND Ref(C,-2)<Ref(C,-3)AND Ref(C,-3) < Ref(C,-4) AND Ref(C,-4) < Ref(C,-5);
  48. /************************************************/
  49. Day6up=C > Ref(C,-1) AND Ref(C,-1)> Ref(C,-2)AND Ref(C,-2)>Ref(C,-3)AND Ref(C,-3) > Ref(C,-4) AND Ref(C,-4) > Ref(C,-5) AND Ref(C,-5) > Ref(C,-6);
  50. Day6Dn=C < Ref(C,-1) AND Ref(C,-1)< Ref(C,-2)AND Ref(C,-2)< Ref(C,-3)AND Ref(C,-3) < Ref(C,-4)AND Ref(C,-4) < Ref(C,-5) AND Ref(C,-5) < Ref(C,-6);
  51. /***********************************************/
  52. Day7up=C > Ref(C,-1) AND Ref(C,-1)> Ref(C,-2)AND Ref(C,-2)>Ref(C,-3)AND Ref(C,-3) > Ref(C,-4) AND Ref(C,-4) > Ref(C,-5)AND Ref(C,-5)>Ref(C,-6) AND Ref(C,-6) > Ref(C,-7);
  53. Day7Dn=C < Ref(C,-1) AND Ref(C,-1)< Ref(C,-2)AND Ref(C,-2)< Ref(C,-3)AND Ref(C,-3) < Ref(C,-4)AND Ref(C,-4) < Ref(C,-5)AND Ref(C,-5)< Ref(C,-6)AND Ref(C,-6) < Ref(C,-7);
  54. /*********************************************/
  55. Day8up=C > Ref(C,-1) AND Ref(C,-1)> Ref(C,-2)AND Ref(C,-2)>Ref(C,-3)AND Ref(C,-3) > Ref(C,-4) AND Ref(C,-4) > Ref(C,-5)AND Ref(C,-5)>Ref(C,-6)AND Ref(C,-6) > Ref(C,-7) AND Ref(C,-7) > Ref(C,-8);
  56. Day8Dn=C < Ref(C,-1) AND Ref(C,-1)< Ref(C,-2)AND Ref(C,-2)< Ref(C,-3)AND Ref(C,-3) < Ref(C,-4)AND Ref(C,-4) < Ref(C,-5)AND Ref(C,-5)< Ref(C,-6)AND Ref(C,-6) < Ref(C,-7)AND Ref(C,-7) < Ref(C,-8);
  57. /**********************************************/
  58. Day9up=C > Ref(C,-1) AND Ref(C,-1)> Ref(C,-2)AND Ref(C,-2)>Ref(C,-3)AND Ref(C,-3) > Ref(C,-4) AND Ref(C,-4) > Ref(C,-5)AND Ref(C,-5)>Ref(C,-6)AND Ref(C,-6) > Ref(C,-7)AND Ref(C,-7) > Ref(C,-8) AND Ref(C,-8) > Ref(C,-9);
  59. Day9Dn=C < Ref(C,-1) AND Ref(C,-1)< Ref(C,-2)AND Ref(C,-2)< Ref(C,-3)AND Ref(C,-3) < Ref(C,-4)AND Ref(C,-4) < Ref(C,-5)AND Ref(C,-5)< Ref(C,-6)AND Ref(C,-6) < Ref(C,-7)AND Ref(C,-7) < Ref(C,-8) AND Ref(C,-8) < Ref(C,-9);
  60. /**********************************************/
  61. Day10up=C > Ref(C,-1) AND Ref(C,-1)> Ref(C,-2)AND Ref(C,-2)>Ref(C,-3)AND Ref(C,-3) > Ref(C,-4) AND Ref(C,-4) > Ref(C,-5)AND Ref(C,-5)>Ref(C,-6)AND Ref(C,-6) > Ref(C,-7)AND Ref(C,-7) > Ref(C,-8)AND Ref(C,-8)> Ref(C,-9) AND Ref(C,-9) > Ref(C,-10);
  62. Day10Dn=C < Ref(C,-1) AND Ref(C,-1)< Ref(C,-2)AND Ref(C,-2)< Ref(C,-3)AND Ref(C,-3) < Ref(C,-4)AND Ref(C,-4) < Ref(C,-5)AND Ref(C,-5)< Ref(C,-6)AND Ref(C,-6) < Ref(C,-7)AND Ref(C,-7) < Ref(C,-8)AND Ref(C,-8) < Ref(C,-9) AND Ref(C,-9) < Ref(C,-10);
  63. /************************************************/
  64. tot1dayUp=LastValue(Cum(Day1up));tot1dayDn=LastValue(Cum(Day1Dn));
  65. tot2dayUp=LastValue(Cum(Day2up));tot2dayDn=LastValue(Cum(Day2Dn));
  66. tot3dayUp=LastValue(Cum(Day3up));tot3dayDn=LastValue(Cum(Day3Dn));
  67. tot4dayUp=LastValue(Cum(Day4up));tot4dayDn=LastValue(Cum(Day4Dn));
  68. tot5dayUp=LastValue(Cum(Day5up));tot5dayDn=LastValue(Cum(Day5Dn));
  69. tot6dayUp=LastValue(Cum(Day6up));tot6dayDn=LastValue(Cum(Day6Dn));
  70. tot7dayUp=LastValue(Cum(Day7up));tot7dayDn=LastValue(Cum(Day7Dn));
  71. tot8dayUp=LastValue(Cum(Day8up));tot8dayDn=LastValue(Cum(Day8Dn));
  72. tot9dayUp=LastValue(Cum(Day9up));tot9dayDn=LastValue(Cum(Day9Dn));
  73. tot10dayUp=LastValue(Cum(Day10up));tot10dayDn=LastValue(Cum(Day10Dn));
  74. /*********Percentages for Up or Down as a % of total bars *************/
  75. totpct1dayup=tot1dayUp/(Cum(1))*100;
  76. totpct1dayDn=tot1dayDn/(Cum(1))*100;
  77. totpct2dayUp=tot2dayUp/(Cum(1))*100;
  78. totpct2dayDn=tot2dayDn/(Cum(1))*100;
  79. totpct3dayUp=tot3dayUp/(Cum(1))*100;
  80. totpct3dayDn=tot3dayDn/(Cum(1))*100;
  81. totpct4dayUp=tot4dayUp/(Cum(1))*100;
  82. totpct4dayDn=tot4dayDn/(Cum(1))*100;
  83. totpct5dayUp=tot5dayUp/(Cum(1))*100;
  84. totpct5dayDn=tot5dayDn/(Cum(1))*100;
  85. totpct6dayUp=tot6dayUp/(Cum(1))*100;
  86. totpct6dayDn=tot6dayDn/(Cum(1))*100;
  87. totpct7dayUp=tot7dayUp/(Cum(1))*100;
  88. totpct7dayDn=tot7dayDn/(Cum(1))*100;
  89. totpct8dayUp=tot8dayUp/(Cum(1))*100;
  90. totpct8dayDn=tot8dayDn/(Cum(1))*100;
  91. totpct9dayUp=tot9dayUp/(Cum(1))*100;
  92. totpct9dayDn=tot9dayDn/(Cum(1))*100;
  93. totpct10dayUp=tot10dayUp/(Cum(1))*100;
  94. totpct10dayDn=tot10dayDn/(Cum(1))*100;
  95. /*******************************************************/
  96. /**********Percent up or down / previous up or down***********************/
  97. Pct2dayUp=(tot2dayUp/tot1dayup)*100;
  98. Pct2dayDn=(tot2dayDn/tot1dayDn)*100;
  99. pct3dayUp=(tot3dayUp/tot2dayup)*100;
  100. pct3dayDn=(tot3dayDn/tot2dayDn)*100;
  101. pct4dayUp=(tot4dayUp/tot3dayUp)*100;
  102. pct4dayDn=(tot4dayDn/tot3dayDn)*100;
  103. pct5dayUp=(tot5dayUp/tot4dayUp)*100;
  104. pct5dayDn=(tot5dayDn/tot4dayDn)*100;
  105. pct6dayUp=(tot6dayUp/tot5dayUp)*100;
  106. pct6dayDn=(tot6dayDn/tot5dayDn)*100;
  107. pct7dayUp=(tot7dayUp/tot6dayUp)*100;
  108. pct7dayDn=(tot7dayDn/tot6dayDn)*100;
  109. pct8dayUp=(tot8dayUp/tot7dayUp)*100;
  110. pct8dayDn=(tot8dayDn/tot7dayDn)*100;
  111. pct9dayUp=(tot9dayUp/tot8dayUp)*100;
  112. pct9dayDn=(tot9dayDn/tot8dayDn)*100;
  113. pct10dayUp=(tot10dayUp/tot9dayUp)*100;
  114. pct10dayDn=(tot10dayDn/tot9dayDn)*100;
  115. Filter=1;
  116. /***Current State of the Closes******/
  117. stateUp=BarsSince(C < Ref(C,-1));
  118. stateDown=BarsSince(C > Ref(C,-1));
  119. AddColumn(stateUp,"currentUp");
  120. AddColumn(statedown,"CurrentDown");
  121. /***Close is equal to previous close*****/
  122. AddColumn(Cum(C == Ref(C,-1)),"Close==PrevClose",1);
  123. /***Days in a row ,whether up or down ( A Run )********/
  124. //1 day
  125. AddColumn(tot1dayUp,"1dayUp",1);AddColumn(tot1dayDn,"1dayDn",1);AddColumn(0,"%Up/PrevUp");AddColumn(0,"%Dn/PrevDn");AddColumn(totpct1dayUp,"%Up[bars]");AddColumn(totpct1dayDn,"%Dn[bars]");
  126. //2 days 
  127. AddColumn(tot2dayUp,"2dayUp",1);AddColumn(tot2dayDn,"2dayDn",1);AddColumn(pct2dayUp,"%Up/prevUp");AddColumn(pct2dayDn,"%Dn/prevDn");AddColumn(totpct2dayUp,"%Up[bars]");AddColumn(totpct2dayDn,"%Dn[bars]");
  128. //3 days
  129. AddColumn(tot3dayUp,"3dayUp",1);AddColumn(tot3dayDn,"3dayDn",1);AddColumn(pct3dayUp,"%Up/prevUp");AddColumn(pct3dayDn,"%Dn/prevDn");AddColumn(totpct3dayUp,"%Up[bars]");AddColumn(totpct3dayDn,"%Dn[bars]");
  130. //4 days
  131. AddColumn(tot4dayUp,"4dayUp",1);AddColumn(tot4dayDn,"4dayDn",1);AddColumn(pct4dayup,"%Up/prevUp");AddColumn(pct4dayDn,"%Dn/prevDn");AddColumn(totpct4dayUp,"%Up[bars]");AddColumn(totpct4dayDn,"%Dn[bars]");
  132. //5 days
  133. AddColumn(tot5dayUp,"5dayUp",1);AddColumn(tot5dayDn,"5dayDn",1);AddColumn(pct5dayup,"%Up/prevUp");AddColumn(pct5dayDn,"%Dn/prevDn");AddColumn(totpct5dayUp,"%Up[bars]");AddColumn(totpct5dayDn,"%Dn[bars]");
  134. //6 days
  135. AddColumn(tot6dayUp,"6dayUp",1);AddColumn(tot6dayDn,"6dayDn",1);AddColumn(pct6dayUp,"%Up/prevUp");AddColumn(pct6dayDn,"%Dn/prevDn");AddColumn(totpct6dayUp,"%Up[bars]");AddColumn(totpct6dayDn,"%Dn[bars]");
  136. //7 days
  137. AddColumn(tot7dayUp,"7dayUp",1);AddColumn(tot7dayDn,"7dayDn",1);AddColumn(pct7dayUp,"%Up/prevUp");AddColumn(pct7dayDn,"%Dn/prevDn");AddColumn(totpct7dayUp,"%Up[bars]");AddColumn(totpct7dayDn,"%Dn[bars]");
  138. //8 days
  139. AddColumn(tot8dayUp,"8dayUp",1);AddColumn(tot8dayDn,"8dayDn",1);AddColumn(pct8dayUp,"%Up/prevUp");AddColumn(pct8dayDn,"%Dn/PrevDn");AddColumn(totpct8dayUp,"%Up[bars]");AddColumn(totpct8dayDn,"%Dn[bars]");
  140. //9 days
  141. AddColumn(tot9dayUp,"9dayUp",1);AddColumn(tot9dayDn,"9dayDn",1);AddColumn(pct9dayUp,"%Up/prevUp");AddColumn(pct9dayDn,"%Dn/prevDn");AddColumn(totpct9dayUp,"%Up[bars]");AddColumn(totpct9dayDn,"%Dn[bars]");
  142. //10 days
  143. AddColumn(tot10dayUp,"10dayUp",1);AddColumn(tot10dayDn,"10dayDn",1);AddColumn(pct10dayUp,"%Up/prevUp");AddColumn(pct10dayDn,"%Dn/prevDn");AddColumn(totpct10dayUp,"%Up[bars]");AddColumn(totpct10dayDn,"%Dn[bars]");
  144. AddColumn(bars,"bars",1);