Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ See http://developer.oanda.com/rest-live/streaming/ for further documentation.
stream.rates(account, instruments="EUR_USD,EUR_JPY,US30_USD,DE30_EUR")


The same procedure can be used for streaming events.
Events Streaming
======

Like the streaming rates we can get streaming events:


stream = MyStreamer(environment="practice", access_token="abcdefghijk...")
Expand Down
150 changes: 150 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
Unittests
==============

Authentication
---------------

The tests need to be run against an enviroment. Default it is set to "practice".
Various orders are created by the tests leading to positions in different instruments!!. ** DO NOT CHANGE TO YOUR 'live' ACCOUNT ** .

You must provide the accountnumber and token in _account.txt_ and _token.txt_ ortherwise the environment setup will fail and you will get an error message.

Running tests
-------------

To run the tests you must clone the git repository.
Then from there you can run:

$ cd oandapy
$ python setup.py test

This wil run all tests and should give back something like:

----------------------------------------------------------------------
Ran 32 tests in 41.780s

OK

This goes for both Python 2.7.x and Python 3.x.

Test Groups
-----------

The tests are grouped according the API documentation: Rates, Accounts etc.
Tests can be run by group:

$ cd oandapy
$ python setup.py test -s test.test_orders


Nosetests
------------

You can also use **nosetests** to perform the tests.

[hootnot@centos7 oandapy]$ nosetests -v tests
get account ... ok
get all accounts ... ok
get commitment of traders ... ok
get economic calendar info ... ok
get historical position ratios ... ok
get historical spreads ... ok
get orderbook data ... ok
close an order ... ok
create a new limit order resulting in a pending order ... ok
create a new limit order resulting in an error ... ok
create a new market order resulting in a trade ... ok
get information for an order ... ok
get all orders for an account ... ok
modify an existing order ... ok
close an existing position ... ok
get position for specific instrument ... ok
get all open positions for account ... ok
get history ... ok
get history ... request count records of granularity M15 ... ok
get history ... request count records ... ok
get requested instruments ... ok
get all instruments ... ok
get current prices ... ok
get current prices: since ... ok
get records from stream and including heartbeats, ... ok
get records from stream and ignore heartbeats, ... ok
close an open trade ... ok
get specific trade ... ok
get all open trades for account ... ok
modify properties of a trade ... ok
get information on transaction ... ok
get transaction history ... ok

----------------------------------------------------------------------
Ran 32 tests in 45.046s

OK


### Python 3.x

You can apply the **nosetests** command of different Python versions:

[hootnot@centos7 oandapy]$ /usr/local/bin/nosetests-3.4 -v tests

... the tests ...

----------------------------------------------------------------------
Ran 32 tests in 44.215s



### Codecoverage

Making use of **nosetests**, it is also possible to generate a codecoverage report.The result of the command below is that you will find a navigatable HTML report
of the covered module, oandapy in this case, in the directory
_/tmp/oandapy.coverage_.

It will show in detail the lines tested and the lines not tested. The text output
of the **nosetest** command will give you the same stats in text.

[hootnot@centos7 oandapy]$ nosetests -v tests --cover-html --cover-html-dir /tmp/oandapy.coverage --with-coverage --cover-package=oandapy
get account ... ok
get all accounts ... ok
get commitment of traders ... ok
get economic calendar info ... ok
get historical position ratios ... ok
get historical spreads ... ok
get orderbook data ... ok
close an order ... ok
create a new limit order resulting in a pending order ... ok
create a new limit order resulting in an error ... ok
create a new market order resulting in a trade ... ok
get information for an order ... ok
get all orders for an account ... ok
modify an existing order ... ok
close an existing position ... ok
get position for specific instrument ... ok
get all open positions for account ... ok
get history ... ok
get history ... request count records of granularity M15 ... ok
get history ... request count records ... ok
get requested instruments ... ok
get all instruments ... ok
get current prices ... ok
get current prices: since ... ok
get records from stream and including heartbeats, ... ok
get records from stream and ignore heartbeats, ... ok
close an open trade ... ok
get specific trade ... ok
get all open trades for account ... ok
modify properties of a trade ... ok
get information on transaction ... ok
get transaction history ... ok

Name Stmts Miss Cover Missing
-----------------------------------------------
oandapy 1 0 100%
oandapy.oandapy 149 13 91% 45-46, 221, 224-225, 235, 264-265, 294-295, 323, 340, 348
-----------------------------------------------
TOTAL 150 13 91%
----------------------------------------------------------------------
Ran 32 tests in 45.046s

OK
46 changes: 46 additions & 0 deletions tests/test_accounts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import unittest
import oandapy
import sys
import json
from . import unittestsetup
from .unittestsetup import environment as environment

access_token = None
account = None
api = None
instrumentList = ["EUR_USD", "EUR_JPY", "DE30_EUR"]


class TestAccounts(unittest.TestCase):

def setUp(self):
global access_token
global account
global api
# self.maxDiff = None
try:
account, access_token = unittestsetup.auth()
except Exception as e:
print("%s" % e)
exit(0)

api = oandapy.API(environment=environment, access_token=access_token)

def test__get_acounts(self):
""" get all accounts
normally a user has at least one account
"""
result = api.get_accounts()
count = len(result['accounts'])
self.assertGreaterEqual(count, 1)

