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

 
Konstantin Nikitin:

Yes, that's what I mean. Okay, we missed it. The main thing is for him to understand what's what...

He who? That's where I started.

Forum on trading, automated trading systems and strategy testing

Any questions from beginners on MQL4 and MQL5, help and discussion on algorithms and codes

Alexey Viktorov, 2019.06.11 09:06

You can't use SymbolInfoInteger to get Point())).

And in general, it's necessary to remember that Point() or _Point only for current symbol. For symbols other than the one on which the EA works, you should use this function.

The SymbolInfoInteger function returns not only int values, but also long, bool and enumerations.

Next, of course, he meant Digits() or _Digits and wrote what he had written. And the last line of answer concerned the second part of the question. Don't forget the Russian proverb: "A clever man will not tell, a fool will not guess." guessed and said, so it's neither one thing nor the other.
 
Alexey Viktorov:

Может вам очень не повезло, но я не люблю колупаться в чужом коде. Тем более править его...

Alexey Viktorov:

Next question: How often do you think the value of indicator will be exactly 50?

As I understood the problem, it should be one colour >50 and another <=50, but ==50 I haven't seen anywhere. Of course, to avoid loosing in case ==50, we should add >=50 to one of them and <=50 to another. It may look better.

And the second question: how much of a necessity was it to change the entire buffer indexing? After reindexing, did you fix everything in your code? Maybe it would be easier to make colour buffer index 4, and auxiliary buffers 5 and 6 accordingly. You would have less to edit and less chances to make a few more errors.

  • Thanks for the answer. If Luby had poked around in someone else's code, we would have most likely lost you as an expert, so it's just LUCKY in this respect.
  • About <= or >= I agree, omission, of course one of them must be present, as soon as I run through, but for now it's not a matter of principle.
  • Tried, reassigned one colour buffer, reassigned whole groups, swapped groups in places (above/below) - nothing helps. I will now finish the post and try to make a separate histogram indicator, without lines. If it works, I will try to combine them again. This is a matter of principle. I do not want to leave behind white spots of incomprehension.

 
Sergey Voytsekhovsky:

  • Thank you for your response. If Lubilee had been poking around in other people's code, we'd probably have lost you as an expert, so it's just LUCKY in that respect.
  • About <= or >= I agree, omission, of course one of them must be present, as soon as I run through, but so far not a matter of principle.
  • Tried, reassigned one colour buffer, reassigned whole groups, swapped groups (above/below) - nothing helps. I will now finish the post and try to make a separate histogram indicator, without lines. If it works, I will try to combine them again. This is a matter of principle. I do not want to leave white spots of incomprehension behind.

You must have racked your brains when you were changing the buffer indexing.

Go back and take the old code from Message 8806 and change only the buffer sequence.

   SetIndexBuffer(4,ColorHistogram_2Colors,INDICATOR_COLOR_INDEX);
   SetIndexBuffer(5,ExtHighesBuffer,INDICATOR_CALCULATIONS);
   SetIndexBuffer(6,ExtLowesBuffer,INDICATOR_CALCULATIONS);
Then trace conditions when it is necessary to put 0 and when it is necessary to put 1
 
Alexey Viktorov:

You must not have been thinking too hard when you changed the buffer indexing.

Go back and take the old code from post 8806 and change just the buffer sequence there.

Then trace the conditions when to put 0 in the colour buffer and when to put 1

I did that, didn't help, admitted I was making mistakes, after your post I did it again. Here are the code places I changed. The old one is uncommented.

//+------------------------------------------------------------------+
//|                                       Stoch_HISTOGRAM_MQL5_4.mq5 |
//|                   Copyright 2009-2017, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "2009-2017, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
//--- indicator settings
#property indicator_separate_window
#property indicator_buffers 8
#property indicator_plots   3

#property indicator_type1   DRAW_LINE       // основная
#property indicator_color1  clrLightSeaGreen
#property indicator_style1  STYLE_SOLID

#property indicator_type2   DRAW_LINE       // сигнальная
#property indicator_color2  clrYellow
#property indicator_style2  STYLE_SOLID

#property indicator_type3   DRAW_COLOR_HISTOGRAM2
#property indicator_color3  clrGreen,clrRed
#property indicator_style3  STYLE_SOLID

#property indicator_width1  3 
#property indicator_width2  2 
#property indicator_width3  1 
//--- input parameters
input int InpKPeriod=5;  // K period
input int InpDPeriod=3;  // D period
input int InpSlowing=3;  // Slowing
//--- indicator buffers
double    ExtMainBuffer[];
double    ExtSignalBuffer[];
double    ColorHistogram_2Buffer1[]; 
double    ColorHistogram_2Buffer2[]; 
     //double    ExtHighesBuffer[];
     //double    ExtLowesBuffer[];
     //double    ColorHistogram_2Colors[];
double    ColorHistogram_2Colors[];
double    ExtHighesBuffer[];
double    ExtLowesBuffer[];
color     colors[]={clrRed,clrGreen};
int       cl;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,ExtMainBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,ExtSignalBuffer,INDICATOR_DATA);
   SetIndexBuffer(2,ColorHistogram_2Buffer1,INDICATOR_DATA);
   SetIndexBuffer(3,ColorHistogram_2Buffer2,INDICATOR_DATA);
        //SetIndexBuffer(4,ExtHighesBuffer,INDICATOR_CALCULATIONS);
        //SetIndexBuffer(5,ExtLowesBuffer,INDICATOR_CALCULATIONS);
        //SetIndexBuffer(6,ColorHistogram_2Colors,INDICATOR_COLOR_INDEX);
   SetIndexBuffer(4,ColorHistogram_2Colors,INDICATOR_COLOR_INDEX);
   SetIndexBuffer(5,ExtHighesBuffer,INDICATOR_CALCULATIONS);
   SetIndexBuffer(6,ExtLowesBuffer,INDICATOR_CALCULATIONS);
   PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,0);
//--- set accuracy
   IndicatorSetInteger(INDICATOR_DIGITS,2);
//--- set levels
   IndicatorSetInteger(INDICATOR_LEVELS,3);
   IndicatorSetDouble(INDICATOR_LEVELVALUE,0,20);
   IndicatorSetDouble(INDICATOR_LEVELVALUE,1,50);
   IndicatorSetDouble(INDICATOR_LEVELVALUE,2,80);
//--- set maximum and minimum for subwindow 
   IndicatorSetDouble(INDICATOR_MINIMUM,0);
   IndicatorSetDouble(INDICATOR_MAXIMUM,100);
//--- name for DataWindow and indicator subwindow label
   IndicatorSetString(INDICATOR_SHORTNAME,"Stoch_HISTOGRAM("+(string)InpKPeriod+","+(string)InpDPeriod+","+(string)InpSlowing+")");
   //PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,0);
   PlotIndexSetString(0,PLOT_LABEL,"Main");
   PlotIndexSetString(1,PLOT_LABEL,"Signal");
        //PlotIndexSetString(2,PLOT_LABEL,"UP");
        //PlotIndexSetString(3,PLOT_LABEL,"LOW");
//--- sets first bar from what index will be drawn
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpKPeriod+InpSlowing-2);
   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,InpKPeriod+InpDPeriod);
   PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,InpKPeriod+InpSlowing-2);
   PlotIndexSetInteger(3,PLOT_DRAW_BEGIN,InpKPeriod+InpSlowing-2);
//--- initialization done
  }
//+------------------------------------------------------------------+
//| 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,start;
//--- check for bars count
   if(rates_total<=InpKPeriod+InpDPeriod+InpSlowing)
      return(0);
//---
   start=InpKPeriod-1;
   if(start+1<prev_calculated) start=prev_calculated-2;
   else
     {
      for(i=0;i<start;i++)
        {
         ExtLowesBuffer[i]=0.0;
         ExtHighesBuffer[i]=0.0;
        }
     }
//--- calculate HighesBuffer[] and ExtHighesBuffer[]
   for(i=start;i<rates_total && !IsStopped();i++)
     {
      double dmin=1000000.0;
      double dmax=-1000000.0;
      for(k=i-InpKPeriod+1;k<=i;k++)
        {
         if(dmin>low[k])  dmin=low[k];
         if(dmax<high[k]) dmax=high[k];
        }
      ExtLowesBuffer[i]=dmin;
      ExtHighesBuffer[i]=dmax;
     }
