インディケータ: 色入力つきカスタム移動平均

 

色入力つきカスタム移動平均:

「カスタム移動平均」指標を修正したもので、入力パラメータに線の色を渡すことができます。


作者: Vladimir Karputov

 
任意のインジケータの任意のライン/ヒストグラム(その他)を、ビデオに示された原理に従って変更するユーティリティを書くことは可能です。
 
よくやった!ありがとう!
 

行の太さとスタイルの設定を含むようにコードを修正した:


//+------------------------------------------------------------------+
//| カスタム移動平均入力 Colour.mq5 |.
//| Copyright 2009-2017, MetaQuotes Software Corp.
//|http://mql5.commql5.com
//+------------------------------------------------------------------+
#property copyright "2009-2017, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.001"
//--- インジケーターの設定
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
//--- 入力パラメータ
input int               InpMAPeriod=13;            // 期間
input int               InpMAShift=0;              // シフト
input ENUM_MA_METHOD    InpMAMethod=MODE_SMMA;     // メソッド
input color             InpColor=clrYellow;        // カラー
input ENUM_LINE_STYLE   InpMAStyle=STYLE_SOLID;      // スタイル
input int               InpMAWidth=1;              // 幅


//--- インジケータ・バッファ
double               ExtLineBuffer[];
//+------------------------------------------------------------------+
//| 単純移動平均|
//+------------------------------------------------------------------+
void CalculateSimpleMA(int rates_total,int prev_calculated,int begin,const double &price[])
  {
   int i,limit;
//--- 最初の計算またはバーの数が変更された
   if(prev_calculated==0)// 最初の計算
     {
      limit=InpMAPeriod+begin;
      //--- 最初のリミット・バーには空の値を設定する
      for(i=0;i<limit-1;i++) ExtLineBuffer[i]=0.0;
      //--- 最初に見える値を計算する
      double firstValue=0;
      for(i=begin;i<limit;i++)
         firstValue+=price[i];
      firstValue/=InpMAPeriod;
      ExtLineBuffer[limit-1]=firstValue;
     }
   else limit=prev_calculated-1;
//--- メインループ
   for(i=limit;i<rates_total && !IsStopped();i++)
      ExtLineBuffer[i]=ExtLineBuffer[i-1]+(price[i]-price[i-InpMAPeriod])/InpMAPeriod;
//---
  }
//+------------------------------------------------------------------+
//| 指数移動平均|
//+------------------------------------------------------------------+
void CalculateEMA(int rates_total,int prev_calculated,int begin,const double &price[])
  {
   int    i,limit;
   double SmoothFactor=2.0/(1.0+InpMAPeriod);
//--- 最初の計算またはバーの数が変更された
   if(prev_calculated==0)
     {
      limit=InpMAPeriod+begin;
      ExtLineBuffer[begin]=price[begin];
      for(i=begin+1;i<limit;i++)
         ExtLineBuffer[i]=price[i]*SmoothFactor+ExtLineBuffer[i-1]*(1.0-SmoothFactor);
     }
   else limit=prev_calculated-1;
//--- メインループ
   for(i=limit;i<rates_total && !IsStopped();i++)
      ExtLineBuffer[i]=price[i]*SmoothFactor+ExtLineBuffer[i-1]*(1.0-SmoothFactor);
//---
  }
//+------------------------------------------------------------------+
//| 線形加重移動平均|
//+------------------------------------------------------------------+
void CalculateLWMA(int rates_total,int prev_calculated,int begin,const double &price[])
  {
   int        i,limit;
   static int weightsum;
   double     sum;
//--- 最初の計算またはバーの数が変更された
   if(prev_calculated==0)
     {
      weightsum=0;
      limit=InpMAPeriod+begin;
      //--- 最初のリミット・バーには空の値を設定する
      for(i=0;i<limit;i++) ExtLineBuffer[i]=0.0;
      //--- 最初に見える値を計算する
      double firstValue=0;
      for(i=begin;i<limit;i++)
        {
         int k=i-begin+1;
         weightsum+=k;
         firstValue+=k*price[i];
        }
      firstValue/=(double)weightsum;
      ExtLineBuffer[limit-1]=firstValue;
     }
   else limit=prev_calculated-1;
//--- メインループ
   for(i=limit;i<rates_total && !IsStopped();i++)
     {
      sum=0;
      for(int j=0;j<InpMAPeriod;j++) sum+=(InpMAPeriod-j)*price[i-j];
      ExtLineBuffer[i]=sum/weightsum;
     }
//---
  }
//+------------------------------------------------------------------+
//| 平滑化移動平均|
//+------------------------------------------------------------------+
void CalculateSmoothedMA(int rates_total,int prev_calculated,int begin,const double &price[])
  {
   int i,limit;
//--- 最初の計算またはバーの数が変更された
   if(prev_calculated==0)
     {
      limit=InpMAPeriod+begin;
      //--- 最初のリミット・バーには空の値を設定する
      for(i=0;i<limit-1;i++) ExtLineBuffer[i]=0.0;
      //--- 最初に見える値を計算する
      double firstValue=0;
      for(i=begin;i<limit;i++)
         firstValue+=price[i];
      firstValue/=InpMAPeriod;
      ExtLineBuffer[limit-1]=firstValue;
     }
   else limit=prev_calculated-1;
//--- メインループ
   for(i=limit;i<rates_total && !IsStopped();i++)
      ExtLineBuffer[i]=(ExtLineBuffer[i-1]*(InpMAPeriod-1)+price[i])/InpMAPeriod;
//---
  }
//+------------------------------------------------------------------+
//| カスタムインジケータ初期化関数
//+------------------------------------------------------------------+
void OnInit()
  {
//--- インジケータ・バッファのマッピング
   SetIndexBuffer(0,ExtLineBuffer,INDICATOR_DATA);
//--- 精度の設定
   IndicatorSetInteger(INDICATOR_DIGITS,_Digits+1);
//--- どのインデックスから最初のバーが描画されるかを設定する
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpMAPeriod);
//---- 描画時に線がずれる
   PlotIndexSetInteger(0,PLOT_SHIFT,InpMAShift);
//--- カラーライン
   PlotIndexSetInteger(0,PLOT_LINE_COLOR,InpColor);
//--- スタイルライン
   PlotIndexSetInteger(0,PLOT_LINE_STYLE,InpMAStyle);
//--- 幅線
   PlotIndexSetInteger(0,PLOT_LINE_WIDTH,InpMAWidth);

//--- データウィンドウの名前
   string short_name="unknown ma";
   switch(InpMAMethod)
     {
      case MODE_EMA :  short_name="EMA";  break;
      case MODE_LWMA : short_name="LWMA"; break;
      case MODE_SMA :  short_name="SMA";  break;
      case MODE_SMMA : short_name="SMMA"; break;
     }
   IndicatorSetString(INDICATOR_SHORTNAME,short_name+"("+string(InpMAPeriod)+")");
//---- 空の描画線を設定する--
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);
//---- 初期化完了
  }
//+------------------------------------------------------------------+
//| 移動平均|
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[])
  {
//--- バー数をチェックする
   if(rates_total<InpMAPeriod-1+begin)
      return(0);// 計算にはバーが足りない
//--- 最初の計算またはバーの数が変更された
   if(prev_calculated==0)
      ArrayInitialize(ExtLineBuffer,0);
//--- インデックスが描画される最初のバーを設定する
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpMAPeriod-1+begin);

//--- 計算
   switch(InpMAMethod)
     {
      case MODE_EMA:  CalculateEMA(rates_total,prev_calculated,begin,price);        break;
      case MODE_LWMA: CalculateLWMA(rates_total,prev_calculated,begin,price);       break;
      case MODE_SMMA: CalculateSmoothedMA(rates_total,prev_calculated,begin,price); break;
      case MODE_SMA:  CalculateSimpleMA(rates_total,prev_calculated,begin,price);   break;
     }
//--- 次の呼び出しのためにprev_calculatedの値を返す
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
また、EAから線の太さを変更できるようにできないだろうか?
 
Pirata Independent #:
また、EAから線の太さを変更できるようになりませんか?

はい、インジケーターの最初にあります:


 
また、EAのコードからどのように変更されているのか?