def test__get_acount(self):
""" get account
the details of specified account
"""
result = api.get_account(account)
self.assertEqual(result['accountName'], "Primary")

if __name__ == "__main__":

unittest.main()
68 changes: 68 additions & 0 deletions tests/test_forexlabs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import unittest
import oandapy
import sys
import json
from . import unittestsetup
from .unittestsetup import environment as environment

access_token = None
account = None
api = None


class TestForexLabs(unittest.TestCase):

def setUp(self):
global access_token
global account
global api

try:
account, access_token = unittestsetup.auth()
except Exception as e:
print("%s" % e)
exit(0)

api = oandapy.API(environment=environment, access_token=access_token)

def test__get_eco_calendar(self):
""" get economic calendar info """
# request a month of events, so we should have at least some events
r = api.get_eco_calendar(period=2592000)
count = len(r)
self.assertTrue(count > 0 and "title" in r[0] and "timestamp" in r[0])

def test__get_historical_position_ratios(self):
""" get historical position ratios """
instrument = "EUR_USD"
r = api.get_historical_position_ratios(instrument=instrument,
period=2592000)
# response should contain { "data": { "EUR_USD": { "data": [ [..],
self.assertTrue(len(r["data"][instrument]["data"]) and
r["data"][instrument]["label"])

def test__get_historical_spreads(self):
""" get historical spreads """
r = api.get_historical_spreads(instrument="EUR_USD",
period=604800)
bodyHas = "max" in r and "min" in r and "avg" in r
self.assertTrue(bodyHas)

def test__get_commitments_of_traders(self):
""" get commitment of traders """
r = api.get_commitments_of_traders(instrument="EUR_USD")
self.assertTrue(len(r['EUR_USD']) > 0)

def test__get_orderbook(self):
""" get orderbook data """
# r contains a body with hashes having an epoch time as a key
# these can be converted:
# - time.strftime("%Y%m%d %H:%M:%S", time.localtime(1382037600))
# - time.gmtime(1382037600)
r = api.get_orderbook(instrument="EUR_USD")
self.assertTrue(len(r.keys()) > 0)


if __name__ == "__main__":

unittest.main()
104 changes: 104 additions & 0 deletions tests/test_orders.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import unittest
import oandapy
import sys
import json
from . import unittestsetup
from .unittestsetup import orders as orders
from .unittestsetup import environment as environment

access_token = None
account = None
api = None


class TestOrders(unittest.TestCase):

def setUp(self):
global access_token
global account
global api
# self.maxDiff = None
try:
account, access_token = unittestsetup.auth()
except Exception as e:
print("%s" % e)
exit(0)

api = oandapy.API(environment=environment, access_token=access_token)

# tests
def test__get_orders(self):
""" get all orders for an account """
orderSpec = orders['EUR_USD_limit']

r = api.get_orders(account)
countBefore = len(r['orders'])
# now create an order
r = api.create_order(account, **orderSpec)
r = api.get_orders(account)
countAfter = len(r['orders'])
self.assertTrue(countAfter == countBefore + 1)

def test__create_order_market(self):
""" create a new market order resulting in a trade """
orderSpec = orders['EUR_USD_market']

r = api.create_order(account, **orderSpec)
self.assertEqual(orderSpec['units'], r['tradeOpened']['units'])

def test__create_order_limit(self):
""" create a new limit order resulting in a pending order """
orderSpec = orders['EUR_USD_limit']

r = api.create_order(account, **orderSpec)
self.assertEqual(orderSpec['units'], r['orderOpened']['units'])

def test__create_order_limit_No_expiry(self):
""" create a new limit order resulting in an error """
orderSpec = orders['EUR_USD_limit_No_expiry']

oErr = None
with self.assertRaises(oandapy.OandaError) as oErr:
r = api.create_order(account, **orderSpec)
# print "%s" % oErr.exception
self.assertEqual(oErr.exception.error_response['code'], 2)

def test__get_order(self):
""" get information for an order """
orderSpec = orders['EUR_USD_limit']

r1 = api.create_order(account, **orderSpec)
orderId = r1['orderOpened']['id']
r1 = r1['orderOpened']
r2 = api.get_order(account, orderId)
self.assertTrue(r1, r2)

def test__modify_order(self):
""" modify an existing order """
orderSpec = orders['EUR_USD_limit']

r1 = api.create_order(account, **orderSpec)
orderId = r1['orderOpened']['id']
# modify the price
r2 = api.modify_order(account, orderId, price=0.8)
self.assertTrue(r1['price'] == 0.7 and r2['price'] == 0.8)

def test__close_order(self):
""" close an order """
orderSpec = orders['EUR_USD_limit']

r1 = api.create_order(account, **orderSpec)
orderId = r1['orderOpened']['id']
# close the order
r2 = api.close_order(account, orderId)
# it should be gone, perform a get_order on the orderId should give
# an error: 11 ... Order not found
oErr = None
with self.assertRaises(oandapy.OandaError) as oErr:
api.get_order(account, orderId)
self.assertEqual(oErr.exception.error_response['code'], 11)


if __name__ == "__main__":

unittest.main()
Loading