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

 
There is a need to hide objects according to a criterion. How can I hide objects on a graph (mainly lines)? Objects can be selected by prefix.
 
Seric29:
There is a need to hide objects according to a criterion. How can I hide objects on a graph (mainly lines)? The objects can be selected by prefix.

OBJPROP_TIMEFRAMES helps to do it. But not all in a bunch, but in a loop one by one.

 
Alexey Viktorov:

OBJPROP_TIMEFRAMES would help with that. But not all in a bunch, but in a loop one at a time.

I will do so, I want a period-by-period mapping and will experiment, thanks for the advice.

 

How will the program run faster?

1. var.- if you write universal functions. In this case, the number of checks increases, but the amount of code decreases, because in the end there are fewer functions, less initialized variables, but there is a disadvantage mentioned earlier.

The 2nd variant is if we write more functions that will perform less complex calculations. In this case, the amount of code increases, more variables are initialized and the compiler will have to run code idly often to find the right function, but there is a plus, the number of checks decreases.

Who thinks about it?

 
Seric29:

How will the program run faster?

1. var.- if you write universal functions. In this case, the number of checks increases, but the amount of code decreases, because in the end there are fewer functions, less initialized variables, but there is a disadvantage mentioned earlier.

The 2nd variant is if we write more functions that will perform less complex calculations. In this case, the amount of code increases, more variables are initialized and the compiler will have to run code idly often to find the right function, but there is a plus, the number of checks decreases.

Who thinks about it?

Option 2, lots of small functions, but you're 2 months in the dark about the difference between compiler and interpreter, the compiler doesn't run code idly, it creates tables of links to functions, variables, constants... and then uses those tables to jump in at runtime

here readhttps://habr.com/ru/company/intel/blog/143446/

the developers wrote the same thing here somewhere search the threadhttps://www.mql5.com/ru/forum/304239/page36#comment_11049194


SZZ: here's how the compiler workshttps://habr.com/ru/sandbox/114988/

Делиться не всегда полезно: оптимизируем работу с кэш-памятью
Делиться не всегда полезно: оптимизируем работу с кэш-памятью
  • habr.com
Делиться с ближним своим для нас, божьих тварей, это очень характерно, считается добродетелью, и вообще, как утверждает первоисточник, положительно отражается на карме. Однако в мире, созданном архитекторами микропроцессоров, такое поведение не всегда приводит к хорошим результатам, особенно если это касается разделения памяти между потоками...
 
Alexey Viktorov:

It's just amazing. How can you read documentation to see DRAW_HISTOGRAM2 and not see DRAW_COLOR_HISTOGRAM2

Oh, and there are extra buffers declared.

Good evening. Tried for a long time and in vain. The histogram draws correctly, but the coloring in different colours (above and below level 50) has not won. Please tell me where I screwed up. Text below, file is linked.

//+------------------------------------------------------------------+
//|                                       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[];
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);
   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);
////--- установим пустое значение для HISTOGRAM2
//   PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,0);   
//--- 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;
   
   //PlotIndexSetInteger(2,PLOT_LINE_COLOR,0);
   
//--- 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=0;
         ColorHistogram_2Buffer1[i]=50; 
         ColorHistogram_2Buffer2[i]=ExtMainBuffer[i]; 
         ColorHistogram_2Colors[i]=colors[cl];
         } 
      if(ExtMainBuffer[i]<50){
         cl=1;
         ColorHistogram_2Buffer1[i]=ExtMainBuffer[i]; 
         ColorHistogram_2Buffer2[i]=50; 
         ColorHistogram_2Colors[i]=colors[cl];
         } 
     }
//--- 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.
   return(rates_total);
  }
//+------------------------------------------------------------------+ 

 
Sergey Voytsekhovsky:

Good evening. Tried for a long time and in vain. The histogram draws correctly, but the coloring in different colours (above and below level 50) has not won. Please tell me where I screwed up. Text below, file is linked.


In debug mode I was looking through the values step by step:

  • ExtMainBuffer[i]
  • cl
  • ColorHistogram_2Buffer1[i]
  • ColorHistogram_2Buffer2[i]
  • ColorHistogram_2Colors[i]
It seems to get everything right, but the bar graphs are only green, those below 50 are not coloured red.
 
Sergey Voytsekhovsky:

In debug mode, looked through the values step by step:

  • ExtMainBuffer[i]
  • cl
  • ColorHistogram_2Buffer1[i]
  • ColorHistogram_2Buffer2[i]
  • ColorHistogram_2Colors[i]
It seems to count correctly, but histograms are only green, those below 50 are not coloured red.
 
Sergey Voytsekhovsky:

In debug mode, looked through the values step by step:

  • ExtMainBuffer[i]
  • cl
  • ColorHistogram_2Buffer1[i]
  • ColorHistogram_2Buffer2[i]
  • ColorHistogram_2Colors[i]
It seems to get everything right, but histograms are only green, those below 50 are not colored in red.
Take a look at the code
Normalized_Volume
Normalized_Volume
  • www.mql5.com
Индикатор Normalized Volume - индикатор нормализованного объёма. Отображает средний объём в диапазоне баров в виде цветной гистограммы с пороговым уровнем. Имеет два настраиваемых параметра:
 
Igor Makanu:

but you're on the pull ...

Well that's understandable I just figuratively called the program a compiler, but in general in which case would it be better?

Reason: