🌈

MQL5でGMMA

2024/04/11に公開

完成イメージ

ポイント

  • MAの値をそっくりそのままこのインジケーター内に移す
  • MAの計算がされていないとエラーになるので、計算完了待機処理を入れる
  • Bufferも配列にしたいが、一次元目を静的にすると二次元目は動的にできないので個別で定義

コード(MQL5)

GMMA.mq5
#property copyright "okap_goldman"
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 12
#property indicator_plots 12

#define GMMA_LENGTH_BY_COUNT (6)

input int gmma_trader1 = 3;
input int gmma_trader2 = 5;
input int gmma_trader3 = 8;
input int gmma_trader4 = 10;
input int gmma_trader5 = 12;
input int gmma_trader6 = 15;
input int gmma_investor1 = 30;
input int gmma_investor2 = 35;
input int gmma_investor3 = 40;
input int gmma_investor4 = 45;
input int gmma_investor5 = 50;
input int gmma_investor6 = 60;

int handle_trader[GMMA_LENGTH_BY_COUNT];
int handle_investor[GMMA_LENGTH_BY_COUNT];

double buffer_trader1[];
double buffer_trader2[];
double buffer_trader3[];
double buffer_trader4[];
double buffer_trader5[];
double buffer_trader6[];
double buffer_investor1[];
double buffer_investor2[];
double buffer_investor3[];
double buffer_investor4[];
double buffer_investor5[];
double buffer_investor6[];

#property indicator_label1  "GMMA Trader1"
#property indicator_type1   DRAW_LINE
#property indicator_color1  Red
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1

#property indicator_label2  "GMMA Trader2"
#property indicator_type2   DRAW_LINE
#property indicator_color2  Red
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1

#property indicator_label3  "GMMA Trader3"
#property indicator_type3   DRAW_LINE
#property indicator_color3  Red
#property indicator_style3  STYLE_SOLID
#property indicator_width3  1

#property indicator_label4  "GMMA Trader4"
#property indicator_type4   DRAW_LINE
#property indicator_color4  Red
#property indicator_style4  STYLE_SOLID
#property indicator_width4  1

#property indicator_label5  "GMMA Trader5"
#property indicator_type5   DRAW_LINE
#property indicator_color5  Red
#property indicator_style5  STYLE_SOLID
#property indicator_width5  1

#property indicator_label6  "GMMA Trader6"
#property indicator_type6   DRAW_LINE
#property indicator_color6  Red
#property indicator_style6  STYLE_SOLID
#property indicator_width6  1

#property indicator_label7  "GMMA Investor1"
#property indicator_type7   DRAW_LINE
#property indicator_color7  Green
#property indicator_style7  STYLE_SOLID
#property indicator_width7  1

#property indicator_label8  "GMMA Investor2"
#property indicator_type8   DRAW_LINE
#property indicator_color8  Green
#property indicator_style8  STYLE_SOLID
#property indicator_width8  1

#property indicator_label9  "GMMA Investor3"
#property indicator_type9   DRAW_LINE
#property indicator_color9  Green
#property indicator_style9  STYLE_SOLID
#property indicator_width9  1

#property indicator_label10  "GMMA Investor4"
#property indicator_type10   DRAW_LINE
#property indicator_color10  Green
#property indicator_style10  STYLE_SOLID
#property indicator_width10  1

#property indicator_label11  "GMMA Investor5"
#property indicator_type11   DRAW_LINE
#property indicator_color11  Green
#property indicator_style11  STYLE_SOLID
#property indicator_width11  1

#property indicator_label12  "GMMA Investor6"
#property indicator_type12   DRAW_LINE
#property indicator_color12  Green
#property indicator_style12  STYLE_SOLID
#property indicator_width12  1

