Code Monkey home page Code Monkey logo

oanda-trading-environment's Introduction

OANDA Trading Environment

PyPI version

The OANDA Trading Environment is built using the OANDA REST-API by making use of the https://github.com/oanda/oandapy API-wrapper.

Streaming Candles

Main part is the OANDAd daemon that parses the streaming quotes in configurable timeframes, by 1 minute, 5 minutes, 15 minutes etc. This makes it produce streaming candles.

As of version 0.3.0 OANDAd detects automatically the use of REST-V1 / REST-V20, by the account_id string in the OANDAd.cfg.

Candle data:

      {"data": {"instrument": "EUR_JPY",
                "granularity" : "M1",
                "start": "2015-09-02 15:36:00"
                "end": "2015-09-02 15:37:00",
                "completed": True,
                "data": {"high": 134.967, 
                         "open": 134.962,
                         "last": 134.9565,
                          "low": 134.9475,
                       "volume": 19
                 },
               }
       }

The larger timeframes can be requested using the API.

Streaming data can be controlled by the 'fabricate' setting in the 'streamer:' config section.

  • atEndOfTimeFrame - the default mode is to fabricate completed timeframes
  • dancingBear - generates dancing bear records with the 'completed' : False, until the record is completed. At that moment the record gets the status 'completed' : True.
  • dancingBearHighLowExtreme - generate dancing bear records only when the high or the low changes

The dancingBear setting generates as many records as ticks are received. This can be a lot. A compromise is the dancingBearHighLowExtreme. In case of extreme market moves the records will be generated also, but in a less volatile market less records will be generated.

Actions

When a timeframe is completed it can be handled by one or more plugins. Plugins have a configfile based on the name of the plugin-file, but in lowercase. The plugins can be found under etc/OANDA/plugins and the plugin configs under etc/OANDA/config/plugins.

Plugins need to be enabled in the config file, see the 'plugins:' section in etc/OANDA/config/OANDAd.cfg.

The environment comes with a few plugins:

Publish/Subscribe - plugin

This plugin can be configured to 'publish' the candle using a publisher/subscriber mechanism. This is achieved by using the 0MQ library and the python binding for it: pyzmq

Other trading applications can easily subscribe to receive the candle data. See here for a ZMQ subscription example.

This plugin is enabled by default.

Plainfile - plugin

This plugin can be configured to write candle records to a flatfile in a directory structure.

Example:

 /tmp/oandadb
 |-- BCO_USD
 |   |-- M1
 |   |   `-- cache
 |   |-- M15
 |   |   `-- cache
 |   `-- M5
 |       `-- cache
 |-- DE30_EUR
 |   |-- M1
 |   |   `-- cache
 |   |-- M15
 |   |   `-- cache
 |   `-- M5
 |       `-- cache
 |-- EUR_CHF
 |   |-- M1
 |   |   `-- cache
 |   |-- M15
 |   |   `-- cache
 |   `-- M5
 |       `-- cache
 |-- EUR_GBP
 |   |-- M1
 |   |   `-- cache
 |   |-- M15
 |   |   `-- cache
 |   `-- M5
 |       `-- cache
 |-- EUR_JPY
 |   |-- M1
 |   |   `-- cache
 |   |-- M15
 |   |   `-- cache
 |   `-- M5
 |       `-- cache
 |-- EUR_USD
     |-- M1
     |   `-- cache
     |-- M15
     |   `-- cache
     `-- M5
         `-- cache

MySQL - plugin

The MySQL plugin can be configured to insert records into a database. This plugin is provided as an example, since it needs details that depend on your databasemodel.

Auto trading

By using a ZMQ client

The desired approach is to create stand-alone applications that subscribe for quotes, see, example for a ZMQ subscription example.

By using plugins

Though it is possible to use the plugin facility to perform auto-trading, the way to go is to use ZMQ client and subscribe for quotes. This way you can completely isolate your trading code from the OANDAd daemon.

Security

The enviroment makes use of a token that gives access to crucial information.

Please pay attention to where you install this software. Never use this on a system that is not owned by you.

Make sure to secure your system as much as possible:

  • restrict network access
  • make no use of, or limit other network services (NFS, SAMBA, printserver etc.)
  • limit user access, preferable only you
  • use encryption
  • who about physical access ?

Specs and Prerequisites

To access the OANDA services you will need a token, see https://developer.oanda.com for details.

Install

Git

Install by using a virtual environment and git:

      $ cd <somewhere>
      $ mkdir OANDA
      $ cd OANDA
      $ virtualenv venv

Optionally use --system-site-packages to use the standard available packages for the python modules available on your system: pyyaml, pyzmq. Check for the packages on the distribution you use.

      $ . ./venv/bin/activate
      $ git clone https://github.com/hootnot/oanda-trading-environment.git
      $ cd oanda-trading-environment
      $ python setup.py install

OANDA has not made the oandapy module pip installable. A hack to get oandapy installed as a standalone module:

      $ pip install git+https://github.com/hootnot/oandapy

This will install the latest oandapy using the setup.py from the oandapy fork.

      $ pip list | grep oanda
      oanda-trading-environment (0.0.1)
      oandapy (0.1)

pip

Install from pypi:

using a virtual environment:

      $ cd <somewhere>
      $ mkdir OANDA
      $ cd OANDA
      $ virtualenv [--system-site-packages] venv
      $ . ./venv/bin/activate
      $ pip install git+https://github.com/hootnot/oandapy
      $ pip install --no-binary :all: oanda-trading-environment