//--- %K
   start=InpKPeriod-1+InpSlowing-1;
   if(start+1<prev_calculated) start=prev_calculated-2;
   else
     {
      for(i=0;i<start;i++) ExtMainBuffer[i]=0.0;
     }
//--- main cycle
   for(i=start;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;
      if(ExtMainBuffer[i]>=50){
         cl=1;
         ColorHistogram_2Buffer1[i]=50; 
         ColorHistogram_2Buffer2[i]=ExtMainBuffer[i]; 
         ColorHistogram_2Colors[i]=colors[cl];
     Print("ExtMainBuffer[i]=",ExtMainBuffer[i],
           " cl=",cl,
           " ColorHistogram_2Buffer1[i]=",ColorHistogram_2Buffer1[i],
           " ColorHistogram_2Buffer2[i]=",ColorHistogram_2Buffer2[i],
           " ColorHistogram_2Colors[i]=",ColorHistogram_2Colors[i]);
         } 
      if(ExtMainBuffer[i]<50){
         cl=0;
         ColorHistogram_2Buffer1[i]=ExtMainBuffer[i]; 
         ColorHistogram_2Buffer2[i]=50; 
         ColorHistogram_2Colors[i]=colors[cl];
     Print("ExtMainBuffer[i]=",ExtMainBuffer[i],
           " cl=",cl,
           " ColorHistogram_2Buffer1[i]=",ColorHistogram_2Buffer1[i],
           " ColorHistogram_2Buffer2[i]=",ColorHistogram_2Buffer2[i],
           " ColorHistogram_2Colors[i]=",ColorHistogram_2Colors[i]);
         } 
     }
//--- signal
   start=InpDPeriod-1;
   if(start+1<prev_calculated) start=prev_calculated-2;
   else
     {
      for(i=0;i<start;i++) ExtSignalBuffer[i]=0.0;
     }
   for(i=start;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.
   //PlotIndexSetInteger(2,PLOT_LINE_COLOR,colors[i]);
   return(rates_total);
  }
//+------------------------------------------------------------------+ 

This is how it was before I made the changes. The histogram is there, the right colour isn't.

This is how it is now. The histogram is gone, it is not displayed.

I guess I'm not thinking straight again.


 
Sergey Voytsekhovsky:

I did so, it didn't help, I made mistakes, after your post I did it again. Here are the places in the code that I changed. The old one is uncommented.

This is how it was before I made the changes. The histogram is there, the right colour isn't.

This is how it is now. The histogram is gone, it is not displayed.

I must be racking my brains again.


There is no logic here at all. The sequence should be as follows: The histogram value is determined and the i-th index of the indicator buffer is filled in. Then a separate check, if this value >=50 the colour buffer is filled in with 0, otherwise 1. It is defined by colour sequence in the directive

#property indicator_color3  clrGreen,clrRed

If 0, the colour clrGreen is taken and if 1, clrRed

And this line is completely unnecessary, and the sequence is the opposite of the first.

color     colors[]={clrRed,clrGreen};

And this

int       cl;

added for what purpose? It's easier to fill directly.

ColorHistogram_2Colors[i]=0;
// или 
ColorHistogram_2Colors[i]=1;

To fill colour buffer, I would use this construct

         ColorHistogram_2Colors[i]=ExtMainBuffer[i] >= 50 ? 0 : 1; 
The ?: operator is equivalent to if else but applied more easily
 
Alexey Viktorov:

The logic here is broken in general. The sequence should be as follows: Determine the histogram value, fill in the i-th index of the indicator buffer. Then a separate check, if this value >=50 the colour buffer is filled with 0, otherwise 1. This is determined by the colour sequence in the directive

If 0, the colour clrGreen is taken and if 1, clrRed

And this line is absolutely unnecessary, and the sequence is the opposite of the first.

And this

added for what purpose? It's easier to fill directly.

To fill the colour buffer, I would use this construct

The ?: operator is equivalent to if else but applied more easily

