This commit is contained in:
Kavinkumar 2022-02-27 10:09:35 +05:30
parent edee3259ed
commit 6db6d8dcd2
2 changed files with 25 additions and 24 deletions

View File

@ -709,7 +709,7 @@ class FreqtradeBot(LoggingMixin):
'stake_amount': trade.stake_amount, 'stake_amount': trade.stake_amount,
'stake_currency': self.config['stake_currency'], 'stake_currency': self.config['stake_currency'],
'fiat_currency': self.config.get('fiat_display_currency', None), 'fiat_currency': self.config.get('fiat_display_currency', None),
'amount': order.get('filled') or order.get('amount'), 'amount': order.get('amount') if fill else order.get('filled'),
'open_date': trade.open_date or datetime.utcnow(), 'open_date': trade.open_date or datetime.utcnow(),
'current_rate': current_rate, 'current_rate': current_rate,
'sub_trade': sub_trade, 'sub_trade': sub_trade,
@ -1257,10 +1257,9 @@ class FreqtradeBot(LoggingMixin):
if sub_trade: if sub_trade:
amount = order.get('filled') or order.get('amount') or 0 amount = order.get('filled') or order.get('amount') or 0
profit_rate = order.get('average') or order.get('price') or 0 profit_rate = order.get('average') or order.get('price') or 0
profit = trade.process_sell_sub_trade(order, is_closed=False) profit = self.close_profit_abs
open_rate = trade.get_open_rate(profit, profit_rate, amount) open_rate = trade.get_open_rate(profit, profit_rate, amount)
open_cost=open_rate * amount * (1+ trade.fee_open) profit_ratio = self.close_profit
profit_ratio = ( open_cost + profit)/open_cost - 1
else: else:
profit_rate = trade.close_rate if trade.close_rate else trade.close_rate_requested profit_rate = trade.close_rate if trade.close_rate else trade.close_rate_requested
profit = trade.calc_profit(rate=profit_rate) profit = trade.calc_profit(rate=profit_rate)
@ -1484,6 +1483,7 @@ class FreqtradeBot(LoggingMixin):
return order_amount return order_amount
return self.fee_detection_from_trades(trade, order, order_amount, order.get('trades', [])) return self.fee_detection_from_trades(trade, order, order_amount, order.get('trades', []))
rpc_msg = {}
def fee_detection_from_trades(self, trade: Trade, order: Dict, order_amount: float, def fee_detection_from_trades(self, trade: Trade, order: Dict, order_amount: float,
trades: List) -> float: trades: List) -> float:
""" """
@ -1502,7 +1502,9 @@ class FreqtradeBot(LoggingMixin):
} }
self.rpc.send_msg(msg) if not self.rpc_msg.get(trade.id):
self.rpc.send_msg(msg)
self.rpc_msg[trade.id] = 1
return order_amount return order_amount
fee_currency = None fee_currency = None
amount = 0 amount = 0

View File

@ -491,7 +491,7 @@ class LocalTrade():
raise ValueError(f'Unknown order type: {order_type}') raise ValueError(f'Unknown order type: {order_type}')
Trade.commit() Trade.commit()
def process_sell_sub_trade(self, order: Dict, is_closed: bool = True) -> float: def process_sell_sub_trade(self, order: Dict) -> float:
orders = (self.select_filled_orders('buy')) orders = (self.select_filled_orders('buy'))
if len(orders)<1: if len(orders)<1:
@ -502,11 +502,11 @@ class LocalTrade():
# is_closed = order['ft_is_open'] # is_closed = order['ft_is_open']
sell_amount = order.get('filled') or order.get('amount') sell_amount = order.get('filled') or order.get('amount')
sell_rate = order.get('average') or order.get('price') sell_rate = order.get('average') or order.get('price')
if is_closed: sell_stake_amount = sell_rate * sell_amount * (1 - self.fee_close)
if sell_amount == self.amount: if sell_amount == self.amount:
self.close(sell_rate) self.close(sell_rate)
Trade.commit() Trade.commit()
return return
profit = 0.0 profit = 0.0
idx = -1 idx = -1
while sell_amount: while sell_amount:
@ -515,26 +515,25 @@ class LocalTrade():
buy_rate = b_order.average or b_order.price buy_rate = b_order.average or b_order.price
if sell_amount < buy_amount: if sell_amount < buy_amount:
amount = sell_amount amount = sell_amount
if is_closed: b_order.filled -= amount
b_order.filled -= amount
else: else:
if is_closed: b_order.is_fully_realized = True
b_order.is_fully_realized = True b_order.order_update_date = datetime.now(timezone.utc)
b_order.order_update_date = datetime.now(timezone.utc) self.update_order(b_order)
self.update_order(b_order)
idx -= 1 idx -= 1
amount = buy_amount amount = buy_amount
sell_amount -= amount sell_amount -= amount
profit += self.calc_profit2(buy_rate, sell_rate, amount) profit += self.calc_profit2(buy_rate, sell_rate, amount)
b_order2 = orders[idx] b_order2 = orders[idx]
amount2 = b_order2.filled or b_order2.amount amount2 = b_order2.filled or b_order2.amount
if is_closed: b_order2.average = (b_order2.average * amount2 - profit) / amount2
b_order2.average = (b_order2.average * amount2 - profit) / amount2 b_order2.order_update_date = datetime.now(timezone.utc)
b_order2.order_update_date = datetime.now(timezone.utc) self.update_order(b_order2)
self.update_order(b_order2) Order.query.session.commit()
Order.query.session.commit() self.recalc_trade_from_orders()
self.recalc_trade_from_orders() self.close_profit_abs = profit
Trade.commit() self.close_profit = sell_stake_amount / (sell_stake_amount - profit) -1
Trade.commit()
return profit return profit
def calc_profit2(self, open_rate: float, close_rate: float, def calc_profit2(self, open_rate: float, close_rate: float,