using a system install:

      $ sudo pip install oanda-trading-environment
      $ sudo pip install git+https://github.com/hootnot/oandapy

Configure the OANDAd.cfg config file and start the daemon.

Quick start

After installing you need to configure the environment by editing the config file etc/OANDA/config/OANDAd.cfg. This is a YAML based configfile.

Configure the environment, default set to practice and the token. Alter the list of instruments you want to follow.

Though accounts can be requested using the API, the environment uses the account setting primarily to initialize the OANDAd for the streaming quotes. Therefore you need to configure the account_id also.

The pubsub plugin publishes by default at localhost, port 5550. These can be altered in the 'pubsub' config: etc/OANDA/config/plugins/pubsub.cfg.

Controlling OANDAd

OANDAd is built using daemoncle. The 'start', 'status' and 'stop' commands are implemented. The daemon forks itself and the child will process the stream. When there are issues, TIME-OUT for instance, the child will exit and a new child will be spawned.

      $ OANDAd start 
      Starting OANDAd ... OK

      $ OANDAd status
      OANDAd -- pid: 51931, status: sleeping, uptime: 0m, %cpu: 0.0, %mem: 1.8

      $ OANDAd stop
      Stopping OANDAd ... OK

The daemon will process streaming quotes now and process the timeframes as configured. Timeframes are currently based on the midprice of bid/ask.

Logging

The ticks received from the stream are written to a logfile:

 streamdata.<date>

The daemon itself logs to OANDAd.log

Loglevel and the streamdata logfile extension is configurable. Check the OANDAd.cfg file for details.

ZMQ - client

This simple piece of code acts as a subscriber to the daemon. All completed timeframes are written to stdout. Using ZMQ make it easy to program different strategies completely independent from each other. By using 'topics' it is possible to subscribe for a certain time granularity like M1, M5 etc. Check the ZMQ for details.

     import zmq

     context = zmq.Context()
     socket = context.socket( zmq.SUB)
     socket.connect("tcp://127.0.0.1:5550")
     socket.setsockopt(zmq.SUBSCRIBE, "")

     socket.setsockopt( zmq.RCVBUF, 1000)
     while True:
         msg = socket.recv()
         print "GOT: ", msg

This will show candle data like below, every time a timeframe is completed.

     GOT:  {"data": {
                     "instrument": "EUR_GBP",
                     "granularity": "M1",
                     "start": "2015-09-04 17:45:00",
                     "end": "2015-09-04 17:46:00",
                     "completed": True,
                     "data": {
                              "high": 0.734445,
                              "open": 0.734399,
                              "last": 0.73437,
                              "low": 0.734345,
                              "volume": 16
                             }
                    }
           }

     GOT:  {"data": {
                     "instrument": "EUR_JPY",
                     "granularity": "M1",
                     "start": "2015-09-04 17:45:00",
                     "end": "2015-09-04 17:46:00",
                     "completed": True,
                     "data": {
                              "high": 132.629,
                              "open": 132.619,
                              "last": 132.6185,
                              "low": 132.608,
                              "volume": 15
                             }
                    }
            }

     GOT:  {"data": {
                     "instrument": "SPX500_USD",
                     "granularity": "M1",
                     "start": "2015-09-04 17:45:00",
                     "end": "2015-09-04 17:46:00",
                     "completed": True,
                     "data": {
                              "high": 1915.35,
                              "open": 1914.75,
                              "last": 1915.25,
                              "low": 1914.75,
                              "volume": 33
                             }
                    }
            }

About this software

The oanda-trading-environment software is a personal project. I have no prior or existing relationship with OANDA.

If you have any questions regarding this software, please take a look at the documentation first.

If you still have questions/issues you can open an issue on Gitub: https://github.com/hootnot/oanda-trading-environment

oanda-trading-environment's People

Contributors

hootnot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

oanda-trading-environment's Issues

neworder.py syntax error

Line 35 is currently: "api = API(access_token=token)" but it should be: "api = API(access_token=token, headers={"Content-Type": "application/json"})

It also might be a good idea to add: "environment="practice"" so that if somebody executed this program and doesn't notice that it is in a live environment they don't get financially hurt.

Ansible deployment issues

Referring #2

Ansible version 2.2.0 gives an error when deploying the ote.yml playbook:

failed: [localhost] (item=[u'packages']) => {"failed": true, "item": ["packages"], "msg": "No package matching 'packages' is available"}

Install issue

Hello, I would like to use you daemon, but even after adding account id and access token to OANDAd.cfg during setup I get this error:


*** Please add the OANDA-user first to your system ***
*** missing: username: oanda, group: oanda ***


Then I tried to fill in some arbitrary data for user and group, but I still get the same error. Seems like a bug because in the readme it is noted that only account id and access token are needed to start using the app.

I tried pip installing as well while my virtual env is active. I have no idea where does the app get installed because I cannot import it, or find it in the installed modules.

I am using a conda virtual env. with python 2.7 on Ubunti 16.10.
Hope to get an answer, seems like a very useful library.

setup.py install raises "invalid token" error

Getting below error while installing... working with Python 3.7.5

(venv) C:\Users\user\source\repos\oanda\oanda-trading-environment>python setup.py install
  File "setup.py", line 17
    "etc/OANDA": 0700,
                    ^
SyntaxError: invalid token

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.