• Stars
    star
    321
  • Rank 130,752 (Top 3 %)
  • Language
    Python
  • License
    MIT License
  • Created almost 11 years ago
  • Updated over 3 years ago

Reviews

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

Repository Details

Python wrapper for the OANDA REST API

oandapy

oandapy is a python wrapper for OANDA's REST API.

Install

Using pip:

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

oandapy depends on python-requests, which will be installed automatically.

Usage

Include the oandapy module and create an oandapy instance with your account credentials. For FxGame and FxTrade, an access token must be provided.

import oandapy

oanda = oandapy.API(environment="practice", access_token="abcdefghijk...")

Keyword arguments to functions are mapped to the functions available for each endpoint in the Oanda API docs, so changes to the API aren't held up from you using them by this library. For each api call, oandapy returns a native python object, converted from JSON so you don't have to.

The EndpointsMixin class in oandapy.py holds a mixin of all Oanda API endpoints.

Examples

Get price for an instrument

response = oanda.get_prices(instruments="EUR_USD")
prices = response.get("prices")
asking_price = prices[0].get("ask")

Open a limit order

# required datetime functions
from datetime import datetime, timedelta

# sample account_id
account_id = 1813880

# set the trade to expire after one day
trade_expire = datetime.utcnow() + timedelta(days=1)
trade_expire = trade_expire.isoformat("T") + "Z"

response = oanda.create_order(account_id,
    instrument="USD_CAD",
    units=1000,
    side='sell',
    type='limit',
    price=1.15,
    expiry=trade_expire
)

Rates Streaming

Create a custom streamer class to setup how you want to handle the data. Each tick is sent through the on_success and on_error functions. Since these methods are abstract methods, you need to override these methods to handle the streaming data.

The following example prints count ticks from the stream then disconnects.

class MyStreamer(oandapy.Streamer):
    def __init__(self, count=10, *args, **kwargs):
        super(MyStreamer, self).__init__(*args, **kwargs)
        self.count = count
        self.reccnt = 0

    def on_success(self, data):
        print data, "\n"
        self.reccnt += 1
        if self.reccnt == self.count:
            self.disconnect()

    def on_error(self, data):
        self.disconnect()

Initialize an instance of your custom streamer, and start connecting to the stream. See http://developer.oanda.com/rest-live/streaming/ for further documentation.

account = "12345"
stream = MyStreamer(environment="practice", access_token="abcdefghijk...")
stream.rates(account, instruments="EUR_USD,EUR_JPY,US30_USD,DE30_EUR")

The same procedure can be used for streaming events.

stream = MyStreamer(environment="practice", access_token="abcdefghijk...")
stream.events(ignore_heartbeat=False)

More Repositories

1

v20-python-samples

Sample python code that uses the v20 python library
Python
266
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