初心者の方からの質問 MQL5 MT5 MetaTrader 5 - ページ 1164

 
Roman:

メモリ、初期化、値戻しのレベルで、どのようにコードが実行されるかをあらかじめ考えておく必要があります。

よく考えなくても、こういうことはマニュアルがあります。MQLのメモリ割り当てのマニュアルはこれまで一度もなく、開発者からのメッセージだけで、しばしば実装が変わる可能性があることを明示しています

OK、議論は誰がどんなプログラミングの本を読んでいるかに集約されます。私は高校生の頃から読んでいて、この30年間も読み続けています。

 
Igor Makanu:

よく考えなくても、こういうことはマニュアルがあります。MQLのメモリ割り当てのマニュアルはこれまで一度もなく、開発者からのメッセージだけで、しばしば実装が変わる可能性があることを明示しています

OK、議論は誰がどんなプログラミングの本を読んでいるかに集約されます。私は高校生の頃から読んでいて、この30年間はまだ読んでいます

もちろん、よく考える必要はありません、なぜそうしなければならないのか...。コンパイラが勝手にやってくれる。))
C#はCではない

そして、__inlineの動画を見てください。
そこでは、機能で違いがわからない人のために、メモリーでどのように動くかを説明しています。

 
Vladimir Karputov:

まず絵を描いて、シフトパラメータを持つインジケータの「ゼロバー」が自分にとってどのようなものかを指定します。

描かれている。ゼロバーが縦線で強調表示されます。


 
RickD:

描かれている。ゼロバーが縦線で強調表示されます。


コード例

//+------------------------------------------------------------------+
//|                                        iMA Values on a Chart.mq5 |
//|                              Copyright © 2019, Vladimir Karputov |
//|                                           http://wmua.ru/slesar/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2019, Vladimir Karputov"
#property link      "http://wmua.ru/slesar/"
#property version   "1.00"
//--- input parameters
input int                  Inp_MA_ma_period     = 12;          // MA: averaging period
input int                  Inp_MA_ma_shift      = 5;           // MA: horizontal shift
input ENUM_MA_METHOD       Inp_MA_ma_method     = MODE_SMA;    // MA: smoothing type
input ENUM_APPLIED_PRICE   Inp_MA_applied_price = PRICE_CLOSE; // MA: type of price
//---
int    handle_iMA;                           // variable for storing the handle of the iMA indicator
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create handle of the indicator iMA
   handle_iMA=iMA(Symbol(),Period(),Inp_MA_ma_period,Inp_MA_ma_shift,
                  Inp_MA_ma_method,Inp_MA_applied_price);
//--- if the handle is not created
   if(handle_iMA==INVALID_HANDLE)
     {
      //--- tell about the failure and output the error code
      PrintFormat("Failed to create handle of the iMA indicator for the symbol %s/%s, error code %d",
                  Symbol(),
                  EnumToString(Period()),
                  GetLastError());
      //--- the indicator is stopped early
      return(INIT_FAILED);
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   double array_ma[];
   ArraySetAsSeries(array_ma,true);
   int start_pos=0,count=3;
   if(!iGetArray(handle_iMA,0,start_pos,count,array_ma))
      return;

   string text="";
   for(int i=0; i<count; i++)
      text=text+IntegerToString(i)+": "+DoubleToString(array_ma[i],Digits()+1)+"\n";

   Comment(text);
  }
//+------------------------------------------------------------------+
//| Get value of buffers                                             |
//+------------------------------------------------------------------+
bool iGetArray(const int handle,const int buffer,const int start_pos,
               const int count,double &arr_buffer[])
  {
   bool result=true;
   if(!ArrayIsDynamic(arr_buffer))
     {
      PrintFormat("ERROR! EA: %s, FUNCTION: %s, this a no dynamic array!",__FILE__,__FUNCTION__);
      return(false);
     }
   ArrayFree(arr_buffer);
//--- reset error code
   ResetLastError();
//--- fill a part of the iBands array with values from the indicator buffer
   int copied=CopyBuffer(handle,buffer,start_pos,count,arr_buffer);
   if(copied!=count)
     {
      //--- if the copying fails, tell the error code
      PrintFormat("ERROR! EA: %s, FUNCTION: %s, amount to copy: %d, copied: %d, error code %d",
                  __FILE__,__FUNCTION__,count,copied,GetLastError());
      //--- quit with zero result - it means that the indicator is considered as not calculated
      return(false);
     }
   return(result);
  }
//+------------------------------------------------------------------+

結果


ご覧のように、何も手を加えなくてもコピー可能なのです。

ファイル:
 
Vladimir Karputov:

コード例

結果


ご覧のように、タンバリンなしで簡単にコピーできます。

コード例指標に基づく。インジケータはバッファメモリの確保に追われています。

#property copyright "Copyright 2019"
#property link      ""
#property version   "1.00"

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   0

//---

input int                  MA_period = 12;
input int                  MA_shift = 5;
input ENUM_MA_METHOD       MA_method = MODE_SMA;
input ENUM_APPLIED_PRICE   MA_applied_price = PRICE_CLOSE;

