How to make two Magic Numbers in an Expert Advisor with a condition

 

Hi,

In my EA, i have set the magic number in initialization function as below

trade.SetExpertMagicNumber(Magic);

This magic number will be used for the Buy & Sell trades

Now in the same EA, I want to give another one magic number for Buy Limit & Sell Limit orders as "Magic1"

How to do that?  Someone help plz

 
Either call the method trade.SetExpertMagicNumber(Magic); with the Magic no. you want again in OnTick()
or copy Trade.mqh and re-name it e.g. JustinTrade.mqh (otherwise the next build will probably overwrite all your changes) and amend the methods that create the orders so that you have to hand over even the Magic number.
 
Carl Schreiber #:
Either call the method trade.SetExpertMagicNumber(Magic); with the Magic no. you want again in OnTick()
or copy Trade.mqh and re-name it e.g. JustinTrade.mqh (otherwise the next build will probably overwrite all your changes) and amend the methods that create the orders so that you have to hand over even the Magic number.

Thanks, but if i call again as per this, will that apply the magic number to only buy limit or sell limit orders only  " all the method trade.SetExpertMagicNumber(Magic); with the Magic no. you want again in OnTick()"

 
Sathish Justin #:

Thanks, but if i call again as per this, will that apply the magic number to only buy limit or sell limit orders only  " all the method trade.SetExpertMagicNumber(Magic); with the Magic no. you want again in OnTick()"

Use the other suggestion.
 
Carl Schreiber #:
Use the other suggestion.

Hi,

I tried, it is giving me lot of error as below


error

 

Sathish Justin:
In my EA, i have set the magic number in initialization function as: trade.SetExpertMagicNumber(Magic)

Now in the same EA, I want to give another one magic number for Buy Limit & Sell Limit orders as "Magic1" How to do that?

  1. Ctrade trade;
    Ctrade trade1;
    ⋮
       trade.SetExpertMagicNumber(Magic);
       trade1.SetExpertMagicNumber(Magic1);
  2. Why do you need a second MN? Are you doing something different when it opens?

    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.

 
Sathish Justin #:

Hi,

I tried, it is giving me lot of error as below

You can't use both!

  1. Make a copy,
  2. load your copy: #include "Trade\ JustinTrade .mqh" (or where ever it is located),
  3. start amending carefully and stepwise your copy and compile after each step.
  4. Then run your EA in the debugger and control the behaviour in the debugger:
    https://www.metatrader5.com/en/metaeditor/help/development/debug // Code debugging
    https://www.mql5.com/en/articles/2041 // Error Handling and Logging in MQL5
    https://www.mql5.com/en/articles/272 // Tracing, Debugging and Structural Analysis of Source Code
    https://www.mql5.com/en/articles/35 // scrol down to: "Launching and Debuggin"


Code debugging - Developing programs - MetaEditor Help
Code debugging - Developing programs - MetaEditor Help
  • www.metatrader5.com
MetaEditor has a built-in debugger allowing you to check a program execution step by step (by individual functions). Place breakpoints in the code...