wallet sync drafted

This commit is contained in:
misagh 2018-11-17 18:47:13 +01:00
parent de57da3249
commit 69dd56b237
2 changed files with 44 additions and 2 deletions

View File

@ -18,7 +18,7 @@ from freqtrade import (DependencyException, OperationalException,
TemporaryError, __version__, constants, persistence) TemporaryError, __version__, constants, persistence)
from freqtrade.exchange import Exchange from freqtrade.exchange import Exchange
from freqtrade.edge import Edge from freqtrade.edge import Edge
from freqtrade.persistence import Trade from freqtrade.persistence import Trade, Wallet
from freqtrade.rpc import RPCManager, RPCMessageType from freqtrade.rpc import RPCManager, RPCMessageType
from freqtrade.state import State from freqtrade.state import State
from freqtrade.strategy.interface import SellType from freqtrade.strategy.interface import SellType
@ -800,3 +800,17 @@ class FreqtradeBot(object):
# Send the message # Send the message
self.rpc.send_msg(msg) self.rpc.send_msg(msg)
Trade.session.flush() 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()

View File

@ -9,7 +9,7 @@ from typing import Any, Dict, Optional
import arrow import arrow
from sqlalchemy import (Boolean, Column, DateTime, Float, Integer, String, from sqlalchemy import (Boolean, Column, DateTime, Float, Integer, String,
create_engine, inspect) create_engine, inspect, PrimaryKeyConstraint)
from sqlalchemy.exc import NoSuchModuleError from sqlalchemy.exc import NoSuchModuleError
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm.scoping import scoped_session 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})') f'is no valid database URL! (See {_SQL_DOCS_URL})')
session = scoped_session(sessionmaker(bind=engine, autoflush=True, autocommit=True)) session = scoped_session(sessionmaker(bind=engine, autoflush=True, autocommit=True))
Trade.session = session() Trade.session = session()
Trade.query = session.query_property() Trade.query = session.query_property()
Wallet.session = session()
Wallet.query = session.query_property()
_DECL_BASE.metadata.create_all(engine) _DECL_BASE.metadata.create_all(engine)
check_migrate(engine) check_migrate(engine)
@ -341,3 +346,26 @@ class Trade(_DECL_BASE):
) )
profit_percent = (close_trade_price / open_trade_price) - 1 profit_percent = (close_trade_price / open_trade_price) - 1
return float(f"{profit_percent:.8f}") 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),
{})