RUTVOL timing signal with BB Scoring routine.afl
上传用户:shiqiang
上传日期:2009-06-12
资源大小:1289k
文件大小:7k
源码类别:

金融证券系统

开发平台:

Others

  1. //------------------------------------------------------------------------------
  2. //
  3. //  Formula Name:    RUTVOL timing signal with BB Scoring routine
  4. //  Author/Uploader: Gary Serkhoshian 
  5. //  E-mail:          serkhoshian777@yahoo.com
  6. //  Date/Time Added: 2003-11-18 15:05:44
  7. //  Origin:          
  8. //  Keywords:        Russell 2000, RUTVOL, Scoring example
  9. //  Level:           semi-advanced
  10. //  Flags:           system
  11. //  Formula URL:     http://www.amibroker.com/library/formula.php?id=307
  12. //  Details URL:     http://www.amibroker.com/library/detail.php?id=307
  13. //
  14. //------------------------------------------------------------------------------
  15. //
  16. //  RUVOL signal was developed by Werner Gansz who was kind enough to share it
  17. //  with the FT-Talk community. RUTVOL is an intermediate-term signal, and in
  18. //  this code example it is coupled with a Bollinger Band scoring routine as a
  19. //  selection methodology for stocks.
  20. //
  21. //------------------------------------------------------------------------------
  22. /*RUTVOL SIGNAL LOGIC FROM .INI FILES
  23. EXPLAINATION:  RUTVOL BASES THE FOLLOWING INDICATORS ON RUSSELL 2K INDEX.
  24.  1.  STOCHASTICS
  25.  2.  MACD
  26.  3.  RSI
  27. CALCULATIONS BASED ON THE NASDAQ TVOLQ,UVOLQ,DVOLQ AS FOLLOWS
  28. 1. ACCUTRACK
  29. 2. STOCHASTICS
  30. TRANSLATED:  8/18/03
  31. NOTE: THIS IS A MODIFIED VERSION OF RUTVOL.  THIS VERSION PRODUCES MODESTLY BETTER RETURNS WITH SAME DD AS MEASURED AGAINST $RUT.  BELOW ARE THE CHANGES NEED TO CONVERT TO ORIGINAL RUTVOL.
  32. 1. RUTTR_BUYCOND has no RSI filtering in the current RUTTR.
  33. 2. Current version of RUTVOL does not use the volume AccuTrak in the volume buy condition
  34. */
  35. // STEP #1:  ESTABLISH PARAMETERS
  36. RUT = Foreign("!RUT","C");
  37. //STOCHASTICS
  38. AVERAGE = 53; //Optimize("AVERAGE",53,27,80,5);
  39. SMOOTH = 49; //Optimize("SMOOTH",49,25,74,5);
  40. TRIGGER = 28; //Optimize("TRIGGER",28,14,42,5);
  41. BuyVALUE = 0;
  42. SellVALUE = 0;
  43. //MACD
  44. ShortMA = 45; //Optimize("SHORTMA",45,23,90,5);
  45. LONGMA = 90; //Optimize("LONGMA",90,45,135,5);
  46. SignalMA = 8;  //Optimize("SIGNALMA",8,4,12,1);
  47. BuyLEVEL = 0;
  48. SellLEVEL = 0;
  49. //RSI
  50. RSILEN = 14; //Optimize("RSILEN",14,7,21,1);
  51. BuyRSI = 63;
  52. SellRSI = 47;
  53. ShortRSI = 37;
  54. //VOLUME EMA
  55. VOLEMA1 = 60; //Optimize("VOLEMA1",60,30,90,5);
  56. VOLEMA2 = 120; //Optimize("VOLEMA2",120,60,180,5);
  57. //VOLUME ACCUTRACK
  58. ACCU_SHORTPERIOD = 11; //Optimize("ACCU_SHORTPERIOD",11,5,16,1);
  59. ACCU_LONGPERIOD = 44; //Optimize("ACCU_LONGPERIOD",44,22,66,3);
  60. //TOTAL VOLUME STOCH CALC
  61. TVOL_AVG = 41;  //Optimize("TVOL AVG",41,22,66,2);
  62. TVOL_SMOOTH = 10;  //Optimize("TVOL_SMOOTH",10,5,15,1);
  63. TVOL_TRIGGER = 8; //Optimize("TVOL_TRIGGER",8,4,12,1);
  64. //********RUTTR CALC*********//
  65. //STOCH CALC BEGIN
  66. KSTOCH = 100 * (RUT - LLV(RUT,AVERAGE)) / (HHV(RUT,AVERAGE) - LLV(RUT,AVERAGE));
  67. DSTOCH = EMA(KSTOCH,SMOOTH);
  68. SignalLINE = EMA(DSTOCH,TRIGGER);
  69. STOCH_HISTO = DSTOCH - SignalLINE;
  70. //MACD CALC BEGIN
  71. RUTMACD = EMA(RUT,ShortMA) - EMA(RUT,LONGMA);
  72. MACDSignalLINE = EMA(RUTMACD, SignalMA);
  73. MACD_HISTO = RUTMACD - MACDSIGNALLINE;
  74. //RSI FILTER BEGIN
  75. RSIFILTER_SELL = RSIa(RUT,RSILEN) < Ref(RSIa(RUT,RSILEN),-3) AND RUT < Ref(RUT,-1) AND RSIa(RUT,RSILEN) < SellRSI;
  76. RSIFILTER_BUY = RSIa(RUT,RSILEN) > BuyRSI;
  77. //RUTTR SIGNAL LOGIC
  78. //STEP#1:  BUY & SELL COND
  79. RUTTR_BUYCOND = (Stoch_HISTO > 0 AND MACD_HISTO > 0) OR RSIFILTER_BUY;
  80. RUTTR_SELLCOND = (Stoch_HISTO < 0 AND MACD_HISTO < 0)  AND RSIFILTER_SELL;
  81. //STEP#2: BUY & SELL STATE
  82. RUTTR_BUYSTATE = Flip(RUTTR_BUYCOND,RUTTR_SELLCOND);
  83. //RUTTR_SELLSTATE = Flip(RUTTR_SELLCOND,RUTTR_BUYCOND);
  84. RUTTR_SELLSTATE = NOT RUTTR_BUYSTATE;
  85. //*********RUTVOL CALC*********//
  86. TVOLQ = Foreign("!NQ-V","C");
  87. UVOLQ = Foreign("!NQ-AV","C");
  88. DVOLQ = Foreign("!NQ-DV","C");
  89. NQVOLEMA = EMA(TVOLQ,VOLEMA1);
  90. NQUVOLEMA = EMA(UVOLQ,VOLEMA2);
  91. NQDVOLEMA = EMA(DVOLQ,VOLEMA2);
  92. //ACCUTRACK CALC OF NQ UP/DN VOL
  93. UPVOLCHG = (NQUVOLEMA - Ref(NQUVOLEMA,-1)) / Ref(NQUVOLEMA,-1);
  94. DNVOLCHG = (NQDVOLEMA - Ref(NQDVOLEMA,-1)) / Ref(NQDVOLEMA,-1);
  95. UPVOL = EMA(UPVOLCHG, ACCU_LONGPERIOD);
  96. DNVOL = EMA(DNVOLCHG, ACCU_LONGPERIOD);
  97. VOL_DIFF = UPVOL - DNVOL;
  98. ACCU_UPDNVOL = EMA(VOL_DIFF,ACCU_SHORTPERIOD);
  99. ACCU_UPDNVOL_BUY = Cross(ACCU_UPDNVOL, 0); 
  100. //ACCU_UPDNVOL_SELL = Cross(0, ACCU_UPDNVOL); 
  101. // NASDAQ TOTAL VOLUME STOCHASTICS CALC
  102. NQVOL_KSTOCH = 100 * (NQVOLEMA - LLV(NQVOLEMA,TVOL_AVG)) / (HHV(NQVOLEMA,TVOL_AVG) - LLV(NQVOLEMA,TVOL_AVG));
  103. NQVOL_DSTOCH = EMA(NQVOL_KSTOCH,TVOL_SMOOTH);
  104. NQVOL_SIGNALLINE = EMA(NQVOL_DSTOCH,TVOL_TRIGGER);
  105. TVOL_STOCH_BUYCOND = Cross(NQVOL_DSTOCH,20) OR Cross(NQVOL_DSTOCH,80) OR ACCU_UPDNVOL_BUY;
  106. TVOL_STOCH_SELLCOND = Cross(20, NQVOL_DSTOCH) OR Cross(80,NQVOL_DSTOCH);
  107. TVOL_STOCH_BUYSTATE = Flip(TVOL_STOCH_BUYCOND,TVOL_STOCH_SELLCOND);
  108. //*********RUTVOL SIGNAL LOGIC*********//
  109. //CONDITIONS
  110. RUTVOL_BUYCOND = TVOL_STOCH_BUYSTATE AND RUTTR_BUYSTATE;
  111. RUTVOL_SELLCOND = NOT TVOL_STOCH_BUYSTATE OR NOT RUTTR_BUYSTATE;
  112. //STATES
  113. RUTVOL_BUYSTATE = Flip(RUTVOL_BUYCOND,RUTVOL_SELLCOND);
  114. //SIGNALS
  115. Buy = RUTVOL_BUYSTATE;
  116. Sell = NOT RUTVOL_BUYSTATE;
  117. //EXREM SIGNALS
  118. Buy = ExRem(Buy,Sell);
  119. Sell = ExRem(Sell,Buy);
  120. ApplyStop(stopTypeLoss,stopModePercent,Optimize("MaxLoss",10,1,20,1),True,True);
  121. //--------------------------------------------------  PORTFOLIO TRADING CODE BEGINS  ------------------------------------------------------------------
  122. //Set Trade Delays and Initial Equity
  123. SetOption("InitialEquity", 100000);
  124. SetTradeDelays(1,1,1,1);
  125. RoundLotSize = 100;
  126. //Position Size Info
  127. SetOption("MinShares",100);
  128. MaxPos = Optimize("Max Positions",5,1,15,1);
  129. SetOption("MaxOpenPositions",MaxPos);
  130. PositionSize = -100/MaxPos;
  131. //Scoring Routine Begins
  132. BBandWid = 2;
  133. UBBand   = BBandTop(Close, 21, BBandWid);
  134. LBBand   = BBandBot(Close, 21, BBandWid);
  135. PositionScore   = 100 - 100 * (Close - LBBand) / (UBBand - LBBand);//0 when C == Upper Band, 100 when C == Lower Band
  136. //********EXPLORE CODE*********//
  137. Filter = 1;//Status("LastBarInRange");
  138. RUTVOLSIG = IIf(RUTVOL_BUYSTATE == 1,1,0);
  139. AddToComposite(RUTVOLSIG,"~RUTVOL","X",atcFlagDefaults | atcFlagEnableInExplore);
  140. AddColumn(RUTVOLSIG,"RUTVOL STATE",8.0);
  141. AddColumn(RUTTR_BUYSTATE,"RUTTR",8.0, IIf(RUTTR_BUYSTATE == 1,colorGreen,colorDefault), colorDefault);
  142. AddColumn(TVOL_STOCH_BUYSTATE,"VOLUME",8.0,IIf(TVOL_STOCH_BUYSTATE == 1,colorGreen,colorDefault), colorDefault);
  143. AddColumn(Buy,"RUTVOL BUY",8.0,colorDefault,IIf(Buy == 1,colorGreen,colorDefault));
  144. AddColumn(Sell,"RUTVOL SELL",8.0,colorDefault,IIf(Sell == 1,colorYellow,colorDefault));
  145. //*********INDICATOR CODE*********//
  146. Title = "RUTVOL:   " + EncodeColor(colorBrightGreen) + "GREEN = BUY   " + EncodeColor(colorYellow) + "YELLOW = CASH";
  147. Plot(0,"",colorLightGrey,styleNoLine+styleNoLabel);
  148. PlotShapes(IIf(Buy ==1, shapeUpArrow,shapeNone),colorBrightGreen,0,0,10);
  149. PlotShapes(IIf(Sell ==1, shapeHollowUpArrow,shapeNone),colorYellow,0,0,10);