brighter-trading/data.py

79 lines
3.2 KiB
Python

from binance.client import Client
import config
from candles import Candles
from Configuration import Configuration
from exchange_info import ExchangeInfo
from indicators import Indicators
class BrighterData:
def __init__(self):
# Initialise a connection to the Binance client API
self.client = Client(config.API_KEY, config.API_SECRET)
# Configuration and settings for the user interface and charts
self.config = Configuration()
# Load any saved data from file
self.config.config_and_states('load')
# Object that maintains candlestick and price data.
self.candles = Candles(self.config, self.client)
# Object that interacts with and maintains data from available indicators
self.indicators = Indicators(self.candles, self.config)
# Object that maintains exchange and account data
self.exchange_info = ExchangeInfo(self.client)
def get_js_init_data(self):
"""Returns a JSON object of initialization data
for the javascript in the rendered HTML"""
js_data = {'i_types': self.indicators.indicator_types,
'indicators': self.indicators.indicator_list,
'interval': self.config.chart_interval,
'trading_pair': self.config.trading_pair}
return js_data
def get_rendered_data(self):
"""
Data to be rendered in the HTML
"""
rd = {}
rd['title'] = self.config.application_title # Title of the page
rd['my_balances'] = self.exchange_info.balances # Balances on the exchange
rd['symbols'] = self.exchange_info.symbols # Symbols information from the exchange
rd['intervals'] = self.exchange_info.intervals # Time candle time intervals available to stream
rd['chart_interval'] = self.config.chart_interval # The charts current interval setting
rd['indicator_types'] = self.indicators.indicator_types # All the types indicators Available
rd['indicator_list'] = self.indicators.get_indicator_list() # indicators available
rd['enabled_indicators'] = self.indicators.get_enabled_indicators() # list of indicators that are enabled
rd['ma_vals'] = self.indicators.bb_ma_val # A list of acceptable values to use with bolenger band creation
return rd
def received_cdata(self, cdata):
# If this is the first candle received,
# then set last_candle. Then set a new candle.
if not self.candles.last_candle:
self.candles.last_candle = cdata
# If this candle is the same as last candle return nothing to do.
elif cdata['time']:
if cdata['time'] == self.candles.last_candle['time']:
return
# New candle is received update the instance data records. And the indicators.
self.candles.set_new_candle(cdata)
updates = self.indicators.update_indicators()
return updates
def received_new_signal(self, data):
# Check the data.
if 'sigName' not in data:
return 'data.py:received_new_signal() - The new signal has no name. '
print(data)
# TODO lets go!
app_data = BrighterData()