Multiple Currency pairs - auto setting magic#

 

I am growing very tired of going to each currency pair chart and setting the magic number each time I compile my code on the test (not talking about tester) I am running. Is there a code snippet out there where I can count all the currency pair charts open with an EA called "bob" (for example) and then assign the magic number to be the last count + 1?

For example, say there are 5 charts open, 3 of them have the Bob EA on them. So when I add the 6th chart and apply "Bob" EA, I want the EA code to recognize that there are 3 current "Bob" EA driven charts so the magic number for the 6th chart will be MagicNumberTotalCount +1.

 

int magicForThisChart=0;



int init()

{

magicForThisChart = GlobalVariableGet("MagicNumberTotalCount"); // value for this chart

GlobalVariableSet("MagicNumberTotalCount", magicForThisChart+1); // update the value for the next chart.

}

 
abstract_mind wrote >>

int magicForThisChart=0;

int init()

{

magicForThisChart = GlobalVariableGet("MagicNumberTotalCount"); // value for this chart

GlobalVariableSet("MagicNumberTotalCount", magicForThisChart+1); // update the value for the next chart.

}

You are awesome!!! Thanks.

 

Ok, so I modified the code to be like this below, I disabled the "Expert Advisors" button, compiled the EA, then enabled the "Expert Advisors" button. Now 2 of the open trades has a magicnumber that is the same as a new one, but the new one is on a different currency pair now. Any suggestions? My next thought is maybe a global variable name that has the currency pair tied to it, but not sure this will work either.

   magicForThisChart = GlobalVariableGet("MagicNumberTotalCount"); // value for this chart
   if (magicForThisChart == 0)
      {
         magicForThisChart = magicnumber; // the default user entry value
         GlobalVariableSet("MagicNumberTotalCount", magicForThisChart+1); // update the value for the next chart.
      }
   else
      {
         magicForThisChart = GlobalVariableGet("MagicNumberTotalCount"); // value for this chart
         GlobalVariableSet("MagicNumberTotalCount", magicForThisChart+1); // update the value for the next chart.
      }
   
   magicnumber = magicForThisChart;
 

Maybe you should put an initial value for the global variable in "terminal->tools->Global Variables".

On the other hand, I offer you this function, which I use for synchronization between EAs. This only works if you use one chart per currency.



int encode(string symbol)
{
int value=0;
for(int i=0;i<StringLen(symbol);i++)
value += i*StringGetChar(symbol,i);
return(value);
}


int start()

{

...

...

magicForThisChart = encode(Symbol())

}

 
The global variable scheme is not good. Once the client restarts, MAGIC will be confused.
 
  1. Joao Rosas #:

    Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum (2019)
              Messages Editor

  2. Larry: I am growing very tired of going to each currency pair chart and setting the magic number each time I compile my code

    Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number/symbol filtering on your OrderSelect / Position select loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum (2013)
              PositionClose is not working - MQL5 programming forum (2020)
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles (2006)
              Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles (2011)
              Limit one open buy/sell position at a time - General - MQL5 programming forum (2022)

    You need one Magic Number for each symbol/timeframe/strategy. Trade current timeframe, one strategy, and filter by symbol requires one MN.

 

When I did something similar, I had a controlling EA which added a chart with a template I'd previously created.

The template held the EA, and also a text object which would hold the magic number (this was so I could loop and find charts that had the EA.) 

The controlling EA would programmatically edit the template file to change the magicno in the EA description and the text object before applying the template it to the new chart.

From memory, I just based the MN on the index of a list of symbols and maybe the time.

Reason: