freqtrade_origin/persistence.py
2017-05-21 17:58:54 +02:00

49 lines
1.6 KiB
Python

from datetime import datetime
from sqlalchemy import Boolean, Column, DateTime, Float, Integer, String, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.types import Enum
from exchange import Exchange
from utils import get_conf
if get_conf().get('dry_run', False):
db_handle = 'sqlite:///tradesv2.dry_run.sqlite'
else:
db_handle = 'sqlite:///tradesv2.sqlite'
engine = create_engine(db_handle, echo=False)
Session = scoped_session(sessionmaker(bind=engine, autoflush=True, autocommit=True))
Base = declarative_base()
class Trade(Base):
__tablename__ = 'trades'
query = Session.query_property()
id = Column(Integer, primary_key=True)
exchange = Column(Enum(Exchange), nullable=False)
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)
btc_amount = Column(Float, nullable=False)
amount = Column(Float, nullable=False)
open_date = Column(DateTime, nullable=False, default=datetime.utcnow)
close_date = Column(DateTime)
open_order_id = Column(String)
def __repr__(self):
return 'Trade(id={}, pair={}, amount={}, open_rate={}, open_since={})'.format(
self.id,
self.pair,
self.amount,
self.open_rate,
'closed' if not self.is_open else round((datetime.utcnow() - self.open_date).total_seconds() / 60, 2)
)
Base.metadata.create_all(engine)