Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 350

 
Rustam Bikbulatov:

Hi. Can you tell me why it doesn't seeima=iMA(NULL,60,24,0,1,0,k). The comment shows a much higher quote than the line itself. That is why it does not show min/max at all.

Use identifiers from enumerations, not numbers. It's much clearer:

for(i=pos; i<rates_total && !IsStopped(); i++)
     {
      double dmin= DBL_MAX;
      double dmax=-DBL_MAX;
      for(k=i-InpKPeriod+1; k<=i; k++)
        {double ima=iMA(NULL,PERIOD_M1,60,0,MODE_EMA,PRICE_CLOSE,k);
         if(ima<dmin)  dmin=ima;
         if(ima>dmax)  dmax=ima;
         Comment("pos="+(string)pos+", k="+(string)k+", iMA="+DoubleToString(ima,Digits()));
        }
      ExtLowesBuffer[i]=dmin;
      ExtHighesBuffer[i]=dmax;
     }

You can see right away that you're asking iMA(). For example:

you have iMA(NULL,60,24,0,1,0,k)

and so iMA(NULL,PERIOD_H1,24,0,MODE_EMA,PRICE_CLOSE,k)

Which is more clear?

You're taking MAK data from the hourly chart. If the program is running on the hourly chart, what data do you expect to get and see?

 
Artyom Trishkin:

Use identifiers from enumerations, not numbers. It's much clearer:

It is immediately obvious that you are asking iMA(). For example:

you have iMA(NULL,60,24,0,1,0,k)

and so iMA(NULL,PERIOD_H1,24,0,MODE_EMA,PRICE_CLOSE,k)

Which is more clear?

You're taking the MASK data from the hourly chart. If the program is running on the hourly chart, what data do you expect to get and see?

I put this on the minute chart
   for(i=pos; i<rates_total && !IsStopped(); i++)
     {
      double dmin=1000000.0;
      double dmax=-1000000.0;
      for(k=i-InpKPeriod+1; k<=i; k++)
        {double ima=iMA(Symbol(),1,60,0,1,0,k);
         if(dmin>ima)  dmin=ima;
         if(dmax<ima)  dmax=ima;
         Comment(dmax);
        }
      ExtLowesBuffer[i]=dmin;
      ExtHighesBuffer[i]=dmax;
     }

Nothing works with k

 
Rustam Bikbulatov:

It's an unintentional bet. It doesn't change anything. The very point is that it shows wrong with k!

Even though I'm half-soon and my eyes are blurry, it's quite obvious you have the wrong MA values coming from the wrong side :-) apart from the timeframe confusion

   // основной цикл
   // перебираем бары от последнего нерасчитанного к актуальному
   // (просто пример - поэтому инициализация и заполнение буферов пропущенны!!)
   for(int bar=prev_calculated;bar<rates_total && !IsStopped();bar++) {
      // bar использовать для обращения к массивам, i - к таймсериям
      int i=rates_total-bar-1;
      // считаем min,max от некой MA за DEPTH баров
      double min=DBL_MAX;
      double max=DBL_MIN;
      if (bar<DEPTH) {
         // данных ещё недостаточно
         continue;
      }
      for(int t=0;t<DEPTH;t++) {
         double ma=iMA(_Symbol,_Period,MA_PERIOD,0,MA_METHOD,MA_PRICE,i+t); // берём значение MA (! как в таймсериях 0-последнее)
         if (ma>0) {
            if (min<ma) min=ma;
            if (max>ma) max=ma;
         }
      }
      if (min!=DBL_MAX && max!=DBL_MIN) {
         // что-то делаем с полученными максимум/минимум MA
      }
   }
 
Maxim Kuznetsov:

Even though I'm half-asleep and my eye is a bit muddy, it's quite obvious you have the wrong MA values coming from the wrong side :-) apart from the timeframe confusion


Interesting idea. I'll give it a try. If anything it is an ordinary stochastic indicator. The idea appeared to take extremums of bars for some period and extremums of MA lines. It is in the line 100-113. I think this will improve the indicator

//+------------------------------------------------------------------+
///+------------------------------------------------------------------+
//|                                                   Stochastic.mq4 |
//|                   Copyright 2005-2014, MetaQuotes Software Corp. |
//|                                              http://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright   "2005-2014, MetaQuotes Software Corp."
#property link        "http://www.mql4.com"
#property description "Stochastic Oscillator"
#property strict

