The magic number is a MetaTrader concept used to track the open positions of an expert advisor. The concept allows the EA to distinguish the trades that it opened versus those that it did not.
Every car has a license plate. When you see a car in a different state or even a different country, you know that every plate that you see is unique. The police can look up the number to determine who owns the car.
Magic numbers act like the license plates for EAs. When an expert advisor reads an open trade, called a ticket, it often requests its magic number. If the magic number of the ticket matches the number that the expert advisor expects, then it knows to manage the trade.
Magic numbers are useful, particularly when you want to trade multiple time frames of the same currency pair. Traders often use settings that differ on M1 charts versus those that would use on the D1. If they applied the EA with the same magic number on all charts, the result would be chaos. The expert advisor would open and close trades at random. Setting each EA to use a different magic number prevents the robots from interfering with the others.
Magic number facts
The magic number of a manually opened trade is 0.
The value of a max number must be between 0 and 2147483647. The MQL programming language calls that last number EMPTY_VALUE and reserves the name as an integer value.