Questions from Beginners MQL5 MT5 MetaTrader 5 - page 1344

 
Igor Makanu #:

no way

If you call it programmatically, you can generate a parameter randomly

and you need to run it manually, you will still have to change the input parameter


try to see what is now in the predefined macros, was __COUNT___ and now there is something with random, I have not checked how __COUNT___ works - it turns out static, it has little effect, maybe you can use random for your task

I found __RANDOM__, but I need to compile code to change the value. Or maybe I'm missing something?

#define  MACRO __RANDOM__ 
input ulong MASlow = MACRO; // Период медленной МА

Can it be changed at runtime somehow?

__RANDOM__
 
Компилятор для каждого объявления __RANDOM__ подставляет в код случайное ulong-число.
 
 
isn't a new number on every call?
 
Fast235 #:
isn't there a new number on every call?

No

 
Fast235 #:
isn't a new number on every call?
Compiler, so at compile time.
 
Vitaly Muzichenko #:

No

Well, then the problem is unsolvable.

What is the problem? - You have to try to formulate it differently.

 
Vitaly Muzichenko #:

No

Vitaly, what's all this for? Is it necessary to have > 1 indicator window to get a window number and put something in it?
 
Alexey Viktorov #:
Vitaliy, what for? Is it necessary to have > 1 indicator window to get a window number and put something in it?

Yes, that's right.

You need to get the number of the window where the indicator is located. To do this, click the button and get the data

  if(id==CHARTEVENT_OBJECT_CLICK && StringFind(sparam,(string)wndNum+"_butt")!=-1) {
    Alert("Window number:"+(string)wndNum," => Program name:"+prog_name," => Short name:"+short_name);
    ObjectSetInteger(0,sparam,OBJPROP_STATE,false);
  }

mt4 works


mt5 only works in one - the first one installed, copies are ignored


 

It's come to this, there's no way to solve the problem of the week :(

//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2020, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, IgorM"
#property link      "https://www.mql5.com/ru/users/igorm"
#property version   "1.00"
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots   1
//--- plot Label1
#property indicator_label1  "Label1"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
//#property indicator_width1  1

input ulong MASlow = __COUNTER__; // Период медленной МА

//+------------------------------------------------------------------+
int wndNum;
string prog_name,short_name;
double Label1Buffer[];
bool flag=false;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
  prog_name=MQLInfoString(MQL_PROGRAM_NAME);

  int q=0;
  for(int k=1; k<ChartGetInteger(0,CHART_WINDOWS_TOTAL); k++) {
    for(int e=0; e<ChartGetInteger(0,CHART_WINDOWS_TOTAL); e++) {
      if(ChartIndicatorName(0,k,e)==prog_name) {
        q++;
      }
    }
  }

  short_name="Set ("+(string)MASlow+")";
  IndicatorSetString(INDICATOR_SHORTNAME,short_name);
  wndNum=ChartWindowFind();
  
  wndNum=q;
  
  ButtonCreate(0,(string)wndNum+"_butt",44,18,40,14,CORNER_RIGHT_LOWER,"= "+(string)wndNum+" =","Alert","Arial",9,clrBlack,C'236,233,216',clrGray,false,false,false);
  
  SetIndexBuffer(0,Label1Buffer);
  PlotIndexSetInteger(0,PLOT_LINE_WIDTH,q+1);

  flag=true;

  Print("INIT SUCCEEDED: "+(string)wndNum," => Count: ",q);
//---
  return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
  ObjectsDeleteAll(0,(string)wndNum+"_butt");
  ChartRedraw();
}

//+------------------------------------------------------------------+
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[])
{
  for(int i = prev_calculated == 0 ? 0 : prev_calculated - 1; i < rates_total; i++) {
    Label1Buffer[i] = close[i];
  }
  if(flag==false) OnInit();
  return(rates_total);
}

//+------------------------------------------------------------------+
//| OnChartEvent function                                            |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{
  if(id==CHARTEVENT_OBJECT_CLICK && StringFind(sparam,(string)wndNum+"_butt")!=-1) {
    Alert("Window number:"+(string)wndNum," => Program name:"+prog_name," => Short name:"+short_name);
    ObjectSetInteger(0,sparam,OBJPROP_STATE,false);
  }
  if(id==CHARTEVENT_CHART_CHANGE) {
    ButtonCreate(0,(string)wndNum+"_butt",44,18,40,14,CORNER_RIGHT_LOWER,"= "+(string)wndNum+" =","Alert","Arial",9,clrBlack,C'236,233,216',clrGray,false,false,false);
    OnInit();
  }
}

//===============================================================================================
//--------------------------------------- Создает кнопку ---------------------------------------+
//===============================================================================================
void ButtonCreate(long chart_ID=0,string nm="Button",int xd=0,int yd=0,int width=50,int height=18,int cr=0,
                  string text="Button",string tooltip="Button",string font="Arial",int font_size=9,color clr=clrBlack,
                  color back_clr=C'236,233,216',color border_clr=clrNONE,bool state=false,bool selection=true,bool selectable=true,bool bc=false)
{
  if(ObjectFind(chart_ID,nm)<0) {
    ObjectCreate(chart_ID,nm,OBJ_BUTTON,wndNum,0,0);
    ObjectSetInteger(chart_ID,nm,OBJPROP_XSIZE,width);
    ObjectSetInteger(chart_ID,nm,OBJPROP_YSIZE,height);
    ObjectSetInteger(chart_ID,nm,OBJPROP_CORNER,cr);
    ObjectSetString(chart_ID,nm,OBJPROP_TEXT,text);
    ObjectSetString(chart_ID,nm,OBJPROP_TOOLTIP,tooltip);
    ObjectSetString(chart_ID,nm,OBJPROP_FONT,font);
    ObjectSetInteger(chart_ID,nm,OBJPROP_FONTSIZE,font_size);
    ObjectSetInteger(chart_ID,nm,OBJPROP_COLOR,clr);
    ObjectSetInteger(chart_ID,nm,OBJPROP_BGCOLOR,back_clr);
    ObjectSetInteger(chart_ID,nm,OBJPROP_BORDER_COLOR,border_clr);
    ObjectSetInteger(chart_ID,nm,OBJPROP_BACK,bc);
    ObjectSetInteger(chart_ID,nm,OBJPROP_STATE,state);
    ObjectSetInteger(chart_ID,nm,OBJPROP_SELECTABLE,selectable);
    ObjectSetInteger(chart_ID,nm,OBJPROP_SELECTED,selection);
    ObjectSetInteger(chart_ID,nm,OBJPROP_HIDDEN,false);
    ObjectSetInteger(chart_ID,nm,OBJPROP_YDISTANCE,yd);
    ObjectSetInteger(chart_ID,nm,OBJPROP_ZORDER,10);
    ObjectSetInteger(chart_ID,nm,OBJPROP_XDISTANCE,xd);
    ChartRedraw();
  }
}
//+------------------------------------------------------------------+
 
Vitaly Muzichenko #:

Yes, that's right.

You need to get the number of the window where the indicator is located. To do this, click the button and get the data

mt4 works


mt5 only works in one - the first one installed, it ignores copies


Well, apparently this is a consequence of increased productivity ... Why recalculate the indicator 100500 times at the whim of not very clever user? But it just so happens that some features suffer from protection.

Probably need to enter an external parameter in the short name of the indicator.

input int MAFast = 8;  // Период быстрой МА
input string shortName = "1";  // Префикс…
string short_name = "";
/********************************************************************\
|           Custom indicator initialization function                 |
\********************************************************************/
int OnInit()
 {
//--- indicator buffers mapping
  short_name = shortName+MQLInfoString(MQL_PROGRAM_NAME) + "=Set (" + (string)MASlow + "/" + (string)MAFast + ")";
  IndicatorSetString(INDICATOR_SHORTNAME, short_name);
  SetIndexBuffer(0, Label1Buffer, INDICATOR_DATA);
  Print("INIT_SUCCEEDED");
  return(INIT_SUCCEEDED);
 }/******************************************************************/

It works this way. Tested.

 
Alexey Viktorov #:

Well, apparently this is a consequence of increased performance... Why recalculate the indicator 100500 times at the whim of a not-so-smart user? But it just so happens that some features suffer from protection.

Probably need to enter an external parameter in the short name of the indicator.

It works this way. Tested.

Again, need to change the prefix when loading the next copy?

Reason: