Adding MagicNumber easy way? not like this

 

I'm working the course of Jim Hodges at the moment he is teaching how to use magicNumbers, my question is there a better way to do it or is this the normal way(generating Magicnumber)?


//+------------------------------------------------------------------+
//|                                                    blablabla.mq4 |
//|                        Copyright 2021, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#include <stderror.mqh>
#include <stdlib.mqh>
#include <stderror.mqh>
#include <stdlib.mqh>

/*

*/

extern double StopLoss=20;
extern double TakeProfit=50;
extern int    FastEma = 5;
extern int    SlowEma = 21;
input double  LotSize=0.01;
extern int    Slippage=30;
extern int    MagicSeed=1000;

input string  Password = "HolyGrail";//Please Enter Your Password.
double pips;

int magic=0;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   magic = MagicNumberGenerator();
   Print("MagicNumber is ",magic);
   HideTestIndicators(false);
   pips =Point; //.00001 or .0001. .001 .01.
   if(Digits==3||Digits==5)
   pips*=10;
   #include<InitChecks.mqh>
   Comment("Expert Loaded Successfully");
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   Comment(" ");
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   CheckForSignal();
  }

//+------------------------------------------------------------------+
//| Trigger Function                                                 |
//+------------------------------------------------------------------+
void CheckForSignal(){
     static datetime candletime = 0;
      if(candletime != Time[0]){
         double upArrow = iCustom(NULL,0,"NEWMaCrossSignal",0,FastEma,0,1,0,0,4,0,0,SlowEma,0,1,0,0,4,0,3,1);
         if(upArrow != EMPTY_VALUE)
         EnterTrade(OP_BUY);
      
         double downArrow = iCustom(NULL,0,"NEWMaCrossSignal",0,FastEma,0,1,0,0,4,0,0,SlowEma,0,1,0,0,4,0,2,1);
         if(downArrow != EMPTY_VALUE)
         EnterTrade(OP_SELL);
      candletime=Time[0];
   }
}
//+------------------------------------------------------------------+
//| Trade Placing Function                                           |
//+------------------------------------------------------------------+
void EnterTrade(int type){

   int err=0;
   double price=Bid, sl=0, tp=0;
   if(type == OP_BUY)
      price =Ask;
      
   int ticket =  OrderSend(Symbol(),type,LotSize,price,Slippage,0,0,"MAEA Trade",magic,0,Magenta); 
   if(ticket>0){
      if(OrderSelect(ticket,SELECT_BY_TICKET)){
         sl = OrderOpenPrice()+(StopLoss*pips);
         tp = OrderOpenPrice()-(TakeProfit*pips);
         if(OrderType()==OP_BUY){
            sl = OrderOpenPrice()-(StopLoss*pips);
            tp = OrderOpenPrice()+(TakeProfit*pips);
         }
         if(!OrderModify(ticket,price,sl,tp,0,Magenta)) { //not true is true
            err = GetLastError();
            Print("Encountered an error during modification!"+(string)err+" "+ErrorDescription(err)  );
         }
      }
      else{//in case it fails to select the order for some reason 
         Print("Failed to Select Order ",ticket);
         err = GetLastError();
         Print("Encountered an error while seleting order "+(string)ticket+" error number "+(string)err+" "+ErrorDescription(err)  );
      }
   }
   else{//in case it fails to place the order and send us back a ticket number.
      err = GetLastError();
      Print("Encountered an error during order placement!"+(string)err+" "+ErrorDescription(err)  );
      if(err==ERR_TRADE_NOT_ALLOWED)MessageBox("You can not place a trade because \"Allow Live Trading\" is not checked in your options. Please check the \"Allow Live Trading\" Box!","Check Your Settings!");
   }
}

//+------------------------------------------------------------------+
//| Magicnumber generating Function                                  |
//+------------------------------------------------------------------+
int MagicNumberGenerator()
  {
   string mySymbol=StringSubstr(_Symbol,0,6); 
   int pairNumber=0;
   int GeneratedNumber=0;
   if(mySymbol=="AUDCAD")           pairNumber=1;
   else if(mySymbol == "AUDCHF")    pairNumber=2;
   else if(mySymbol == "AUDJPY")    pairNumber=3;
   else if(mySymbol == "AUDNZD")    pairNumber=4;
   else if(mySymbol == "AUDUSD")    pairNumber=5;
   else if(mySymbol == "CADCHF")    pairNumber=6;
   else if(mySymbol == "CADJPY")    pairNumber=7;
   else if(mySymbol == "CHFJPY")    pairNumber=8;
   else if(mySymbol == "EURAUD")    pairNumber=9;
   else if(mySymbol == "EURCAD")    pairNumber=10;
   else if(mySymbol == "EURCHF")    pairNumber=11;
   else if(mySymbol == "EURGBP")    pairNumber=12;
   else if(mySymbol == "EURJPY")    pairNumber=13;
   else if(mySymbol == "EURNZD")    pairNumber=14;
   else if(mySymbol == "EURUSD")    pairNumber=15;
   else if(mySymbol == "GBPAUD")    pairNumber=16;
   else if(mySymbol == "GBPCAD")    pairNumber=17;
   else if(mySymbol == "GBPCHF")    pairNumber=18;
   else if(mySymbol == "GBPJPY")    pairNumber=19;
   else if(mySymbol == "GBPNZD")    pairNumber=20;
   else if(mySymbol == "GBPUSD")    pairNumber=21;
   else if(mySymbol == "NZDCAD")    pairNumber=22;
   else if(mySymbol == "NZDJPY")    pairNumber=23;
   else if(mySymbol == "NZDCHF")    pairNumber=24;
   else if(mySymbol == "NZDUSD")    pairNumber=25;
   else if(mySymbol == "USDCAD")    pairNumber=26;
   else if(mySymbol == "USDCHF")    pairNumber=27;
   else if(mySymbol == "USDJPY")    pairNumber=28;
   else if(mySymbol == "XAGUSD")    pairNumber=29;
   else if(mySymbol == "XAUUSD")    pairNumber=30;
   else if(mySymbol == "SPX500")    pairNumber=31;
   else if(mySymbol == "AUS200")    pairNumber=32;
   else mySymbol=StringSubstr(mySymbol,0,5);
   if(mySymbol      == "GER30")     pairNumber=33;
   else if(mySymbol == "FRA40")     pairNumber=34;
   else mySymbol=StringSubstr(mySymbol,0,4);
   if(mySymbol == "US30")      pairNumber=35;
   GeneratedNumber=MagicSeed+(pairNumber*1000)+_Period;
   return(GeneratedNumber);
  }
//+------------------------------------------------------------------+
 
noSkill06s: my question is there a better way to do it or is this the normal way(generating Magicnumber)?

Magic number allows the EA to identify its orders.

You only need one per strategy per timeframe.

You do not need one per symbol; that is part of your Order Select loop filtering.

There is no need to generate them. Simply make sure all different EAs use different ones.

 
William Roeder:

Magic number allows the EA to identify its orders.

You only need one per strategy per timeframe.

You do not need one per symbol; that is part of your Order Select loop filtering.

There is no need to generate them. Simply make sure all different EAs use different ones.

ok thx

 
William Roeder:

Magic number allows the EA to identify its orders.

You only need one per strategy per timeframe.

You do not need one per symbol; that is part of your Order Select loop filtering.

There is no need to generate them. Simply make sure all different EAs use different ones.

one more question Sir, do I need to use for every order a different MagicNumber?

Example:  EURUSD M5 Order1 magic: 123456

EURUSD M5 Order2 magic: 123456


or

Example:  EURUSD M5 Order1 magic: 123456

EURUSD M5 Order2 magic: 654321


does MetaTrader have randomNumber generator?

 
noSkill06s: one more question Sir, do I need to use for every order a different MagicNumber?

No, not needed as each order gets a unique ticket number that you can track! You can continue to use same magic number for all orders irrespective of symbol, for a particular EA working on a particular time-frame.

  • different Symbol = same magic number (as long as your code checks for Symbol name as well)
  • different Time-frame = different magic number
  • different EA = different magic number

 
noSkill06s: does MetaTrader have randomNumber generator?

Yes, MathRand(). Please read the documentation!

EDIT: However, I DO NOT recommend generating random magic number as you will end up having a different Magic each time an EA starts and then you will have a much more difficult task when you want to pick up with trades that were left stranded if the terminal or EA crashes and needs to restart.

And you will also have a very difficult time keeping track of all magic numbers in order to produce reports and analysis of your trading.

Use fixed magic number as per your requirements and keep a hard record of how they are used, per EA per time-frame.

MathRand - Math Functions - MQL4 Reference
MathRand - Math Functions - MQL4 Reference
  • docs.mql4.com
MathRand - Math Functions - MQL4 Reference
 
Fernando Carreiro:

No, not needed as each order gets a unique ticket number that you can track! You can continue to use same magic number for all orders irrespective of symbol, for a particular EA working on a particular time-frame.

  • different Symbol = same magic number (as long as your code checks for Symbol name as well)
  • different Time-frame = different magic number
  • different EA = different magic number

Thank you so much Sir that was all I need to know 

 
Fernando Carreiro:

Yes, MathRand(). Please read the documentation!

EDIT: However, I DO NOT recommend generating random magic number as you will end up having a different Magic each time an EA starts and then you will have a much more difficult task when you want to pick up with trades that were left stranded if the terminal or EA crashes and needs to restart.

And you will also have a very difficult time keeping track of all magic numbers in order to produce reports and analysis of your trading.

Use fixed magic number as per your requirements and keep a hard record of how they are used, per EA per time-frame.

thx for the function name "mathrand()"