Unique Magic Number based on Currency Pairs

 

Hello,


I am trying to add Automatic Unique Magic number based on Currency pairs for my EA.


Code 1 :


//+------------------------------------------------------------------+
//|                                                      UniqueNumber |
//|                        Copyright 2024, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property strict

// Function to calculate the sum of ASCII values of all characters in the symbol
int automagic_CalculateSymbolASCII(string automagic_symbol) {
    int automagic_sum = 0;

    // Calculate the sum of ASCII values of all characters in the symbol
    for (int i = 0; i < StringLen(automagic_symbol); i++) {
        automagic_sum += (int)StringGetChar(automagic_symbol, i);
    }

    return automagic_sum;
}

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit() {
    // Get the symbol of the current chart


    return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int automagic_reason) {
}

//+------------------------------------------------------------------+
//| Expert tick function                                            |
//+------------------------------------------------------------------+
void OnTick() {

    string automagic_symbol = Symbol();

    // Calculate the unique number based on the sum of ASCII values of all characters in the symbol
    int automagic_uniqueNumber = automagic_CalculateSymbolASCII(automagic_symbol);

    // Display the generated unique number as a comment
    Comment("Unique Number for ", automagic_symbol, ": ", automagic_uniqueNumber);
}


Code 2 :


//+------------------------------------------------------------------+
//|                                                     UniqueNumber |
//|                        Copyright 2024, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property strict

// Function to generate a unique number based on symbol limited to 5 digits
int GenerateUniqueNumber(string symbol) {
    int uniqueNumber = 0;

    // Considering the ASCII values of multiple characters from the symbol
    for (int i = 0; i < MathMin(StringLen(symbol), 5); i++) {
        uniqueNumber += (int)StringGetChar(symbol, i);
    }

    // Limit the unique number to 5 digits
    uniqueNumber %= 100000;

    return uniqueNumber;
}

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit() {
    // Get the symbol of the current chart
    string symbol = Symbol();

    // Generate a unique number based on the symbol
    int uniqueNumber = GenerateUniqueNumber(symbol);

    // Display the generated unique number as a comment
    Comment("Unique Number for ", symbol, ": ", uniqueNumber);

    return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason) {
}

//+------------------------------------------------------------------+
//| Expert tick function                                            |
//+------------------------------------------------------------------+
void OnTick() {
}
 
anuj71:

Hello,


I am trying to add Automatic Unique Magic number based on Currency pairs for my EA.


Code 1 :



Code 2 :


Suggestion to this task:

Prerequisites:
Symbols are limited to max 8 chars.
If that's not given, some more arithmetic is required. A maximum of 10 chars can be possible, maybe even 12, if limited to capital letters only.

Use StringToCharArray to convert the string to a char array.

Use a union to convert the char array to a ulong value.

Use the ulong value from the union for your Magic Number.

Another approach could be to calculate a crc64 value from the string, this way you could encode more than just the symbol name, like the timeframe, account number or what ever you want to make the magic number be unique.
 
anuj71: I am trying to add Automatic Unique Magic number based on Currency pairs for my EA.
Unnecessary.

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.
     If trading multiple timeframes, and filter by symbol requires use a range of MN (base plus timeframe).
          Why are MT5 ENUM_TIMEFRAMES strange? - General - MQL5 programming forum - Page 2 #11 (2020)