均线与布林带指标 ParMA_BB.mq4-MT4指标-峰汇在线
诚信为本
量力而为
当前位置:峰汇在线 > MetaTrader 4 > MT4指标 > 正文

均线与布林带指标 ParMA_BB.mq4

ParMA_BB 指标在创建均线与布林带。

图表:ParMA_BB 指标

源码:

//+------------------------------------------------------------------+
//|                                                     ParMA_BB.mq4 |
//|                        Copyright 2006, MetaQuotes Software Corp. |
//+------------------------------------------------------------------+
#property copyright "Copyright 2006, MetaQuotes Software Corp."
#property link      "http://www.125808047.com"
 
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 BlueViolet
#property indicator_color2 DodgerBlue
#property indicator_color3 DodgerBlue

extern int ParMA_Period  = 30;
extern bool BandsEnabled = true;
extern double BandsDev   = 1.0;

double ParMABuffer[];
double UpperBandBuffer[];
double LowerBandBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{ 
  if (ParMA_Period <= 1)
   { ParMA_Period = 3; } 
  IndicatorDigits(Digits);
  SetIndexStyle(0, DRAW_LINE);
  SetIndexBuffer(0, ParMABuffer);
  SetIndexLabel(0, "ParMA Line");
  SetIndexDrawBegin(0, ParMA_Period);
  if (BandsEnabled == true)
  {
   SetIndexStyle(1, DRAW_LINE);
   SetIndexBuffer(1, UpperBandBuffer);
   SetIndexLabel(1, "ParMA Upper Band");
   SetIndexDrawBegin(1, ParMA_Period);
   SetIndexStyle(2, DRAW_LINE);
   SetIndexBuffer(2, LowerBandBuffer);
   SetIndexLabel(2, "ParMA Lower Band");
   SetIndexDrawBegin(2, ParMA_Period);
  }
   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 counted_bars = IndicatorCounted();
  int Limit, cnt, i, k;
  double old_val, new_val, sum, std_dev;
  if (counted_bars < 0) { return(-1); }
  Limit = Bars - counted_bars;
  if (Limit > ParMA_Period) 
   { cnt = Limit - ParMA_Period; }
  else 
   { cnt = Limit; }
  for (i=cnt; i>=0; i--)
   { ParMABuffer[i] = GetParMA(i, ParMA_Period); }
  if (BandsEnabled == true)
   {
    i = Bars - ParMA_Period;
    if (counted_bars > (ParMA_Period - 1)) 
     { i = Bars - counted_bars - 1; }
    while (i >= 0)
     {
      sum = 0.0;
      k = i + ParMA_Period - 1;
      old_val = ParMABuffer[i];
      while (k >= i)
       {
        new_val = Close[k] - old_val;
        sum += new_val * new_val;
        k--;
       }
      std_dev = MathSqrt(sum / ParMA_Period);
      UpperBandBuffer[i] = old_val + BandsDev * std_dev;
      LowerBandBuffer[i] = old_val - BandsDev * std_dev;
      i--;
     }
    Comment("ParMA = ", NormalizeDouble(ParMABuffer[0], Digits), "\n",
            "UpperBand = ", NormalizeDouble(UpperBandBuffer[0], Digits), "\n",
            "LowerBand = ", NormalizeDouble(LowerBandBuffer[0], Digits));
   }
  if (BandsEnabled != true)
   { Comment("ParMA = ", NormalizeDouble(ParMABuffer[0], Digits)); }
  return(rates_total);
 }
//+------------------------------------------------------------------+
double GetParMA(int shift, int ma_period)
 {
  static int ro_cnt = 1;
  static double sum_x = 0.0, sum_x2 = 0.0, sum_x3 = 0.0, sum_x4 = 0.0;
  int i, loop_begin;
  double sum_y, sum_xy, sum_x2y, var_tmp;
  double A, B, C, D, E, F, K, L, M, P, Q, R, S; 
  double B0, B1, B2; 
  double ret_val; 
  while (ro_cnt <= ma_period)
   {
    var_tmp  = ro_cnt;
    sum_x   += var_tmp;
    var_tmp *= ro_cnt;
    sum_x2  += var_tmp;
    var_tmp *= ro_cnt;
    sum_x3  += var_tmp;
    var_tmp *= ro_cnt;
    sum_x4  += var_tmp;
    ro_cnt++;
   }
  loop_begin = shift + ma_period - 1;
  sum_y   = 0.0;
  sum_xy  = 0.0;
  sum_x2y = 0.0;
  for (i = 1; i<=ma_period; i++)
   {
    var_tmp  = Close[loop_begin-i+1];
    sum_y   += var_tmp;
    sum_xy  += i * var_tmp;     
    sum_x2y += i * i * var_tmp; 
   }
  A = ma_period;
  B = sum_x; C = sum_x2; F = sum_x3;
  M = sum_x4; P = sum_y; R = sum_xy; S = sum_x2y;
  D = B; E = C; K = C; L = F;
  Q = D / A; E = E - Q * B; F = F - Q * C;
  R = R - Q * P; Q = K / A; L = L - Q * B;
  M = M - Q * C; S = S - Q * P; Q = L / E;
  B2 = (S - R * Q) / (M - F * Q);
  B1 = (R - F * B2) / E;
  B0 = (P - B * B1 - C * B2) / A;
  ret_val = B0 + (B1 + B2 * A) * A;
  return(ret_val);
 }
//+------------------------------------------------------------------+
打赏
版权所有转载请注明标题及链接:峰汇在线 » 均线与布林带指标 ParMA_BB.mq4
上一篇:
下一篇:
分享到: 更多 (0)

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

支付宝扫一扫打赏

微信扫一扫打赏