MetaApi is a powerful, fast, cost-efficient, easy to use and standards-driven cloud forex trading API for MetaTrader 4 and MetaTrader 5 platform designed for traders, investors and forex application developers to boost forex application development process. MetaApi can be used with any broker and does not require you to be a brokerage.
CopyFactory is a simple yet powerful copy-trading API which is a part of MetaApi. See below for CopyFactory readme section.
MetaApi is a paid service, but API access to one MetaTrader account is free of charge.
The MetaApi pricing was developed with the intent to make your charges less or equal to what you would have to pay for hosting your own infrastructure. This is possible because over time we managed to heavily optimize our MetaTrader infrastructure. And with MetaApi you can save significantly on application development and maintenance costs and time thanks to high-quality API, open-source SDKs and convenience of a cloud service.
Official REST and websocket API documentation: https://metaapi.cloud/docs/client/
Please note that this SDK provides an abstraction over REST and websocket API to simplify your application logic.
For more information about SDK APIs please check docstring documentation in source codes located inside lib folder of this package.
pip install metaapi-cloud-sdk
Please check this short video to see how you can download samples via our web application.
You can find code examples at examples folder of our github repo or in the examples folder of the pip package.
We have composed a short guide explaining how to use the example code
Please use one of these ways:
- https://app.metaapi.cloud/token web UI to obtain your API token.
- An account access token which grants access to a single account. See section below on instructions on how to retrieve account access token.
Supply token to the MetaApi class constructor.
from metaapi_cloud_sdk import MetaApi
token = '...'
api = MetaApi(token)
Account access token grants access to a single account. You can retrieve account access token via API:
account_id = '...'
account = await api.metatrader_account_api.get_account(account_id)
account_access_token = account.access_token
print(account_access_token)
Alternatively, you can retrieve account access token via web UI on https://app.metaapi.cloud/accounts page (see this video).
Before you can use the API you have to add an MT account to MetaApi and start an API server for it.
However, before you can create an account, you have to create a provisioning profile.
You can manage provisioning profiles here: https://app.metaapi.cloud/provisioning-profiles
# if you do not have created a provisioning profile for your broker,
# you should do it before creating an account
provisioningProfile = await api.provisioning_profile_api.create_provisioning_profile({
'name': 'My profile',
'version': 5,
'brokerTimezone': 'EET',
'brokerDSTSwitchTimezone': 'EET'
})
# servers.dat file is required for MT5 profile and can be found inside
# config directory of your MetaTrader terminal data folder. It contains
# information about available broker servers
await provisioningProfile.upload_file('servers.dat', '/path/to/servers.dat')
# for MT4, you should upload an .srv file instead
await provisioningProfile.upload_file('broker.srv', '/path/to/broker.srv')
provisioningProfiles = await api.provisioning_profile_api.get_provisioning_profiles()
provisioningProfile = await api.provisioning_profile_api.get_provisioning_profile('profileId')
await provisioningProfile.update({'name': 'New name'})
# for MT5, you should upload a servers.dat file
await provisioningProfile.upload_file('servers.dat', '/path/to/servers.dat')
# for MT4, you should upload an .srv file instead
await provisioningProfile.upload_file('broker.srv', '/path/to/broker.srv')
await provisioningProfile.remove()
You can manage MetaTrader accounts here: https://app.metaapi.cloud/accounts
account = await api.metatrader_account_api.create_account({
'name': 'Trading account #1',
'type': 'cloud',
'login': '1234567',
# password can be investor password for read-only access
'password': 'qwerty',
'server': 'ICMarketsSC-Demo',
'provisioningProfileId': provisioningProfile.id,
'application': 'MetaApi',
'magic': 123456,
'quoteStreamingIntervalInSeconds': 2.5 # set to 0 to receive quote per tick
})
# filter and paginate accounts, see doc for full list of filter options available
accounts = await api.metatraderAccountApi.get_accounts({
'limit': 10,
'offset': 0,
'query': 'ICMarketsSC-MT5',
'state': ['DEPLOYED']
})
# get accounts without filter (returns 1000 accounts max)
accounts = await api.metatraderAccountApi.get_accounts();
account = await api.metatraderAccountApi.get_account('accountId')
await account.update({
'name': 'Trading account #1',
'login': '1234567',
# password can be investor password for read-only access
'password': 'qwerty',
'server': 'ICMarketsSC-Demo',
'quoteStreamingIntervalInSeconds': 2.5
})
await account.remove()
await account.deploy()
await account.undeploy()
await account.redeploy()
RPC API let you query the trading terminal state. You should use RPC API if you develop trading monitoring apps like myfxbook or other simple trading apps.
connection = await account.connect()
await connection.wait_synchronized()
# retrieve balance and equity
print(await connection.get_account_information())
# retrieve open positions
print(await connection.get_positions())
# retrieve a position by id
print(await connection.get_position('1234567'))
# retrieve pending orders
print(await connection.get_orders())
# retrieve a pending order by id
print(await connection.get_order('1234567'))
# retrieve history orders by ticket
print(await connection.get_history_orders_by_ticket('1234567'))
# retrieve history orders by position id
print(await connection.get_history_orders_by_position('1234567'))
# retrieve history orders by time range
print(await connection.get_history_orders_by_time_range(start_time, end_time))
# retrieve history deals by ticket
print(await connection.get_deals_by_ticket('1234567'))
# retrieve history deals by position id
print(await connection.get_deals_by_position('1234567'))
# retrieve history deals by time range
print(await connection.get_deals_by_time_range(start_time, end_time))
connection = await account.connect()
await connection.wait_synchronized()
# first, subscribe to market data
await connection.subscribe_to_market_data('GBPUSD')
# read contract specification
print(await connection.get_symbol_specification('GBPUSD'))
# read current price
print(await connection.get_symbol_price('GBPUSD'))
Real-time streaming API is good for developing trading applications like trade copiers or automated trading strategies. The API synchronizes the terminal state locally so that you can query local copy of the terminal state really fast.
account = await api.metatrader_account_api.get_account('accountId')
# access local copy of terminal state
terminalState = connection.terminal_state
# wait until synchronization completed
await connection.wait_synchronized()
print(terminalState.connected)
print(terminalState.connected_to_broker)
print(terminalState.account_information)
print(terminalState.positions)
print(terminalState.orders)
# symbol specifications
print(terminalState.specifications)
print(terminalState.specification('EURUSD'))
print(terminalState.price('EURUSD'))
# access history storage
historyStorage = connection.history_storage
# both orderSynchronizationFinished and dealSynchronizationFinished
# should be true once history synchronization have finished
print(historyStorage.order_synchronization_finished)
print(historyStorage.deal_synchronization_finished)
By default history is stored in memory only. You can override history storage to save trade history to a persistent storage like MongoDB database.
from metaapi_cloud_sdk import HistoryStorage
class MongodbHistoryStorage(HistoryStorage):
# implement the abstract methods, see MemoryHistoryStorage for sample
# implementation
historyStorage = MongodbHistoryStorage()
# Note: if you will not specify history storage, then in-memory storage
# will be used (instance of MemoryHistoryStorage)
connection = await account.connect(historyStorage)
# access history storage
historyStorage = connection.history_storage;
# invoke other methods provided by your history storage implementation
print(await historyStorage.yourMethod())
You can override SynchronizationListener in order to receive synchronization event notifications, such as account/position/order/history updates or symbol quote updates.
from metaapi_cloud_sdk import SynchronizationListener
# receive synchronization event notifications
# first, implement your listener
class MySynchronizationListener(SynchronizationListener):
# override abstract methods you want to receive notifications for
# now add the listener
listener = MySynchronizationListener()
connection.add_synchronization_listener(listener)
# remove the listener when no longer needed
connection.remove_synchronization_listener(listener)
connection = await account.connect()
await connection.wait_synchronized()
# first, subscribe to market data
await connection.subscribe_to_market_data('GBPUSD')
# read constract specification
print(terminalState.specification('EURUSD'))
# read current price
print(terminalState.price('EURUSD'))
connection = await account.connect()
await connection.wait_synchronized()
# trade
print(await connection.create_market_buy_order('GBPUSD', 0.07, 0.9, 2.0, {'comment': 'comment',
'clientId': 'TE_GBPUSD_7hyINWqAl'}))
print(await connection.create_market_sell_order('GBPUSD', 0.07, 2.0, 0.9, {'comment': 'comment',
'clientId': 'TE_GBPUSD_7hyINWqAl'}))
print(await connection.create_limit_buy_order('GBPUSD', 0.07, 1.0, 0.9, 2.0, {'comment': 'comment',
'clientId': 'TE_GBPUSD_7hyINWqAl'}))
print(await connection.create_limit_sell_order('GBPUSD', 0.07, 1.5, 2.0, 0.9, {'comment': 'comment',
'clientId': 'TE_GBPUSD_7hyINWqAl'}))
print(await connection.create_stop_buy_order('GBPUSD', 0.07, 1.5, 0.9, 2.0, {'comment': 'comment',
'clientId': 'TE_GBPUSD_7hyINWqAl'}))
print(await connection.create_stop_sell_order('GBPUSD', 0.07, 1.0, 2.0, 0.9, {'comment': 'comment',
'clientId': 'TE_GBPUSD_7hyINWqAl'}))
print(await connection.modify_position('46870472', 2.0, 0.9))
print(await connection.close_position_partially('46870472', 0.9))
print(await connection.close_position('46870472'))
print(await connection.close_positions_by_symbol('EURUSD'))
print(await connection.modify_order('46870472', 1.0, 2.0, 0.9))
print(await connection.cancel_order('46870472'))
# if you need to, check the extra result information in stringCode and numericCode properties of the response
result = await connection.create_market_buy_order('GBPUSD', 0.07, 0.9, 2.0, {'comment': 'comment',
'clientId': 'TE_GBPUSD_7hyINWqAl'})
print('Trade successful, result code is ' + result['stringCode'])
Please note that not all MT4/MT5 servers allows you to create demo accounts using the method below.
demo_account = await api.metatrader_demo_account_api.create_mt4_demo_account(provisioningProfile.id, {
'balance': 100000,
'email': 'example@example.com',
'leverage': 100,
'serverName': 'Exness-Trial4'
})
demo_account = await api.metatrader_demo_account_api.create_mt5_demo_account(provisioningProfile.id, {
'balance': 100000,
'email': 'example@example.com',
'leverage': 100,
'serverName': 'ICMarketsSC-Demo'
})
CopyFactory is a powerful trade copying API which makes developing forex trade copying applications as easy as writing few lines of code.
At this point this feature is experimental and we have not yet defined a final price for it.
We found that developing reliable and flexible trade copier is a task which requires lots of effort, because developers have to solve a series of complex technical tasks to create a product.
We decided to share our product as it allows developers to start with a powerful solution in almost no time, save on development and infrastructure maintenance costs.
Features supported:
- low latency trade copying
- connect arbitrary number of strategy providers and subscribers
- subscribe accounts to multiple strategies at once
- select arbitrary copy ratio for each subscription
- apply advanced risk filters on strategy provider side
- override risk filters on subscriber side
- provide multiple strategies from a single account based on magic or symbol filters
- reliable trade copying
- supports manual trading on subscriber accounts while copying trades
- synchronize subscriber account with strategy providers
- monitor trading history
- calculate trade copying commissions for account managers
In order to configure trade copying you need to:
- add MetaApi MetaTrader accounts with CopyFactory as application field value (see above)
- create CopyFactory master and slave accounts and connect them to MetaApi accounts via connectionId field
- create a strategy being copied
- subscribe slave CopyFactory accounts to the strategy
from metaapi_cloud_sdk import MetaApi, CopyFactory
token = '...'
metaapi = MetaApi(token)
copy_factory = CopyFactory(token)
# retrieve MetaApi MetaTrader accounts with CopyFactory as application field value
master_metaapi_account = await api.metatrader_account_api.get_account('masterMetaapiAccountId')
if master_metaapi_account.application != 'CopyFactory'
raise Exception('Please specify CopyFactory application field value in your MetaApi account in order to use it in CopyFactory API')
slave_metaapi_account = await api.metatrader_account_api.get_account('slaveMetaapiAccountId')
if slave_metaapi_account.application != 'CopyFactory'
raise Exception('Please specify CopyFactory application field value in your MetaApi account in order to use it in CopyFactory API')
# create CopyFactory master and slave accounts and connect them to MetaApi accounts via connectionId field
configuration_api = copy_factory.configuration_api
master_account_id = configuration_api.generate_account_id()
slave_account_id = configuration_api.generate_account_id()
await configuration_api.update_account(master_account_id, {
'name': 'Demo account',
'connectionId': master_metaapi_account.id,
'subscriptions': []
})
# create a strategy being copied
strategy_id = await configuration_api.generate_strategy_id()
await configuration_api.update_strategy(strategy_id['id'], {
'name': 'Test strategy',
'description': 'Some useful description about your strategy',
'positionLifecycle': 'hedging',
'connectionId': slave_metaapi_account.id,
'maxTradeRisk': 0.1,
'stopOutRisk': {
'value': 0.4,
'startTime': '2020-08-24T00:00:00.000Z'
},
'timeSettings': {
'lifetimeInHours': 192,
'openingIntervalInMinutes': 5
}
})
# subscribe slave CopyFactory accounts to the strategy
await configuration_api.update_account(master_account_id, {
'name': 'Demo account',
'connectionId': master_metaapi_account.id,
'subscriptions': [
{
'strategyId': strategy_id,
'multiplier': 1
}
]
})
See in-code documentation for full definition of possible configuration options.
CopyFactory allows you to monitor transactions conducted on trading accounts in real time.
history_api = copy_factory.history_api
# retrieve list of subscribers
print(await history_api.get_subscribers())
# retrieve list of strategies provided
print(await history_api.get_provided_strategies())
# retrieve trading history, please note that this method support pagination and limits number of records
print(await history_api.get_provided_strategies_transactions(datetime.fromisoformat('2020-08-01'), datetime.fromisoformat('2020-09-01')))
history_api = copy_factory.history_api
# retrieve list of providers
print(await history_api.get_providers())
# retrieve list of strategies subscribed to
print(await history_api.get_strategies_subscribed())
# retrieve trading history, please note that this method support pagination and limits number of records
print(await history_api.get_strategies_subscribed_transactions(datetime.fromisoformat('2020-08-01'), datetime.fromisoformat('2020-09-01')))
There is a configurable time limit during which the trades can be opened. Sometimes trades can not open in time due to broker errors or trading session time discrepancy. You can resynchronize a slave account to place such late trades. Please note that positions which were closed manually on a slave account will also be reopened during resynchronization.
account_id = '...' # CopyFactory account id
# resynchronize all strategies
await copy_factory.trading_api.resynchronize(account_id)
# resynchronize specific strategy
await copy_factory.trading_api.resynchronize(account_id, ['ABCD'])
A subscription to a strategy can be stopped if the strategy have exceeded allowed risk limit.
trading_api = copy_factory.trading_api
account_id = '...' # CopyFactory account id
# retrieve list of strategy stopouts
print(await trading_api.get_stopouts(account_id))
# reset a stopout so that subscription can continue
await trading_api.reset_stopout(account_id, 'daily-equity)
Keywords: MetaTrader API, MetaTrader REST API, MetaTrader websocket API, MetaTrader 5 API, MetaTrader 5 REST API, MetaTrader 5 websocket API, MetaTrader 4 API, MetaTrader 4 REST API, MetaTrader 4 websocket API, MT5 API, MT5 REST API, MT5 websocket API, MT4 API, MT4 REST API, MT4 websocket API, MetaTrader SDK, MetaTrader SDK, MT4 SDK, MT5 SDK, MetaTrader 5 SDK, MetaTrader 4 SDK, MetaTrader python SDK, MetaTrader 5 python SDK, MetaTrader 4 python SDK, MT5 python SDK, MT4 python SDK, FX REST API, Forex REST API, Forex websocket API, FX websocket API, FX SDK, Forex SDK, FX python SDK, Forex python SDK, Trading API, Forex API, FX API, Trading SDK, Trading REST API, Trading websocket API, Trading SDK, Trading python SDK