FirstBarIndex(), LastBarIndex().afl
上传用户:shiqiang
上传日期:2009-06-12
资源大小:1289k
文件大小:3k
源码类别:

金融证券系统

开发平台:

Others

  1. //------------------------------------------------------------------------------
  2. //
  3. //  Formula Name:    FirstBarIndex(), LastBarIndex()
  4. //  Author/Uploader: Antonio Marra 
  5. //  E-mail:          ant.marra@virgilio.it
  6. //  Date/Time Added: 2004-07-20 06:38:08
  7. //  Origin:          
  8. //  Keywords:        BarIndex
  9. //  Level:           basic
  10. //  Flags:           showemail,function
  11. //  Formula URL:     http://www.amibroker.com/library/formula.php?id=363
  12. //  Details URL:     http://www.amibroker.com/library/detail.php?id=363
  13. //
  14. //------------------------------------------------------------------------------
  15. //
  16. //  It retrieve the first or the last BarIndex() Value when a given condition
  17. //  is true.
  18. //
  19. //------------------------------------------------------------------------------
  20. /*_________________________________________________________________________________________ 
  21.   SYNTAX:
  22.          FirstBarIndex(Condition)
  23.          LastBarIndex(Condition)
  24.   
  25.   PURPOSE:
  26.           It gets the first BarIndex() Value when a given condition is true.
  27.           It gets the last BarIndex() Value when a given condition is true.
  28.   
  29.   HOW TO USE:
  30.           Copy this file in your include directory or append it to another file that You 
  31.           use as "functions database".
  32.   EXAMPLE:
  33.           1. Let's suppose you want to know when a given condition comes true for the
  34.              first time and for the last time.
  35.              
  36.              //We will serch for the first and last DOJI candle
  37.              // Condition is:
  38.              Doji = (O == C and O < H and O > L);
  39.              
  40.              First_Bar = FirstBarIndex(Doji);
  41.              First_Doji_Close = ValueWhen( BarIndex()== First_Bar,C,1);
  42.              
  43.              Last_Bar = LastBarIndex(Doji);
  44.              Last_Doji_Close = ValueWhen( BarIndex()== Last_Bar,C,1);
  45.           
  46.   ________________________________________________________________________________________  
  47.                                                                                           */
  48.                                                                                           
  49.                                                                                           
  50. function FirstBarIndex(Condition)
  51. {
  52.      TotalBarsIndex = LastValue(BarIndex());
  53.      a = 0;
  54.      Counter = 0;
  55.      for (a = 0 ;a < TotalBarsIndex; a++)
  56.      {
  57.        Counter = Counter+1;
  58.        if (
  59.            IsTrue(Condition[a])
  60.           )
  61.        a = TotalBarsIndex;
  62.        }
  63.      result = Counter-1;
  64.      return result;
  65. }
  66. function LastBarIndex(Condition)
  67. {
  68.      TotalBarsIndex = LastValue(BarIndex());
  69.      a = TotalBarsIndex;
  70.      Counter = TotalBarsIndex+1;
  71.      for (a = TotalBarsIndex ;a > 0; a--)
  72.      {
  73.        Counter = Counter-1;
  74.        if (
  75.            IsTrue(Condition[a])
  76.           )
  77.        a = 0;
  78.      }
  79.      result = Counter;
  80.      return result;
  81. }