[ARCHIVE!] Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Can't go anywhere without you - 4. - page 132

 
alsu:
IndicatorShortName should be moved to init()
When you open the indicator, the object doesn't appear, you should additionally open the window with indicator properties, close it and then the object will be visible.
 
is win_idx the right one?
 
alsu:
does win_idx get it right?

win_ind turns out to be correct.

ObjectCreate("max",OBJ_HLINE,win_idx,0,top); draws in the correct window,


ObjectSet("max",OBJPROP_COLOR,Yellow); draws in the correct colour,

ObjectSet("max",OBJPROP_WIDTH,0); draws the correct line,

but does not define the top parameter in

double top=WindowPriceMax(win_idx);

and topBuffer[] is empty.


The code

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Lime
#property indicator_color2 Red
//--- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double topBuffer[];


bool initFinished=false; // add a variable that will remember the state of initialization.
// false - initialization has not yet happened.
// true - was initialized
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,ExtMapBuffer1);
SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(1,ExtMapBuffer2);
SetIndexStyle(2,DRAW_NONE);
SetIndexBuffer(2,topBuffer);

IndicatorDigits(5);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int counted_bars=IndicatorCounted(),
limit;

if(counted_bars>0)
counted_bars--;

limit=Bars-counted_bars;
//----
for(int i=0;i<limit;i++)

{
ExtMapBuffer1[i]=iMACD(0,0,12,26,9,PRICE_CLOSE,MODE_MAIN,i);
ExtMapBuffer2[i]=iMACD(0,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,i);

if(initFinished==false)
{
IndicatorShortName("MACD_max-min");

int win_idx=WindowFind("MACD_max-min");
// find the number of our indicator subwindow

if(win_idx<0)
{
// if the subwindow number is -1, there is an error
Print("Window not found");
return(0);
}

double top=WindowPriceMax(win_idx);
//find the maximum value of the vertical scale of the specified sub-window of the current chart


ObjectCreate("max",OBJ_HLINE,win_idx,0,top);
// draw a horizontal line in a subwindow of our indicator

ObjectSet("max",OBJPROP_COLOR,Yellow);
ObjectSet("max",OBJPROP_WIDTH,0);

WindowRedraw();
// redraw the window to see the line

initFinished=true;
// drawing is finished

}

topBuffer[i]=top;


}

//----
return(0);
}
//+------------------------------------------------------------------+

 
MK07:

win_ind turns out to be correct.

ObjectCreate("max",OBJ_HLINE,win_idx,0,top); draws in the correct window,


ObjectSet("max",OBJPROP_COLOR,Yellow); draws in the correct colour,

ObjectSet("max",OBJPROP_WIDTH,0); draws the correct line,

but does not define the top parameter in

double top=WindowPriceMax(win_idx);

and topBuffer[] is empty.


Code

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Lime
#property indicator_color2 Red
//--- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double topBuffer[];


bool initFinished=false; // add a variable that will remember the state of initialization.
// false - initialization has not yet happened.
// true - was initialized
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,ExtMapBuffer1);
SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(1,ExtMapBuffer2);
SetIndexStyle(2,DRAW_NONE);
SetIndexBuffer(2,topBuffer);

IndicatorDigits(5);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int counted_bars=IndicatorCounted(),
limit;

if(counted_bars>0)
counted_bars--;

limit=Bars-counted_bars;
//----
for(int i=0;i<limit;i++)

{
ExtMapBuffer1[i]=iMACD(0,0,12,26,9,PRICE_CLOSE,MODE_MAIN,i);
ExtMapBuffer2[i]=iMACD(0,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,i);

if(initFinished==false)
{
IndicatorShortName("MACD_max-min");

int win_idx=WindowFind("MACD_max-min");
// find the number of our indicator subwindow

if(win_idx<0)
{
// if the subwindow number is -1, there is an error
Print("Window not found");
return(0);
}

double top=WindowPriceMax(win_idx);
//find the maximum value of the vertical scale of the specified sub-window of the current chart


ObjectCreate("max",OBJ_HLINE,win_idx,0,top);
// draw a horizontal line in a subwindow of our indicator

ObjectSet("max",OBJPROP_COLOR,Yellow);
ObjectSet("max",OBJPROP_WIDTH,0);

WindowRedraw();
// redraw the window to see the line

initFinished=true;
// drawing is finished

}

topBuffer[i]=top;


}

//----
return(0);
}
//+------------------------------------------------------------------+



Sorry for meddling.

topBuffer[i] behind the loop and drawing, try this:

int start()
  {
   int counted_bars=IndicatorCounted(),
       limit;  
       
    if(counted_bars>0)
      counted_bars--;
   
      limit=Bars-counted_bars;
//----
   for(int i=0;i<limit;i++)
       
      { 
        ExtMapBuffer1[i]=iMACD(0,0,12,26,9,PRICE_CLOSE,MODE_MAIN,i);
        ExtMapBuffer2[i]=iMACD(0,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,i);
        
          if(initFinished==false)
           { 
             int win_idx=WindowFind("MACD_max-min");
             // находим номер подокна нашего индикатора
      
             if(win_idx<0)
              {
               // если номер подокна равен -1, то возникла ошибка
                 Print("Окно не найдено");
                 return(0);
              }  
              
             double   top=WindowPriceMax(win_idx); 
              //находим максимальное значение вертикальной шкалы указанного подокна текущего графика
             topBuffer[i]=top;
             Comment( "\n"," top = ",top); 

             ObjectCreate("max",OBJ_HLINE,win_idx,0,0);
             // рисуем горизонтальную линию в подокне нашего индикатора
             ObjectSet("max", OBJPROP_PRICE1, top);  
             ObjectSet("max",OBJPROP_COLOR,Yellow);
             ObjectSet("max",OBJPROP_WIDTH,0);
 
             WindowRedraw();      
             // перерисовываем окно, чтобы увидить линиию   
      
             initFinished=true;
            // рисование закончено  
        
           } 
        
            
                
        
      }  
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
 
//Пробуйте с учетом мнения alsu. 
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Lime
#property indicator_color2 Red
double ExtMapBuffer1[],ExtMapBuffer2[];
int init(){SetIndexStyle(0,DRAW_LINE);SetIndexBuffer(0,ExtMapBuffer1);
SetIndexStyle(1,DRAW_LINE);SetIndexBuffer(1,ExtMapBuffer2);
IndicatorShortName("MACD_max-min");return(0);}
int deinit(){return(0);}
//+------------------------------------------------------------------+
int start(){int counted_bars=IndicatorCounted(),limit,win_idx;double top;
if(counted_bars>0)counted_bars--;limit=Bars-counted_bars;win_idx=WindowFind("MACD_max-min");
for(int i=0;i<limit;i++){ExtMapBuffer1[i]=iMACD(0,0,12,26,9,PRICE_CLOSE,MODE_MAIN,i);
  ExtMapBuffer2[i]=iMACD(0,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,i);}
  top=WindowPriceMax(win_idx);ObjectDelete("max");
  if(ObjectFind("max")!=win_idx){ObjectCreate("max",OBJ_HLINE,win_idx,Time[0],top);
  ObjectSet("max",OBJPROP_COLOR,Yellow);ObjectSet("max",OBJPROP_WIDTH,0);}return(0);}
//+------------------------------------------------------------------+
 
Good day please help. i am a newbie and installed MetaFX and expert advisor on the demo all works and does not want to work on the original does not want to just stand
 

Hello !

Please point me in the right direction:

I am given N external variables of the same type. How to choose maximal value from them without overloading code - MathMax returns max of two values, but if its, let's say, 20 ?

Or, how to create an array from this data, which then apply ArrayMaximum?

extern int A = 10;
extern int .......;

extern int N = 1253;

Thanks in advance !

 
Roll:

https://docs.mql4.com/ru/array/ArrayMaximum

int ArrayMaximum( double array[], int count=WHOLE_ARRAY, int start=0)
Search for the element with the maximal value. The function returns the position of the maximal element in the array.
Parameters:
array[] - Numeric array, that is searched.
count - Number of elements to search.
Start - The starting index for searching.
Example:
double num_array[15]={4,1,6,3,9,4,1,6,3,9,4,1,6,3,9};
int maxValueIdx=ArrayMaximum(num_array);
Print("Max value = ", num_array[maxValueIdx]);


But, in the form of

int Max [N] = {A,.....,N};

the array is not created.

Where: A,......N are the given variables of the same type. Number of variables = N

 

Example of creating a string array from external variables (script):

extern string sym1="GBPUSD";
extern string sym2="GBPJPY";
extern string sym3="EURUSD";
extern string sym4="EURJPY";
extern string sym5="AUDUSD";
extern string sym6="AUDJPY";
extern string sym7="NZDUSD";
extern string sym8="USDCAD";

string sym[8];
//+------------------------------------------------------------------+
int init(){sym[1]=sym1;sym[2]=sym2;sym[3]=sym3;sym[4]=sym4;
   sym[5]=sym5;sym[6]=sym6;sym[7]=sym7;sym[8]=sym8;return(0);}

int start(){Alert("символ  ",sym[1]);return(0);}
 
Roll:

Example of creating a string array from external variables (script):


Thank you !

It worked, but with a correction: instead of

int init(){sym[1]=sym1;............

do the following

int init(){sym[0]=sym1;.......

and then all elements are counted.