• Stars
    star
    266
  • Rank 154,103 (Top 4 %)
  • Language
    Python
  • License
    MIT License
  • Created about 8 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Sample python code that uses the v20 python library

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.com
streaming_hostname: stream-fxpractice.oanda.com
port: 443
ssl: true
token: e6ab562b039325f12a026c6fdb7b71bb-b3d8721445817159410f01514acd19hbc
username: user
accounts:
- 101-001-100000-001
- 101-001-100000-002
active_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 File Entry Point Description
src/configure.py v20-configure Create/update a v20.conf file
src/market_order_full_example.py v20-market-order-full-example Limited Market Order example that does not use the v20.conf file
src/account/details.py v20-account-details Get the details of the current active Account
src/account/summary.py v20-account-summary Get the summary of the current active Account
src/account/instruments.py v20-account-instruments Get the list of tradeable instruments for the current active Account
src/account/changes.py v20-account-changes Follow changes to the current active Account
src/account/configure.py v20-account-configure Set configuration in the current active Account
src/instrument/candles.py v20-instrument-candles Fetch candles for an instrument
src/instrument/candles_poll.py v20-instrument-candles-poll Fetch and poll for candle updates for an instrument
src/order/get.py v20-order-get Get the details of an order in the current active Account
src/order/list_pending.py v20-order-list-pending List all pending Orders for the current active Account
src/order/cancel.py v20-order-cancel Cancel a pending Order in the current active Account
src/order/set_client_extensions.py v20-order-set-client-extensions Set the client extensions for a pending Order in the current active Account
src/order/market.py v20-order-market Create a Market Order in the current active Account
src/order/entry.py v20-order-entry Create or replace an Entry Order in the current active Account
src/order/limit.py v20-order-limit Create or replace a Limit Order in the current active Account
src/order/stop.py v20-order-stop Create or replace a Stop Order in the current active Account
src/order/take-profit.py v20-order-take-profit Create or replace a Take Profit Order in the current active Account
src/order/stop-loss.py v20-order-stop-loss Create or replace a Stop Loss Order in the current active Account
src/order/trailing-stop-loss.py v20-order-trailing-stop-loss Create or replace a Trailing Stop Loss Order in the current active Account
src/pricing/get.py v20-pricing-get Fetch/poll the current Prices for a list of Instruments
src/pricing/stream.py v20-pricing-stream Stream Prices for a list of Instruments
src/transaction/stream.py v20-transaction-stream Stream Transactions for the current active Account
src/transaction/poll.py v20-transaction-poll Poll Transactions for the current active Account
src/transaction/get.py v20-transaction-get Get details for a Transaction in the current active Account
src/transaction/range.py v20-transaction-range Get a range of Transactions in the current active Account
src/trade/get.py v20-trade-get Get all open Trades or a specific Trade in the current active Account
src/trade/close.py v20-trade-close Close (partially or fully) a Trade in the current active Account
src/trade/set_client_extensions.py v20-trade-set-client-extensions Set the client extensions for an open Trade in the current active Account
src/position/close.py v20-position-close Close a position for an instrument in the current active Account

More Repositories

1

oandapy

Python wrapper for the OANDA REST API
Python
321
star
2

v20-python

OANDA v20 bindings for Python
Python
220
star
3

py-api-trading

Sample programs trading with the OANDA API through Python2.7
Python
79
star
4

v20-javascript

OANDA v20 bindings for Javascript
JavaScript
66
star
5

v20-java

v20 API libraries for the java programming language
Java
39
star
6

apidocs

OANDA REST API Documentation
35
star
7

oandajs

Javascript wrapper for the OANDA REST API
JavaScript
30
star
8

v20-openapi

OpenAPI Specification for OANDA's v20 REST API
19
star
9

rest-csharp-refimpl

Reference implementation of the OANDA REST API in C#
C#
17
star
10

oanda-shell

Bash script to access the oanda-api
Shell
16
star
11

simple-rates-panel

Javascript program that displays an updating quote panel for any currency pair using the OANDA REST API.
HTML
15
star
12

candlestick-graph

Candlestick charting using the REST API
JavaScript
14
star
13

v20-java-samples

Some sample applications using the v20 java libraries
Java
14
star
14

CSharpLibAPISample

Sample C# library that provides easy access to the new OANDA rest api
C#
14
star
15

libfixed

A C++ fixed point math library suitable for financial applications.
C++
13
star
16

CAPISample

C
11
star
17

mt4-fxlabs

MQL4 bindings for the OANDA fxLabs API
11
star
18

py-position-aggregation

Sample Python app for aggregating positions across multiple accounts using the OANDA api
Python
11
star
19

v20-javascript-samples

JavaScript
10
star
20

iOSNetworkingWithOandaApi

A small library that wraps the OANDA API, for use in iOS
Objective-C
10
star
21

csharp-exchange-rates

OANDA Exchange Rates API client module for C#
C#
9
star
22

java-exchange-rates

Java
8
star
23

nodejs-exchange-rates

OANDA Exchange Rates API client module for nodejs
JavaScript
7
star
24

rescript-openapi

ReScript bindings to OpenAPI 3 + JSON Schema spec
ReScript
7
star
25

spread_for_node

Spread Toolkit for Node.js
JavaScript
6
star
26

js_client_side_oauth_flow

A simple JavaScript application to demonstrate the client side OAUTH flow
JavaScript
6
star
27

cl-restapi-demo

A simple candle average price calculator in common lisp
Common Lisp
5
star
28

AndroidRatesAPISample

Simple android app that hits the new OANDA API for rates
Java
5
star
29

fixapidocs

OANDA FIX API
5
star
30

oflux

a domain specific language with event-based runtime for reactive C++ programs
C++
4
star
31

streamingapi-demo

Demo app for the streaming rates API
JavaScript
4
star
32

AccountManagerPHP

An account manager demo implementing the OANDA API.
JavaScript
4
star
33

oanda-restapi-latency-test

Oanda REST API latency test
Python
4
star
34

c-api-streaming

A sample C application that connects to OANDA's HTTP based events stream.
C
4
star
35

javadocs

Documentation for our Java API
Java
3
star
36

android-logging-library

An Android library that adds a write-to-file layer before Android's Log class
Java
3
star
37

SliderWithNumberPad

A slider control that shows the input value directly on the slider thumb and has a popup number pad for precision input.
Objective-C
3
star
38

rb-api-streaming

A sample Ruby application that connects to OANDA's HTTP based events stream.
Ruby
2
star
39

HelpCarousel

A carousel used to showcase slides of information.
Objective-C
2
star
40

pl-api-streaming

A sample Perl application that connects to OANDA's HTTP based events stream.
Perl
2
star
41

pair-first

A pair-centric approach and paradigm shift on the traditional account-centric Forex trading UIs.
JavaScript
2
star
42

oandajs-gui

GUI for OANDA's API javascript wrapper
CSS
1
star
43

git-deps

Lightweight alternative to submodules
Shell
1
star
44

perl-webservice-oanda-exchangerates

Perl wrapper for the OANDA Exchange Rates API
Perl
1
star