freqtrade_origin/freqtrade/persistence.py

88 lines
2.8 KiB
Python
Raw Normal View History

2017-05-12 17:11:56 +00:00
from datetime import datetime
2017-09-08 19:39:31 +00:00
from typing import Optional
2017-05-12 17:11:56 +00:00
from sqlalchemy import Boolean, Column, DateTime, Float, Integer, String, create_engine
from sqlalchemy.ext.declarative import declarative_base
2017-09-03 06:50:48 +00:00
from sqlalchemy.orm.scoping import scoped_session
from sqlalchemy.orm.session import sessionmaker
2017-05-12 17:11:56 +00:00
from sqlalchemy.types import Enum
2017-09-28 21:26:28 +00:00
from freqtrade import exchange
2017-09-08 21:10:22 +00:00
_CONF = {}
2017-05-12 17:11:56 +00:00
2017-05-21 15:58:54 +00:00
Base = declarative_base()
2017-05-12 17:11:56 +00:00
2017-09-08 21:10:22 +00:00
def init(config: dict, db_url: Optional[str] = None) -> None:
"""
Initializes this module with the given config,
registers all known command handlers
and starts polling for message updates
:param config: config to use
2017-09-08 19:39:31 +00:00
:param db_url: database connector string for sqlalchemy (Optional)
:return: None
"""
2017-09-08 21:10:22 +00:00
_CONF.update(config)
2017-09-08 19:39:31 +00:00
if not db_url:
2017-09-08 21:10:22 +00:00
if _CONF.get('dry_run', False):
2017-09-08 19:39:31 +00:00
db_url = 'sqlite:///tradesv2.dry_run.sqlite'
else:
db_url = 'sqlite:///tradesv2.sqlite'
2017-09-08 19:39:31 +00:00
engine = create_engine(db_url, echo=False)
session = scoped_session(sessionmaker(bind=engine, autoflush=True, autocommit=True))
Trade.session = session()
Trade.query = session.query_property()
Base.metadata.create_all(engine)
2017-05-12 17:11:56 +00:00
class Trade(Base):
__tablename__ = 'trades'
id = Column(Integer, primary_key=True)
2017-10-06 10:22:04 +00:00
exchange = Column(String, nullable=False)
2017-05-12 17:11:56 +00:00
pair = Column(String, nullable=False)
is_open = Column(Boolean, nullable=False, default=True)
open_rate = Column(Float, nullable=False)
close_rate = Column(Float)
close_profit = Column(Float)
2017-09-11 11:59:11 +00:00
stake_amount = Column(Float, name='btc_amount', nullable=False)
2017-05-12 17:11:56 +00:00
amount = Column(Float, nullable=False)
open_date = Column(DateTime, nullable=False, default=datetime.utcnow)
close_date = Column(DateTime)
2017-05-14 12:14:16 +00:00
open_order_id = Column(String)
2017-05-12 17:11:56 +00:00
def __repr__(self):
2017-09-08 21:10:22 +00:00
if self.is_open:
open_since = 'closed'
else:
open_since = round((datetime.utcnow() - self.open_date).total_seconds() / 60, 2)
2017-05-12 17:11:56 +00:00
return 'Trade(id={}, pair={}, amount={}, open_rate={}, open_since={})'.format(
self.id,
self.pair,
self.amount,
self.open_rate,
2017-09-08 21:10:22 +00:00
open_since
2017-05-12 17:11:56 +00:00
)
2017-09-01 19:11:46 +00:00
def exec_sell_order(self, rate: float, amount: float) -> float:
2017-06-08 18:01:01 +00:00
"""
Executes a sell for the given trade and updated the entity.
:param rate: rate to sell for
:param amount: amount to sell
:return: current profit as percentage
"""
profit = 100 * ((rate - self.open_rate) / self.open_rate)
# Execute sell and update trade record
order_id = exchange.sell(str(self.pair), rate, amount)
2017-06-08 18:01:01 +00:00
self.close_rate = rate
self.close_profit = profit
self.close_date = datetime.utcnow()
self.open_order_id = order_id
# Flush changes
Trade.session.flush()
2017-06-08 18:01:01 +00:00
return profit