Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contrib factories #105

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions oandapyV20/contrib/factories/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from .history import InstrumentsCandlesFactory
from .csv import CSVFactory
from .dataframe import DataFrameFactory

__all__ = (
'InstrumentsCandlesFactory',
'CSVFactory',
'DataFrameFactory',
)
27 changes: 27 additions & 0 deletions oandapyV20/contrib/factories/conv.py
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions oandapyV20/contrib/factories/csv.py
Original file line number Diff line number Diff line change
@@ -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))])
31 changes: 31 additions & 0 deletions oandapyV20/contrib/factories/dataframe.py
Original file line number Diff line number Diff line change
@@ -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