add columns fee_open and fee_close, update value

This commit is contained in:
Matthias Voppichler 2018-04-21 19:47:08 +02:00
parent 7f4c70827a
commit a620aa8352
2 changed files with 34 additions and 14 deletions

View File

@ -316,11 +316,14 @@ class FreqtradeBot(object):
)
)
# Fee is applied twice because we make a LIMIT_BUY and LIMIT_SELL
fee = exchange.get_fee(symbol=pair, taker_or_maker='maker')
trade = Trade(
pair=pair,
stake_amount=stake_amount,
amount=amount,
fee=exchange.get_fee(taker_or_maker='maker'),
fee=fee,
fee_open=fee,
fee_close=fee,
open_rate=buy_limit,
open_date=datetime.utcnow(),
exchange=exchange.get_id(),
@ -355,7 +358,19 @@ class FreqtradeBot(object):
if trade.open_order_id:
# Update trade with order values
self.logger.info('Found open order for %s', trade)
trade.update(exchange.get_order(trade.open_order_id, trade.pair))
order = exchange.get_order(trade.open_order_id, trade.pair)
# TODO: correct place here ??
# Try update amount (binance-fix)
try:
new_amount = self.get_real_amount(trade)
if order['amount'] != new_amount:
order['amount'] = new_amount
trade.fee_open = 0
except OperationalException as exception:
self.logger.warning("could not update trade amount: %s", exception)
trade.update(order)
if trade.is_open and trade.open_order_id is None:
# Check if we can sell our current pair
@ -374,18 +389,18 @@ class FreqtradeBot(object):
if len(trades) == 0:
raise OperationalException("get_real_amount: no trade found")
amount = 0
fee = 0
fee_abs = 0
for trade in trades:
amount += trade["amount"]
if "fee" in trade:
# only applies if fee is in quote currency!
if order.pair.startswith(trade["fee"]["currency"]):
fee += trade["fee"]["cost"]
fee_abs += trade["fee"]["cost"]
# TODO: create order using amount_lots would be better
if amount != order.amount:
self.logger.warning("amount {} does not match amount {}".format(amount, order.amount))
raise OperationalException("Half bought? Amounts don't match")
real_amount = amount - fee
real_amount = amount - fee_abs
return real_amount
def maybe_update_real_amount(self, trade: Trade) -> bool:
@ -403,10 +418,12 @@ class FreqtradeBot(object):
self.logger.warning("could not update trade amount: %s", exception)
return False
# updating amount
self.logger.info("Updating amount for Trade {} from {} to {}".format(
trade, trade.amount, new_amount))
trade.amount = new_amount
Trade.session.flush()
if trade.amount != new_amount:
self.logger.info("Updating amount for Trade {} from {} to {}".format(
trade, trade.amount, new_amount))
trade.amount = new_amount
trade.fee_open = 0 # Fee was applied - set to 0 for buy
Trade.session.flush()
return True
def handle_trade(self, trade: Trade) -> bool:
@ -452,6 +469,7 @@ class FreqtradeBot(object):
# Check if trade is still actually open
if int(order['remaining']) == 0:
# self.maybe_update_real_amount(trade)
continue
if order['side'] == 'buy' and ordertime < timeoutthreashold:

View File

@ -86,6 +86,8 @@ class Trade(_DECL_BASE):
pair = Column(String, nullable=False)
is_open = Column(Boolean, nullable=False, default=True)
fee = Column(Float, nullable=False, default=0.0)
fee_open = Column(Float, nullable=False, default=0.0)
fee_close = Column(Float, nullable=False, default=0.0)
open_rate = Column(Float)
close_rate = Column(Float)
close_profit = Column(Float)
@ -156,7 +158,7 @@ class Trade(_DECL_BASE):
getcontext().prec = 8
buy_trade = (Decimal(self.amount) * Decimal(self.open_rate))
fees = buy_trade * Decimal(fee or self.fee)
fees = buy_trade * Decimal(fee or self.fee_open)
return float(buy_trade + fees)
def calc_close_trade_price(
@ -177,7 +179,7 @@ class Trade(_DECL_BASE):
return 0.0
sell_trade = (Decimal(self.amount) * Decimal(rate or self.close_rate))
fees = sell_trade * Decimal(fee or self.fee)
fees = sell_trade * Decimal(fee or self.fee_close)
return float(sell_trade - fees)
def calc_profit(
@ -195,7 +197,7 @@ class Trade(_DECL_BASE):
open_trade_price = self.calc_open_trade_price()
close_trade_price = self.calc_close_trade_price(
rate=(rate or self.close_rate),
fee=(fee or self.fee)
fee=(fee or self.fee_close)
)
return float("{0:.8f}".format(close_trade_price - open_trade_price))
@ -215,7 +217,7 @@ class Trade(_DECL_BASE):
open_trade_price = self.calc_open_trade_price()
close_trade_price = self.calc_close_trade_price(
rate=(rate or self.close_rate),
fee=(fee or self.fee)
fee=(fee or self.fee_close)
)
return float("{0:.8f}".format((close_trade_price / open_trade_price) - 1))