int start_pos = 0;

//---

double MA_Calc_Buf[];

int hMA = INVALID_HANDLE;


int OnInit()
{
  SetIndexBuffer(0, MA_Calc_Buf, INDICATOR_CALCULATIONS); 
  ArraySetAsSeries(MA_Calc_Buf, true);
 
  hMA = iMA(NULL, 0, MA_period, MA_shift, MA_method, MA_applied_price);   
  if (hMA == INVALID_HANDLE)
  {
    int LErr = GetLastError();
    PrintFormat("iMA create failed (%d)", LErr);
    return (INIT_FAILED);
  }
 
  return (INIT_SUCCEEDED);
}

void OnDeinit(const int reason)
{
  if (hMA != INVALID_HANDLE)
  {
    IndicatorRelease(hMA);
    hMA = INVALID_HANDLE;
  }
}

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 copied;
  copied = CopyBuffer(hMA, 0, start_pos, rates_total, MA_Calc_Buf);
  if (copied == -1)
  {
    int LErr = GetLastError();
    PrintFormat("CopyBuffer(hMA) failed (%d)", LErr);
    return (prev_calculated);
  }
 
  if (copied == 0)
  {
    PrintFormat("CopyBuffer(hMA) copied 0 bars");
    return (prev_calculated);
  } 
 
  string text = "";
  for (int i=0; i<15; i++)
    text = text + IntegerToString(i) + ": " + DoubleToString(MA_Calc_Buf[i], Digits()+1) + "\n";
 
  Comment(text);

  return (rates_total);
}

start_pos = 0 のとき,チャートの0番目のバーに対応する値は,ポジション 5 にある。1.017041 あなたのEAではポジション0にあります。 オッケーです。


しかし、この値をゼロの位置で取得する必要があります。

start_pos = 5としました。必要な結果が得られない。私が求めている価値は、やはり第5位です。


start_pos = -5 としました。思うような結果が得られない。今回も私が求めている価値は、ポジション5です。


start_pos = -10としました。そして、今になって初めて期待通りの結果を得ることができました。


 
RickD:

コード例指標に基づく。インジケータはバッファメモリの割り当てでビジー状態になっています。

start_pos = 0 のとき、5番目の位置に表示されるチャートの0番目のバーに対応する値。1.017041 あなたのEAではポジション0にあります。オッケーです。


しかし、この値をゼロの位置で取得する必要があります。

start_pos = 5としました。思うような結果が得られない。私が求めている価値は、やはり第5位です。


start_pos = -5 としました。思うような結果が得られない。今回も私が求めている価値は、ポジション5です。


start_pos = -10としました。そして、今になって初めて期待通りの結果を得ることができました。


Expert Advisorからの作業とインジケータからの作業の違いを理解する必要があります。インジケータからの作業には、ヘルプの例(iMA)を使用してください。

Документация по MQL5: Технические индикаторы / iMA
Документация по MQL5: Технические индикаторы / iMA
  • www.mql5.com
//|                                                     Demo_iMA.mq5 | //|                        Copyright 2011, MetaQuotes Software Corp. | //|                                             https://www.mql5.com | //| Перечисление способов создания хэндла                            |  Creation             type=Call_iMA;                ...
 
Vladimir Karputov:

EAからの操作とインジケーターからの操作の違いを理解する必要があります。インジケータから作業するには、ヘルプの例(iMA)を使用します。

そこで、iMAのヘルプにある例を参考に、以下を追加します。

   ArraySetAsSeries(iMABuffer, true);
   comm = (string)DoubleToString(iMABuffer[0], Digits());
   ArraySetAsSeries(iMABuffer, false);

   Comment(comm);   

で、その値がEAの出力値とちょうどma_shift分のバー数だけ異なっていることを確認します。

一方、私は、あなたがExpert Advisorでarray_ma[0]に持っているような値をiMABuffer[0]のインジケータで取得する必要があります。

少なくとも現時点では、Expert Advisor とインジケータに対する CopyBuffer の動作は異なることが確認されています。EAとインジケータを扱う際のCopyBufferの違いを理解されている方は、ドキュメントの該当箇所を指定して勉強してください。

 

質問を単純化してみる。これらのMA値(赤い縦線から始まって 左側)をインジケータのバッファに取り込むにはどうしたらいいでしょうか? 例を書いていただけますか?


 
RickD:

質問を単純化してみる。これらのMA値(赤い縦線から始まって 左側)をインジケータのバッファに取り込むにはどうしたらいいでしょうか?例を書いていただけますか?


スクリーンショットでは、バッファの表示が 5本分右にずれています。では - インジケータバッファの5本目のバー(インデックス4)、さらにリストの左側にあるバーを取得するには、どこから取得すればいいのでしょうか?Buffer[4]から、さらに左へ。

理論上は。実際には、長い間、つまり1年近くもインジケータのコードを開いたり、作業したりしていません......。試してみてください。

 
Artyom Trishkin:


理論上は。実際には、長い間、つまり1年近くもインジケータのコードを開いたり、作業したりしていません......。試してみてください。

スキルは失わない。

理由: