Using From and To dates from Auto Analysis in Code.afl
上传用户:shiqiang
上传日期:2009-06-12
资源大小:1289k
文件大小:2k
源码类别:

金融证券系统

开发平台:

Others

  1. //------------------------------------------------------------------------------
  2. //
  3. //  Formula Name:    Using From and To dates from Auto Analysis in Code
  4. //  Author/Uploader: Bruce M. Moore 
  5. //  E-mail:          bmoore8702@sbcglobal.net
  6. //  Date/Time Added: 2005-06-06 22:40:53
  7. //  Origin:          Author
  8. //  Keywords:        Status, FirstBarInRange, LastBarInRange
  9. //  Level:           semi-advanced
  10. //  Flags:           exploration
  11. //  Formula URL:     http://www.amibroker.com/library/formula.php?id=466
  12. //  Details URL:     http://www.amibroker.com/library/detail.php?id=466
  13. //
  14. //------------------------------------------------------------------------------
  15. //
  16. //  This code illustrates how to use the "From" and "To" date settings from
  17. //  Automatic Analyzer in AFL code. It is far faster to do it this way than to
  18. //  run your code from "0 to Barcount-1".
  19. //
  20. //  This method of extracting the dates and converting them to bar counts is
  21. //  rather convoluted, as you will see. I would like to see new keywords added
  22. //  to the AFL that allow the programmer to access the the "From" and "To" bar
  23. //  counts directly. In the meantime, this is the best work-around I've found.
  24. //
  25. //  The simple example given calculates a moving average, then plots price and
  26. //  the moving average.
  27. //
  28. //  Feedback is always welcome.
  29. //
  30. //------------------------------------------------------------------------------
  31. Periods=40;
  32. x=BarIndex();
  33. xMA=tcnt=tsum=Null;
  34. xfirst = LastValue(ValueWhen(Status("FirstBarInRange"), x, 1));
  35. xlast  = LastValue(ValueWhen(Status("LastBarInRange" ), x, 1));
  36. if(xfirst>Periods){
  37. for (i = xfirst; i < xlast + 1; i++){
  38. tcnt[i]=0;
  39. tsum[i]=0;
  40. for (j=0; j<Periods; j++){ 
  41. tcnt[i]++;
  42. tsum[i]= tsum[i] + Close[i-j];
  43. }                      
  44. xMA[i]=tsum[i]/tcnt[i];
  45. }
  46. Title= "xFirst= " + WriteVal(xFirst,1.0) + ", xLast= " + WriteVal(xLast,1.0);
  47. Plot(Close,"Close",colorBlack,styleLine+styleThick);
  48. Plot(xMA,"xMA",colorRed,styleLine+styleThick);
  49. PlotShapes( IIf( xfirst==x, shapeCircle, shapeNone ), colorIndigo );
  50. PlotShapes( IIf( xlast==x, shapeCircle, shapeNone ), colorIndigo );
  51. Buy=Cross(C,xMA);
  52. Sell=Cross(xMA,C);
  53. Buy   = ExRem(Buy,  Sell);
  54. Sell  = ExRem(Sell, Buy);
  55. PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorGreen );
  56. PlotShapes( IIf( Sell, shapeDownArrow, shapeNone ), colorRed );
  57. Filter=  C>10;
  58. AddColumn(Close,       "Close"            , 1.2);
  59. AddColumn(Volume,      "Volume"           , 1.0);
  60. }