mql5_zmq_backtrader package

Submodules

mql5_zmq_backtrader.adapter module

class mql5_zmq_backtrader.adapter.Adapter(raw)[source]

Bases: object

class mql5_zmq_backtrader.adapter.BalanceAdapter(raw)[source]

Bases: mql5_zmq_backtrader.adapter.Adapter

class mql5_zmq_backtrader.adapter.OrderAdapter(raw)[source]

Bases: mql5_zmq_backtrader.adapter.Adapter

class mql5_zmq_backtrader.adapter.PositionAdapter(raw)[source]

Bases: mql5_zmq_backtrader.adapter.Adapter

mql5_zmq_backtrader.cli module

Console script for mql5_zmq_backtrader.

mql5_zmq_backtrader.mql5_zmq_backtrader module

Main module.

mql5_zmq_backtrader.mt5broker module

class mql5_zmq_backtrader.mt5broker.MTraderBroker(**kwargs)[source]

Bases: backtrader.broker.BrokerBase

Broker implementation for MetaTrader 5.

This class maps the orders/positions from MetaTrader to the internal API of backtrader.

Params:

  • use_positions (Ram default:True): When connecting to the broker provider use the existing positions to kickstart the broker.

    Set to False during instantiation to disregard any existing position

MTraderBroker(datas=None)
buy(owner, data, size, price=None, plimit=None, exectype=None, valid=None, tradeid=0, oco=None, trailamount=None, trailpercent=None, parent=None, transmit=True, **kwargs)[source]
cancel(order)[source]
data_started(data)[source]
frompackages = ()
get_notification()[source]
getcash()[source]
getposition(data, clone=True)[source]
getvalue(datas=None)[source]
next()[source]
notify(order)[source]
orderstatus(order)[source]
packages = ()
params

alias of backtrader.metabase.AutoInfoClass_BrokerBase_MTraderBroker

sell(owner, data, size, price=None, plimit=None, exectype=None, valid=None, tradeid=0, oco=None, trailamount=None, trailpercent=None, parent=None, transmit=True, **kwargs)[source]
start()[source]
stop()[source]
class mql5_zmq_backtrader.mt5broker.MTraderCommInfo[source]

Bases: backtrader.comminfo.CommInfoBase

frompackages = ()
getoperationcost(size, price)[source]

Returns the needed amount of cash an operation would cost

getvaluesize(size, price)[source]

Returns the value of size for given a price. For future-like objects it is fixed at size * margin

packages = ()
params

alias of backtrader.metabase.AutoInfoClass_CommInfoBase_MTraderCommInfo

class mql5_zmq_backtrader.mt5broker.MetaMTraderBroker(name, bases, dct)[source]

Bases: backtrader.broker.MetaBroker

mql5_zmq_backtrader.mt5data module

class mql5_zmq_backtrader.mt5data.MTraderData(**kwargs)[source]

Bases: backtrader.feed.DataBase

MTrader Data Feed.

TODO: implement tick data. Main problem is that Backtrader is not tick oriented. TODO: test backfill_from

Params:

  • historical (default: False)

    If set to True the data feed will stop after doing the first download of data.

    The standard data feed parameters fromdate and todate will be used as reference.

  • backfill (default: True)

    Perform backfilling after a disconnection/reconnection cycle. The gap duration will be used to download the smallest possible amount of data

  • backfill_from (default: None)

    An additional data source can be passed to do an initial layer of backfilling. Once the data source is depleted and if requested, backfilling from IB will take place. This is ideally meant to backfill from already stored sources like a file on disk, but not limited to.

  • include_last (default: False)

    Last historical candle is not closed. It will be updated in live stream

  • reconnect (default: True)

    Reconnect when network connection is down

alias = ()
aliased = ''
frompackages = ()
haslivedata()[source]
islive()[source]

True notifies Cerebro that preloading and runonce should be deactivated

linealias

alias of backtrader.metabase.AutoInfoClass_la_LineSeries_la_DataSeries_la_OHLC_la_OHLCDateTime_la_AbstractDataBase_la_DataBase_la_MTraderData

lines

alias of backtrader.lineseries.Lines_LineSeries_DataSeries_OHLC_OHLCDateTime_AbstractDataBase_DataBase_MTraderData

packages = ()
params

