mt4 部分内置函数


一。
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[])
  {

 Print("time[0]=",time[0]," rates_total=",rates_total," prev_calculated=",prev_calculated," open=",open[0]," high=",high[0]," close=",close[0]," tick_volume=",tick_volume[0]);


return (rates_total);

}


在5分钟欧美图上测试:


可以看出,
此函数基本是每个tick被调用一次,1秒或2秒。

time[0],open[0],high[0],close[0],low[0] 分别是最后(右)一根K线的开盘时间,开盘价,最高价,收盘价,最低价

rates_total:当前图形上K线的总个数。
prev_calculated:上次计算到第几根K线了。

一般是这个函数需要返回rates_total,到下次再调用这个函数的时候传进来的参数prev_calculated的值就是上次返回的rates_total。以避免重复计算。
tick_volume[0]代表在当前持续的时间周期内,当前是第几个tick


二。

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 sizetime = ArraySize(time);
int sizeopen = ArraySize(open);
int sizehigh = ArraySize(high);
int sizelow = ArraySize(low);
int sizeclose = ArraySize(close);
int sizetick_volume = ArraySize(tick_volume);
int sizevolume = ArraySize(volume);
int sizespread = ArraySize(spread);


Print("sizetime=",sizetime," sizeopen=",sizeopen," sizeopen=",sizeopen," sizehigh=",sizehigh," sizelow=",sizelow," sizeclose=",sizeclose," sizetick_volume=",sizetick_volume," sizevolume=",sizevolume," sizespread=",sizespread);
Print(" rates_total=",rates_total," prev_calculated=",prev_calculated," time[0]=",time[0]," open=",open[0]," high=",high[0]," close=",close[0]," tick_volume=",tick_volume[0]);

   ArraySetAsSeries(time,true);
for( int k = 0 ;k < 5 ; k++){
   Print("true time[",k,"]",time[k]);
}
for( int k = rates_total-5 ;k < rates_total ; k++){
   Print("true time[",k,"]",time[k]);
}


   ArraySetAsSeries(time,false);
   
for( int k = 0 ;k < 5 ; k++){
   Print("false time[",k,"]",time[k]);
}


for( int k = rates_total-5 ;k < rates_total ; k++){
   Print("false time[",k,"]",time[k]);
}
   return(rates_total);
   }

log如下:
可看出所有数组的大小都等于图形上的k线的根数(在5分钟图上有100根K线,则数组大小是100;在1小时图上有50根,则数组大小是50)
可以看出数组的第0个元素默认代表最右一根K线。但是被ArraySetAsSeries false之后,数组的第0个元素就代表了最左一根K线,数组最后一个元素就代表了最右一根K线。






一般如下调用:先用 ArraySetAsSeries把某个数组反转(使最左边的蜡烛柱子序号是0),然后从 prev_calculated 遍历到rates_total,由于每次进来的时候prev_calculated 都是变化的都是等于上一次返回的rates_total,所以这样遍历不会重复计算
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[])
  {


   ArraySetAsSeries(time,false);  
   int i=0;
   static int day =0;
   for(i = prev_calculated ;i< rates_total;i++){
   
       int nowday = TimeDay(time[i]);
      //Print("nowday = ",nowday ,",time =",time[i] );
      if(nowday == day){
         continue;
      }
      
     // Print("new day,name= ",time[i]);
      day = nowday;
      VLineCreate(0,time[i],0,time[i],InpColor,InpStyle,InpWidth,InpBack, InpSelection,InpHidden,InpZOrder);
      
   }
//--- return value of prev_calculated for next call
   return(rates_total);
  }






四。
注释RSI指标
环境:用M5图测试3月16-3月17之间的欧美K线的RSI,同时初始K线有1000根(即还有3月16号之前的1000根K线)

//+------------------------------------------------------------------+
//|                                                          RSI.mq4 |
//|                   Copyright 2005-2014, MetaQuotes Software Corp. |
//|                                              http://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright   "2005-2017, MetaQuotes Software Corp."
#property link        "http://www.song.com"
#property description "Relative Strength Index"
#property strict

#property indicator_separate_window
#property indicator_minimum    0
#property indicator_maximum    100
#property indicator_buffers    1
#property indicator_color1     DodgerBlue
#property indicator_level1     30.0
#property indicator_level2     70.0
#property indicator_levelcolor clrSilver
#property indicator_levelstyle STYLE_DOT
//--- input parameters
input int InpRSIPeriod=14; // RSI Period
//--- buffers
double ExtRSIBuffer[];
double ExtPosBuffer[];
double ExtNegBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit(void)
  {
   string short_name;
//--- 2 additional buffers are used for counting.
   IndicatorBuffers(3);
   SetIndexBuffer(1,ExtPosBuffer);
   SetIndexBuffer(2,ExtNegBuffer);
//--- indicator line
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ExtRSIBuffer);
//--- name for DataWindow and indicator subwindow label
   short_name="RSI("+string(InpRSIPeriod)+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);
//--- check for input
   if(InpRSIPeriod<2)
     {
      Print("Incorrect value for input variable InpRSIPeriod = ",InpRSIPeriod);
      return(INIT_FAILED);
     }
