Created
May 17, 2021 16:20
-
-
Save fbradyirl/0756756ac9751ff086f36c4a49cd32c6 to your computer and use it in GitHub Desktop.
hassio_idkravitz_default_strategy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import sys | |
import time | |
import os | |
import json | |
from datetime import datetime, timezone | |
from binance_trade_bot.auto_trader import AutoTrader | |
from binance_trade_bot.models import Trade | |
class Strategy(AutoTrader): | |
def initialize(self): | |
super().initialize() | |
self.initialize_current_coin() | |
def scout(self): | |
""" | |
Scout for potential jumps from the current coin to another coin | |
""" | |
current_coin = self.db.get_current_coin() | |
current_coin_price = self.manager.get_ticker_price(current_coin + self.config.BRIDGE) | |
if current_coin_price is None: | |
self.logger.info("Skipping scouting... current coin {} not found".format(current_coin + self.config.BRIDGE)) | |
return | |
# Update Home Assistant sensor | |
total_balance_usdt = 0 | |
attributes = {} | |
attributes['bridge'] = self.config.BRIDGE_SYMBOL | |
attributes['current_coin'] = str(current_coin).replace("<", "").replace(">", "") | |
attributes['wallet'] = {} | |
for asset in self.manager.binance_client.get_account()["balances"]: | |
if float(asset['free']) > 0: | |
if asset['asset'] not in ['BUSD', 'USDT']: | |
current_price = self.manager.get_ticker_price(asset['asset'] + self.config.BRIDGE_SYMBOL) | |
if isinstance(current_price, float): | |
asset_value = float(asset['free']) * float(current_price) | |
else: | |
self.logger.warning("No price found for current asset={}".format(asset['asset'])) | |
asset_value = 0 | |
else: | |
asset_value = float(asset['free']) | |
total_balance_usdt += asset_value | |
if asset_value > 1: | |
attributes['wallet'][asset['asset']] = {'balance': float(asset['free']), 'current_price': float(current_price), 'asset_value': float(asset_value)} | |
with self.db.db_session() as session: | |
try: | |
trade = session.query(Trade).order_by(Trade.datetime.desc()).limit(1).one().info() | |
if trade: | |
attributes['last_transaction_attempt'] = datetime.strptime(trade['datetime'], "%Y-%m-%dT%H:%M:%S.%f").replace(tzinfo=timezone.utc).astimezone(tz=None).strftime("%d/%m/%Y %H:%M:%S") | |
except: | |
pass | |
attributes['last_sensor_update'] = datetime.now().strftime("%d/%m/%Y %H:%M:%S") | |
data = {'state': round(total_balance_usdt, 2), 'attributes': attributes} | |
os.system("/scripts/update_ha_sensor.sh '" + str(json.dumps(data)) + "'") | |
# END - Update Home Assistant sensor | |
self._jump_to_best_coin(current_coin, current_coin_price) | |
self.bridge_scout() | |
def bridge_scout(self): | |
current_coin = self.db.get_current_coin() | |
if self.manager.get_currency_balance(current_coin.symbol) > self.manager.get_min_notional( | |
current_coin.symbol, self.config.BRIDGE.symbol | |
): | |
# Only scout if we don't have enough of the current coin | |
return | |
new_coin = super().bridge_scout() | |
if new_coin is not None: | |
self.db.set_current_coin(new_coin) | |
def initialize_current_coin(self): | |
""" | |
Decide what is the current coin, and set it up in the DB. | |
""" | |
if self.db.get_current_coin() is None: | |
current_coin_symbol = self.config.CURRENT_COIN_SYMBOL | |
if not current_coin_symbol: | |
current_coin_symbol = random.choice(self.config.SUPPORTED_COIN_LIST) | |
self.logger.info(f"Setting initial coin to {current_coin_symbol}") | |
if current_coin_symbol not in self.config.SUPPORTED_COIN_LIST: | |
sys.exit("***\nERROR!\nSince there is no backup file, a proper coin name must be provided at init\n***") | |
self.db.set_current_coin(current_coin_symbol) | |
# if we don't have a configuration, we selected a coin at random... Buy it so we can start trading. | |
if self.config.CURRENT_COIN_SYMBOL == "": | |
current_coin = self.db.get_current_coin() | |
self.logger.info(f"Purchasing {current_coin} to begin trading") | |
self.manager.buy_alt(current_coin, self.config.BRIDGE) | |
self.logger.info("Ready to start trading") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment