<commit_before># -*- coding: utf-8 -*-
import datetime
import time
import logging
import sqlalchemy as sa
from . import Base, engine, db
from .strategy import BUY, SELL

log = logging.getLogger(__name__)


def init_db():
    Base.metadata.create_all(engine)


def get_bot(market, strategy, resolution, timeframe, btc):
    try:
        bot = db.query(Cointrader).filter(Cointrader.market == market._name).one()
        bot._market = market
        bot.strategy = str(strategy)
        bot._strategy = strategy
        bot._resolution = resolution
        bot._timeframe = timeframe
    except sa.orm.exc.NoResultFound:
        bot = Cointrader(market, strategy, resolution, timeframe)
        if btc is None:
            balances = market._exchange.get_balance()
            btc = balances["BTC"]["quantity"]
        bot.set_btc(btc)
        db.add(bot)
    db.commit()
    return bot


class Trade(Base):
    """Trading log"""
    __tablename__ = "trades"
    id = sa.Column(sa.Integer, primary_key=True)
    bot_id = sa.Column(sa.Integer, sa.ForeignKey('bots.id'))
    date = sa.Column(sa.DateTime, nullable=False, default=datetime.datetime.utcnow)
    order_type = sa.Column(sa.String, nullable=False)
    order_id = sa.Column(sa.Integer, nullable=False)
    trade_id = sa.Column(sa.Integer, nullable=False)
    market = sa.Column(sa.String, nullable=False)
    amount = sa.Column(sa.Float, nullable=False)
    rate = sa.Column(sa.Float, nullable=False)
    btc = sa.Column(sa.Float, nullable=False)
    btc_taxed = sa.Column(sa.Float, nullable=False)

    def __init__(self, bot_id, date, order_type, order_id, trade_id, market, amount, rate, btc, btc_taxed):
        """TODO: to be defined1.

        :bot_id: ID of the bot which initiated the trade
        :date: Date of the order
        :order_id: ID of the order
        :trade_id: ID of a single trade within the order
        :market: Currency_pair linke BTC_DASH
        :amount: How many coins bought/sold in order
        :rate: Rate for the order
        :order_type: Type of order. Can be either "BUY, SELL"
        :btc: How many BTC you placed in order
        :btc_taxed: How many BTC are actually used in order after applying the tax

        """
        self.bot_id = bot_id
        self.date = datetime.datetime.strptime(date, "%Y-%m-%d %H:%M:%S")
        self.order_type = order_type
        self.order_id = order_id
        self.trade_id = trade_id
        self.market = market
        self.amount = amount
        self.rate = rate
        self.btc = btc
        self.btc_taxed = btc_taxed
        log.info("{}: Bought {} for {}".format(self.date, self.amount, self.rate))


class Cointrader(Base):<commit_msg>Add logging to Tragelog.<commit_after># -*- coding: utf-8 -*-
import datetime
import time
import logging
import sqlalchemy as sa
from . import Base, engine, db
from .strategy import BUY, SELL

log = logging.getLogger(__name__)


def init_db():
    Base.metadata.create_all(engine)


def get_bot(market, strategy, resolution, timeframe, btc):
    try:
        bot = db.query(Cointrader).filter(Cointrader.market == market._name).one()
        bot._market = market
        bot.strategy = str(strategy)
        bot._strategy = strategy
        bot._resolution = resolution
        bot._timeframe = timeframe
    except sa.orm.exc.NoResultFound:
        bot = Cointrader(market, strategy, resolution, timeframe)
        if btc is None:
            balances = market._exchange.get_balance()
            btc = balances["BTC"]["quantity"]
        bot.set_btc(btc)
        db.add(bot)
    db.commit()
    return bot


class Trade(Base):
    """Trading log"""
    __tablename__ = "trades"
    id = sa.Column(sa.Integer, primary_key=True)
    bot_id = sa.Column(sa.Integer, sa.ForeignKey('bots.id'))
    date = sa.Column(sa.DateTime, nullable=False, default=datetime.datetime.utcnow)
    order_type = sa.Column(sa.String, nullable=False)
    order_id = sa.Column(sa.Integer, nullable=False)
    trade_id = sa.Column(sa.Integer, nullable=False)
    market = sa.Column(sa.String, nullable=False)
    amount = sa.Column(sa.Float, nullable=False)
    rate = sa.Column(sa.Float, nullable=False)
    btc = sa.Column(sa.Float, nullable=False)
    btc_taxed = sa.Column(sa.Float, nullable=False)

    def __init__(self, bot_id, date, order_type, order_id, trade_id, market, amount, rate, btc, btc_taxed):
        """TODO: to be defined1.

        :bot_id: ID of the bot which initiated the trade
        :date: Date of the order
        :order_id: ID of the order
        :trade_id: ID of a single trade within the order
        :market: Currency_pair linke BTC_DASH
        :amount: How many coins bought/sold in order
        :rate: Rate for the order
        :order_type: Type of order. Can be either "BUY, SELL"
        :btc: How many BTC you placed in order
        :btc_taxed: How many BTC are actually used in order after applying the tax

        """
        self.bot_id = bot_id
        self.date = datetime.datetime.strptime(date, "%Y-%m-%d %H:%M:%S")
        self.order_type = order_type
        self.order_id = order_id
        self.trade_id = trade_id
        self.market = market
        self.amount = amount
        self.rate = rate
        self.btc = btc
        self.btc_taxed = btc_taxed
        if self.order_type == "BUY":
            log.info("{}: BUY {} @ {} paid -> {} BTC".format(self.date, self.amount, self.rate, self.btc_taxed))
        else:
            log.info("{}: SELL {} @ {} earned -> {} BTC".format(self.date, self.amount, self.rate, self.btc_taxed))


class Cointrader(Base):
{'fraction_non_alphanumeric': 0.11509501613481535, 'fraction_numerical': 0.0007171029042667623, 'mean_word_length': 2.711909514304724, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '204525', 'n_tokens_mistral': 1844, 'n_tokens_neox': 1809, 'n_words': 470}