metatrader
Read account, symbols, rates and history, and send trades through a local MetaTrader 5 bridge.
import metatrader
metatrader is a client for a local bridge to a running MetaTrader 5 terminal. The package speaks HTTP and JSON: every call sends a request to the bridge, and the bridge asks the terminal for the data or the trade. There is no DLL, no COM and no terminal plugin inside your program, so the same Salam program runs on Linux, macOS and Windows as long as the bridge is reachable.
The terminal itself must be running and logged in, and a bridge must be listening on the address you set. The default is http://127.0.0.1:18812, the value of DEFAULT_BRIDGE_URL. The bridge implements the endpoints below; the package only calls them and decodes the JSON.
| Endpoint | Used by |
|---|---|
POST /initialize, POST /login, POST /shutdown | Initialize, Login, Shutdown |
GET /version, GET /account_info, GET /terminal_info | Version, GetAccountInfo, GetTerminalInfo |
GET /symbols_total, GET /symbols_get, GET /symbol_info, GET /symbol_info_tick, POST /symbol_select | Symbol calls |
GET /copy_rates_from, GET /copy_rates_from_pos, GET /copy_rates_range | CopyRates* |
GET /copy_ticks_from, GET /copy_ticks_range | CopyTicks* |
GET /orders_total, GET /orders_get, GET /positions_total, GET /positions_get | Order and position reads |
GET /history_orders_*, GET /history_deals_* | History calls |
POST /order_calc_margin, POST /order_calc_profit, POST /order_check, POST /order_send | Trading calls |
POST /market_book_add, GET /market_book_get, POST /market_book_release | Market depth calls |
Start with the account
Set the bridge address once, call Initialize, then read the account. Every call returns a value even when it fails, so check Ok or LastError after the calls that matter, and stop when the bridge is not there.
import metatrader
func main:
metatrader.SetBridgeUrl("http://127.0.0.1:18812")
if not metatrader.Initialize("", 0 as i64, "", "", 60000, false):
e := metatrader.LastError()
printerrln "no bridge:", e.code, e.description
ret 1
end
a := metatrader.GetAccountInfo()
if not metatrader.Ok():
e := metatrader.LastError()
printerrln "account failed:", e.code, e.description
metatrader.Shutdown()
ret 1
end
println a.login, a.name, a.currency
println "balance", a.balance, "equity", a.equity
println "margin", a.margin, "free", a.margin_free
t := metatrader.GetTerminalInfo()
println t.company, t.name, t.build, t.connected
metatrader.Shutdown()
ret 0
endInitialize and Login mark the session connected only when the bridge answers with a 2xx status. IsConnected reports that flag, not a live socket: call Version or GetTerminalInfo when you need to know that the terminal is still there. Shutdown only asks the bridge to close; it does not free anything in your program.
Errors and retcodes
A failed call returns the zero value of its type, an empty vector, an empty struct, or false, and leaves the reason in LastError. Ok is true exactly when the last code is RES_S_OK. The bridge uses the RES_E_* constants for transport and parameter errors, and the trade server uses TRADE_RETCODE_* in the result of OrderCheck and OrderSend.
a := metatrader.GetAccountInfo()
if not metatrader.Ok():
e := metatrader.LastError()
printerrln "account failed:", e.code, e.description
ret 1
end
println a.balance
An HTTP error and a rejected trade are different: the first leaves Ok false, the second is a normal answer with a retcode. Always read retcode after OrderSend; a successful HTTP call does not mean the trade was accepted.
Symbols, prices and ticks
SymbolsGet returns every symbol with its contract rules and session data; the group forms filter with a mask such as *USD*, where * matches any text. GetSymbolInfo asks for one symbol, and SymbolInfoTick is the cheap call for the current bid and ask.
import metatrader
func main:
metatrader.SetBridgeUrl("http://127.0.0.1:18812")
println "symbols:", metatrader.SymbolsTotal()
each s in metatrader.SymbolsGet():
println s.name, s.digits, s.spread, s.trade_mode
end
majors := metatrader.SymbolsGetGroup("*USD*")
println "usd symbols:", majors.len()
info := metatrader.GetSymbolInfo("EURUSD")
println info.bid, info.ask, info.point, info.digits
println info.volume_min, info.volume_step, info.volume_max
println info.trade_stops_level, info.trade_contract_size
tick := metatrader.SymbolInfoTick("EURUSD")
println tick.time, tick.bid, tick.ask, tick.last, tick.volume
if not metatrader.SymbolSelect("EURUSD", true):
printerrln "cannot select EURUSD"
end
ret 0
enddigits and point tell you how to round a price; volume_min, volume_max and volume_step tell you which volumes the server accepts; trade_stops_level is the shortest distance in points between the price and a stop loss or take profit.
Candles and ticks
Rates are returned oldest first. CopyRatesFromPos counts from the newest candle, so position 0 is the candle that is still forming, and CopyRatesFrom starts at a time. Times are seconds since the epoch, as in time; tick times are milliseconds in time_msc, which is what CopyTicksRange expects.
import metatrader
func main:
metatrader.SetBridgeUrl("http://127.0.0.1:18812")
from := metatrader.CopyRatesFrom("EURUSD", metatrader.TIMEFRAME_H1, 1700000000 as i64, 10)
each r in from:
println r.time, r.open, r.high, r.low, r.close, r.tick_volume
end
recent := metatrader.CopyRatesFromPos("EURUSD", metatrader.TIMEFRAME_M5, 0, 5)
println "recent:", recent.len()
window := metatrader.CopyRatesRange("EURUSD", metatrader.TIMEFRAME_D1, 1700000000 as i64, 1700086400 as i64)
println "daily:", window.len()
ticks := metatrader.CopyTicksFrom("EURUSD", 1700000000 as i64, 100, metatrader.COPY_TICKS_ALL)
each t in ticks:
println t.time_msc, t.bid, t.ask, t.last, t.flags
end
trade_ticks := metatrader.CopyTicksRange("EURUSD", 1700000000000 as i64, 1700000600000 as i64, metatrader.COPY_TICKS_TRADE)
println "trade ticks:", trade_ticks.len()
ret 0
endThe tick filters keep the payload small: COPY_TICKS_INFO keeps bid and ask changes, COPY_TICKS_TRADE keeps trades, and COPY_TICKS_ALL keeps both. Check flags on each tick when you need to know which side changed.
Checking and sending an order
Fill a TradeRequest, call OrderCheck, and look at its margin fields before you send anything. Then call OrderSend and read retcode. A check returning 0 means the request is valid; the send returns TRADE_RETCODE_DONE for a filled order, TRADE_RETCODE_PLACED for a pending order, and an error code otherwise.
import metatrader
func main:
metatrader.SetBridgeUrl("http://127.0.0.1:18812")
println "orders:", metatrader.OrdersTotal()
each o in metatrader.OrdersGet():
println o.ticket, o.symbol, o.order_type, o.state, o.volume_current, o.price_open
end
each p in metatrader.PositionsGet():
println p.ticket, p.symbol, p.position_type, p.volume, p.price_open, p.profit
end
mut req := metatrader.TradeRequest {}
req.action = metatrader.TRADE_ACTION_DEAL
req.symbol = "EURUSD"
req.volume = 0.10
req.price = 1.0821
req.order_type = metatrader.ORDER_TYPE_BUY
req.type_filling = metatrader.ORDER_FILLING_IOC
req.type_time = metatrader.ORDER_TIME_GTC
req.deviation = 10
req.magic = 2026
req.comment = "salam"
check := metatrader.OrderCheck(req)
println "check:", check.retcode, check.margin, check.margin_free, check.comment
if check.retcode != 0:
ret 1
end
sent := metatrader.OrderSend(req)
println "send:", sent.retcode, sent.order, sent.deal, sent.price
if sent.retcode != metatrader.TRADE_RETCODE_DONE and sent.retcode != metatrader.TRADE_RETCODE_PLACED:
printerrln "order rejected:", sent.comment
ret 1
end
margin := metatrader.OrderCalcMargin(metatrader.ORDER_TYPE_BUY, "EURUSD", 0.10, 1.0821)
profit := metatrader.OrderCalcProfit(metatrader.ORDER_TYPE_BUY, "EURUSD", 0.10, 1.0821, 1.09)
println margin, profit
ret 0
endaction selects what the request does: TRADE_ACTION_DEAL for a market order, TRADE_ACTION_PENDING for a pending one, TRADE_ACTION_SLTP to change stops, TRADE_ACTION_MODIFY and TRADE_ACTION_REMOVE for pending orders, and TRADE_ACTION_CLOSE_BY to close one position with another. Set type_filling to a mode the symbol allows, and set magic so you can find your own orders later.
Orders, positions and history
OrdersGet returns active orders, PositionsGet returns open positions, and the history calls return what is already closed. Every filter has a total and a get form; the get form returns a vector, even when you ask by ticket, because the bridge keeps one answer shape.
History times are seconds since the epoch. Give both ends of the range; an empty range returns an empty vector, not an error.
import metatrader
func main:
metatrader.SetBridgeUrl("http://127.0.0.1:18812")
date_from := 1700000000 as i64
date_to := 1700086400 as i64
println "orders:", metatrader.HistoryOrdersTotal(date_from, date_to)
each o in metatrader.HistoryOrdersGet(date_from, date_to):
println o.ticket, o.symbol, o.state, o.volume_initial, o.price_open
end
println "deals:", metatrader.HistoryDealsTotal(date_from, date_to)
each d in metatrader.HistoryDealsGet(date_from, date_to):
println d.ticket, d.order, d.position_id, d.deal_type, d.entry, d.profit, d.commission, d.swap
end
if metatrader.MarketBookAdd("EURUSD"):
each b in metatrader.MarketBookGet("EURUSD"):
println b.book_type, b.price, b.volume
end
metatrader.MarketBookRelease("EURUSD")
end
ret 0
endMarket depth
Market depth is a subscription. Call MarketBookAdd once, read the levels with MarketBookGet as often as you need, and call MarketBookRelease when you stop. The depth stays empty until the terminal has it and the subscription is active.
Types and units
| Type | What it holds |
|---|---|
AccountInfo | Balance, equity, margin, free margin, margin level, currency and account limits. |
TerminalInfo | Connection, build, folders, language and permissions of the terminal. |
SymbolInfo | Prices, spread, digits, point, contract size, volume limits, swap and session data. |
Tick, Rate | One price change and one candle; times are seconds, and tick times also have milliseconds. |
TradeOrder | An order with its state, volume, prices and the position it belongs to. |
TradePosition | An open position with its volume, profit, swap and stop prices. |
TradeDeal | An executed deal with its profit, commission, swap and fee. |
TradeRequest, OrderCheckResult, OrderSendResult | The input and the two possible answers of the trading calls. |
What the package does not do
- It does not start the terminal. The bridge must be running and the terminal logged in;
Initializeis a request to the bridge, not to the terminal process. - It does not retry. A timeout, a closed bridge or an HTTP 5xx is returned once; decide in your own code whether to repeat the call.
- It does not replace
OrderCheckwith local validation. The broker has the last word on volume, stops, filling and market hours. - It does not keep a cache. Every call asks the bridge for fresh data, so a tight loop of
SymbolInfoTickis a lot of HTTP requests; sleep between them.
Types
LastErrorInfo
struct LastErrorInfo| Name | Type | Description |
|---|---|---|
code | int = 1 | The result code; RES_S_OK on success. |
description | str = "ok" | The human readable description of the code or instrument. |
VersionInfo
struct VersionInfo| Name | Type | Description |
|---|---|---|
terminal_version | int = 0 | The version of the terminal. |
build | int = 0 | The build number. |
build_release_date | str = "" | The release date of that build. |
AccountInfo
struct AccountInfo| Name | Type | Description |
|---|---|---|
login | i64 = 0 | The account number. |
trade_mode | int = 0 | The trade mode of the symbol or account. |
leverage | i64 = 0 | The leverage of the account. |
limit_orders | int = 0 | The greatest number of open orders the account allows. |
margin_so_mode | int = 0 | The stop out mode of the account. |
trade_allowed | bool = false | True when trading is allowed. |
trade_expert | bool = false | True when expert trading is allowed. |
margin_mode | int = 0 | The margin calculation mode of the account. |
currency_digits | int = 0 | The digits used for the deposit currency. |
fifo_close | bool = false | True when positions must close in the order they opened. |
balance | f64 = 0 | The account balance in the deposit currency. |
credit | f64 = 0 | The credit granted to the account. |
profit | f64 = 0 | The floating profit or the profit of the deal. |
equity | f64 = 0 | Balance plus floating profit and credit. |
margin | f64 = 0 | The margin currently used. |
margin_free | f64 = 0 | Free margin available for new positions. |
margin_level | f64 = 0 | Equity divided by margin, in percent. |
margin_so_call | f64 = 0 | The margin level that triggers a margin call, in percent. |
margin_so_so | f64 = 0 | The margin level that triggers a stop out, in percent. |
margin_initial | f64 = 0 | The initial margin of one lot. |
margin_maintenance | f64 = 0 | The maintenance margin of one lot. |
assets | f64 = 0 | Total assets in the account. |
liabilities | f64 = 0 | Total liabilities in the account. |
commission_blocked | f64 = 0 | The commission blocked on the account. |
name | str = "" | The name of the account owner. |
server | str = "" | The trade server of the account. |
currency | str = "" | The deposit currency of the account. |
company | str = "" | The company that provides the account. |
TerminalInfo
struct TerminalInfo| Name | Type | Description |
|---|---|---|
community_account | bool = false | True when the account is a MQL5.community account. |
community_connection | bool = false | True when the terminal has a MQL5.community connection. |
connected | bool = false | True while the terminal is connected to the trade server. |
dlls_allowed | bool = false | True when the terminal allows DLL calls. |
trade_allowed | bool = false | True when trading is allowed. |
tradeapi_disabled | bool = false | True when the trade API is disabled in the terminal. |
email_enabled | bool = false | True when email is enabled in the terminal. |
ftp_enabled | bool = false | True when FTP is enabled in the terminal. |
notifications_enabled | bool = false | True when push notifications are enabled. |
mqid | bool = false | True when the terminal has a MetaQuotes ID. |
build | int = 0 | The build number. |
maxbars | int = 0 | The greatest number of bars the terminal keeps. |
codepage | int = 0 | The code page of the terminal. |
ping_last | int = 0 | The last ping to the trade server, in milliseconds. |
community_balance | f64 = 0 | The MQL5.community balance. |
retransmission | f64 = 0 | The share of retransmitted data, in percent. |
company | str = "" | The company that provides the account. |
name | str = "" | The name of the terminal. |
language | str = "" | The interface language of the terminal. |
path | str = "" | The installation folder of the terminal. |
data_path | str = "" | The data folder of this terminal. |
commondata_path | str = "" | The shared data folder of all terminals. |
SymbolInfo
struct SymbolInfo| Name | Type | Description |
|---|---|---|
custom | bool = false | True when the symbol is custom, not from the server. |
chart_mode | int = 0 | How the symbol is shown in Market Watch. |
select | bool = false | True when the symbol is selected in Market Watch. |
visible | bool = false | True when the symbol is visible in Market Watch. |
session_deals | int = 0 | The deals made in the session. |
session_buy_orders | int = 0 | The buy orders placed in the session. |
session_sell_orders | int = 0 | The sell orders placed in the session. |
volume | i64 = 0 | The tick volume of the day. |
volumehigh | i64 = 0 | The highest volume of the day. |
volumelow | i64 = 0 | The lowest volume of the day. |
time | i64 = 0 | The time in seconds since the epoch. |
digits | int = 0 | The digits after the decimal point in the price. |
spread | int = 0 | The spread in points. |
spread_float | bool = false | True when the spread floats. |
ticks_bookdepth | int = 0 | The depth of the book the ticks cover. |
trade_calc_mode | int = 0 | The profit calculation mode of the symbol. |
trade_mode | int = 0 | The trade mode of the symbol or account. |
start_time | i64 = 0 | The time the symbol starts trading, in seconds since the epoch. |
expiration_time | i64 = 0 | The expiration time of the symbol, in seconds since the epoch; 0 when it does not expire. |
trade_stops_level | int = 0 | The shortest allowed distance to the price for stops, in points. |
trade_freeze_level | int = 0 | The distance in points where orders cannot be changed. |
trade_exemode | int = 0 | The execution mode of the symbol. |
swap_mode | int = 0 | The swap calculation mode of the symbol. |
swap_rollover3days | int = 0 | The weekday with triple swap: 0 is Sunday. |
margin_hedged_use_leg | bool = false | True when the hedged margin uses the larger leg. |
expiration_mode | int = 0 | The expiration modes the symbol allows. |
filling_mode | int = 0 | The filling modes the symbol allows, as a bit mask. |
order_mode | int = 0 | The order types the symbol allows, as a bit mask. |
order_gtc_mode | int = 0 | The GTC order mode of the symbol. |
option_mode | int = 0 | The option mode of the symbol. |
option_right | int = 0 | The option right of the symbol. |
bid | f64 = 0 | The best bid price. |
bidhigh | f64 = 0 | The highest bid of the session. |
bidlow | f64 = 0 | The lowest bid of the session. |
ask | f64 = 0 | The best ask price. |
askhigh | f64 = 0 | The highest ask of the session. |
asklow | f64 = 0 | The lowest ask of the session. |
last | f64 = 0 | The last trade price. |
lasthigh | f64 = 0 | The highest last price of the session. |
lastlow | f64 = 0 | The lowest last price of the session. |
volume_real | f64 = 0 | The real volume, in contracts or units. |
volumehigh_real | f64 = 0 | The highest real volume of the day. |
volumelow_real | f64 = 0 | The lowest real volume of the day. |
option_strike | f64 = 0 | The strike price of the option. |
point | f64 = 0 | The point size of the symbol. |
trade_tick_value | f64 = 0 | The value of one tick in the deposit currency. |
trade_tick_value_profit | f64 = 0 | The value of one tick for a profit. |
trade_tick_value_loss | f64 = 0 | The value of one tick for a loss. |
trade_tick_size | f64 = 0 | The tick size used for profit. |
trade_contract_size | f64 = 0 | The size of one contract. |
trade_accrued_interest | f64 = 0 | The accrued interest of the instrument. |
trade_face_value | f64 = 0 | The face value of the instrument. |
trade_liquidity_rate | f64 = 0 | The liquidity rate of the instrument. |
volume_min | f64 = 0 | The smallest volume of one order. |
volume_max | f64 = 0 | The greatest volume of one position. |
volume_step | f64 = 0 | The step between volumes. |
volume_limit | f64 = 0 | The greatest volume of one order. |
swap_long | f64 = 0 | The swap of a long position per lot per day. |
swap_short | f64 = 0 | The swap of a short position per lot per day. |
margin_initial | f64 = 0 | The initial margin of one lot. |
margin_maintenance | f64 = 0 | The maintenance margin of one lot. |
session_volume | f64 = 0 | The volume of the session. |
session_turnover | f64 = 0 | The turnover of the session. |
session_interest | f64 = 0 | The open interest of the session. |
session_buy_orders_volume | f64 = 0 | The volume of the buy orders of the session. |
session_sell_orders_volume | f64 = 0 | The volume of the sell orders of the session. |
session_open | f64 = 0 | The open price of the session. |
session_close | f64 = 0 | The close price of the session. |
session_aw | f64 = 0 | The weighted average price of the session. |
session_price_settlement | f64 = 0 | The settlement price of the session. |
session_price_limit_min | f64 = 0 | The lower price limit of the session. |
session_price_limit_max | f64 = 0 | The upper price limit of the session. |
margin_hedged | f64 = 0 | The margin of a hedged position. |
price_change | f64 = 0 | The price change of the day, in percent. |
price_volatility | f64 = 0 | The price volatility of the instrument. |
price_theoretical | f64 = 0 | The theoretical price of the option. |
price_greeks_delta | f64 = 0 | The delta of the option. |
price_greeks_theta | f64 = 0 | The theta of the option. |
price_greeks_gamma | f64 = 0 | The gamma of the option. |
price_greeks_vega | f64 = 0 | The vega of the option. |
price_greeks_rho | f64 = 0 | The rho of the option. |
price_greeks_omega | f64 = 0 | The omega of the option. |
price_sensitivity | f64 = 0 | The price sensitivity of the instrument. |
basis | str = "" | The basis of the instrument. |
category | str = "" | The category of the instrument. |
currency_base | str = "" | The base currency of the symbol. |
currency_profit | str = "" | The profit currency of the symbol. |
currency_margin | str = "" | The margin currency of the symbol. |
bank | str = "" | The bank of the instrument, for exchange instruments. |
description | str = "" | The human readable description of the code or instrument. |
exchange | str = "" | The exchange of the instrument. |
formula | str = "" | The pricing formula of the instrument. |
isin | str = "" | The ISIN of the instrument. |
name | str = "" | The name of the account, symbol or terminal. |
page | str = "" | The web page of the instrument. |
path | str = "" | The path of the symbol in the symbol tree. |
Tick
struct Tick| Name | Type | Description |
|---|---|---|
time | i64 = 0 | The time in seconds since the epoch. |
bid | f64 = 0 | The best bid price. |
ask | f64 = 0 | The best ask price. |
last | f64 = 0 | The last trade price. |
volume | i64 = 0 | The tick volume. |
time_msc | i64 = 0 | The time in milliseconds since the epoch. |
flags | int = 0 | The tick flags, a combination of TICK_FLAG_*. |
volume_real | f64 = 0 | The real volume, in contracts or units. |
Rate
struct Rate| Name | Type | Description |
|---|---|---|
time | i64 = 0 | The time in seconds since the epoch. |
open | f64 = 0 | The open price of the candle. |
high | f64 = 0 | The high price of the candle. |
low | f64 = 0 | The low price of the candle. |
close | f64 = 0 | The close price of the candle. |
tick_volume | i64 = 0 | The tick volume of the candle. |
spread | int = 0 | The spread in points. |
real_volume | i64 = 0 | The real volume of the candle or tick, in contracts. |
BookInfo
struct BookInfo| Name | Type | Description |
|---|---|---|
book_type | int = 0 | The side of the book level, BOOK_TYPE_SELL or BOOK_TYPE_BUY. |
price | f64 = 0 | The price of the request, the book level or the result. |
volume | i64 = 0 | The volume of the book level, in lots. |
volume_dbl | f64 = 0 | The volume of the book level as a floating point number. |
TradeRequest
struct TradeRequestOrderCheck and OrderSend.| Name | Type | Description |
|---|---|---|
action | int = 0 | The trade operation, one of the TRADE_ACTION_* values. |
magic | i64 = 0 | The magic number that identifies the expert. |
order | i64 = 0 | The order ticket. |
symbol | str = "" | The trading symbol. |
volume | f64 = 0 | The volume to trade, in lots. |
price | f64 = 0 | The price of the request; for a market order the current price. |
stoplimit | f64 = 0 | The stop limit price, in points from the stop price. |
sl | f64 = 0 | The stop loss price; 0 when there is none. |
tp | f64 = 0 | The take profit price; 0 when there is none. |
deviation | int = 0 | The allowed slippage in points. |
order_type | int = 0 | The order type, one of ORDER_TYPE_*. |
type_filling | int = 0 | The filling type of the order, one of ORDER_FILLING_*. |
type_time | int = 0 | The time type of the order, one of ORDER_TIME_*. |
expiration | i64 = 0 | The expiration time of the request, in seconds since the epoch. |
comment | str = "" | The comment stored with the record. |
position | i64 = 0 | The ticket of the position the request changes. |
position_by | i64 = 0 | The ticket of the opposite position for a close by. |
OrderCheckResult
struct OrderCheckResultOrderCheck reports: the result code and the margin the order would use.| Name | Type | Description |
|---|---|---|
retcode | int = 0 | The result code of the trade request. |
balance | f64 = 0 | The balance after the order. |
equity | f64 = 0 | The equity after the order. |
profit | f64 = 0 | The profit the order would add. |
margin | f64 = 0 | The margin the order would use. |
margin_free | f64 = 0 | The free margin after the order. |
margin_level | f64 = 0 | Equity divided by margin, in percent. |
comment | str = "" | The comment stored with the record. |
request | TradeRequest | The request that produced this result. |
OrderSendResult
struct OrderSendResultOrderSend reports: the result code and the tickets it created.| Name | Type | Description |
|---|---|---|
retcode | int = 0 | The result code of the trade request. |
deal | i64 = 0 | The ticket of the deal the server created. |
order | i64 = 0 | The ticket of the order the server created. |
volume | f64 = 0 | The volume in lots. |
price | f64 = 0 | The price of the request, the book level or the result. |
bid | f64 = 0 | The best bid price. |
ask | f64 = 0 | The best ask price. |
comment | str = "" | The comment stored with the record. |
request_id | int = 0 | The identifier the server assigned to the request. |
retcode_external | int = 0 | The result code of the external system. |
request | TradeRequest | The request that produced this result. |
TradeOrder
struct TradeOrder| Name | Type | Description |
|---|---|---|
ticket | i64 = 0 | The ticket of the record. |
time_setup | i64 = 0 | The time the order was placed, in seconds since the epoch. |
time_setup_msc | i64 = 0 | The time the order was placed, in milliseconds since the epoch. |
time_done | i64 = 0 | The time the order was done, in seconds since the epoch. |
time_done_msc | i64 = 0 | The time the order was done, in milliseconds since the epoch. |
time_expiration | i64 = 0 | The expiration time of the order, in seconds since the epoch. |
order_type | int = 0 | The order type, one of ORDER_TYPE_*. |
type_time | int = 0 | The time type of the order, one of ORDER_TIME_*. |
type_filling | int = 0 | The filling type of the order, one of ORDER_FILLING_*. |
state | int = 0 | The order state, one of ORDER_STATE_*. |
magic | i64 = 0 | The magic number that identifies the expert. |
position_id | i64 = 0 | The identifier of the position. |
position_by_id | i64 = 0 | The identifier of the opposite position. |
reason | int = 0 | The reason the record was created, one of the reason constants. |
volume_initial | f64 = 0 | The volume the order was placed with. |
volume_current | f64 = 0 | The volume still open on the order. |
price_open | f64 = 0 | The price the order was placed at. |
sl | f64 = 0 | The stop loss price; 0 when there is none. |
tp | f64 = 0 | The take profit price; 0 when there is none. |
price_current | f64 = 0 | The current price of the order or position. |
price_stoplimit | f64 = 0 | The stop limit price of a stop limit order. |
symbol | str = "" | The trading symbol. |
comment | str = "" | The comment stored with the record. |
external_id | str = "" | The exchange identifier of the order, position or deal. |
TradePosition
struct TradePosition| Name | Type | Description |
|---|---|---|
ticket | i64 = 0 | The ticket of the record. |
time | i64 = 0 | The time in seconds since the epoch. |
time_msc | i64 = 0 | The time in milliseconds since the epoch. |
time_update | i64 = 0 | The time of the last update, in seconds since the epoch. |
time_update_msc | i64 = 0 | The time of the last update, in milliseconds since the epoch. |
position_type | int = 0 | The position type, POSITION_TYPE_BUY or POSITION_TYPE_SELL. |
magic | i64 = 0 | The magic number that identifies the expert. |
identifier | i64 = 0 | The identifier of the position. |
reason | int = 0 | The reason the record was created, one of the reason constants. |
volume | f64 = 0 | The volume of the position, in lots. |
price_open | f64 = 0 | The price the position opened at. |
sl | f64 = 0 | The stop loss price; 0 when there is none. |
tp | f64 = 0 | The take profit price; 0 when there is none. |
price_current | f64 = 0 | The current price of the order or position. |
swap | f64 = 0 | The swap charged on the position or deal. |
profit | f64 = 0 | The floating profit or the profit of the deal. |
symbol | str = "" | The trading symbol. |
comment | str = "" | The comment stored with the record. |
external_id | str = "" | The exchange identifier of the order, position or deal. |
TradeDeal
struct TradeDeal| Name | Type | Description |
|---|---|---|
ticket | i64 = 0 | The ticket of the record. |
order | i64 = 0 | The ticket of the order the deal came from. |
time | i64 = 0 | The time in seconds since the epoch. |
time_msc | i64 = 0 | The time in milliseconds since the epoch. |
deal_type | int = 0 | The deal type, one of the DEAL_TYPE_* values. |
entry | int = 0 | How the deal entered or left the position, one of DEAL_ENTRY_*. |
magic | i64 = 0 | The magic number that identifies the expert. |
position_id | i64 = 0 | The identifier of the position. |
reason | int = 0 | The reason the record was created, one of the reason constants. |
volume | f64 = 0 | The volume of the deal, in lots. |
price | f64 = 0 | The price of the request, the book level or the result. |
commission | f64 = 0 | The commission charged for the deal. |
swap | f64 = 0 | The swap charged on the position or deal. |
profit | f64 = 0 | The floating profit or the profit of the deal. |
fee | f64 = 0 | The fee charged for the deal. |
symbol | str = "" | The trading symbol. |
comment | str = "" | The comment stored with the record. |
external_id | str = "" | The exchange identifier of the order, position or deal. |
Constants and variables
DEFAULT_BRIDGE_URL
const DEFAULT_BRIDGE_URL: str = "http://127.0.0.1:18812"DEFAULT_TIMEOUT_MS
const DEFAULT_TIMEOUT_MS: i32 = 60000Timeframes TIMEFRAME_…
ENUM_TIMEFRAMES.| Name | Value |
|---|---|
TIMEFRAME_M1 | 1 |
TIMEFRAME_M2 | 2 |
TIMEFRAME_M3 | 3 |
TIMEFRAME_M4 | 4 |
TIMEFRAME_M5 | 5 |
TIMEFRAME_M6 | 6 |
TIMEFRAME_M10 | 10 |
TIMEFRAME_M12 | 12 |
TIMEFRAME_M15 | 15 |
TIMEFRAME_M20 | 20 |
TIMEFRAME_M30 | 30 |
TIMEFRAME_H1 | 16385 |
TIMEFRAME_H2 | 16386 |
TIMEFRAME_H3 | 16387 |
TIMEFRAME_H4 | 16388 |
TIMEFRAME_H6 | 16390 |
TIMEFRAME_H8 | 16392 |
TIMEFRAME_H12 | 16396 |
TIMEFRAME_D1 | 16408 |
TIMEFRAME_W1 | 32769 |
TIMEFRAME_MN1 | 49153 |
Order types ORDER_TYPE_…
ENUM_ORDER_TYPE.| Name | Value |
|---|---|
ORDER_TYPE_BUY | 0 |
ORDER_TYPE_SELL | 1 |
ORDER_TYPE_BUY_LIMIT | 2 |
ORDER_TYPE_SELL_LIMIT | 3 |
ORDER_TYPE_BUY_STOP | 4 |
ORDER_TYPE_SELL_STOP | 5 |
ORDER_TYPE_BUY_STOP_LIMIT | 6 |
ORDER_TYPE_SELL_STOP_LIMIT | 7 |
ORDER_TYPE_CLOSE_BY | 8 |
Order states ORDER_STATE_…
ENUM_ORDER_STATE.| Name | Value |
|---|---|
ORDER_STATE_STARTED | 0 |
ORDER_STATE_PLACED | 1 |
ORDER_STATE_CANCELED | 2 |
ORDER_STATE_PARTIAL | 3 |
ORDER_STATE_FILLED | 4 |
ORDER_STATE_REJECTED | 5 |
ORDER_STATE_EXPIRED | 6 |
ORDER_STATE_REQUEST_ADD | 7 |
ORDER_STATE_REQUEST_MODIFY | 8 |
ORDER_STATE_REQUEST_CANCEL | 9 |
Filling modes ORDER_FILLING_…
ENUM_ORDER_TYPE_FILLING.| Name | Value |
|---|---|
ORDER_FILLING_FOK | 0 |
ORDER_FILLING_IOC | 1 |
ORDER_FILLING_RETURN | 2 |
ORDER_FILLING_BOC | 3 |
Order lifetimes ORDER_TIME_…
ENUM_ORDER_TYPE_TIME.| Name | Value |
|---|---|
ORDER_TIME_GTC | 0 |
ORDER_TIME_DAY | 1 |
ORDER_TIME_SPECIFIED | 2 |
ORDER_TIME_SPECIFIED_DAY | 3 |
Order reasons ORDER_REASON_…
ENUM_ORDER_REASON.| Name | Value |
|---|---|
ORDER_REASON_CLIENT | 0 |
ORDER_REASON_MOBILE | 1 |
ORDER_REASON_WEB | 2 |
ORDER_REASON_EXPERT | 3 |
ORDER_REASON_SL | 4 |
ORDER_REASON_TP | 5 |
ORDER_REASON_SO | 6 |
Trade actions TRADE_ACTION_…
ENUM_TRADE_REQUEST_ACTIONS.| Name | Value |
|---|---|
TRADE_ACTION_DEAL | 1 |
TRADE_ACTION_PENDING | 5 |
TRADE_ACTION_SLTP | 6 |
TRADE_ACTION_MODIFY | 7 |
TRADE_ACTION_REMOVE | 8 |
TRADE_ACTION_CLOSE_BY | 10 |
Position types POSITION_TYPE_…
ENUM_POSITION_TYPE.| Name | Value |
|---|---|
POSITION_TYPE_BUY | 0 |
POSITION_TYPE_SELL | 1 |
Position reasons POSITION_REASON_…
ENUM_POSITION_REASON.| Name | Value |
|---|---|
POSITION_REASON_CLIENT | 0 |
POSITION_REASON_MOBILE | 1 |
POSITION_REASON_WEB | 2 |
POSITION_REASON_EXPERT | 3 |
Deal types DEAL_TYPE_…
ENUM_DEAL_TYPE.| Name | Value |
|---|---|
DEAL_TYPE_BUY | 0 |
DEAL_TYPE_SELL | 1 |
DEAL_TYPE_BALANCE | 2 |
DEAL_TYPE_CREDIT | 3 |
DEAL_TYPE_CHARGE | 4 |
DEAL_TYPE_CORRECTION | 5 |
DEAL_TYPE_BONUS | 6 |
DEAL_TYPE_COMMISSION | 7 |
DEAL_TYPE_COMMISSION_DAILY | 8 |
DEAL_TYPE_COMMISSION_MONTHLY | 9 |
DEAL_TYPE_COMMISSION_AGENT_DAILY | 10 |
DEAL_TYPE_COMMISSION_AGENT_MONTHLY | 11 |
DEAL_TYPE_INTEREST | 12 |
DEAL_TYPE_BUY_CANCELED | 13 |
DEAL_TYPE_SELL_CANCELED | 14 |
Deal kinds DEAL_…
| Name | Value |
|---|---|
DEAL_DIVIDEND | 15 |
DEAL_DIVIDEND_FRANKED | 16 |
DEAL_TAX | 17 |
Deal entries DEAL_ENTRY_…
ENUM_DEAL_ENTRY.| Name | Value |
|---|---|
DEAL_ENTRY_IN | 0 |
DEAL_ENTRY_OUT | 1 |
DEAL_ENTRY_INOUT | 2 |
DEAL_ENTRY_OUT_BY | 3 |
Deal reasons DEAL_REASON_…
ENUM_DEAL_REASON.| Name | Value |
|---|---|
DEAL_REASON_CLIENT | 0 |
DEAL_REASON_MOBILE | 1 |
DEAL_REASON_WEB | 2 |
DEAL_REASON_EXPERT | 3 |
DEAL_REASON_SL | 4 |
DEAL_REASON_TP | 5 |
DEAL_REASON_SO | 6 |
DEAL_REASON_ROLLOVER | 7 |
DEAL_REASON_VMARGIN | 8 |
DEAL_REASON_SPLIT | 9 |
Trade result codes TRADE_RETCODE_…
retcode.| Name | Value |
|---|---|
TRADE_RETCODE_REQUOTE | 10004 |
TRADE_RETCODE_REJECT | 10006 |
TRADE_RETCODE_CANCEL | 10007 |
TRADE_RETCODE_PLACED | 10008 |
TRADE_RETCODE_DONE | 10009 |
TRADE_RETCODE_DONE_PARTIAL | 10010 |
TRADE_RETCODE_ERROR | 10011 |
TRADE_RETCODE_TIMEOUT | 10012 |
TRADE_RETCODE_INVALID | 10013 |
TRADE_RETCODE_INVALID_VOLUME | 10014 |
TRADE_RETCODE_INVALID_PRICE | 10015 |
TRADE_RETCODE_INVALID_STOPS | 10016 |
TRADE_RETCODE_TRADE_DISABLED | 10017 |
TRADE_RETCODE_MARKET_CLOSED | 10018 |
TRADE_RETCODE_NO_MONEY | 10019 |
TRADE_RETCODE_PRICE_CHANGED | 10020 |
TRADE_RETCODE_PRICE_OFF | 10021 |
TRADE_RETCODE_INVALID_EXPIRATION | 10022 |
TRADE_RETCODE_ORDER_CHANGED | 10023 |
TRADE_RETCODE_TOO_MANY_REQUESTS | 10024 |
TRADE_RETCODE_NO_CHANGES | 10025 |
TRADE_RETCODE_SERVER_DISABLES_AT | 10026 |
TRADE_RETCODE_CLIENT_DISABLES_AT | 10027 |
TRADE_RETCODE_LOCKED | 10028 |
TRADE_RETCODE_FROZEN | 10029 |
TRADE_RETCODE_INVALID_FILL | 10030 |
TRADE_RETCODE_CONNECTION | 10031 |
TRADE_RETCODE_ONLY_REAL | 10032 |
TRADE_RETCODE_LIMIT_ORDERS | 10033 |
TRADE_RETCODE_LIMIT_VOLUME | 10034 |
TRADE_RETCODE_INVALID_ORDER | 10035 |
TRADE_RETCODE_POSITION_CLOSED | 10036 |
TRADE_RETCODE_INVALID_CLOSE_VOLUME | 10038 |
TRADE_RETCODE_CLOSE_ORDER_EXIST | 10039 |
TRADE_RETCODE_LIMIT_POSITIONS | 10040 |
TRADE_RETCODE_REJECT_CANCEL | 10041 |
TRADE_RETCODE_LONG_ONLY | 10042 |
TRADE_RETCODE_SHORT_ONLY | 10043 |
TRADE_RETCODE_CLOSE_ONLY | 10044 |
TRADE_RETCODE_FIFO_CLOSE | 10045 |
TRADE_RETCODE_HEDGE_PROHIBITED | 10046 |
Book levels BOOK_TYPE_…
ENUM_BOOK_TYPE.| Name | Value |
|---|---|
BOOK_TYPE_SELL | 0 |
BOOK_TYPE_BUY | 1 |
BOOK_TYPE_SELL_MARKET | 2 |
BOOK_TYPE_BUY_MARKET | 3 |
Tick filters COPY_TICKS_…
CopyTicksFrom and CopyTicksRange return, the values of ENUM_COPY_TICKS.| Name | Value |
|---|---|
COPY_TICKS_ALL | -1 |
COPY_TICKS_INFO | 1 |
COPY_TICKS_TRADE | 2 |
Tick flags TICK_FLAG_…
flags.| Name | Value |
|---|---|
TICK_FLAG_BID | 2 |
TICK_FLAG_ASK | 4 |
TICK_FLAG_LAST | 8 |
TICK_FLAG_VOLUME | 16 |
TICK_FLAG_BUY | 32 |
TICK_FLAG_SELL | 64 |
Symbol trade modes SYMBOL_TRADE_MODE_…
ENUM_SYMBOL_TRADE_MODE.| Name | Value |
|---|---|
SYMBOL_TRADE_MODE_DISABLED | 0 |
SYMBOL_TRADE_MODE_LONGONLY | 1 |
SYMBOL_TRADE_MODE_SHORTONLY | 2 |
SYMBOL_TRADE_MODE_CLOSEONLY | 3 |
SYMBOL_TRADE_MODE_FULL | 4 |
Symbol calculation modes SYMBOL_CALC_MODE_…
ENUM_SYMBOL_CALC_MODE.| Name | Value |
|---|---|
SYMBOL_CALC_MODE_FOREX | 0 |
SYMBOL_CALC_MODE_FOREX_NO_LEVERAGE | 1 |
SYMBOL_CALC_MODE_FUTURES | 2 |
SYMBOL_CALC_MODE_CFD | 3 |
SYMBOL_CALC_MODE_CFDINDEX | 4 |
SYMBOL_CALC_MODE_CFDLEVERAGE | 5 |
SYMBOL_CALC_MODE_EXCH_STOCKS | 32 |
SYMBOL_CALC_MODE_EXCH_FUTURES | 33 |
SYMBOL_CALC_MODE_EXCH_FUTURES_FORTS | 34 |
SYMBOL_CALC_MODE_EXCH_BONDS | 35 |
SYMBOL_CALC_MODE_EXCH_STOCKS_MOEX | 36 |
SYMBOL_CALC_MODE_EXCH_BONDS_MOEX | 37 |
SYMBOL_CALC_MODE_SERV_COLLATERAL | 38 |
Bridge result codes RES_…
LastError.| Name | Value |
|---|---|
RES_S_OK | 1 |
RES_E_FAIL | -1 |
RES_E_INVALID_PARAMS | -2 |
RES_E_NO_MEMORY | -3 |
RES_E_NOT_FOUND | -4 |
RES_E_INVALID_VERSION | -5 |
RES_E_AUTH_FAILED | -6 |
RES_E_UNSUPPORTED | -7 |
RES_E_AUTO_TRADING_DISABLED | -8 |
RES_E_INTERNAL_FAIL | -10000 |
RES_E_INTERNAL_FAIL_SEND | -10001 |
RES_E_INTERNAL_FAIL_RECEIVE | -10002 |
RES_E_INTERNAL_FAIL_INIT | -10003 |
RES_E_INTERNAL_FAIL_CONNECT | -10003 |
RES_E_INTERNAL_FAIL_TIMEOUT | -10005 |
Functions
CopyRatesFrom
func CopyRatesFrom(symbol: str, timeframe: int, date_from: i64, count: int): Vector<Rate>date_from, then up to count candles.import metatrader
func main:
metatrader.SetBridgeUrl("http://127.0.0.1:18812")
from := metatrader.CopyRatesFrom("EURUSD", metatrader.TIMEFRAME_H1, 1700000000 as i64, 10)
each r in from:
println r.time, r.open, r.high, r.low, r.close, r.tick_volume
end
recent := metatrader.CopyRatesFromPos("EURUSD", metatrader.TIMEFRAME_M5, 0, 5)
println "recent:", recent.len()
window := metatrader.CopyRatesRange("EURUSD", metatrader.TIMEFRAME_D1, 1700000000 as i64, 1700086400 as i64)
println "daily:", window.len()
ticks := metatrader.CopyTicksFrom("EURUSD", 1700000000 as i64, 100, metatrader.COPY_TICKS_ALL)
each t in ticks:
println t.time_msc, t.bid, t.ask, t.last, t.flags
end
trade_ticks := metatrader.CopyTicksRange("EURUSD", 1700000000000 as i64, 1700000600000 as i64, metatrader.COPY_TICKS_TRADE)
println "trade ticks:", trade_ticks.len()
ret 0
endCopyRatesFromPos
func CopyRatesFromPos(symbol: str, timeframe: int, start_pos: int, count: int): Vector<Rate>start_pos is counted from the current candle, 0 being the newest, so 0 and 10 give the last ten candles.The example for this item is shown with the ones above: metatrader-Rates.salam
CopyRatesRange
func CopyRatesRange(symbol: str, timeframe: int, date_from: i64, date_to: i64): Vector<Rate>The example for this item is shown with the ones above: metatrader-Rates.salam
SymbolsTotal
func SymbolsTotal(): intimport metatrader
func main:
metatrader.SetBridgeUrl("http://127.0.0.1:18812")
println "symbols:", metatrader.SymbolsTotal()
each s in metatrader.SymbolsGet():
println s.name, s.digits, s.spread, s.trade_mode
end
majors := metatrader.SymbolsGetGroup("*USD*")
println "usd symbols:", majors.len()
info := metatrader.GetSymbolInfo("EURUSD")
println info.bid, info.ask, info.point, info.digits
println info.volume_min, info.volume_step, info.volume_max
println info.trade_stops_level, info.trade_contract_size
tick := metatrader.SymbolInfoTick("EURUSD")
println tick.time, tick.bid, tick.ask, tick.last, tick.volume
if not metatrader.SymbolSelect("EURUSD", true):
printerrln "cannot select EURUSD"
end
ret 0
endSymbolsGet
func SymbolsGet(): Vector<SymbolInfo>The example for this item is shown with the ones above: metatrader-Symbols.salam
SymbolsGetGroup
func SymbolsGetGroup(group: str): Vector<SymbolInfo>*USD*.The example for this item is shown with the ones above: metatrader-Symbols.salam
GetSymbolInfo
func GetSymbolInfo(symbol: str): SymbolInfoThe example for this item is shown with the ones above: metatrader-Symbols.salam
SymbolInfoTick
func SymbolInfoTick(symbol: str): TickThe example for this item is shown with the ones above: metatrader-Symbols.salam
SymbolSelect
func SymbolSelect(symbol: str, enable: bool): boolThe example for this item is shown with the ones above: metatrader-Symbols.salam
SetBridgeUrl
func SetBridgeUrl(url: str)http://127.0.0.1:18812. It applies to every later call.import metatrader
func main:
metatrader.SetBridgeUrl("http://127.0.0.1:18812")
println metatrader.BridgeUrl()
if not metatrader.Initialize("", 12345678 as i64, "secret", "Broker-Demo", 60000, false):
e := metatrader.LastError()
printerrln "initialize failed:", e.code, e.description
ret 1
end
if not metatrader.Login(12345678 as i64, "secret", "Broker-Demo", 60000):
e := metatrader.LastError()
printerrln "login failed:", e.code, e.description
ret 1
end
v := metatrader.Version()
println "connected:", metatrader.IsConnected()
println "terminal:", v.terminal_version, "build", v.build, v.build_release_date
if not metatrader.Ok():
e := metatrader.LastError()
printerrln e.code, e.description
end
metatrader.Shutdown()
ret 0
endBridgeUrl
func BridgeUrl(): strDEFAULT_BRIDGE_URL.The example for this item is shown with the ones above: metatrader-Connect.salam
Initialize
func Initialize(path: str = "", login: i64 = 0, password: str = "", server: str = "", timeout_ms: int, portable: bool = false): bool/initialize endpoint, optionally changing the bridge address, and marks the session connected when the bridge answers. The login and password are passed through for bridges that start the terminal themselves.The example for this item is shown with the ones above: metatrader-Connect.salam
Login
func Login(login: i64, password: str = "", server: str = "", timeout_ms: int): bool/login endpoint with an account number, password, server and timeout, and marks the session connected on success.The example for this item is shown with the ones above: metatrader-Connect.salam
Shutdown
func Shutdown()/shutdown and marks the session disconnected. Call it before the program ends so the bridge can close the terminal cleanly.The example for this item is shown with the ones above: metatrader-Connect.salam
IsConnected
func IsConnected(): boolInitialize or Login and before Shutdown; it reports the last call, not a live check.The example for this item is shown with the ones above: metatrader-Connect.salam
Version
func Version(): VersionInfoThe example for this item is shown with the ones above: metatrader-Connect.salam
LastError
func LastError(): LastErrorInfoThe example for this item is shown with the ones above: metatrader-Connect.salam
Ok
func Ok(): boolLastError is RES_S_OK.The example for this item is shown with the ones above: metatrader-Connect.salam
OrdersTotal
func OrdersTotal(): intLastError set.import metatrader
func main:
metatrader.SetBridgeUrl("http://127.0.0.1:18812")
println "orders:", metatrader.OrdersTotal()
each o in metatrader.OrdersGet():
println o.ticket, o.symbol, o.order_type, o.state, o.volume_current, o.price_open
end
each p in metatrader.PositionsGet():
println p.ticket, p.symbol, p.position_type, p.volume, p.price_open, p.profit
end
mut req := metatrader.TradeRequest {}
req.action = metatrader.TRADE_ACTION_DEAL
req.symbol = "EURUSD"
req.volume = 0.10
req.price = 1.0821
req.order_type = metatrader.ORDER_TYPE_BUY
req.type_filling = metatrader.ORDER_FILLING_IOC
req.type_time = metatrader.ORDER_TIME_GTC
req.deviation = 10
req.magic = 2026
req.comment = "salam"
check := metatrader.OrderCheck(req)
println "check:", check.retcode, check.margin, check.margin_free, check.comment
if check.retcode != 0:
ret 1
end
sent := metatrader.OrderSend(req)
println "send:", sent.retcode, sent.order, sent.deal, sent.price
if sent.retcode != metatrader.TRADE_RETCODE_DONE and sent.retcode != metatrader.TRADE_RETCODE_PLACED:
printerrln "order rejected:", sent.comment
ret 1
end
margin := metatrader.OrderCalcMargin(metatrader.ORDER_TYPE_BUY, "EURUSD", 0.10, 1.0821)
profit := metatrader.OrderCalcProfit(metatrader.ORDER_TYPE_BUY, "EURUSD", 0.10, 1.0821, 1.09)
println margin, profit
ret 0
endOrdersGet
func OrdersGet(): Vector<TradeOrder>LastError set.The example for this item is shown with the ones above: metatrader-Trade.salam
OrdersGetSymbol
func OrdersGetSymbol(symbol: str): Vector<TradeOrder>The example for this item is shown with the ones above: metatrader-Trade.salam
OrdersGetGroup
func OrdersGetGroup(group: str): Vector<TradeOrder>*USD*.The example for this item is shown with the ones above: metatrader-Trade.salam
OrdersGetTicket
func OrdersGetTicket(ticket: i64): Vector<TradeOrder>The example for this item is shown with the ones above: metatrader-Trade.salam
OrderCalcMargin
func OrderCalcMargin(action: int, symbol: str, volume: f64, price: f64): f64The example for this item is shown with the ones above: metatrader-Trade.salam
OrderCalcProfit
func OrderCalcProfit(action: int, symbol: str, volume: f64, price_open: f64, price_close: f64): f64The example for this item is shown with the ones above: metatrader-Trade.salam
OrderCheck
func OrderCheck(request: TradeRequest): OrderCheckResultThe example for this item is shown with the ones above: metatrader-Trade.salam
OrderSend
func OrderSend(request: TradeRequest): OrderSendResultThe example for this item is shown with the ones above: metatrader-Trade.salam
PositionsTotal
func PositionsTotal(): intThe example for this item is shown with the ones above: metatrader-Trade.salam
PositionsGet
func PositionsGet(): Vector<TradePosition>The example for this item is shown with the ones above: metatrader-Trade.salam
PositionsGetSymbol
func PositionsGetSymbol(symbol: str): Vector<TradePosition>The example for this item is shown with the ones above: metatrader-Trade.salam
PositionsGetGroup
func PositionsGetGroup(group: str): Vector<TradePosition>The example for this item is shown with the ones above: metatrader-Trade.salam
PositionsGetTicket
func PositionsGetTicket(ticket: i64): Vector<TradePosition>The example for this item is shown with the ones above: metatrader-Trade.salam
HistoryOrdersTotal
func HistoryOrdersTotal(date_from: i64, date_to: i64): intimport metatrader
func main:
metatrader.SetBridgeUrl("http://127.0.0.1:18812")
date_from := 1700000000 as i64
date_to := 1700086400 as i64
println "orders:", metatrader.HistoryOrdersTotal(date_from, date_to)
each o in metatrader.HistoryOrdersGet(date_from, date_to):
println o.ticket, o.symbol, o.state, o.volume_initial, o.price_open
end
println "deals:", metatrader.HistoryDealsTotal(date_from, date_to)
each d in metatrader.HistoryDealsGet(date_from, date_to):
println d.ticket, d.order, d.position_id, d.deal_type, d.entry, d.profit, d.commission, d.swap
end
if metatrader.MarketBookAdd("EURUSD"):
each b in metatrader.MarketBookGet("EURUSD"):
println b.book_type, b.price, b.volume
end
metatrader.MarketBookRelease("EURUSD")
end
ret 0
endHistoryOrdersGet
func HistoryOrdersGet(date_from: i64, date_to: i64): Vector<TradeOrder>The example for this item is shown with the ones above: metatrader-History.salam
HistoryOrdersGetGroup
func HistoryOrdersGetGroup(date_from: i64, date_to: i64, group: str): Vector<TradeOrder>The example for this item is shown with the ones above: metatrader-History.salam
HistoryOrdersGetTicket
func HistoryOrdersGetTicket(ticket: i64): Vector<TradeOrder>The example for this item is shown with the ones above: metatrader-History.salam
HistoryOrdersGetPosition
func HistoryOrdersGetPosition(position: i64): Vector<TradeOrder>The example for this item is shown with the ones above: metatrader-History.salam
HistoryDealsTotal
func HistoryDealsTotal(date_from: i64, date_to: i64): intThe example for this item is shown with the ones above: metatrader-History.salam
HistoryDealsGet
func HistoryDealsGet(date_from: i64, date_to: i64): Vector<TradeDeal>The example for this item is shown with the ones above: metatrader-History.salam
HistoryDealsGetGroup
func HistoryDealsGetGroup(date_from: i64, date_to: i64, group: str): Vector<TradeDeal>The example for this item is shown with the ones above: metatrader-History.salam
HistoryDealsGetTicket
func HistoryDealsGetTicket(ticket: i64): Vector<TradeDeal>The example for this item is shown with the ones above: metatrader-History.salam
HistoryDealsGetPosition
func HistoryDealsGetPosition(position: i64): Vector<TradeDeal>The example for this item is shown with the ones above: metatrader-History.salam
CopyTicksFrom
func CopyTicksFrom(symbol: str, date_from: i64, count: int, flags: int): Vector<Tick>count, filtered by the COPY_TICKS_* flags.The example for this item is shown with the ones above: metatrader-Rates.salam
CopyTicksRange
func CopyTicksRange(symbol: str, date_from: i64, date_to: i64, flags: int): Vector<Tick>COPY_TICKS_* flags.The example for this item is shown with the ones above: metatrader-Rates.salam
GetAccountInfo
func GetAccountInfo(): AccountInfoimport metatrader
func main:
metatrader.SetBridgeUrl("http://127.0.0.1:18812")
a := metatrader.GetAccountInfo()
if not metatrader.Ok():
e := metatrader.LastError()
printerrln "account failed:", e.code, e.description
ret 1
end
println a.login, a.name, a.server, a.currency
println "balance", a.balance, "equity", a.equity
println "margin", a.margin, "free", a.margin_free, "level", a.margin_level
println "leverage", a.leverage, "limit", a.limit_orders
if a.trade_allowed:
println "trading is allowed"
end
t := metatrader.GetTerminalInfo()
println t.company, t.name, t.build, t.connected
println t.path
println t.data_path
ret 0
endGetTerminalInfo
func GetTerminalInfo(): TerminalInfoThe example for this item is shown with the ones above: metatrader-Account.salam
MarketBookAdd
func MarketBookAdd(symbol: str): boolMarketBookGet and stop it with MarketBookRelease.The example for this item is shown with the ones above: metatrader-History.salam
MarketBookGet
func MarketBookGet(symbol: str): Vector<BookInfo>The example for this item is shown with the ones above: metatrader-History.salam
MarketBookRelease
func MarketBookRelease(symbol: str): boolThe example for this item is shown with the ones above: metatrader-History.salam