brighter-trading/data.py

141 lines
5.7 KiB
Python

from binance.client import Client
import config
from Strategies import Strategies
from candles import Candles
from Configuration import Configuration
from exchange_info import ExchangeInfo
from indicators import Indicators
from Signals import Signals
import json
class BrighterData:
def __init__(self):
# Initialise a connection to the Binance client API
self.client = Client(config.API_KEY, config.API_SECRET)
# Object that maintains signals
self.signals = Signals()
# Configuration and settings for the user interface and charts
self.config = Configuration()
# Load any saved data from file
self.config.config_and_states('load')
# Initialize signals with loaded data.
self.signals.init_loaded_signals(self.config.signals_list)
# 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)
# Object that maintains the strategies data
self.strategies = Strategies(self.config.strategies_list)
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)
i_updates = self.indicators.update_indicators()
# Process the signals based on the last indicator updates.
state_changes = self.signals.process_all_signals(self.indicators)
updates = {'i_updates': i_updates}
if state_changes:
print(state_changes)
updates.update({'s_updates': state_changes})
return updates
def received_new_signal(self, data):
# Check the data.
if 'name' not in data:
return 'data.py:received_new_signal() - The new signal has no name. '
# Forward the new signal data to the signals instance. So it can create a new signal.
self.signals.new_signal(data)
# Forward the new signal data to config. So it can save it to file.
self.config.new_signal(data)
# Send the data back to where it came from.
return data
def received_new_strategy(self, data):
# Check the data.
if 'name' not in data:
return 'data.py:received_new_strategy() - The new strategy has no name. '
# Forward the new strategy data to the strategy's instance. So it can create a new strategy.
self.strategies.new_strategy(data)
# Forward the new signal data to config. So it can save it to file.
self.config.new_strategy(data)
# Send the data back to where it came from.
return data
def delete_strategy(self, strategy_name):
# Delete the signal from the signals instance.
self.strategies.delete_strategy(strategy_name)
# Delete the signal from the configuration file.
self.config.remove('strategies', strategy_name)
def get_signals(self):
""" Return a JSON object of all the signals in the signals instance."""
sigs = self.signals.get_signals()
json_str = []
for sig in sigs:
json_str.append(json.dumps(sig.__dict__))
return json_str
def get_strategies(self):
""" Return a JSON object of all the signals in the signals instance."""
strats = self.strategies.get_strategies()
json_str = []
for strat in strats:
json_str.append(json.dumps(strat))
return json_str
def delete_signal(self, signal_name):
# Delete the signal from the signals instance.
self.signals.delete_signal(signal_name)
# Delete the signal from the configuration file.
self.config.remove('signals', signal_name)
app_data = BrighterData()