GetOrderMagicNumber

Returns the magic number of the currently selected order.

Syntax

GetOrderMagicNumber(): number

Return Value

Returns a number representing the magic number assigned to the currently selected order.

Description

The GetOrderMagicNumber method returns the magic number (also known as magic ID or expert ID) of the currently selected order. Magic numbers are unique identifiers used to distinguish orders placed by different expert advisors, strategies, or trading systems.

Using magic numbers allows you to identify which orders were placed by your specific strategy, even when multiple strategies are running simultaneously on the same account. This is particularly useful when implementing complex trading systems with multiple entry and exit rules.

Before using this method, you must first select an order using the SelectOrder method.

Example

// Define a magic number for this strategy
const STRATEGY_MAGIC_NUMBER = 12345;

// Select an order by ticket number
if (this.api.SelectOrder(67890, EOrderSelectMode.SELECT_ORDER_BY_TICKET)) {
  // Get the order's magic number
  const magicNumber = this.api.GetOrderMagicNumber();

  // Check if this order belongs to our strategy
  if (magicNumber === STRATEGY_MAGIC_NUMBER) {
    console.log("Order #67890 was placed by this strategy");
    // Process the order according to the strategy logic
  } else {
    console.log("Order #67890 was placed by a different strategy or manually");
    // Skip this order
  }
}

Notes

  • An order must be selected first using SelectOrder before calling this method.

  • It's a good practice to use different magic numbers for different strategies to avoid confusion when analyzing trading history.

Last updated