// 全てのMAハンドルを初期化
void InitializeMaHandles(void)
  {
   handle_trader[0] = iMA(_Symbol, _Period, gmma_trader1, 0, MODE_EMA, PRICE_CLOSE);
   handle_trader[1] = iMA(_Symbol, _Period, gmma_trader2, 0, MODE_EMA, PRICE_CLOSE);
   handle_trader[2] = iMA(_Symbol, _Period, gmma_trader3, 0, MODE_EMA, PRICE_CLOSE);
   handle_trader[3] = iMA(_Symbol, _Period, gmma_trader4, 0, MODE_EMA, PRICE_CLOSE);
   handle_trader[4] = iMA(_Symbol, _Period, gmma_trader5, 0, MODE_EMA, PRICE_CLOSE);
   handle_trader[5] = iMA(_Symbol, _Period, gmma_trader6, 0, MODE_EMA, PRICE_CLOSE);
   handle_investor[0] = iMA(_Symbol, _Period, gmma_investor1, 0, MODE_EMA, PRICE_CLOSE);
   handle_investor[1] = iMA(_Symbol, _Period, gmma_investor2, 0, MODE_EMA, PRICE_CLOSE);
   handle_investor[2] = iMA(_Symbol, _Period, gmma_investor3, 0, MODE_EMA, PRICE_CLOSE);
   handle_investor[3] = iMA(_Symbol, _Period, gmma_investor4, 0, MODE_EMA, PRICE_CLOSE);
   handle_investor[4] = iMA(_Symbol, _Period, gmma_investor5, 0, MODE_EMA, PRICE_CLOSE);
   handle_investor[5] = iMA(_Symbol, _Period, gmma_investor6, 0, MODE_EMA, PRICE_CLOSE);
  }

// バッファーのセット & インデックス方向を指定
void SetupBuffers(void)
  {
   SetIndexBuffer(0, buffer_trader1, INDICATOR_DATA);
   SetIndexBuffer(1, buffer_trader2, INDICATOR_DATA);
   SetIndexBuffer(2, buffer_trader3, INDICATOR_DATA);
   SetIndexBuffer(3, buffer_trader4, INDICATOR_DATA);
   SetIndexBuffer(4, buffer_trader5, INDICATOR_DATA);
   SetIndexBuffer(5, buffer_trader6, INDICATOR_DATA);
   SetIndexBuffer(6, buffer_investor1, INDICATOR_DATA);
   SetIndexBuffer(7, buffer_investor2, INDICATOR_DATA);
   SetIndexBuffer(8, buffer_investor3, INDICATOR_DATA);
   SetIndexBuffer(9, buffer_investor4, INDICATOR_DATA);
   SetIndexBuffer(10, buffer_investor5, INDICATOR_DATA);
   SetIndexBuffer(11, buffer_investor6, INDICATOR_DATA);

   ArraySetAsSeries(buffer_trader1, true);
   ArraySetAsSeries(buffer_trader2, true);
   ArraySetAsSeries(buffer_trader3, true);
   ArraySetAsSeries(buffer_trader4, true);
   ArraySetAsSeries(buffer_trader5, true);
   ArraySetAsSeries(buffer_trader6, true);
   ArraySetAsSeries(buffer_investor1, true);
   ArraySetAsSeries(buffer_investor2, true);
   ArraySetAsSeries(buffer_investor3, true);
   ArraySetAsSeries(buffer_investor4, true);
   ArraySetAsSeries(buffer_investor5, true);
   ArraySetAsSeries(buffer_investor6, true);
  }

// 各種MAが計算済か検証する
bool CheckBarsCalculated(
   const int rates_total
)
  {
   if(BarsCalculated(handle_trader[0])<rates_total)
     {
      Print("Not enough data to calculate GMMA for trader 0");
      return false;
     }
   if(BarsCalculated(handle_trader[1])<rates_total)
     {
      Print("Not enough data to calculate GMMA for trader 1");
      return false;
     }
   if(BarsCalculated(handle_trader[2])<rates_total)
     {
      Print("Not enough data to calculate GMMA for trader 2");
      return false;
     }
   if(BarsCalculated(handle_trader[3])<rates_total)
     {
      Print("Not enough data to calculate GMMA for trader 3");
      return false;
     }
   if(BarsCalculated(handle_trader[4])<rates_total)
     {
      Print("Not enough data to calculate GMMA for trader 4");
      return false;
     }
   if(BarsCalculated(handle_trader[5])<rates_total)
     {
      Print("Not enough data to calculate GMMA for trader 5");
      return false;
     }
   if(BarsCalculated(handle_investor[0])<rates_total)
     {
      Print("Not enough data to calculate GMMA for investor 0");
      return false;
     }
   if(BarsCalculated(handle_investor[1])<rates_total)
     {
      Print("Not enough data to calculate GMMA for investor 1");
      return false;
     }
   if(BarsCalculated(handle_investor[2])<rates_total)
     {
      Print("Not enough data to calculate GMMA for investor 2");
      return false;
     }
   if(BarsCalculated(handle_investor[3])<rates_total)
     {
      Print("Not enough data to calculate GMMA for investor 3");
      return false;
     }
   if(BarsCalculated(handle_investor[4])<rates_total)
     {
      Print("Not enough data to calculate GMMA for investor 4");
      return false;
     }
   if(BarsCalculated(handle_investor[5])<rates_total)
     {
      Print("Not enough data to calculate GMMA for investor 5");
      return false;
     }
   return true;
  }

// MAの値をこのインジケーターのBufferに移す
bool MoveNewValueToBuffer(const int length)
  {
   if(CopyBuffer(handle_trader[0], 0, 0, length, buffer_trader1) < length)
     {
      Print("Error copying trader buffer 0");
      return false;
     }
   if(CopyBuffer(handle_trader[1], 0, 0, length, buffer_trader2) < length)
     {
      Print("Error copying trader buffer 1");
      return false;
     }
   if(CopyBuffer(handle_trader[2], 0, 0, length, buffer_trader3) < length)
     {
      Print("Error copying trader buffer 2");
      return false;
     }
   if(CopyBuffer(handle_trader[3], 0, 0, length, buffer_trader4) < length)
     {
      Print("Error copying trader buffer 3");
      return false;
     }
   if(CopyBuffer(handle_trader[4], 0, 0, length, buffer_trader5) < length)
     {
      Print("Error copying trader buffer 4");
      return false;
     }
   if(CopyBuffer(handle_trader[5], 0, 0, length, buffer_trader6) < length)
     {
      Print("Error copying trader buffer 5");
      return false;
     }
   if(CopyBuffer(handle_investor[0], 0, 0, length, buffer_investor1) < length)
     {
      Print("Error copying investor buffer 0");
      return false;
     }
   if(CopyBuffer(handle_investor[1], 0, 0, length, buffer_investor2) < length)
     {
      Print("Error copying investor buffer 1");
      return false;
     }
   if(CopyBuffer(handle_investor[2], 0, 0, length, buffer_investor3) < length)
     {
      Print("Error copying investor buffer 2");
      return false;
     }
   if(CopyBuffer(handle_investor[3], 0, 0, length, buffer_investor4) < length)
     {
      Print("Error copying investor buffer 3");
      return false;
     }
   if(CopyBuffer(handle_investor[4], 0, 0, length, buffer_investor5) < length)
     {
      Print("Error copying investor buffer 4");
      return false;
     }
   if(CopyBuffer(handle_investor[5], 0, 0, length, buffer_investor6) < length)
     {
      Print("Error copying investor buffer 5");
      return false;
     }
   return true;
  }

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   InitializeMaHandles();
   SetupBuffers();

   IndicatorSetInteger(INDICATOR_DIGITS,2);

   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Expert tick 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[])
  {
   const int length = rates_total - prev_calculated;

   if(prev_calculated == 0 && !CheckBarsCalculated(rates_total))
     {
      Print("Not enough data to calculate GMMA");
      return 0;
     }

   if(length > 0 && !MoveNewValueToBuffer(length))
     {
      Print("Error moving new value to buffer");
      return 0;
     }

   return rates_total;
  }

//+------------------------------------------------------------------+

Discussion