Welcome to mql5_zmq_backtrader’s documentation!

mql5_zmq_backtrader

https://img.shields.io/pypi/v/mql5_zmq_backtrader.svg https://img.shields.io/travis/parrondo/mql5_zmq_backtrader.svg Documentation Status

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.

Features

  • TODO

Credits

This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.

Installation

Stable release

To install mql5_zmq_backtrader, run this command in your terminal:

$ pip install mql5_zmq_backtrader

This is the preferred method to install mql5_zmq_backtrader, as it will always install the most recent stable release.

If you don’t have pip installed, this Python installation guide can guide you through the process.

From sources

The sources for mql5_zmq_backtrader can be downloaded from the Github repo.

You can either clone the public repository:

$ git clone git://github.com/parrondo/mql5_zmq_backtrader

Or download the tarball:

$ curl -OJL https://github.com/parrondo/mql5_zmq_backtrader/tarball/master

Once you have a copy of the source, you can install it with:

$ python setup.py install

Usage

To use mql5_zmq_backtrader in a project:

import mql5_zmq_backtrader

mql5_zmq_backtrader

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.

Contributing

Contributions are welcome, and they are greatly appreciated! Every little bit helps, and credit will always be given.

You can contribute in many ways:

Types of Contributions

Report Bugs

Report bugs at https://github.com/parrondo/mql5_zmq_backtrader/issues.

If you are reporting a bug, please include:

  • Your operating system name and version.
  • Any details about your local setup that might be helpful in troubleshooting.
  • Detailed steps to reproduce the bug.

Fix Bugs

Look through the GitHub issues for bugs. Anything tagged with “bug” and “help wanted” is open to whoever wants to implement it.

Implement Features

Look through the GitHub issues for features. Anything tagged with “enhancement” and “help wanted” is open to whoever wants to implement it.

Write Documentation

mql5_zmq_backtrader could always use more documentation, whether as part of the official mql5_zmq_backtrader docs, in docstrings, or even on the web in blog posts, articles, and such.

Submit Feedback

The best way to send feedback is to file an issue at https://github.com/parrondo/mql5_zmq_backtrader/issues.

If you are proposing a feature:

  • Explain in detail how it would work.
  • Keep the scope as narrow as possible, to make it easier to implement.
  • Remember that this is a volunteer-driven project, and that contributions are welcome :)

Get Started!

Ready to contribute? Here’s how to set up mql5_zmq_backtrader for local development.

  1. Fork the mql5_zmq_backtrader repo on GitHub.

  2. Clone your fork locally:

    $ git clone git@github.com:your_name_here/mql5_zmq_backtrader.git
    
  3. Install your local copy into a virtualenv. Assuming you have virtualenvwrapper installed, this is how you set up your fork for local development:

    $ mkvirtualenv mql5_zmq_backtrader
    $ cd mql5_zmq_backtrader/
    $ python setup.py develop
    
  4. Create a branch for local development:

    $ git checkout -b name-of-your-bugfix-or-feature
    

    Now you can make your changes locally.

  5. When you’re done making changes, check that your changes pass flake8 and the tests, including testing other Python versions with tox:

    $ flake8 mql5_zmq_backtrader tests
    $ python setup.py test or pytest
    $ tox
    

    To get flake8 and tox, just pip install them into your virtualenv.

  6. Commit your changes and push your branch to GitHub:

    $ git add .
    $ git commit -m "Your detailed description of your changes."
    $ git push origin name-of-your-bugfix-or-feature
    
  7. Submit a pull request through the GitHub website.

Pull Request Guidelines

Before you submit a pull request, check that it meets these guidelines:

  1. The pull request should include tests.
  2. If the pull request adds functionality, the docs should be updated. Put your new functionality into a function with a docstring, and add the feature to the list in README.rst.
  3. The pull request should work for Python 3.5, 3.6, 3.7 and 3.8, and for PyPy. Check https://travis-ci.com/parrondo/mql5_zmq_backtrader/pull_requests and make sure that the tests pass for all supported Python versions.

Tips

To run a subset of tests:

$ python -m unittest tests.test_mql5_zmq_backtrader

Deploying

A reminder for the maintainers on how to deploy. Make sure all your changes are committed (including an entry in HISTORY.rst). Then run:

$ bump2version patch # possible: major / minor / patch
$ git push
$ git push --tags

Travis will then deploy to PyPI if tests pass.

Credits

Development Lead

    1. Martin Parrondo

Contributors

None yet. Why not be the first?

History

0.1.0 (2020-02-16)

  • First release on PyPI.

Indices and tables