From 69dd56b237a8c964dddd42ec3eed70220830097d Mon Sep 17 00:00:00 2001 From: misagh Date: Sat, 17 Nov 2018 18:47:13 +0100 Subject: [PATCH] wallet sync drafted --- freqtrade/freqtradebot.py | 16 +++++++++++++++- freqtrade/persistence.py | 30 +++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index eb92375ec..428e5a726 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -18,7 +18,7 @@ from freqtrade import (DependencyException, OperationalException, TemporaryError, __version__, constants, persistence) from freqtrade.exchange import Exchange from freqtrade.edge import Edge -from freqtrade.persistence import Trade +from freqtrade.persistence import Trade, Wallet from freqtrade.rpc import RPCManager, RPCMessageType from freqtrade.state import State from freqtrade.strategy.interface import SellType @@ -800,3 +800,17 @@ class FreqtradeBot(object): # Send the message self.rpc.send_msg(msg) Trade.session.flush() + + def update_wallets(self) -> bool: + wallets = self.exchange.get_balances() + + for currency in wallets: + wallet = Wallet( + exchange=self.exchange._api.id, + currency=currency, + free=wallets[currency]['free'], + used=wallets[currency]['used'], + total=wallets[currency]['total'] + ) + Wallet.session.add(wallet) + Wallet.session.flush() diff --git a/freqtrade/persistence.py b/freqtrade/persistence.py index 51a8129fb..ac7833d79 100644 --- a/freqtrade/persistence.py +++ b/freqtrade/persistence.py @@ -9,7 +9,7 @@ from typing import Any, Dict, Optional import arrow from sqlalchemy import (Boolean, Column, DateTime, Float, Integer, String, - create_engine, inspect) + create_engine, inspect, PrimaryKeyConstraint) from sqlalchemy.exc import NoSuchModuleError from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm.scoping import scoped_session @@ -50,8 +50,13 @@ def init(config: Dict) -> None: f'is no valid database URL! (See {_SQL_DOCS_URL})') session = scoped_session(sessionmaker(bind=engine, autoflush=True, autocommit=True)) + Trade.session = session() Trade.query = session.query_property() + + Wallet.session = session() + Wallet.query = session.query_property() + _DECL_BASE.metadata.create_all(engine) check_migrate(engine) @@ -341,3 +346,26 @@ class Trade(_DECL_BASE): ) profit_percent = (close_trade_price / open_trade_price) - 1 return float(f"{profit_percent:.8f}") + + +class Wallet(_DECL_BASE): + """ + Class for wallet structure + It is a mirror of wallets on an exchange + """ + __tablename__ = 'wallets' + + exchange = Column(String, nullable=False, primary_key=True, index=True) + currency = Column(String, nullable=False, primary_key=True, index=True) + + free = Column(Float, index=True) + used = Column(Float) + total = Column(Float) + base = Column(Boolean, index=True, default=False) + quote = Column(Boolean, index=True, default=False) + + __table_args__ = ( + PrimaryKeyConstraint( + exchange, + currency), + {})