From 5328459a3877be7657ba3c5df590194db1315e1a Mon Sep 17 00:00:00 2001 From: femtotrader Date: Mon, 25 Jul 2016 14:45:12 +0200 Subject: [PATCH] Update README.md Add syntax highlight --- README.md | 95 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 53 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index bff1842..8773f83 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,9 @@ Install Using pip: - $ pip install git+https://github.com/oanda/oandapy.git +```bash +$ pip install git+https://github.com/oanda/oandapy.git +``` oandapy depends on python-requests, which will be installed automatically. @@ -16,9 +18,11 @@ 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 +```python +import oandapy - oanda = oandapy.API(environment="practice", access_token="abcdefghijk...") +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](http://developer.oanda.com/), 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. @@ -28,29 +32,34 @@ Examples ====== ### Get price for an instrument - response = oanda.get_prices(instruments="EUR_USD") - prices = response.get("prices") - asking_price = prices[0].get("ask") +```python +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 +```python +# required datetime functions +from datetime import datetime, timedelta - # set the trade to expire after one day - trade_expire = datetime.utcnow() + timedelta(days=1) - trade_expire = trade_expire.isoformat("T") + "Z" +# sample account_id +account_id = 1813880 - response = oanda.create_order(account_id, - instrument="USD_CAD", - units=1000, - side='sell', - type='limit', - price=1.15, - expiry=trade_expire - ) +# 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 ====== @@ -61,34 +70,36 @@ 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): +```python +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") - +```python +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) - +```python +stream = MyStreamer(environment="practice", access_token="abcdefghijk...") +stream.events(ignore_heartbeat=False) +```