[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
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
IndicatorShortName should be moved to init()
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);
}
//+------------------------------------------------------------------+
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:
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 !
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.
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):
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.