2017-11-18 07:34:32 +00:00
|
|
|
"""
|
|
|
|
Functions to analyze ticker data with indicators and produce buy and sell signals
|
|
|
|
"""
|
2018-03-25 19:37:14 +00:00
|
|
|
import logging
|
2018-08-05 04:41:06 +00:00
|
|
|
import pandas as pd
|
2018-03-02 15:22:00 +00:00
|
|
|
from pandas import DataFrame, to_datetime
|
2018-03-17 21:44:47 +00:00
|
|
|
|
2018-03-25 19:37:14 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2018-07-10 10:04:37 +00:00
|
|
|
def parse_ticker_dataframe(ticker: list) -> DataFrame:
|
|
|
|
"""
|
|
|
|
Analyses the trend for the given ticker history
|
2018-08-02 08:58:04 +00:00
|
|
|
:param ticker: See exchange.get_candle_history
|
2018-07-10 10:04:37 +00:00
|
|
|
:return: DataFrame
|
|
|
|
"""
|
|
|
|
cols = ['date', 'open', 'high', 'low', 'close', 'volume']
|
|
|
|
frame = DataFrame(ticker, columns=cols)
|
|
|
|
|
|
|
|
frame['date'] = to_datetime(frame['date'],
|
|
|
|
unit='ms',
|
|
|
|
utc=True,
|
|
|
|
infer_datetime_format=True)
|
|
|
|
|
|
|
|
# group by index and aggregate results to eliminate duplicate ticks
|
|
|
|
frame = frame.groupby(by='date', as_index=False, sort=True).agg({
|
|
|
|
'open': 'first',
|
|
|
|
'high': 'max',
|
|
|
|
'low': 'min',
|
|
|
|
'close': 'last',
|
|
|
|
'volume': 'max',
|
|
|
|
})
|
|
|
|
frame.drop(frame.tail(1).index, inplace=True) # eliminate partial candle
|
|
|
|
return frame
|
2018-08-05 04:41:06 +00:00
|
|
|
|
|
|
|
|
2018-08-05 13:08:07 +00:00
|
|
|
def order_book_to_dataframe(bids: list, asks: list) -> DataFrame:
|
2018-08-05 04:41:06 +00:00
|
|
|
"""
|
|
|
|
Gets order book list, returns dataframe with below format per suggested by creslin
|
|
|
|
-------------------------------------------------------------------
|
|
|
|
b_sum b_size bids asks a_size a_sum
|
|
|
|
-------------------------------------------------------------------
|
|
|
|
"""
|
|
|
|
cols = ['bids', 'b_size']
|
2018-08-05 13:08:07 +00:00
|
|
|
|
|
|
|
bids_frame = DataFrame(bids, columns=cols)
|
2018-08-05 04:41:06 +00:00
|
|
|
# add cumulative sum column
|
|
|
|
bids_frame['b_sum'] = bids_frame['b_size'].cumsum()
|
|
|
|
cols2 = ['asks', 'a_size']
|
2018-08-05 13:08:07 +00:00
|
|
|
asks_frame = DataFrame(asks, columns=cols2)
|
2018-08-05 04:41:06 +00:00
|
|
|
# add cumulative sum column
|
|
|
|
asks_frame['a_sum'] = asks_frame['a_size'].cumsum()
|
|
|
|
|
|
|
|
frame = pd.concat([bids_frame['b_sum'], bids_frame['b_size'], bids_frame['bids'],
|
|
|
|
asks_frame['asks'], asks_frame['a_size'], asks_frame['a_sum']], axis=1,
|
|
|
|
keys=['b_sum', 'b_size', 'bids', 'asks', 'a_size', 'a_sum'])
|
|
|
|
# logger.info('order book %s', frame )
|
|
|
|
return frame
|