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をチャートに投げるなどして探しています。

もっと普通の方法で、エラーのある行を取得する方法はないでしょうか?

問題は、ある特定の状態でのみエラーが発生することです。すなわち、ある時間枠で、新しいローソク足で、エラーが消えることがあります。4時の位置で気がつくといい感じです。探す時間はある。

 
Valeriy Yastremskiy:

もっと一般的な方法で、エラーのある行を取得する方法はないのでしょうか?

エキスパートログにゼロ除算が 行われたファイルと行番号のエントリがあるはずです。

 
Igor Makanu:

Expert Advisor のログに、ゼロによる除算が 行われたファイルと行番号のエントリがある必要があります。

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--)

で、最初のコードはエラーなしで動作します。