新人对MQL4和MQL5的任何问题,对算法和代码的帮助和讨论 - 页 1306

 

请帮助,我在指标中又增加了一个缓冲区(double ma_buffer_stop[];),但它没有画出或出现错误(

//+------------------------------------------------------------------+
//|                                                   MTF_Moving.mq4 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                             https://www.mql5.com/ru/users/melnik |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com/ru/users/melnik"
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 2
#property indicator_color1 Red
#property indicator_color2 Blue
#property indicator_color3 Black
#property indicator_type1 DRAW_LINE
#property indicator_type2 DRAW_LINE
#property indicator_type3 DRAW_LINE

double ma_buffer_slow[];
double ma_buffer_fast[];
double ma_buffer_stop[];

//--- input parameters
input int                     PeriodMaSlow=21;  //Period slow Ma
input int                     PeriodMaFast=13;  //Pertiod fast Ma
input ENUM_APPLIED_PRICE      PriceMa=0;        //Applied price
input ENUM_MA_METHOD          MethodMa=0;       //Method Ma
input ENUM_TIMEFRAMES         Timeframe=60;     //Timeframe for calculate

ENUM_TIMEFRAMES prd;

int index=-1;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0, ma_buffer_slow, INDICATOR_DATA);
   SetIndexBuffer(1, ma_buffer_fast, INDICATOR_DATA);
   SetIndexBuffer(2, ma_buffer_stop, INDICATOR_DATA);
//---
   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[])
  {
//---
   if((rates_total-prev_calculated-PeriodMaSlow)<=0)return(0);
  
   if(Period()>Timeframe) prd=PERIOD_CURRENT;
   if(Period()<=Timeframe) prd=prd=Timeframe;
  
   for(int i=rates_total-prev_calculated-PeriodMaSlow-1;i>=0;i--)
   {
      if(TimeMinute(time[i])==0)index=iBarShift(Symbol(), prd, time[i], false);
      
      ma_buffer_fast[i]=iMA(Symbol(), prd, PeriodMaFast, 0, MethodMa, PriceMa, index);
      ma_buffer_slow[i]=iMA(Symbol(), prd, PeriodMaSlow, 0, MethodMa, PriceMa, index);
      if(ma_buffer_fast[i+1]>=ma_buffer_slow[i+1]&&ma_buffer_fast[i]<ma_buffer_slow[i])
      {ma_buffer_stop[i]=ma_buffer_slow[i]+50*Point;}
      if(ma_buffer_fast[i+1]<=ma_buffer_slow[i+1]&&ma_buffer_fast[i]>ma_buffer_slow[i])
      {ma_buffer_stop[i]=ma_buffer_slow[i]-50*Point;}
   }
  
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
 

检查一下,我们是否已经进入了这里?

if(ma_buffer_fast[i+1]>=ma_buffer_slow[i+1]&&ma_buffer_fast[i]<ma_buffer_slow[i]) printf(ma_buffer_slow[i]);
这只是我想到的第一件事。
 

有时我在零点时 抓住了除法。(在动态中什么都可能发生)。

我通过评论除法操作和在图表上抛出EA来寻找它。

有什么方法可以用更正常的方法获得带有错误的字符串吗?

问题是,该错误只在某种状态下发生。也就是说,这个错误可能在某个时间段和新的蜡烛上消失。当你在四点钟方向注意到它时,它是好的。有时间去寻找它。

 
Valeriy Yastremskiy:

有什么办法可以用更常规的方法得到有错误的那一行吗?

你应该在专家日志中看到一个条目,其中包括发生除零 的文件和行号。

 
Igor Makanu:

你必须在你的Expert Advisor日志中输入发生除以0 的文件和行号。

2020.11.27 11:55:29.795 qstr7_52_1 EURUSD,H4: 在'qstr7_52_1.mq4'(962,43)中除零。

只有这个。

962.43虽然。

你去那里)))谢谢!!!!!


 
Aleksei Stepanenko:

我们甚至在这里得到了吗?

就在我的脑海中。

前往

如果我这样做,它就会画出

   double prev01,prev02;

   if((rates_total-prev_calculated-PeriodMaSlow)<=0)return(0);
  
   if(Period()>Timeframe) prd=PERIOD_CURRENT;
   if(Period()<=Timeframe) prd=prd=Timeframe;
  
   for(int i=rates_total-prev_calculated-PeriodMaSlow-1;i>=0;i--)
   {
      if(TimeMinute(time[i])==0)index=iBarShift(Symbol(), prd, time[i], false);
      
      ma_buffer_fast[i]=iMA(Symbol(), prd, PeriodMaFast, 0, MethodMa, PriceMa, index);
      ma_buffer_slow[i]=iMA(Symbol(), prd, PeriodMaSlow, 0, MethodMa, PriceMa, index);
      prev01=iMA(Symbol(), prd, PeriodMaFast, 0, MethodMa, PriceMa, index+1);
      prev02=iMA(Symbol(), prd, PeriodMaSlow, 0, MethodMa, PriceMa, index+1);
      if(prev01>=prev02&&ma_buffer_fast[i]<ma_buffer_slow[i])
      {ma_buffer_stop[i]=ma_buffer_slow[i]+50*Point;}
      if(prev01<=prev02&&ma_buffer_fast[i]>ma_buffer_slow[i])
      {ma_buffer_stop[i]=ma_buffer_slow[i]-50*Point;}
   }
  
 

这个数组只在交点处有正确的值,其他地方的值是EMPTY_VALUE。因此,什么都看不见。你需要输入一个全局变量,并在相交的时刻给它赋值。而且它已经给出了相交时刻的当前值,或没有相交时的过去值。

double Stop=0;
int OnCalculate(....

if(ma_buffer_fast[i+1]>=ma_buffer_slow[i+1]&&ma_buffer_fast[i]<ma_buffer_slow[i]) Stop=ma_buffer_slow[i]+50*Point;
if(ma_buffer_fast[i+1]<=ma_buffer_slow[i+1]&&ma_buffer_fast[i]>ma_buffer_slow[i]) Stop=ma_buffer_slow[i]-50*Point;

ma_buffer_stop[i]=Stop;
 
Aleksei Stepanenko:

这个数组只在交点处有正确的值,其他地方的值是EMPTY_VALUE。因此,什么都看不见。你需要输入一个全局变量,并在相交的时刻给它赋值。而且它已经给出了相交时刻的当前值,或没有相交时的过去值。

谢谢你,它成功了)

 
如果指标功能仍在运行,是否可以强制改变时间框架?
 
Aleksei Stepanenko:

这个数组只在交点处有正确的值,其他地方的值是EMPTY_VALUE。因此,什么都看不见。你需要输入一个全局变量,并在相交的时刻给它赋值。而且它已经给出了相交时刻的当前值,或没有相交时的过去值。

我把PeriodMaSlow 乘以2

   if((rates_total-prev_calculated-PeriodMaSlow)<=0)return(0);
  
   if(Period()>Timeframe) prd=PERIOD_CURRENT;
   if(Period()<=Timeframe) prd=prd=Timeframe;
  
   for(int i=rates_total-prev_calculated-PeriodMaSlow-1;i>=0;i--)

和初始代码的工作没有错误

原因: