Magic Number Added To Indicator.

 
I am new to coding and I added a magic number to this Indicator.  I get no errors but when I put the Indicator on 2 charts with 2 different EAs the Indicators TakeProfit Left and StopLoss Left only works with one of the EAs. Any help would be appreciated. 
//+------------------------------------------------------------------+
//|                                                         INFO.mq5 |
//|                              Copyright © 2023, Vladimir Karputov |
//|                      https://www.mql5.com/en/users/barabashkakvn |
//+------------------------------------------------------------------+
/*

I need someone to add some tings to add to my indicator.

I would like Pips Up and Pips Down to change to 0 then there is no trad on.
I would like to display lots that would be traded if I set the percentage of of my account is set.
So if percentage set to say 0.05 percent of my account balance it will show how many lots will be traded.
I would also like StopOut removed from the indicator.
*/
#property copyright "Copyright © 2023, Vladimir Karputov"
#property link      "https://www.mql5.com/en/users/barabashkakvn"
#property version   "1.05"
#property indicator_chart_window
#property indicator_buffers 0
#property indicator_plots   0
//
#include      <Trade/Trade.mqh>
CTrade        trade;
#include <Trade\PositionInfo.mqh>

//---
CPositionInfo  m_position;                   // object of CPositionInfo class
//--- input parameters
input color    InpColor       = clrYellow;     // Color
input color    InpVeriables   = clrRed;      // Veriables
input color    InpTickColor   = clrLimeGreen; // TickColor

input int HitNumber=910;
input int Movement=7;
input int TickNumber=5;
//input int StopOut=2;
input double Percent  = 2; // Percent %
input int     InpMagicNumber       =  44444;         // Magic Number

int   StopLoss  = 1000; // StopLoss in Points


// int StopOut=50;
//---
string         m_prefix       = "INFO_";
int            m_width        = 0;
int            m_height       = 0;
double         m_ask          = 0;
double         m_bid          = 0;
int            m_counter_up   = 0;
int            m_counter_down = 0;
double         m_points      = 0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
//---
   StopLoss = HitNumber;
trade.SetExpertMagicNumber(InpMagicNumber);
   ObjectsDeleteAll(ChartID(),m_prefix,0,OBJ_LABEL);
//---
//
   long chart_id=ChartID();
   int               y=10;                      // Y coordinate
   ENUM_BASE_CORNER  corner=CORNER_RIGHT_UPPER; // chart corner for anchoring
   string            text="Label";              // text
   string            font="Arial";              // font
   int               font_size=-160;              // font size
   color             clr=InpColor;              // color
   double            angle=0.0;                 // text slope
   ENUM_ANCHOR_POINT anchor=ANCHOR_LEFT_UPPER;  // anchor type
   MqlTick tick;
   SymbolInfoTick(Symbol(),tick);
   m_ask=tick.ask;
   m_bid=tick.bid;
   m_points=SymbolInfoDouble(Symbol(),SYMBOL_POINT);
   double Position_Magic_Number = PositionGetInteger(POSITION_MAGIC);
   text="Server time: "+TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS);
   bool text_set_font=TextSetFont(font,font_size);
   TextGetSize(text+"___",m_width,m_height);
//---
   LabelCreate(chart_id,m_prefix+"Server time",0,m_width,y,corner,text,font,font_size/-10,InpColor,angle,anchor);
   y+=m_height+5;
   if(Position_Magic_Number == InpMagicNumber)
   LabelCreate(chart_id,m_prefix+"HitNumber",0,m_width,y,corner,"HitNumber: "+IntegerToString(HitNumber),font,font_size/-10,InpVeriables,angle,anchor);
   y+=m_height+5;
   if(Position_Magic_Number == InpMagicNumber)
   LabelCreate(chart_id,m_prefix+"Movement",0,m_width,y,corner,"Movement: "+IntegerToString(Movement),font,font_size/-10,InpVeriables,angle,anchor);
   y+=m_height+5;
   if(Position_Magic_Number == InpMagicNumber)
   LabelCreate(chart_id,m_prefix+"TickNumber",0,m_width,y,corner,"TickNumber: "+IntegerToString(TickNumber),font,font_size/-10,InpVeriables,angle,anchor);
   y+=m_height+5;
   if(Position_Magic_Number == InpMagicNumber)
 //  LabelCreate(chart_id,m_prefix+"StopOut",0,m_width,y,corner,"StopOut: "+IntegerToString(StopOut),font,font_size/-10,InpVeriables,angle,anchor);
 //  y+=m_height+5;
   
   LabelCreate(chart_id,m_prefix+"Percent",0,m_width,y,corner,"Percent: "+DoubleToString(Percent,2)+"%",font,font_size/-10,InpVeriables,angle,anchor);
   

   // y+=m_height+5;
   // LabelCreate(chart_id,m_prefix+"StopLoss",0,m_width,y,corner,"StopLoss: "+IntegerToString(StopLoss),font,font_size/-10,InpVeriables,angle,anchor);

   y+=m_height+5;
   LabelCreate(chart_id,m_prefix+"MaxLots",0,m_width,y,corner,"Max Lots: "+DoubleToString(0.0,2),font,font_size/-10,InpVeriables,angle,anchor);


   y+=m_height+5;
   LabelCreate(chart_id,m_prefix+"Pips Up",0,m_width,y,corner,"Pips Up: "+IntegerToString(0),font,font_size/-10,InpTickColor,angle,anchor);
   y+=m_height+5;
   LabelCreate(chart_id,m_prefix+"Pips Down",0,m_width,y,corner,"Pips Down: "+IntegerToString(0),font,font_size/-10,InpTickColor,angle,anchor);
   y+=m_height+5;
   LabelCreate(chart_id,m_prefix+"TakeProfit Left",0,m_width,y,corner,"TakeProfit Left: "+IntegerToString(0),font,font_size/-10,InpTickColor,angle,anchor);
   y+=m_height+5;
   LabelCreate(chart_id,m_prefix+"StopLoss Left",0,m_width,y,corner,"StopLoss Left: "+IntegerToString(0),font,font_size/-10,InpTickColor,angle,anchor);
   y+=m_height+5;
   LabelCreate(chart_id,m_prefix+"TakeProfit",0,m_width,y,corner,"TakeProfit: "+IntegerToString(0),font,font_size/-10,InpTickColor,angle,anchor);
   y+=m_height+5;
   LabelCreate(chart_id,m_prefix+"StopLoss",0,m_width,y,corner,"StopLoss: "+IntegerToString(0),font,font_size/-10,InpTickColor,angle,anchor);


//---
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Indicator deinitialization function                              |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
   ObjectsDeleteAll(ChartID(),m_prefix,0,OBJ_LABEL);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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[])
{
   string symbol_name=Symbol();
   if(m_position.SelectByIndex(0)) // selects the position
      symbol_name=m_position.Symbol();
//---
   MqlTick tick;
   SymbolInfoTick(symbol_name,tick);
   string up="*";
   if(m_bid==0.0)
   {
      m_counter_up=0;
      up="0";
   }
   else
   {
      if(tick.bid>m_bid)
      {
         m_counter_up++;
         if(m_counter_up>TickNumber)
            m_counter_up=1;
      }
      up=IntegerToString(m_counter_up);
   }
   string down="*";
   if(m_ask==0.0)
   {
      m_counter_down=0;
      down="0";
   }
   else
   {
      if(tick.ask<m_ask)
      {
         m_counter_down++;
         if(m_counter_down>TickNumber)
            m_counter_down=1;
      }
      down=IntegerToString(m_counter_down);
   }
//
   m_ask    = tick.ask;
   m_bid    = tick.bid;
//
   long chart_id=ChartID();

   int totolPosi = 0;

   for(int i=0; i<PositionsTotal(); i++)
   {
      if(PositionGetTicket(i) && PositionGetString(POSITION_SYMBOL)==Symbol())
      {
         totolPosi++;
      }
   }
//--
   if(totolPosi==0)
   {
      up="0";
      down="0";
   }

   string maxName = m_prefix+"MaxLots";
   double maxLots =0.0;
   if(Percent>0 && Percent<=100 && StopLoss>0)
   {
      double money = Percent*AccountInfoDouble(ACCOUNT_BALANCE) /100;
      double slx = StopLoss*_Point;
      double tv = SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_VALUE);
      double ticksize = SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_SIZE);
      if(tv>0 && ticksize>0)
      {
         maxLots = money/(slx/ticksize *tv);
      }
   }
//---
   LabelTextChange(chart_id,m_prefix + "Server time", "Server time: "+TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS));
   LabelTextChange(chart_id,m_prefix + "Pips Up", "Pips Up:   " + up);
   LabelTextChange(chart_id,m_prefix + "Pips Down", "Pips Down: " + down);

   LabelTextChange(chart_id,m_prefix + "MaxLots", "MaxLots: " + DoubleToString(maxLots,2));
//
   if(m_position.SelectByIndex(0)) // selects the position
   {
      m_points=SymbolInfoDouble(m_position.Symbol(),SYMBOL_POINT);
      if(m_points==0)
         m_points=1;
      //
      double price_current = m_position.PriceCurrent();
      double price_open    = m_position.PriceOpen();
      double stop_loss     = m_position.StopLoss();
      double take_profit   = m_position.TakeProfit();
      //---
      double Position_Magic_Number = PositionGetInteger(POSITION_MAGIC);
      if(m_position.PositionType()==POSITION_TYPE_BUY)

    
      {
         if(take_profit>0.0)
            LabelTextChange(chart_id,m_prefix+"TakeProfit Left","TakeProfit Left: "+DoubleToString((take_profit-price_open)/m_points,0));
         else
            LabelTextChange(chart_id,m_prefix+"TakeProfit Left","TakeProfit Left: "+"0");
         if(stop_loss>0.0)
            LabelTextChange(chart_id,m_prefix+"StopLoss Left","StopLoss Left: "+DoubleToString((price_open-stop_loss)/m_points,0));
         else
            LabelTextChange(chart_id,m_prefix+"StopLoss Left","StopLoss Left: "+"0");
            if(Position_Magic_Number == InpMagicNumber);
      }
      else
      {
         if(take_profit>0.0)
            LabelTextChange(chart_id,m_prefix+"TakeProfit Left","TakeProfit Left: "+DoubleToString((price_open-take_profit)/m_points,0));
         else
            LabelTextChange(chart_id,m_prefix+"TakeProfit Left","TakeProfit Left: "+"0");
         if(stop_loss>0.0)
            LabelTextChange(chart_id,m_prefix+"StopLoss Left","StopLoss Left: "+DoubleToString((stop_loss-price_open)/m_points,0));
         else
            LabelTextChange(chart_id,m_prefix+"StopLoss Left","StopLoss Left: "+"0");
            if(Position_Magic_Number == InpMagicNumber);
      }
       
      if(m_position.PositionType()==POSITION_TYPE_BUY)

    
      {
         if(take_profit>0.0)
            LabelTextChange(chart_id,m_prefix+"TakeProfit","TakeProfit: "+DoubleToString((take_profit-price_current)/m_points,0));
         else
            LabelTextChange(chart_id,m_prefix+"TakeProfit","TakeProfit: "+"0");
         if(stop_loss>0.0)
            LabelTextChange(chart_id,m_prefix+"StopLoss","StopLoss: "+DoubleToString((price_current-stop_loss)/m_points,0));
         else
            LabelTextChange(chart_id,m_prefix+"StopLoss","StopLoss: "+"0");
            if(Position_Magic_Number == InpMagicNumber);
      }
      else
      {
         if(take_profit>0.0)
            LabelTextChange(chart_id,m_prefix+"TakeProfit","TakeProfit: "+DoubleToString((price_current-take_profit)/m_points,0));
         else
            LabelTextChange(chart_id,m_prefix+"TakeProfit","TakeProfit: "+"0");
         if(stop_loss>0.0)
            LabelTextChange(chart_id,m_prefix+"StopLoss","StopLoss: "+DoubleToString((stop_loss-price_current)/m_points,0));
         else
            LabelTextChange(chart_id,m_prefix+"StopLoss","StopLoss: "+"0");
            if(Position_Magic_Number == InpMagicNumber);
      }      
      
      
   }
   else
   {double Position_Magic_Number = PositionGetInteger(POSITION_MAGIC);
      LabelTextChange(chart_id,m_prefix+"TakeProfit Left","TakeProfit Left: "+IntegerToString(0));
      if(Position_Magic_Number == InpMagicNumber);
      LabelTextChange(chart_id,m_prefix+"StopLoss Left","StopLoss Left: "+IntegerToString(0));
      if(Position_Magic_Number == InpMagicNumber);
   }
   

   
//--- return value of prev_calculated for next call
   return(rates_total);
}
//+------------------------------------------------------------------+
//| Create a text label                                              |
//+------------------------------------------------------------------+
bool LabelCreate(const long              chart_ID=0,               // chart's ID
                 const string            name="Label",             // label name
                 const int               sub_window=0,             // subwindow index
                 const int               x=0,                      // X coordinate
                 const int               y=0,                      // Y coordinate
                 const ENUM_BASE_CORNER  corner=CORNER_LEFT_UPPER, // chart corner for anchoring
                 const string            text="Label",             // text
                 const string            font="Arial",             // font
                 const int               font_size=10,             // font size
                 const color             clr=clrRed,               // color
                 const double            angle=0.0,                // text slope
                 const ENUM_ANCHOR_POINT anchor=ANCHOR_LEFT_UPPER,// anchor type
                 const bool              back=false,               // in the background
                 const bool              selection=false,          // highlight to move
                 const bool              hidden=true,              // hidden in the object list
                 const long              z_order=0)                // priority for mouse click
{
//--- reset the error value
   ResetLastError();
//--- create a text label
   if(!ObjectCreate(chart_ID,name,OBJ_LABEL,sub_window,0,0))
   {
      Print(__FUNCTION__,
            ": failed to create text label! Error code = ",GetLastError());
      return(false);
   }
//--- set label coordinates
   ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x);
   ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y);
//--- set the chart's corner, relative to which point coordinates are defined
   ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner);
//--- set the text
   ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);
//--- set text font
   ObjectSetString(chart_ID,name,OBJPROP_FONT,font);
//--- set font size
   ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size);
//--- set the slope angle of the text
   ObjectSetDouble(chart_ID,name,OBJPROP_ANGLE,angle);
//--- set anchor type
   ObjectSetInteger(chart_ID,name,OBJPROP_ANCHOR,anchor);
//--- set color
   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
//--- display in the foreground (false) or background (true)
   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
//--- enable (true) or disable (false) the mode of moving the label by mouse
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);
//--- hide (true) or display (false) graphical object name in the object list
   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);
//--- set the priority for receiving the event of a mouse click in the chart
   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);
//--- successful execution
   return(true);
}
//+------------------------------------------------------------------+
//| Change the label text                                            |
//+------------------------------------------------------------------+
bool LabelTextChange(const long   chart_ID=0,   // chart's ID
                     const string name="Label", // object name
                     const string text="Text")  // text
{
//--- reset the error value
   ResetLastError();
//--- change object text
   if(!ObjectSetString(chart_ID,name,OBJPROP_TEXT,text))
   {
      Print(__FUNCTION__,
            ": failed to change the text! Error code = ",GetLastError());
      return(false);
   }
//--- successful execution
   return(true);
}
//+------------------------------------------------------------------+
 
Ian Macmillan:
I am new to coding and I added a magic number to this Indicator.  I get no errors but when I put the Indicator on 2 charts with 2 different EAs the Indicators TakeProfit Left and StopLoss Left only works with one of the EAs. Any help would be appreciated. 

So you have two EA running using the same indicator.   Are you looking to have different magic numbers for each EA?   Or the same Magic number for both?

You are setting the magic number in the Oninit function, but then calling that same magic number before creating a position.  Maybe try to change where you set the magic number.

trade.SetExpertMagicNumber(InpMagicNumber);


It could be that the EA needs a unique magic number separate from other EAs.   You might need to pass the magic number from the ea to the indicator.


Hope this helps,


chris

 
I have the 2 EAs working with 2 majic numbers. I guess what I need is a magic number for each Indicator, but I am not shure where to put that code.  Yes I need to pass the magic number from the ea to the indicator.  
Reason: