From 239e2b3c9ee891e3d7770e2a2a6a70dbf6809f50 Mon Sep 17 00:00:00 2001 From: hootnot Date: Sat, 21 Oct 2017 18:19:11 +0200 Subject: [PATCH 1/3] CSVFactory / Panda DataFrameFactory --- oandapyV20/contrib/factories/__init__.py | 4 ++++ oandapyV20/contrib/factories/conv.py | 27 +++++++++++++++++++++ oandapyV20/contrib/factories/csv.py | 26 ++++++++++++++++++++ oandapyV20/contrib/factories/dataframe.py | 29 +++++++++++++++++++++++ 4 files changed, 86 insertions(+) create mode 100644 oandapyV20/contrib/factories/conv.py create mode 100644 oandapyV20/contrib/factories/csv.py create mode 100644 oandapyV20/contrib/factories/dataframe.py diff --git a/oandapyV20/contrib/factories/__init__.py b/oandapyV20/contrib/factories/__init__.py index 22447f0..542ad7a 100644 --- a/oandapyV20/contrib/factories/__init__.py +++ b/oandapyV20/contrib/factories/__init__.py @@ -1,5 +1,9 @@ from .history import InstrumentsCandlesFactory +from .csv import CSVFactory +from .dataframe import DataFrameFactory __all__ = ( 'InstrumentsCandlesFactory', + 'CSVFactory', + 'DataFrameFactory', ) diff --git a/oandapyV20/contrib/factories/conv.py b/oandapyV20/contrib/factories/conv.py new file mode 100644 index 0000000..394661a --- /dev/null +++ b/oandapyV20/contrib/factories/conv.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- +from collections import OrderedDict + +column_map_tohlcv = OrderedDict([ + ('time', 'Time'), + ('mid:o', 'Open'), + ('mid:h', 'High'), + ('mid:l', 'Low'), + ('mid:c', 'Close'), + ('volume', 'Volume') +]) + + +def convrec(r, m): + """convrec - convert OANDA candle record. + + return array of values, dynamically constructed, corresponding + with config in mapping m. + """ + v = [] + for keys in [x.split(":") for x in m.keys()]: + _v = r.get(keys[0]) + for k in keys[1:]: + _v = _v.get(k) + v.append(_v) + + return v diff --git a/oandapyV20/contrib/factories/csv.py b/oandapyV20/contrib/factories/csv.py new file mode 100644 index 0000000..bbfc962 --- /dev/null +++ b/oandapyV20/contrib/factories/csv.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +from .conv import convrec, column_map_tohlcv + + +def CSVFactory(r, conv=convrec, column_map=column_map_tohlcv, delim=","): + """CSVFactory - convert candlerecords to CSV strings. + + Dynamically convert candlerecords to CSV strings by the configuration + passed by *conf*. + + >>> column_map_tcv = OrderedDict([ + ... ('time', 'Time'), + ... ('mid:c', 'Close'), + ... ('volume', 'Volume') + ... ]) + >>> params = {"instruments": "EUR_USD,EUR_GBP"} + >>> r = instruments.InstrumentsCandles(instrument=instr, params=params) + >>> api.request(r) + >>> for _r in CSVFactory(r.response): + >>> print(_r) + >>> for _r in CSVFactory(r.response, column_map=column_map_tcv, delim="|"): + >>> print(_r) + """ + for rec in r.get('candles'): + # make all values strings before join + yield delim.join([str(x) for x in list(conv(rec, column_map))]) diff --git a/oandapyV20/contrib/factories/dataframe.py b/oandapyV20/contrib/factories/dataframe.py new file mode 100644 index 0000000..2cb867a --- /dev/null +++ b/oandapyV20/contrib/factories/dataframe.py @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- +import pandas as pd + +from .conv import convrec, column_map_tohlcv + + +def DataFrameFactory(r, column_map=column_map_tohlcv, conv=convrec): + """DataFrameFactory - return a dataframe for candles. + + create a DataFrame from candle records by dynamically converting them + accoring the column_map config. + + >>> column_map_tcv = OrderedDict([ + ... ('time', 'Time'), + ... ('mid:c', 'Close'), + ... ('volume', 'Volume') + ... ]) + >>> params = {"instruments": "EUR_USD,EUR_GBP"} + >>> r = instruments.InstrumentsCandles(instrument=instr, params=params) + >>> api.request(r) + >>> for _r in DataFrameFactory(r.response): + >>> print(_r.head()) + >>> for _r in DataFrameFactory(r.response, column_map=column_map_tcv): + >>> print(_r.head()) + """ + df = pd.DataFrame([list(conv(_r, column_map)) for _r in r.get('candles')]) + df.columns = list(column_map.values()) + df = df.set_index('Time') + return df From 62c7da9dfa0d5bdd0209ee8afff7e72f5045d477 Mon Sep 17 00:00:00 2001 From: hootnot Date: Sat, 21 Oct 2017 19:10:58 +0200 Subject: [PATCH 2/3] correct docs --- oandapyV20/contrib/factories/csv.py | 4 +++- oandapyV20/contrib/factories/dataframe.py | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/oandapyV20/contrib/factories/csv.py b/oandapyV20/contrib/factories/csv.py index bbfc962..de65a93 100644 --- a/oandapyV20/contrib/factories/csv.py +++ b/oandapyV20/contrib/factories/csv.py @@ -13,7 +13,9 @@ def CSVFactory(r, conv=convrec, column_map=column_map_tohlcv, delim=","): ... ('mid:c', 'Close'), ... ('volume', 'Volume') ... ]) - >>> params = {"instruments": "EUR_USD,EUR_GBP"} + >>> params = {"count": 50, + ... "granularity": "D"} + >>> instr = "EUR_USD" >>> r = instruments.InstrumentsCandles(instrument=instr, params=params) >>> api.request(r) >>> for _r in CSVFactory(r.response): diff --git a/oandapyV20/contrib/factories/dataframe.py b/oandapyV20/contrib/factories/dataframe.py index 2cb867a..c31a5b1 100644 --- a/oandapyV20/contrib/factories/dataframe.py +++ b/oandapyV20/contrib/factories/dataframe.py @@ -15,7 +15,9 @@ def DataFrameFactory(r, column_map=column_map_tohlcv, conv=convrec): ... ('mid:c', 'Close'), ... ('volume', 'Volume') ... ]) - >>> params = {"instruments": "EUR_USD,EUR_GBP"} + >>> params = {"count": 50, + ... "granularity": "D"} + >>> instr = "EUR_USD" >>> r = instruments.InstrumentsCandles(instrument=instr, params=params) >>> api.request(r) >>> for _r in DataFrameFactory(r.response): From 1dbeeb2296a5ea99d75eaa8c8626177b363b6ba8 Mon Sep 17 00:00:00 2001 From: hootnot Date: Sat, 21 Oct 2017 19:11:34 +0200 Subject: [PATCH 3/3] pandas needed for tests [ci skip] --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 6127485..b5baf96 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,6 +13,7 @@ install: - pip install nose nose-parameterized - pip install wheel - pip install twine +- pip install pandas script: - coverage run --source=oandapyV20 setup.py test after_success: