EA 交易编程

Types of Orders

  • Market Order
  • Pending Order
  • Stop Order

Market Order

  • Enters a position at current price
  • Types
    • Long position(buy)
    • Short position(sell)

Pending Order

  • Enters a position upon hitting a specific price
  • Types
    • Buy Stop - buy at higher price
    • Sell Stop - sell at lower price
    • Buy Limit - buy at lower price
    • Sell Limit - sell at higher price
  • Buy Stop and Sell Stop - trend follower
  • Buy Limit and Sell Limit - trend reversal

Stop Order

  • Exits a position at a specific price
  • Types
    • Stoploss - usually exits in loss
    • Takeprofit - usually exits in profit

Price

  • Two-way quote
  • Types
    • Bid
    • Ask
  • Order Price
    • Entry price
    • Close price

Spread

  • Difference between bid and ask price
  • One of the ways a broker makes money
  • Fluctuates over time

Price Format

  • Point
  • Pip
  • Fractional pip

Point

  • Last significant figure of price
  • Examples
  • 1.2345 → 1 point = 0.0001
  • 1.65 → 1 point = 0.01
  • 25.0 → 1 point = 1.0

Pip

  • Convention in Foreign Exchange
  • Most currencies: 1 pip = 0.0001
  • Yen currency pairs: 1 pip = 0.01
  • 2- or 4-digit brokers
  • Pre-fractional pip pricing: 1 pip = 1 point

Fractional pip

  • Not offered until recently
  • Additional significant figure - greater accuracy in price
  • Most currencies: 1 fractional pip = 0.00001
  • Yen currency pairs: 1 fractional pip = 0.001
  • 3- or 5-digit brokers
  • 1 point = 1 fractional pip = 1/10 of conventional pip
  • 1 pip = 10 points

Long positions

  • Market(buy) and pending(buy stop and buy limit)
  • Ask price - entry
  • Bid price - exit
  • Profit → price goes up after entry

Short positions

  • Market(sell) and pending(buysell stop and sell limit)
  • Bid price - entry
  • Ask price - exit
  • Profit → price goes down after entry

Trade volume

  • Trade size or lot size
  • 1 lot = buying 100,000 units of a commodity
  • Examples
    • 1 lot EUR/USD long - buying 100,000 euros using dollars at the current exchange rate
    • 1 lot EUR/USD short - buying 100,000 dollars using euros at the current exchange rate

Trade volume cont'd

  • Types based on trading account
    • Standard lot - 1 lot = 100,000 units
    • Mini-lot - 1 lot = 10,000 units
    • Micro-lot - 1 lot = 1,000 units
  • Factors
    • Leverage
    • Money management

Slippage

  • Maximum deviation in price
  • Trade is cancelled by robot once slippage is exceeded

Minimum distance

  • Freeze level and stop level
  • |entry price - exit price|(in points) >= minimum distance(in points)
  • Entry price and stoploss/takeprofit

Order Placement

Bid, Ask & Spread

As a Forex trader, you're probably already familiar with the Bid and Ask prices. But you might not be aware of their role in order placement. It is very important to use the correct price when opening or closing orders.

The Bid price is what you see on the MetaTrader charts. It is usually what we think of when we think of the “current price.” The Ask price is generally just a few pips above the Bid price. The difference between the Bid and the Ask is the the spread, which is the broker's commission for placing the order.

The Ask price is where we open buy orders, and close sell orders. The Bid price is where we open sell orders, and close buy orders. You'll need to indicate the correct price when opening a market order, or when closing an order at market, so remember the difference between the two.

Order Types

There are three types of orders that can be placed in MetaTrader: market, stop and limit orders. Market orders are the most common. A market order opens a position immediately at the prevailing Bid or Ask price.

  • market orders 市价单
  • stop orders 止损单
  • limit orders 限价单

When placing a market order in MQL, we must specify an opening price (generally the latest Bid or Ask quote). If the specified opening price is outdated, due to a fast moving market or a delay in program execution, the terminal will attempt to place the order at the current market price, provided it is within the maximum slippage.

If you place a market order using the New Order dialog in MetaTrader, you'll see a setting at the bottom labeled “Enable maximum deviation from quoted price.” When this is checked, you can then specify the maximum deviation in pips. This is the maximum slippage.

If the current price falls outside of our specified opening price, plus or minus the slippage, a requote error will occur and the order will not be placed. You may have noticed this when attempting to place a market order during a fast moving market. Note that ECN/STP brokers do not use a slippage setting, and will always open market orders at the current price.

A stop order is a type of pending order. Pending orders are a request to open a market order at a certain price. A buy stop order is placed above the current price, while a sell stop order is placed below the current price. The expectation is that the price will eventually rise or fall to that level and continue in that direction, resulting in a profit.

A limit order is the opposite of a stop order. A buy limit order is placed below the current price, while a sell limit order is placed above the current price. The expectation is that the price will rise or fall to that level, triggering the order, and then reversing. Limit orders are not used very often in automated trading.

An expiration time can be set for pending orders. If the order is not filled by the expiration time, the order is automatically deleted. Not all brokers support trade expiration.

The Order Placement Process

The process of placing an order in MQL involves several steps. We must determine the following before placing the order:

  • The type of order to be placed – buy or sell; stop, market or limit.
  • The currency pair to trade – generally the chart that the EA is attached to.
  • The lot size. This can either be a fixed lot size, or one that is calculated using a money management routine.
  • The order opening price. For market orders, this will be the current Bid or Ask price. For pending orders, the opening price must be a minimum distance from the current price, and should be above or below the current price as required by the order type.
  • The stop loss price. The stop loss can be a predetermined price, an indicator value, a fixed number of pips from the order opening price, or it can be dynamically calculated using a risk management routine. The stop loss can be placed with the order, or it can be added to the order afterward.
  • The take profit price. This is generally a fixed number of pips from the order opening price, although it can be calculated using other methods as well. The take profit can be placed with the order, or it can be added to the order afterward.
  • Order identifiers such as an order comment, or a “magic number” that identifies an order as being placed by a specific expert advisor.
  • An optional expiration price for pending orders, if the broker supports it. ### OrderSend()

The OrderSend() function is used to place orders in MQL. The syntax is as follows:

int OrderSend(string Symbol, int Type, double Lots, double Price, 
int Slippage, double StopLoss, double TakeProfit, string Comment = NULL, 
int MagicNumber = 0, datetime Expiration = 0, color Arrow = CLR_NONE);

  • Symbol – A string representing the currency pair to trade, for example GBPUSD. The Symbol() function is used for the current chart's currency pair.
  • Type – The type of order to place: buy or sell; market, stop or limit. This is an integer value, represented by the following constants:
    1. OP_BUY – Buy market order (integer value 0).
    2. OP_SELL – Sell market order (integer value 1).
    3. OP_BUYSTOP – Buy stop order (integer value 2).
    4. OP_SELLSTOP – Sell stop order (integer value 3).
    5. OP_BUYLIMIT – Buy limit order (integer value 4).
    6. OP_SELLLIMIT – Sell limit order (integer value 5).
  • Lots – The number of lots to trade. You can specify mini lots (0.1) or micro lots (0.01) if your broker supports it.
  • Price – The price at which to open the order. For a buy market order, this will be the Ask. For a sell market order, this will be the Bid. For pending orders, this will be any valid price that is above or below the current price.
  • Slippage – The maximum slippage in points. Use a sufficiently large setting when auto trading. Brokers that do not use slippage will ignore this parameter.
  • StopLoss – The stop loss price. For a buy order, the stop loss price is below the order opening price, and for a sell order, above. If set to 0, no stop loss will be used.
  • TakeProfit – The take profit price. For a buy order, the take profit is above the order opening price, and for a sell order, below. If set to 0, no take profit will be used.
  • Comment – An optional string that will serve as an order comment. Comments are shown under the Trade tab in the Terminal window. Order comments can also be used as an order identifier.
  • MagicNumber – An optional integer value that will identify the order as being placed by a specific expert advisor. It is highly recommended that you use this.
  • Expiration – An optional expiration time for pending orders. Not all brokers accept trade expiration times – for these brokers, an error will result if an expiration time is specified.
  • Arrow – An optional color for the arrow that will be drawn on the chart, indicating the opening price and time. If no color is specified, the arrow will not be drawn.

The OrderSend() function returns the ticket number of the order that was just placed. If no order was placed, due to an error condition, the return value will be -1.

We can save the order ticket to a global or static variable for later use. If the order was not placed due to an error condition, we can analyze the error and take appropriate action based on the returned error code.

打印/导出