下载MetaTrader 5

請幫忙檢查哪裡寫錯,謝謝!!

要添加评论,请登录注册
skyelephant
8
skyelephant 2014.12.23 15:47 

//+------------------------------------------------------------------+
//|                                                _MySmoothBand.mq5 |
//|                                                      SkyElephant |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "SkyElephant"
#property link      "http://www.mql5.com"
#property version   "1.00"
#property strict

#property indicator_separate_window
#include <MovingAverages.mqh>
#property indicator_buffers 1
#property indicator_minimum 0
#property indicator_maximum 25
#property indicator_color1 Blue

extern int Length=20;   
input int BShift=0;               
double LnBuf[];

int OnInit(void)
  {
   IndicatorBuffers(1);
   IndicatorDigits(Digits);
   
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,LnBuf);
   SetIndexShift(0,BShift);
   SetIndexLabel(0,"Length");
      
   if(Length<=0)
     {
      Print("Err of Length",Length);
      return(INIT_FAILED);
     }
   SetIndexDrawBegin(0,Length+BShift);
   return(INIT_SUCCEEDED);
  }

double Highest(const double &array[],int range,int fromIndex)
  {
   double res;
   int i;
   res=array[fromIndex];
   for(i=fromIndex;i>fromIndex-range && i>=0;i--)
     {
      if(res<array[i]) res=array[i];
     }
   return(res);
  }
  
double Lowest(const double &array[],int range,int fromIndex)
  {
   double res;
   int i;
   res=array[fromIndex];
   for(i=fromIndex;i>fromIndex-range && i>=0;i--)
     {
      if(res>array[i]) res=array[i];
     }
   return(res);
  }

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
   int i,pos;
   if(rates_total<=Length || Length<=0) return(0);
   
   ArraySetAsSeries(LnBuf,false);
   ArraySetAsSeries(close,false);

   if(prev_calculated<1)
     {
      for(i=0;i<Length;i++)
        {
         LnBuf[i]=EMPTY_VALUE;
        }
     }

   if(prev_calculated>1)
      pos=prev_calculated-1;
   else
      pos=0;

   for(i=pos;i<rates_total;i++)
     {  
      if(High[i]==Highest(high,Length,i) || Low[i]==Lowest(low,Length,i)) 
         Length=MathMax(Length-1,5);
      else 
         Length=MathMin(Length+1,20);    
      LnBuf[i]=Length;   
     }
          
   return(rates_total);
  }

-------------------以上為自己寫的指標--------

 

參考論壇中已些分享出來的指標剪剪貼貼的....

 主要想法是

當K線中的High為近期最高時,Length減去1

同樣Low為近期最低時,Length減去1

若上述兩項都不符合,則Length加1

而Length範圍鎖定在5~20

 不過執行結果很奇怪,創新高或新低時,Length都沒有變化。

 請各位幫忙檢查,謝謝

 

 執行結果

 

 


自动交易和策略测试
自动交易和策略测试
  • www.mql5.com
MQL5:MetaTrader 5客户端内置的交易策略语言。语言允许编写您自己的自动交易系统,技术指标,脚本和函数程序库
enbo lu
版主
1950
enbo lu 2014.12.24 02:30  

贴代码请用 SRC。

 

已修正 

Gang Wu
179
Gang Wu 2014.12.25 10:23  
mark下, 有空慢慢看
要添加评论,请登录注册