From e411717de9397afe29b58c62a7aca5c3aa6f8f65 Mon Sep 17 00:00:00 2001 From: hroff-1902 Date: Fri, 28 Feb 2020 12:36:39 +0300 Subject: [PATCH 1/3] No percent where ratio is to be used --- freqtrade/edge/edge_positioning.py | 12 ++++++------ freqtrade/freqtradebot.py | 12 ++++++------ freqtrade/persistence.py | 4 ++-- freqtrade/rpc/rpc.py | 20 ++++++++++---------- freqtrade/rpc/telegram.py | 2 +- freqtrade/strategy/interface.py | 7 ++++--- tests/edge/test_edge.py | 2 +- tests/rpc/test_rpc_telegram.py | 12 ++++++------ tests/test_freqtradebot.py | 8 ++++---- 9 files changed, 40 insertions(+), 39 deletions(-) diff --git a/freqtrade/edge/edge_positioning.py b/freqtrade/edge/edge_positioning.py index ee5c3e95d..57a8f4a7c 100644 --- a/freqtrade/edge/edge_positioning.py +++ b/freqtrade/edge/edge_positioning.py @@ -246,7 +246,8 @@ class Edge: # we set stake amount to an arbitrary amount. # as it doesn't change the calculation. - # all returned values are relative. they are percentages. + # all returned values are relative. + # they are defined as ratios. stake = 0.015 fee = self.fee open_fee = fee / 2 @@ -269,8 +270,8 @@ class Edge: result['sell_fee'] = result['sell_sum'] * close_fee result['sell_take'] = result['sell_sum'] - result['sell_fee'] - # profit_percent - result['profit_percent'] = (result['sell_take'] - result['buy_spend']) / result['buy_spend'] + # profit_ratio + result['profit_ratio'] = (result['sell_take'] - result['buy_spend']) / result['buy_spend'] # Absolute profit result['profit_abs'] = result['sell_take'] - result['buy_spend'] @@ -399,9 +400,8 @@ class Edge: # trade opens in reality on the next candle open_trade_index += 1 - stop_price_percentage = stoploss + 1 open_price = ohlc_columns[open_trade_index, 0] - stop_price = (open_price * stop_price_percentage) + stop_price = (open_price * (stoploss + 1)) # Searching for the index where stoploss is hit stop_index = utf1st.find_1st( @@ -441,7 +441,7 @@ class Edge: trade = {'pair': pair, 'stoploss': stoploss, - 'profit_percent': '', + 'profit_ratio': '', 'profit_abs': '', 'open_time': date_column[open_trade_index], 'close_time': date_column[exit_index], diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index dffec940c..f50244dac 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -1034,8 +1034,8 @@ class FreqtradeBot: profit_trade = trade.calc_profit(rate=profit_rate) # Use cached ticker here - it was updated seconds ago. current_rate = self.get_sell_rate(trade.pair, False) - profit_percent = trade.calc_profit_ratio(profit_rate) - gain = "profit" if profit_percent > 0 else "loss" + profit_ratio = trade.calc_profit_ratio(profit_rate) + gain = "profit" if profit_ratio > 0 else "loss" msg = { 'type': RPCMessageType.SELL_NOTIFICATION, @@ -1048,7 +1048,7 @@ class FreqtradeBot: 'open_rate': trade.open_rate, 'current_rate': current_rate, 'profit_amount': profit_trade, - 'profit_percent': profit_percent, + 'profit_ratio': profit_ratio, 'sell_reason': trade.sell_reason, 'open_date': trade.open_date, 'close_date': trade.close_date or datetime.utcnow(), @@ -1071,8 +1071,8 @@ class FreqtradeBot: profit_rate = trade.close_rate if trade.close_rate else trade.close_rate_requested profit_trade = trade.calc_profit(rate=profit_rate) current_rate = self.get_sell_rate(trade.pair, False) - profit_percent = trade.calc_profit_ratio(profit_rate) - gain = "profit" if profit_percent > 0 else "loss" + profit_ratio = trade.calc_profit_ratio(profit_rate) + gain = "profit" if profit_ratio > 0 else "loss" msg = { 'type': RPCMessageType.SELL_CANCEL_NOTIFICATION, @@ -1085,7 +1085,7 @@ class FreqtradeBot: 'open_rate': trade.open_rate, 'current_rate': current_rate, 'profit_amount': profit_trade, - 'profit_percent': profit_percent, + 'profit_ratio': profit_ratio, 'sell_reason': trade.sell_reason, 'open_date': trade.open_date, 'close_date': trade.close_date, diff --git a/freqtrade/persistence.py b/freqtrade/persistence.py index fa041abc3..ac084d12e 100644 --- a/freqtrade/persistence.py +++ b/freqtrade/persistence.py @@ -405,8 +405,8 @@ class Trade(_DECL_BASE): rate=(rate or self.close_rate), fee=(fee or self.fee_close) ) - profit_percent = (close_trade_price / self.open_trade_price) - 1 - return float(f"{profit_percent:.8f}") + profit_ratio = (close_trade_price / self.open_trade_price) - 1 + return float(f"{profit_ratio:.8f}") @staticmethod def get_trades(trade_filter=None) -> Query: diff --git a/freqtrade/rpc/rpc.py b/freqtrade/rpc/rpc.py index 1e4eaa3e0..9014c1874 100644 --- a/freqtrade/rpc/rpc.py +++ b/freqtrade/rpc/rpc.py @@ -155,9 +155,9 @@ class RPC: current_rate = self._freqtrade.get_sell_rate(trade.pair, False) except DependencyException: current_rate = NAN - trade_perc = (100 * trade.calc_profit_ratio(current_rate)) + trade_percent = (100 * trade.calc_profit_ratio(current_rate)) trade_profit = trade.calc_profit(current_rate) - profit_str = f'{trade_perc:.2f}%' + profit_str = f'{trade_percent:.2f}%' if self._fiat_converter: fiat_profit = self._fiat_converter.convert_amount( trade_profit, @@ -232,9 +232,9 @@ class RPC: trades = Trade.get_trades().order_by(Trade.id).all() profit_all_coin = [] - profit_all_perc = [] + profit_all_ratio = [] profit_closed_coin = [] - profit_closed_perc = [] + profit_closed_ratio = [] durations = [] for trade in trades: @@ -246,21 +246,21 @@ class RPC: durations.append((trade.close_date - trade.open_date).total_seconds()) if not trade.is_open: - profit_percent = trade.calc_profit_ratio() + profit_ratio = trade.calc_profit_ratio() profit_closed_coin.append(trade.calc_profit()) - profit_closed_perc.append(profit_percent) + profit_closed_ratio.append(profit_ratio) else: # Get current rate try: current_rate = self._freqtrade.get_sell_rate(trade.pair, False) except DependencyException: current_rate = NAN - profit_percent = trade.calc_profit_ratio(rate=current_rate) + profit_ratio = trade.calc_profit_ratio(rate=current_rate) profit_all_coin.append( trade.calc_profit(rate=trade.close_rate or current_rate) ) - profit_all_perc.append(profit_percent) + profit_all_ratio.append(profit_ratio) best_pair = Trade.get_best_pair() @@ -271,7 +271,7 @@ class RPC: # Prepare data to display profit_closed_coin_sum = round(sum(profit_closed_coin), 8) - profit_closed_percent = (round(mean(profit_closed_perc) * 100, 2) if profit_closed_perc + profit_closed_percent = (round(mean(profit_closed_ratio) * 100, 2) if profit_closed_ratio else 0.0) profit_closed_fiat = self._fiat_converter.convert_amount( profit_closed_coin_sum, @@ -280,7 +280,7 @@ class RPC: ) if self._fiat_converter else 0 profit_all_coin_sum = round(sum(profit_all_coin), 8) - profit_all_percent = round(mean(profit_all_perc) * 100, 2) if profit_all_perc else 0.0 + profit_all_percent = round(mean(profit_all_ratio) * 100, 2) if profit_all_ratio else 0.0 profit_all_fiat = self._fiat_converter.convert_amount( profit_all_coin_sum, stake_currency, diff --git a/freqtrade/rpc/telegram.py b/freqtrade/rpc/telegram.py index e3958b31a..ad01700ab 100644 --- a/freqtrade/rpc/telegram.py +++ b/freqtrade/rpc/telegram.py @@ -148,7 +148,7 @@ class Telegram(RPC): elif msg['type'] == RPCMessageType.SELL_NOTIFICATION: msg['amount'] = round(msg['amount'], 8) - msg['profit_percent'] = round(msg['profit_percent'] * 100, 2) + msg['profit_percent'] = round(msg['profit_ratio'] * 100, 2) msg['duration'] = msg['close_date'].replace( microsecond=0) - msg['open_date'].replace(microsecond=0) msg['duration_min'] = msg['duration'].total_seconds() / 60 diff --git a/freqtrade/strategy/interface.py b/freqtrade/strategy/interface.py index ae3dbd307..d23af3f6e 100644 --- a/freqtrade/strategy/interface.py +++ b/freqtrade/strategy/interface.py @@ -364,7 +364,7 @@ class IStrategy(ABC): """ Based on current profit of the trade and configured (trailing) stoploss, decides to sell or not - :param current_profit: current profit in percent + :param current_profit: current profit as ratio """ stop_loss_value = force_stoploss if force_stoploss else self.stoploss @@ -427,8 +427,9 @@ class IStrategy(ABC): def min_roi_reached(self, trade: Trade, current_profit: float, current_time: datetime) -> bool: """ - Based on trade duration, current price and ROI configuration, decides whether bot should - sell. Requires current_profit to be in percent!! + Based on trade duration, current profit of the trade and ROI configuration, + decides whether bot should sell. + :param current_profit: current profit as ratio :return: True if bot should sell at current rate """ # Check if time matches and current rate is above threshold diff --git a/tests/edge/test_edge.py b/tests/edge/test_edge.py index 6b86d9c1f..2a0d19128 100644 --- a/tests/edge/test_edge.py +++ b/tests/edge/test_edge.py @@ -158,7 +158,7 @@ def test_edge_results(edge_conf, mocker, caplog, data) -> None: assert len(trades) == len(data.trades) if not results.empty: - assert round(results["profit_percent"].sum(), 3) == round(data.profit_perc, 3) + assert round(results["profit_ratio"].sum(), 3) == round(data.profit_perc, 3) for c, trade in enumerate(data.trades): res = results.iloc[c] diff --git a/tests/rpc/test_rpc_telegram.py b/tests/rpc/test_rpc_telegram.py index a8b8e0c5a..1ac03f812 100644 --- a/tests/rpc/test_rpc_telegram.py +++ b/tests/rpc/test_rpc_telegram.py @@ -726,7 +726,7 @@ def test_forcesell_handle(default_conf, update, ticker, fee, 'open_rate': 1.099e-05, 'current_rate': 1.172e-05, 'profit_amount': 6.126e-05, - 'profit_percent': 0.0611052, + 'profit_ratio': 0.0611052, 'stake_currency': 'BTC', 'fiat_currency': 'USD', 'sell_reason': SellType.FORCE_SELL.value, @@ -785,7 +785,7 @@ def test_forcesell_down_handle(default_conf, update, ticker, fee, 'open_rate': 1.099e-05, 'current_rate': 1.044e-05, 'profit_amount': -5.492e-05, - 'profit_percent': -0.05478342, + 'profit_ratio': -0.05478342, 'stake_currency': 'BTC', 'fiat_currency': 'USD', 'sell_reason': SellType.FORCE_SELL.value, @@ -833,7 +833,7 @@ def test_forcesell_all_handle(default_conf, update, ticker, fee, mocker) -> None 'open_rate': 1.099e-05, 'current_rate': 1.098e-05, 'profit_amount': -5.91e-06, - 'profit_percent': -0.00589291, + 'profit_ratio': -0.00589291, 'stake_currency': 'BTC', 'fiat_currency': 'USD', 'sell_reason': SellType.FORCE_SELL.value, @@ -1253,7 +1253,7 @@ def test_send_msg_sell_notification(default_conf, mocker) -> None: 'open_rate': 7.5e-05, 'current_rate': 3.201e-05, 'profit_amount': -0.05746268, - 'profit_percent': -0.57405275, + 'profit_ratio': -0.57405275, 'stake_currency': 'ETH', 'fiat_currency': 'USD', 'sell_reason': SellType.STOP_LOSS.value, @@ -1282,7 +1282,7 @@ def test_send_msg_sell_notification(default_conf, mocker) -> None: 'open_rate': 7.5e-05, 'current_rate': 3.201e-05, 'profit_amount': -0.05746268, - 'profit_percent': -0.57405275, + 'profit_ratio': -0.57405275, 'stake_currency': 'ETH', 'sell_reason': SellType.STOP_LOSS.value, 'open_date': arrow.utcnow().shift(days=-1, hours=-2, minutes=-30), @@ -1448,7 +1448,7 @@ def test_send_msg_sell_notification_no_fiat(default_conf, mocker) -> None: 'open_rate': 7.5e-05, 'current_rate': 3.201e-05, 'profit_amount': -0.05746268, - 'profit_percent': -0.57405275, + 'profit_ratio': -0.57405275, 'stake_currency': 'ETH', 'fiat_currency': 'USD', 'sell_reason': SellType.STOP_LOSS.value, diff --git a/tests/test_freqtradebot.py b/tests/test_freqtradebot.py index 852b6b990..61f69bd85 100644 --- a/tests/test_freqtradebot.py +++ b/tests/test_freqtradebot.py @@ -2297,7 +2297,7 @@ def test_execute_sell_up(default_conf, ticker, fee, ticker_sell_up, mocker) -> N 'open_rate': 1.099e-05, 'current_rate': 1.172e-05, 'profit_amount': 6.126e-05, - 'profit_percent': 0.0611052, + 'profit_ratio': 0.0611052, 'stake_currency': 'BTC', 'fiat_currency': 'USD', 'sell_reason': SellType.ROI.value, @@ -2346,7 +2346,7 @@ def test_execute_sell_down(default_conf, ticker, fee, ticker_sell_down, mocker) 'open_rate': 1.099e-05, 'current_rate': 1.044e-05, 'profit_amount': -5.492e-05, - 'profit_percent': -0.05478342, + 'profit_ratio': -0.05478342, 'stake_currency': 'BTC', 'fiat_currency': 'USD', 'sell_reason': SellType.STOP_LOSS.value, @@ -2402,7 +2402,7 @@ def test_execute_sell_down_stoploss_on_exchange_dry_run(default_conf, ticker, fe 'open_rate': 1.099e-05, 'current_rate': 1.044e-05, 'profit_amount': -1.498e-05, - 'profit_percent': -0.01493766, + 'profit_ratio': -0.01493766, 'stake_currency': 'BTC', 'fiat_currency': 'USD', 'sell_reason': SellType.STOP_LOSS.value, @@ -2602,7 +2602,7 @@ def test_execute_sell_market_order(default_conf, ticker, fee, 'open_rate': 1.099e-05, 'current_rate': 1.172e-05, 'profit_amount': 6.126e-05, - 'profit_percent': 0.0611052, + 'profit_ratio': 0.0611052, 'stake_currency': 'BTC', 'fiat_currency': 'USD', 'sell_reason': SellType.ROI.value, From 7a3660cd6b0abca9dc42017f0c2303c602e05a67 Mon Sep 17 00:00:00 2001 From: hroff-1902 Date: Thu, 5 Mar 2020 17:44:21 +0300 Subject: [PATCH 2/3] Adjust webhook tests --- tests/rpc/test_rpc_webhook.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/rpc/test_rpc_webhook.py b/tests/rpc/test_rpc_webhook.py index 3f3f36766..1ced62746 100644 --- a/tests/rpc/test_rpc_webhook.py +++ b/tests/rpc/test_rpc_webhook.py @@ -28,12 +28,12 @@ def get_webhook_dict() -> dict: "webhooksell": { "value1": "Selling {pair}", "value2": "limit {limit:8f}", - "value3": "profit: {profit_amount:8f} {stake_currency}" + "value3": "profit: {profit_amount:8f} {stake_currency} ({profit_ratio})" }, "webhooksellcancel": { "value1": "Cancelling Open Sell Order for {pair}", "value2": "limit {limit:8f}", - "value3": "profit: {profit_amount:8f} {stake_currency}" + "value3": "profit: {profit_amount:8f} {stake_currency} ({profit_ratio})" }, "webhookstatus": { "value1": "Status: {status}", @@ -110,7 +110,7 @@ def test_send_msg(default_conf, mocker): 'open_rate': 0.004, 'current_rate': 0.005, 'profit_amount': 0.001, - 'profit_percent': 0.20, + 'profit_ratio': 0.20, 'stake_currency': 'BTC', 'sell_reason': SellType.STOP_LOSS.value } @@ -136,7 +136,7 @@ def test_send_msg(default_conf, mocker): 'open_rate': 0.004, 'current_rate': 0.005, 'profit_amount': 0.001, - 'profit_percent': 0.20, + 'profit_ratio': 0.20, 'stake_currency': 'BTC', 'sell_reason': SellType.STOP_LOSS.value } From eee5727426fd0001ec8ddfdbf529dac435d503c4 Mon Sep 17 00:00:00 2001 From: hroff-1902 Date: Thu, 5 Mar 2020 17:44:38 +0300 Subject: [PATCH 3/3] Adjust webhook docs --- docs/webhook-config.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/webhook-config.md b/docs/webhook-config.md index e53aa8af5..70a41dd46 100644 --- a/docs/webhook-config.md +++ b/docs/webhook-config.md @@ -23,12 +23,12 @@ Sample configuration (tested using IFTTT). "webhooksell": { "value1": "Selling {pair}", "value2": "limit {limit:8f}", - "value3": "profit: {profit_amount:8f} {stake_currency}" + "value3": "profit: {profit_amount:8f} {stake_currency} ({profit_ratio})" }, "webhooksellcancel": { "value1": "Cancelling Open Sell Order for {pair}", "value2": "limit {limit:8f}", - "value3": "profit: {profit_amount:8f} {stake_currency}" + "value3": "profit: {profit_amount:8f} {stake_currency} ({profit_ratio})" }, "webhookstatus": { "value1": "Status: {status}", @@ -87,7 +87,7 @@ Possible parameters are: * `open_rate` * `current_rate` * `profit_amount` -* `profit_percent` +* `profit_ratio` * `stake_currency` * `fiat_currency` * `sell_reason` @@ -108,7 +108,7 @@ Possible parameters are: * `open_rate` * `current_rate` * `profit_amount` -* `profit_percent` +* `profit_ratio` * `stake_currency` * `fiat_currency` * `sell_reason`