#property indicator_separate_window
#property indicator_minimum    0
#property indicator_maximum    100
#property indicator_buffers    2
#property indicator_color1     LightSeaGreen
#property indicator_color2     Red
#property indicator_level1     20.0
#property indicator_level2     80.0
#property indicator_levelcolor clrSilver
#property indicator_levelstyle STYLE_DOT
//--- input parameters
input int InpKPeriod=60; // K Period
input int InpDPeriod=60; // D Period
input int InpSlowing=1; // Slowing
//--- buffers
double ExtMainBuffer[];
double ExtSignalBuffer[];
double ExtHighesBuffer[];
double ExtLowesBuffer[];
//---
int draw_begin1=0;
int draw_begin2=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit(void)
  {
   string short_name;
//--- 2 additional buffers are used for counting.
   IndicatorBuffers(4);
   SetIndexBuffer(2, ExtHighesBuffer);
   SetIndexBuffer(3, ExtLowesBuffer);
//--- indicator lines
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0, ExtMainBuffer);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1, ExtSignalBuffer);
//--- name for DataWindow and indicator subwindow label
   short_name="Sto("+IntegerToString(InpKPeriod)+","+IntegerToString(InpDPeriod)+","+IntegerToString(InpSlowing)+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);
   SetIndexLabel(1,"Signal");
//---
   draw_begin1=InpKPeriod+InpSlowing;
   draw_begin2=draw_begin1+InpDPeriod;
   SetIndexDrawBegin(0,draw_begin1);
   SetIndexDrawBegin(1,draw_begin2);
//--- initialization done
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Stochastic oscillator                                            |
//+------------------------------------------------------------------+
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    i,k,pos;
//--- check for bars count
   if(rates_total<=InpKPeriod+InpDPeriod+InpSlowing)
      return(0);
//--- counting from 0 to rates_total
   ArraySetAsSeries(ExtMainBuffer,false);
   ArraySetAsSeries(ExtSignalBuffer,false);
   ArraySetAsSeries(ExtHighesBuffer,false);
   ArraySetAsSeries(ExtLowesBuffer,false);
   ArraySetAsSeries(low,false);
   ArraySetAsSeries(high,false);
   ArraySetAsSeries(close,false);
//---
   pos=InpKPeriod-1;
   if(pos+1<prev_calculated)
      pos=prev_calculated-2;
   else
     {
      for(i=0; i<pos; i++)
        {
         ExtLowesBuffer[i]=0.0;
         ExtHighesBuffer[i]=0.0;
        }
     }
//--- calculate HighesBuffer[] and ExtHighesBuffer[]
   for(i=pos; i<rates_total && !IsStopped(); i++)
     {
      double dmin=1000000.0;
      double dmax=-1000000.0;
      for(k=i-InpKPeriod+1; k<=i; k++)
        {double ima=iMA(Symbol(),1,60,0,1,0,k);
         if(dmin>ima)  dmin=ima;
         if(dmax<ima)  dmax=ima;
         Comment(ima);
        }
      ExtLowesBuffer[i]=dmin;
      ExtHighesBuffer[i]=dmax;
     }
//--- %K line
   pos=InpKPeriod-1+InpSlowing-1;
   if(pos+1<prev_calculated)
      pos=prev_calculated-2;
   else
     {
      for(i=0; i<pos; i++)
         ExtMainBuffer[i]=0.0;
     }
//--- main cycle
   for(i=pos; i<rates_total && !IsStopped(); i++)
     {
      double sumlow=0.0;
      double sumhigh=0.0;
      for(k=(i-InpSlowing+1); k<=i; k++)
        {
         sumlow +=(close[k]-ExtLowesBuffer[k]);
         sumhigh+=(ExtHighesBuffer[k]-ExtLowesBuffer[k]);
        }
      if(sumhigh==0.0)
         ExtMainBuffer[i]=100.0;
      else
         ExtMainBuffer[i]=sumlow/sumhigh*100.0;
     }
//--- signal
   pos=InpDPeriod-1;
   if(pos+1<prev_calculated)
      pos=prev_calculated-2;
   else
     {
      for(i=0; i<pos; i++)
         ExtSignalBuffer[i]=0.0;
     }
   for(i=pos; i<rates_total && !IsStopped(); i++)
     {
      double sum=0.0;
      for(k=0; k<InpDPeriod; k++)
         sum+=ExtMainBuffer[i-k];
      ExtSignalBuffer[i]=sum/InpDPeriod;
     }
//--- OnCalculate done. Return new prev_calculated.
   return(rates_total);
  }
//+------------------------------------------------------------------+
 

Good afternoon everyone!

Can you please tell me what to do, if for example I have set the settings in my EA today, then on the next day some of the settings appear to be empty?

It happens every day.

 
Valerius:

Good afternoon everyone!

Can you please tell me what to do, if for example I have set the settings in my EA today, then on the next day some of the settings appear to be empty?

It happens every day.

If you have the source code, then remove input (or extern) in the code and change parameters through the code.

 
Valerius:

Good afternoon everyone!

Can you please tell me what to do, if for example I have set the settings in my EA today, then on the next day some of the settings appear to be empty?

It happens every day.

I should write to Service Desk with as much information on the problem as possible.

 
Nauris Zukas:

If you have source, then remove input (or extern) in the code and change parameters via code.


This is not serious... Why do you need them (input and extern) then....

 
Artyom Trishkin:

Write to servicedesk with as much information on the problem as possible.


Can you give me a link?

 
Valerius:

Can you give me a link?

And take a look at your profile... There's a lot of interesting stuff there.