Docs / Trade Language

Trade Language

Direct access to order execution, live position management, pending orders, and full closed-trade history. All functions are available inside Tick() blocks only.


Trade.*

Function / PropertyReturnsDescription
Trade.Buy(sym, vol, sl?, tp?, comment?, magic?, dev?)intOpen a market buy order. Returns ticket number (>0) if successful.
Trade.Sell(sym, vol, sl?, tp?, comment?, magic?, dev?)intOpen a market sell order.
Trade.BuyLimit(sym, vol, px, sl?, tp?, cmnt?, mgc?, exp?)intPending buy limit order at specific price.
Trade.SellLimit(sym, vol, px, sl?, tp?, cmnt?, mgc?, exp?)intPending sell limit order at specific price.
Trade.BuyStop(sym, vol, px, sl?, tp?, cmnt?, mgc?, exp?)intPending buy stop order.
Trade.SellStop(sym, vol, px, sl?, tp?, cmnt?, mgc?, exp?)intPending sell stop order.
Trade.Close(ticket, sym?, dir?, magic?, comment?)boolClose a specific open position by ticket or filters.
Trade.CloseAll(sym?, dir?, magic?, comment?)boolClose all matching open positions.
Trade.ClosePartial(ticket, vol, sym?, comment?)boolPartially close an existing position.
Trade.Modify(ticket, sym?, sl?, tp?, comment?)boolModify SL and/or TP of an open position.
Trade.ModifyOrder(ticket, sym, vol, px, sl?, tp?, exp?, cmnt?)boolModify a pending order.
Trade.CancelOrder(ticket, sym?, comment?)boolCancel a specific pending order.
Trade.CancelAll(sym?, dir?, magic?, type?, comment?)boolCancel all matching pending orders.
Order executionTDSL
string sym = Symbol(); double point = Symbol.Point(); double ask = Symbol.Ask();
double sl = Symbol.NormalizePrice(ask - 150 * point);
double tp = Symbol.NormalizePrice(ask + 300 * point);
int ticket = Trade.Buy(sym, 0.1, sl, tp, "Entry", 42000);
if (ticket > 0) { Print("Opened: " + ToString(ticket)); }
// Modify stop
Trade.Modify(ticket, sym, Symbol.Bid() - 50*point, tp);
// Close all
Trade.CloseAll(sym, Side.Long);

Position.*

Note
Always call Position.SetIndex(i) at the top of each loop iteration before reading any other Position.* property.
Function / PropertyReturnsDescription
Position.Count(sym?, magic?, dir?)intTotal open positions matching filters.
Position.Exists(sym?, magic?, dir?)boolTrue if at least one matching position exists.
Position.AvgOpenPrice(sym?, mgc?, dir?)doubleWeighted average entry price of matching positions.
Position.SetIndex(i)boolSet the loop cursor to position index i. Returns false if index invalid.
Position.Ticket()intTicket of the selected position.
Position.Direction()intSide.Long or Side.Short.
Position.Volume()doubleLot size.
Position.OpenPrice()doubleEntry price.
Position.SL()doubleStop loss price (0 if none).
Position.TP()doubleTake profit price (0 if none).
Position.Profit()doubleFloating profit/loss.
Position.Swap()doubleAccumulated swap.
Position.Commission()doubleAccumulated commission.
Position.Symbol()stringSymbol name.
Position.OpenTime()datetimeTime position was opened.
Position.Magic()intMagic number.
Position.Comment()stringPosition comment.
Position loop patternTDSL
for (int i = 0; i < Position.Count(); i = i + 1) {
  Position.SetIndex(i);
  int dir = Position.Direction(); double profit = Position.Profit();
  int ticket = Position.Ticket(); string sym = Position.Symbol();
  double op = Position.OpenPrice(); double tp = Position.TP();

  if (dir == Side.Long && profit > 100.0) {
    Trade.Modify(ticket, sym, op, tp); // break-even
  }
}

Order.*

Function / PropertyReturnsDescription
Order.Count(sym?, magic?, dir?)intTotal pending orders matching filters.
Order.SetIndex(i)boolSet the loop cursor to pending order index i.
Order.Ticket()intTicket of the selected order.
Order.Symbol()stringSymbol name.
Order.Type()intOrderType constant (Limit/Stop).
Order.Volume()doubleLot size.
Order.Price()doubleTrigger price.
Order.SL()doubleStop loss price.
Order.TP()doubleTake profit price.
Order.Magic()intMagic number.
Order.Comment()stringOrder comment.

Deal.* & History.*

Function / PropertyReturnsDescription
History.Deals(from?, to?, magic?, sym?)Deal[]Fetch closed deals matching filters.
Deal.Ticket()intDeal ticket number.
Deal.Profit()doubleRealised profit/loss.
Deal.Volume()doubleExecuted lot size.
Deal.Price()doubleExecution price.
Deal.Time()datetimeExecution time.
Deal.Symbol()stringSymbol name.
Deal.Magic()intMagic number.
Deal.Comment()stringDeal comment.

Constants

Side constants
Side.LongSide.ShortSide.BuySide.Sell
OrderType constants
OrderType.BuyLimitOrderType.SellLimitOrderType.BuyStopOrderType.SellStop