诚信为本
量力而为
当前位置:峰汇在线 > MetaTrader 4 > MT4指标 > 正文

Correlation.mq4

Correlation指标计算指定两种货币对的收盘价和均线差。

图表:Correlation指标

源码:

//+------------------------------------------------------------------+
//|                                                   Correlation.mq4|
//|                            Copyright @2006, TrendLaboratory Ltd. |
//+------------------------------------------------------------------+
#property copyright "Copyright @2006, TrendLaboratory Ltd."
#property link      "http://www.125808047.com/"

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 DeepSkyBlue
#property indicator_maximum 1.05
#property indicator_minimum -1.05

extern string    Symbol1="GBPJPY";//"GBPUSD";
extern string    Symbol2="CHFJPY";//"EURUSD";
extern int       cPeriod=20;

double Correlation[];
double DiffBuffer1[];
double DiffBuffer2[];
double PowDiff1[];
double PowDiff2[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit(void)
{
   IndicatorBuffers(5);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,Correlation);
   SetIndexBuffer(1,DiffBuffer1);
   SetIndexBuffer(2,DiffBuffer2);
   SetIndexBuffer(3,PowDiff1);
   SetIndexBuffer(4,PowDiff2);

   string short_name="Correlation("+Symbol1+","+Symbol2+","+cPeriod+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);
   
   SetIndexDrawBegin(0,2*cPeriod);
   Comment("www.125808047.com");
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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 shift,limit,counted_bars=IndicatorCounted();
   
   if ( counted_bars < 0 ) return(-1);
   if ( counted_bars ==0 ) limit=Bars-cPeriod-1;
   if ( counted_bars < 1 ) 
   for(int i=1;i<2*cPeriod;i++) Correlation[Bars-i]=0;    
      
   if(counted_bars>0) limit=Bars-counted_bars;
   limit--;
   
   for( shift=limit; shift>=0; shift--)
   {
   DiffBuffer1[shift]=iClose(Symbol1,0,shift)-iMA(Symbol1,0,cPeriod,0,MODE_SMA,PRICE_CLOSE,shift);
   DiffBuffer2[shift]=iClose(Symbol2,0,shift)-iMA(Symbol2,0,cPeriod,0,MODE_SMA,PRICE_CLOSE,shift);
   PowDiff1[shift]=MathPow(DiffBuffer1[shift],2);
   PowDiff2[shift]=MathPow(DiffBuffer2[shift],2);
   
      double u=0,l=0,s=0;
      
      for( i = cPeriod-1 ;i >= 0 ;i--)
      {
      u += DiffBuffer1[shift+i]*DiffBuffer2[shift+i];
      l += PowDiff1[shift+i];
      s += PowDiff2[shift+i];
      }
   if(l*s >0) Correlation[shift]=u/MathSqrt(l*s);
  }
   return(rates_total);
  }
打赏
版权所有转载请注明标题及链接:峰汇在线 » Correlation.mq4
上一篇:
下一篇:
分享到: 更多 (0)

如您觉得此文不错请扫码打赏!

支付宝扫一扫打赏

微信扫一扫打赏