Skip to content

Latest commit

History

49 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

v20-python-samples

This repo contains a suite of Python sample code that desmonstrates the use of OANDA's v20 REST API along with OANDA's v20 bindings for Python.

Setup

The following procedure describes how to create a virtualenv appropriate for running the v20 sample code:

## Set up the virtualenv and install required packages. By default the# virtualenv will be setup to use python3. If python2 is desired, use the make# target "bootstrap-python2" and the virtualenv will be created under# "env-python2"#
user@host: ~/v20-python-samples$ make bootstrap
## Enter the virtualenv#
user@host: ~/v20-python-samples$ source env/bin/activate
## Create the v20-* launch entry points in the virtualenv. These entry points# are aliases for the scripts which use the v20 REST API to interact with an# account (e.g. v20-market-order, v20-trades-list, etc.)#
(env)user@host: ~/v20-python-samples$ python setup.py develop

Entering the v20 environment

The v20-python-samples virtualenv must be activated to ensure that the current enviroment is set up correctly to run the sample code. This is done using the virualenv's activate script:

user@host: ~/v20-python-samples$ source env/bin/activate
(env)user@host: ~/v20-python-samples$

The "(env)" prefix found in the prompt indicates that we are using the virtualenv "env". To leave the virtualenv, run the deactivate function:

(env)user@host: ~/v20-python-samples$ deactivate
user@host: ~/v20-python-samples$ 

Configuration-free Example

Most of the examples provided use a v20.conf discussed below. For a full example of how to create and use a v20 API context without the configuration wrapper, please examine src/market_order_full_example.py. This program enables the creation of a limited Market Order solely based on command line arguments.

Configuration

Using OANDA's v20 REST API requires configuration to set up connections and interact with the endpoints. This configuration includes:

  • API hostname
  • API port
  • API token
  • username of client
  • Account ID of account being manipulated

To simplify the management of this configuration, the v20 Python sample code requires that a configuration file be created. All of the sample code loads this configuration file prior to connecting to the v20 system.

v20 Configuration File Format

The v20 configuration is stored in a YAML file that resembles the following:

hostname: api-fxpractice.oanda.comstreaming_hostname: stream-fxpractice.oanda.comport: 443ssl: truetoken: e6ab562b039325f12a026c6fdb7b71bb-b3d8721445817159410f01514acd19hbcusername: useraccounts:
- 101-001-100000-001
- 101-001-100000-002active_account: 101-001-100000-001

Generating v20 Configuration files

v20 configuration files may be generated manually, however a script is provided that will generate one interactively located at src/configure.py.

To run it and generate a v20 configuration file, simply run:

(env)user@host: ~/v20-python-samples$ v20-configure

and follow the instructions.

Using v20 Configuration files

There are several ways to load a v20 configuration file in each of v20 sample scripts:

1. Run the script with the --config option

The --config options allows you to specify the location of a valid v20 configuration file v20. Example:

(env)user@host: ~/v20-python-samples$ v20-account-details --config /home/user/v20.conf

2. Use the default v20 configuration file location

The default location for the v20 configuration file is ~/.v20.conf. If a v20 configuration file exists in the default location, no --config option needs to be used. Example:

# Looks for config file at ~/.v20.conf
(env)user@host: ~/v20-python-samples$ v20-account-details

3. Set the location of the V20_CONF environment variable

This V20_CONF environment variable changes what the default location of the v20 configuration file is. If a configuration file exists in this location, no --config option needs to be used. Example:

(env)user@host: ~/v20-python-samples$ export V20_CONF=/home/user/v20.conf
(env)user@host: ~/v20-python-samples$ v20-account-details

Sample Code

Following is a listing of the sample code provided. More details can be found in the READMEs provided in each src directory.

Source FileEntry PointDescription
src/configure.pyv20-configureCreate/update a v20.conf file
src/market_order_full_example.pyv20-market-order-full-exampleLimited Market Order example that does not use the v20.conf file
src/account/details.pyv20-account-detailsGet the details of the current active Account
src/account/summary.pyv20-account-summaryGet the summary of the current active Account
src/account/instruments.pyv20-account-instrumentsGet the list of tradeable instruments for the current active Account
src/account/changes.pyv20-account-changesFollow changes to the current active Account
src/account/configure.pyv20-account-configureSet configuration in the current active Account
src/instrument/candles.pyv20-instrument-candlesFetch candles for an instrument
src/instrument/candles_poll.pyv20-instrument-candles-pollFetch and poll for candle updates for an instrument
src/order/get.pyv20-order-getGet the details of an order in the current active Account
src/order/list_pending.pyv20-order-list-pendingList all pending Orders for the current active Account
src/order/cancel.pyv20-order-cancelCancel a pending Order in the current active Account
src/order/set_client_extensions.pyv20-order-set-client-extensionsSet the client extensions for a pending Order in the current active Account
src/order/market.pyv20-order-marketCreate a Market Order in the current active Account
src/order/entry.pyv20-order-entryCreate or replace an Entry Order in the current active Account
src/order/limit.pyv20-order-limitCreate or replace a Limit Order in the current active Account
src/order/stop.pyv20-order-stopCreate or replace a Stop Order in the current active Account
src/order/take-profit.pyv20-order-take-profitCreate or replace a Take Profit Order in the current active Account
src/order/stop-loss.pyv20-order-stop-lossCreate or replace a Stop Loss Order in the current active Account
src/order/trailing-stop-loss.pyv20-order-trailing-stop-lossCreate or replace a Trailing Stop Loss Order in the current active Account
src/pricing/get.pyv20-pricing-getFetch/poll the current Prices for a list of Instruments
src/pricing/stream.pyv20-pricing-streamStream Prices for a list of Instruments
src/transaction/stream.pyv20-transaction-streamStream Transactions for the current active Account
src/transaction/poll.pyv20-transaction-pollPoll Transactions for the current active Account
src/transaction/get.pyv20-transaction-getGet details for a Transaction in the current active Account
src/transaction/range.pyv20-transaction-rangeGet a range of Transactions in the current active Account
src/trade/get.pyv20-trade-getGet all open Trades or a specific Trade in the current active Account
src/trade/close.pyv20-trade-closeClose (partially or fully) a Trade in the current active Account
src/trade/set_client_extensions.pyv20-trade-set-client-extensionsSet the client extensions for an open Trade in the current active Account
src/position/close.pyv20-position-closeClose a position for an instrument in the current active Account

About

Sample python code that uses the v20 python library

Resources

Stars

286 stars

Watchers

60 watching

Forks

Releases

Packages

Used by

Contributors

Languages