初学者的问题 MQL5 MT5 MetaTrader 5 - 页 1164

 
Roman:

有必要提前考虑代码在内存、初始化和值返回层面是如何执行的。

你不需要考虑清楚,这些事情都有手册,MQL中从来没有内存分配的手册,只有来自开发者的信息,而且经常有一个澄清,即实现可能会改变。

好吧,讨论归结为谁读什么编程书,我从高中就开始读,过去30年我还在读。

 
Igor Makanu:

你不需要考虑清楚,这些事情都有手册,MQL中从来没有内存分配的手册,只有来自开发者的信息,而且经常有一个澄清,即实现可能会改变。

好吧,讨论归结为谁读什么编程书,我从高中就开始读,过去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时,我们看到的图表第0条对应的数值在位置5。1.017041 在你的EA中,它是在0号位置。好的。


但我需要在零位置获得这个值。

我设定start_pos = 5。我没有得到想要的结果。我所寻找的价值还是在第5位。


我设定start_pos = -5。我没有得到想要的结果。我所寻找的价值又是在第5位。


我设定start_pos = -10。而现在我才得到想要的结果。


我们需要了解专家顾问的工作和指标的工作之间的区别。对于来自指标的工作,使用帮助中的例子(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的条数。

另一方面,我需要在iMABuffer[0]的指标中获得你在Expert Advisor的array_ma[0]中的数值。

至少目前我们可以看到,专家顾问和指标的CopyBuffer的行为是不同的。如果你了解CopyBuffer在使用EA和指标时的区别,请指定文档中的相应部分进行学习。

 

我试着简化一下这个问题。我如何将这些MA值(从红色垂直线 开始,向左)送到指标中的缓冲区? 你能写一个例子吗?


 
RickD:

我试着简化一下这个问题。我如何将这些MA值(从红色垂直线 开始,向左)送到指标中的缓冲区?你能写一个例子吗?


在截图中,缓冲区显示 向右移了五格。那么--为了获得指标缓冲区的第五条(索引4),以及在列表中更靠左的位置,我们应该从哪里获得它们?从缓冲区[4]再往左走。

在理论上。实际上--我已经很久没有打开过指标代码或用它工作了--差不多一年了......试一试吧。

 
Artyom Trishkin:


在理论上。实际上--我已经很久没有打开过指标代码或用它工作了--差不多一年了......试一试吧。

你不能失去一个技能。