alias of backtrader.metabase.AutoInfoClass_LineRoot_LineMultiple_LineSeries_DataSeries_OHLC_OHLCDateTime_AbstractDataBase_DataBase_MTraderData

plotinfo

alias of backtrader.metabase.AutoInfoClass_pi_LineSeries_pi_DataSeries_pi_OHLC_pi_OHLCDateTime_pi_AbstractDataBase_pi_DataBase_pi_MTraderData

plotlines

alias of backtrader.metabase.AutoInfoClass_pl_LineSeries_pl_DataSeries_pl_OHLC_pl_OHLCDateTime_pl_AbstractDataBase_pl_DataBase_pl_MTraderData

setenvironment(env)[source]

Receives an environment (cerebro) and passes it over to the store it belongs to

start()[source]

Starts the MTrader connection and gets the real contract and contractdetails if it exists

stop()[source]

Stops and tells the store to stop

class mql5_zmq_backtrader.mt5data.MetaMTraderData(name, bases, dct)[source]

Bases: backtrader.feed.MetaAbstractDataBase

mql5_zmq_backtrader.mt5store module

class mql5_zmq_backtrader.mt5store.MTraderAPI(host=None)[source]

Bases: object

This class implements Python side for MQL5 JSON API See https://github.com/khramkov/MQL5-JSON-API for docs

construct_and_send(**kwargs) → dict[source]

Construct a request dictionary from default and send it to server

live_socket(context=None)[source]

Connect to socket in a ZMQ context

streaming_socket(context=None)[source]

Connect to socket in a ZMQ context

exception mql5_zmq_backtrader.mt5store.MTraderError(*args, **kwargs)[source]

Bases: Exception

class mql5_zmq_backtrader.mt5store.MTraderStore(host='localhost')[source]

Bases: object

Singleton class wrapping to control the connections to MetaTrader.

Balance update occurs at the beginning and after each transaction registered by ‘_t_streaming_events’.

BrokerCls

alias of mql5_zmq_backtrader.mt5broker.MTraderBroker

DataCls

alias of mql5_zmq_backtrader.mt5data.MTraderData

broker_threads()[source]
cancel_order(oid, symbol)[source]
candles(dataname, dtbegin, dtend, timeframe, compression, include_first=False)[source]
check_account() → None[source]

Get MetaTrader 5 account settings

close_position(oid, symbol)[source]
frompackages = ()
get_balance()[source]
get_cash()[source]
get_granularity(timeframe, compression)[source]
get_notifications()[source]

Return the pending “store” notifications

get_positions()[source]
get_value()[source]
classmethod getbroker(*args, **kwargs)[source]

Returns broker with *args, **kwargs from registered BrokerCls

classmethod getdata(*args, **kwargs)[source]

Returns DataCls with args, kwargs

order_cancel(order)[source]
order_create(order, stopside=None, takeside=None, **kwargs)[source]

Creates an order

packages = ()
params

alias of backtrader.metabase.AutoInfoClass_MTraderStore

put_notification(msg, *args, **kwargs)[source]
start(data=None, broker=None)[source]
stop()[source]
streaming_events()[source]
class mql5_zmq_backtrader.mt5store.MetaSingleton(name, bases, dct)[source]

Bases: backtrader.metabase.MetaParams

Metaclass to make a metaclassed class a singleton

exception mql5_zmq_backtrader.mt5store.ServerConfigError(*args, **kwargs)[source]

Bases: mql5_zmq_backtrader.mt5store.MTraderError

exception mql5_zmq_backtrader.mt5store.ServerDataError(*args, **kwargs)[source]

Bases: mql5_zmq_backtrader.mt5store.MTraderError

exception mql5_zmq_backtrader.mt5store.StreamError(*args, **kwargs)[source]

Bases: mql5_zmq_backtrader.mt5store.MTraderError

exception mql5_zmq_backtrader.mt5store.TimeFrameError(*args, **kwargs)[source]

Bases: mql5_zmq_backtrader.mt5store.MTraderError

Module contents

Top-level package for mql5_zmq_backtrader. Project developed to work as a server for Python trading community. It is based on ZeroMQ sockets and uses JSON format to communicate messages. It is a python library for the ZeroMQ API within backtrader framework. It allows rapid trading algo development. For details of API behavior, please see the online API document.