WURRAA, and my soul has sung. Alexey, thanks a lot !!!!!

It all worked, I don't quite understand how it's possible to do without array

color     colors[]={clrRed,clrGreen};

because its description has been deleted. So you can do without extra array here?


 
Sergey Voytsekhovsky:

  • Thank you for your response. If Lubilee had been poking around in other people's code, we'd probably have lost you as an expert, so it's just LUCKY in that respect.
  • About <= or >= I agree, omission, of course one of them must be present, as soon as I run through, but for now it's not a matter of principle.
  • Tried, reassigned one colour buffer, reassigned whole groups, swapped groups in places (above/below) - nothing helps. I will now finish the post and try to make a separate histogram indicator, without lines. If it works, I will try to combine them again. This is a matter of principle. I do not want to leave white spots of incomprehension behind.

My Squeaky is just a miracle, he takes it everywhere and everywhere.

p.s I still haven't learned how to properly attach an image, please excuse me, I'll post screenshots.

Files:
EURUSD.png  52 kb
GBPUSD.png  54 kb
 
Alexey Viktorov:

To fill the colour buffer I would use this construction

The ?: operator is equivalent to if else, but easier to use

Great advice and a great science for the future!!! Went even a little further. Below is the result.

So it was:

if(ExtMainBuffer[i]>=50){
         //cl=1;
         ColorHistogram_2Buffer1[i]=50; 
         ColorHistogram_2Buffer2[i]=ExtMainBuffer[i]; 
         ColorHistogram_2Colors[i]=0;
     Print("ExtMainBuffer[i]=",ExtMainBuffer[i],
           " # ExtSignalBuffer[i]=",ExtSignalBuffer[i],
           " # ColorHistogram_2Buffer1[i]=",ColorHistogram_2Buffer1[i],
           " # ColorHistogram_2Buffer2[i]=",ColorHistogram_2Buffer2[i],
           " # ColorHistogram_2Colors[i]=",ColorHistogram_2Colors[i]);
         } 
if(ExtMainBuffer[i]<50){
         //cl=0;
         ColorHistogram_2Buffer1[i]=ExtMainBuffer[i]; 
         ColorHistogram_2Buffer2[i]=50; 
         ColorHistogram_2Colors[i]=1;
     Print("ExtMainBuffer[i]=",ExtMainBuffer[i],
           " # ExtSignalBuffer[i]=",ExtSignalBuffer[i],
           " # ColorHistogram_2Buffer1[i]=",ColorHistogram_2Buffer1[i],
           " # ColorHistogram_2Buffer2[i]=",ColorHistogram_2Buffer2[i],
           " # ColorHistogram_2Colors[i]=",ColorHistogram_2Colors[i]);
         } 

So it became, the effectiveness of the approach is obvious!!! Thank you.

      ColorHistogram_2Buffer1[i]=ExtMainBuffer[i] >= 50 ? 50 : ExtMainBuffer[i]; 
      ColorHistogram_2Buffer2[i]=ExtMainBuffer[i] >= 50 ? ExtMainBuffer[i] : 50; 
      ColorHistogram_2Colors [i]=ExtMainBuffer[i] >= 50 ? 0 : 1;
         Print("ExtMainBuffer[i]=",ExtMainBuffer[i],
            " # ExtSignalBuffer[i]=",ExtSignalBuffer[i],
            " # ColorHistogram_2Buffer1[i]=",ColorHistogram_2Buffer1[i],
            " # ColorHistogram_2Buffer2[i]=",ColorHistogram_2Buffer2[i],
            " # ColorHistogram_2Colors[i]=",ColorHistogram_2Colors[i]);
 
Sergey Voytsekhovsky:

Great advice and a great science for the future!!! Went even a little further than that. Below is the result.

So it was:

So it became, the effectiveness of the approach is obvious!!! Thank you.

No efficiency here - three if-else conditions instead of one. Here, it's worse just the way it is...

 
Artyom Trishkin:

There is no efficiency here: there are three if-else conditions instead of one. It's worse here...

Yes, indeed. I hadn't noticed that. It looks better, there are fewer letters.

And there is no way to put three executions (I don't know how to say it correctly) under one operator?: at once?