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 / Property | Returns | Description |
|---|---|---|
Trade.Buy(sym, vol, sl?, tp?, comment?, magic?, dev?) | int | Open a market buy order. Returns ticket number (>0) if successful. |
Trade.Sell(sym, vol, sl?, tp?, comment?, magic?, dev?) | int | Open a market sell order. |
Trade.BuyLimit(sym, vol, px, sl?, tp?, cmnt?, mgc?, exp?) | int | Pending buy limit order at specific price. |
Trade.SellLimit(sym, vol, px, sl?, tp?, cmnt?, mgc?, exp?) | int | Pending sell limit order at specific price. |
Trade.BuyStop(sym, vol, px, sl?, tp?, cmnt?, mgc?, exp?) | int | Pending buy stop order. |
Trade.SellStop(sym, vol, px, sl?, tp?, cmnt?, mgc?, exp?) | int | Pending sell stop order. |
Trade.Close(ticket, sym?, dir?, magic?, comment?) | bool | Close a specific open position by ticket or filters. |
Trade.CloseAll(sym?, dir?, magic?, comment?) | bool | Close all matching open positions. |
Trade.ClosePartial(ticket, vol, sym?, comment?) | bool | Partially close an existing position. |
Trade.Modify(ticket, sym?, sl?, tp?, comment?) | bool | Modify SL and/or TP of an open position. |
Trade.ModifyOrder(ticket, sym, vol, px, sl?, tp?, exp?, cmnt?) | bool | Modify a pending order. |
Trade.CancelOrder(ticket, sym?, comment?) | bool | Cancel a specific pending order. |
Trade.CancelAll(sym?, dir?, magic?, type?, comment?) | bool | Cancel 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 / Property | Returns | Description |
|---|---|---|
Position.Count(sym?, magic?, dir?) | int | Total open positions matching filters. |
Position.Exists(sym?, magic?, dir?) | bool | True if at least one matching position exists. |
Position.AvgOpenPrice(sym?, mgc?, dir?) | double | Weighted average entry price of matching positions. |
Position.SetIndex(i) | bool | Set the loop cursor to position index i. Returns false if index invalid. |
Position.Ticket() | int | Ticket of the selected position. |
Position.Direction() | int | Side.Long or Side.Short. |
Position.Volume() | double | Lot size. |
Position.OpenPrice() | double | Entry price. |
Position.SL() | double | Stop loss price (0 if none). |
Position.TP() | double | Take profit price (0 if none). |
Position.Profit() | double | Floating profit/loss. |
Position.Swap() | double | Accumulated swap. |
Position.Commission() | double | Accumulated commission. |
Position.Symbol() | string | Symbol name. |
Position.OpenTime() | datetime | Time position was opened. |
Position.Magic() | int | Magic number. |
Position.Comment() | string | Position 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 / Property | Returns | Description |
|---|---|---|
Order.Count(sym?, magic?, dir?) | int | Total pending orders matching filters. |
Order.SetIndex(i) | bool | Set the loop cursor to pending order index i. |
Order.Ticket() | int | Ticket of the selected order. |
Order.Symbol() | string | Symbol name. |
Order.Type() | int | OrderType constant (Limit/Stop). |
Order.Volume() | double | Lot size. |
Order.Price() | double | Trigger price. |
Order.SL() | double | Stop loss price. |
Order.TP() | double | Take profit price. |
Order.Magic() | int | Magic number. |
Order.Comment() | string | Order comment. |
Deal.* & History.*
| Function / Property | Returns | Description |
|---|---|---|
History.Deals(from?, to?, magic?, sym?) | Deal[] | Fetch closed deals matching filters. |
Deal.Ticket() | int | Deal ticket number. |
Deal.Profit() | double | Realised profit/loss. |
Deal.Volume() | double | Executed lot size. |
Deal.Price() | double | Execution price. |
Deal.Time() | datetime | Execution time. |
Deal.Symbol() | string | Symbol name. |
Deal.Magic() | int | Magic number. |
Deal.Comment() | string | Deal comment. |
Constants
Side constants
Side.LongSide.ShortSide.BuySide.SellOrderType constants
OrderType.BuyLimitOrderType.SellLimitOrderType.BuyStopOrderType.SellStop