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

MT4副图指标 Center_of_Gravity.mq4

Center_of_Gravity指标在副图创建类似KD的指标线。

图表:Center_of_Gravity指标

源码:

//+------------------------------------------------------------------+
//|                                            Center_of_Gravity.mq4 |
//|                       Copyright @2007, MetaQuotes Software Corp. |
//+------------------------------------------------------------------+
#property copyright "Copyright @2007, MetaQuotes Software Corp."
#property link      "http://www.125808047.com/"
//----
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Blue
#property indicator_color2 OrangeRed
//---- input parameters
extern int       Per = 10;
extern int       PriceType = 0; // Close
// Constant          Value Description 
// PRICE_CLOSE       0     Close price. 
// PRICE_OPEN        1     Open price. 
// PRICE_HIGH        2     High price. 
// PRICE_LOW         3     Low price. 
// PRICE_MEDIAN      4     Median price, (high+low)/2. 
// PRICE_TYPICAL     5     Typical price, (high+low+close)/3. 
// PRICE_WEIGHTED    6     Weighted close price, (high+low+close+close)/4. 
extern int       SmoothPer = 3;
extern int       SmoothType = 0; // MODE_SMA
//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double Price(int shift)
  {
//----
   double res;
//----
   switch (PriceType)
     {
       case PRICE_OPEN:     res = Open[shift]; break;
       case PRICE_HIGH:     res = High[shift]; break;
       case PRICE_LOW:      res = Low[shift]; break;
       case PRICE_MEDIAN:   res = (High[shift] + Low[shift]) / 2.0; break;
       case PRICE_TYPICAL:  res = (High[shift] + Low[shift] + 
                                  Close[shift])/3.0; break;
       case PRICE_WEIGHTED: res = (High[shift] + Low[shift] + 
                                  2*Close[shift]) / 4.0; break;
       default:             res = Close[shift]; break;
     }
   return(res);
  }
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnInit(void)
  {
   SetIndexStyle(0, DRAW_LINE);
   SetIndexBuffer(0, ExtMapBuffer1);
   SetIndexLabel(0, "CG Main");
   SetIndexStyle(1, DRAW_LINE);
   SetIndexBuffer(1, ExtMapBuffer2);
   SetIndexLabel(1, "CG Signal");
   IndicatorShortName("CG(" + Per + ", " + SmoothPer + ")");
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
  Comment("www.125808047.com");
  }
//+------------------------------------------------------------------+
//| 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 counted_bars = IndicatorCounted();
   int i, k, limit, limit1;
//----
   if(counted_bars == 0) 
     {
       limit = Bars - Per - 1;
       limit1 = limit - SmoothPer;
     }
   if(counted_bars>0)  
     {
       limit = Bars - counted_bars;
       limit1 = limit;
     }
   double P;
   for(i = limit; i >= 0; i--)
     {
       P = 0;
       for(k = 0; k < Per; k++)
           P += (k + 1)*Price(i + k);
       ExtMapBuffer1[i] = -P / (iMA(NULL, 0, Per, 0, MODE_SMA, PriceType, i)*Per);   
     }   
   for(i = limit1; i >= 0; i--)
       ExtMapBuffer2[i] = iMAOnArray(ExtMapBuffer1, 0, SmoothPer, 0, SmoothType, i);   
//----
   return(rates_total);
  }
//+------------------------------------------------------------------+
打赏
版权所有转载请注明标题及链接:峰汇在线 » MT4副图指标 Center_of_Gravity.mq4
上一篇:
下一篇:
分享到: 更多 (0)

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

支付宝扫一扫打赏

微信扫一扫打赏