//---
   SetIndexDrawBegin(0,InpRSIPeriod);
//--- initialization done
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Relative Strength Index                                          |
//+------------------------------------------------------------------+
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;
	double diff;

	if(Bars<=InpRSIPeriod || InpRSIPeriod<2)
		return(0);

	int sizetime = ArraySize(time);
	int sizeopen = ArraySize(open);
	int sizehigh = ArraySize(high);
	int sizelow = ArraySize(low);
	int sizeclose = ArraySize(close);
	int sizetick_volume = ArraySize(tick_volume);
	int sizevolume = ArraySize(volume);
	int sizespread = ArraySize(spread);


	Print("sizetime=",sizetime," sizeopen=",sizeopen," sizeopen=",sizeopen," sizehigh=",sizehigh," sizelow=",sizelow," sizeclose=",sizeclose," sizetick_volume=",sizetick_volume," sizevolume=",sizevolume," sizespread=",sizespread);
	Print(" rates_total=",rates_total," prev_calculated=",prev_calculated," time[0]=",time[0]," open=",open[0]," high=",high[0]," close=",close[0]," tick_volume=",tick_volume[0]);




	//--- counting from 0 to rates_total
	ArraySetAsSeries(ExtRSIBuffer,false);
	ArraySetAsSeries(ExtPosBuffer,false);
	ArraySetAsSeries(ExtNegBuffer,false);
	ArraySetAsSeries(close,false);
	//--- preliminary calculations
	pos=prev_calculated-1;
	Print("1 pos=",pos," InpRSIPeriod=",InpRSIPeriod);
	
	/*RSI EURUSD,M5: 1 pos=-1 InpRSIPeriod=14
	第一次进来,pos=-1,所以走 if(pos<=InpRSIPeriod)。也就是 if(pos<=InpRSIPeriod)这个条件基本是给初次调用到这个函数时计算初始的14根K线的RSI值的。
	之后,就不进到这个 if(pos<=InpRSIPeriod)了
	*/
	
   if(pos<=InpRSIPeriod)
     {
      //--- first RSIPeriod values of the indicator are not calculated
      ExtRSIBuffer[0]=0.0;
      ExtPosBuffer[0]=0.0;
      ExtNegBuffer[0]=0.0;
      double sump=0.0;
      double sumn=0.0;
      for(i=1; i<=InpRSIPeriod; i++)
        {
         ExtRSIBuffer[i]=0.0;
         ExtPosBuffer[i]=0.0;
         ExtNegBuffer[i]=0.0;
         diff=close[i]-close[i-1];
         if(diff>0)
            sump+=diff;
         else
            sumn-=diff;
        }
		
      //--- calculate first visible value
      ExtPosBuffer[InpRSIPeriod]=sump/InpRSIPeriod;
      ExtNegBuffer[InpRSIPeriod]=sumn/InpRSIPeriod;
      if(ExtNegBuffer[InpRSIPeriod]!=0.0)
         ExtRSIBuffer[InpRSIPeriod]=100.0-(100.0/(1.0+ExtPosBuffer[InpRSIPeriod]/ExtNegBuffer[InpRSIPeriod]));
      else
        {
         if(ExtPosBuffer[InpRSIPeriod]!=0.0)
            ExtRSIBuffer[InpRSIPeriod]=100.0;
         else
            ExtRSIBuffer[InpRSIPeriod]=50.0;
        }
      //--- prepare the position value for main calculation
	  
	  
	  //直接让pos=15,出来if之后进入下面的986次循环
      pos=InpRSIPeriod+1;
     }
     

   
   
//--- the main loop of calculations

/*RSI EURUSD,M5: i=15 pos=15 rates_total=1001
第一次进入这个函数时,就会在这里循环986次把已经存在的第15到1001根K线的RSI值计算出来
之后每次进入这个函数,由于pos等于上次计算过的那个K线,所以基本上就循环一次(在rates_total不变时,但是每次tick时最新K线的close价格一直在变),或循环两次(在rates_total加1时,即新的一条K线刚出来时)
*/
   for(i=pos; i<rates_total && !IsStopped(); i++)
    {
     
      Print("i=",i," pos=",pos," rates_total=",rates_total);
      
      diff=close[i]-close[i-1];
      ExtPosBuffer[i]=(ExtPosBuffer[i-1]*(InpRSIPeriod-1)+(diff>0.0?diff:0.0))/InpRSIPeriod;
      ExtNegBuffer[i]=(ExtNegBuffer[i-1]*(InpRSIPeriod-1)+(diff<0.0?-diff:0.0))/InpRSIPeriod;
      if(ExtNegBuffer[i]!=0.0){
         ExtRSIBuffer[i]=100.0-100.0/(1+ExtPosBuffer[i]/ExtNegBuffer[i]);
		 
		 Print("ExtRSIBuffer[",i,"]=",ExtRSIBuffer[i]);
		 }
      else
        {
         if(ExtPosBuffer[i]!=0.0)
            ExtRSIBuffer[i]=100.0;
         else
            ExtRSIBuffer[i]=50.0;
        }
     }

  //需要返回当前的k线总个数
   return(rates_total);
  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值