stable/persistence.py

44 lines
1.4 KiB
Python
Raw Normal View History

2017-05-12 17:11:56 +00:00
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
Base = declarative_base()
engine = create_engine('sqlite:///tradesv2.sqlite', echo=False)
Session = scoped_session(sessionmaker(bind=engine, autoflush=True, autocommit=True))
2017-05-12 17:11:56 +00:00
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)
2017-05-14 12:14:16 +00:00
open_order_id = Column(String)
2017-05-12 17:11:56 +00:00
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)