请教如何在EA里调用自己写的指标?

 

在自己的指标里已经有了比如KD交叉的判断了,那在EA里用的时候怎么去用呢?

 

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[])
  {
//---- checking the number of bars to be enough for the calculation
   if(BarsCalculated(STO_Handle)<rates_total
      || rates_total<StartBars)
      return(0);

//---- declarations of local variables 
   int to_copy,limit,bar;
   double fastMAnow,slowMAnow,fastMAprevious,slowMAprevious;
   double Range,AvgRange,STOM[],STOS[],Bid,Ask;
   string sTimeCur,sAsk,sBid,sTimeHour,sTimeMinute,sPeriod;
   bool flagval1,flagval2;

//---- calculations of the necessary amount of data to be copied
//---- and the 'limit' starting index for the bars recalculation loop
   if(prev_calculated>rates_total || prev_calculated<=0)// checking for the first start of calculation of an indicator
     {
      to_copy=rates_total;           // calculated number of all bars
      limit=rates_total-StartBars-1; // starting index for calculation of all bars
      flagval1_=false;
      flagval2_=false;
     }
   else
     {
      to_copy=rates_total-prev_calculated+2; // calculated number of new bars only
      limit=rates_total-prev_calculated;     // starting index for calculation of new bars
     }

//---- copy newly appeared data in the arrays
   if(CopyBuffer(STO_Handle,0,0,to_copy,STOM)<=0) return(0);
   if(CopyBuffer(STO_Handle,1,0,to_copy,STOS)<=0) return(0);

//---- indexing elements in arrays as timeseries  
   ArraySetAsSeries(STOM,true);
   ArraySetAsSeries(STOS,true);
   ArraySetAsSeries(high,true);
   ArraySetAsSeries(low,true);
   ArraySetAsSeries(close,true);

//---- restore values of the variables
   flagval1=flagval1_;
   flagval2=flagval2_;

//---- main indicator calculation loop
   for(bar=limit; bar>0; bar--)
     {
      //---- store values of the variables before running at the current bar
      if(rates_total!=prev_calculated && bar==rates_total-1)
        {
         flagval1_ = flagval1;
         flagval2_ = flagval2;
        }

      AvgRange=0;

      for(int iii=bar; iii<bar+10; iii++) AvgRange+=MathAbs(high[iii]-low[iii]);

      Range=AvgRange/10;

      SellBuffer[bar]=0.0;
      BuyBuffer[bar]=0.0;

      fastMAnow=STOM[bar];
      fastMAprevious=STOM[bar+1];
      slowMAnow=STOS[bar];
      slowMAprevious=STOS[bar+1];

      if((show_KD_cross && fastMAnow>=slowMAnow && fastMAprevious<=slowMAprevious)
         && (slowMAnow<=OverSoldLevel && fastMAprevious<=OverSoldLevel))
        {
         if(bar==1 && !flagval1)
           {
            flagval1=true;
            flagval2=false;

            if(SoundON || EmailON)
              {
               Bid=SymbolInfoDouble(Symbol(),SYMBOL_BID);
               Ask=SymbolInfoDouble(Symbol(),SYMBOL_ASK);
               sTimeCur=TimeToString(TimeCurrent(),TIME_DATE);
               sAsk=DoubleToString(Ask,_Digits);
               sBid=DoubleToString(Bid,_Digits);

               MqlDateTime tm;
               TimeToStruct(TimeCurrent(),tm);
               sTimeHour=IntegerToString(tm.hour);
               sTimeMinute=IntegerToString(tm.min);
               sPeriod=EnumToString(Period());
              }

            if(SoundON) Alert("BUY signal at Ask=",sAsk,"\n Bid=",sBid,"\n Time=",sTimeCur," ",sTimeHour,":",sTimeMinute,"\n Symbol=",Symbol()," Period=",sPeriod);
            if(EmailON) SendMail("BUY signal alert","BUY signal at Ask="+sAsk+", Bid="+sBid+", Date="+sTimeCur+" "+sTimeHour+":"+sTimeMinute+" Symbol="+Symbol()+" Period="+sPeriod);
           }

         BuyBuffer[bar]=low[bar]-Range*0.5;
         SellBuffer[bar]=0.0;
        }
      else
         if((show_KD_cross && fastMAnow<=slowMAnow && fastMAprevious>=slowMAprevious)
            && (slowMAprevious>=OverBoughtLevel && fastMAnow>=OverBoughtLevel))
           {
            if(bar==1 && !flagval2)
              {
               flagval2=true;
               flagval1=false;

               if(SoundON || EmailON)
                 {
                  Bid=SymbolInfoDouble(Symbol(),SYMBOL_BID);
                  Ask=SymbolInfoDouble(Symbol(),SYMBOL_ASK);
                  sTimeCur=TimeToString(TimeCurrent(),TIME_DATE);
                  sAsk=DoubleToString(Ask,_Digits);
                  sBid=DoubleToString(Bid,_Digits);

                  MqlDateTime tm;
                  TimeToStruct(TimeCurrent(),tm);
                  sTimeHour=IntegerToString(tm.hour);
                  sTimeMinute=IntegerToString(tm.min);
                  sPeriod=EnumToString(Period());
                 }

               if(SoundON) Alert("SELL signal at Ask=",sAsk,"\n Bid=",sBid,"\n Date=",sTimeCur," ",sTimeHour,":",sTimeMinute,"\n Symbol=",Symbol()," Period=",sPeriod);
               if(EmailON) SendMail("SELL signal alert","SELL signal at Ask="+sAsk+", Bid="+sBid+", Date="+sTimeCur+" "+sTimeHour+":"+sTimeMinute+" Symbol="+Symbol()+" Period="+sPeriod);
              }
            SellBuffer[bar]=high[bar]+Range*0.5;
            BuyBuffer[bar]=0.0;
           }
     }

   SellBuffer[0]=0.0;
   BuyBuffer[0]=0.0;
//----     
   return(rates_total);
  }

 上面这个是指标 的,那EA里要调用KD交叉时应该如何调用呢?谢谢

 

需要用handle调用指标,那后面的买入条件还是要写 类似fastMAnow>=slowMAnow && fastMAprevious<=slowMAprevious这样的吗?如何直接调用呢,请不吝赐教,谢谢!

 
不是啊,用icustom获取指标句柄后,用copybuffer将指标缓存中的值复制出来使用。
 

版主意思调用系统指标和自己的指标方式是不同的,自己写的需要用icustom获取指标句柄后,用copybuffer将指标缓存中的值复制出来使用,就算复制出来了, 那kd交叉的条件是否还有重新写过呢,类似fastMAnow>=slowMAnow && fastMAprevious<=slowMAprevious是否要重新在EA里写过,还是说可以直接调用指标的?能举个例说下嘛,非常感谢

 

你的自定义指标我没细读,我想你的意思是你自定义指标中已经判断了KD交叉,希望直接调用判断结果?

那么你要使你的自定义指标输出的缓存值(之一)就是标识KD交叉的,比方说交叉时值置为1,无交叉时值置为0。

 

谢谢版主的回复!

有个问题,指标里是基于OnCalculate函数的,但EA里大多是基于Ontick的,这样的话是否两者计算的时间上是否一样,我发现EA里的出现交叉的时候比指标里的要晚,这是为什么呢? 

 
king1898:

谢谢版主的回复!

有个问题,指标里是基于OnCalculate函数的,但EA里大多是基于Ontick的,这样的话是否两者计算的时间上是否一样,我发现EA里的出现交叉的时候比指标里的要晚,这是为什么呢? 

你提到的EA中出现交叉比指标晚 ,不是很明白你的意思。不过有一点你可以检查下,看看EA中调用指标是按照什么价格判断交叉的。

OnCalculate 和 Ontick 应该是一样的机制,都是靠ticks触发的。