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: 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..de65a93 --- /dev/null +++ b/oandapyV20/contrib/factories/csv.py @@ -0,0 +1,28 @@ +# -*- 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 = {"count": 50, + ... "granularity": "D"} + >>> instr = "EUR_USD" + >>> 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..c31a5b1 --- /dev/null +++ b/oandapyV20/contrib/factories/dataframe.py @@ -0,0 +1,31 @@ +# -*- 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 = {"count": 50, + ... "granularity": "D"} + >>> instr = "EUR_USD" + >>> 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