Changesets · Branches · Tags · Compare
| Changeset | 35f65cdea2904775d79fe15b99467205dbd08c0e5fb3562930a6f3082fe9e44d |
|---|---|
| Author | Matthias <xmatthias@outlook.com> · 2021-04-20 20:39 +02:00 |
| Committer | GitHub <noreply@github.com> · 2021-04-20 20:39 +02:00 |
| Parents | c2b7079e6e91, 98a00ac2170c |
14 files changed
modified config_full.json.example +2 −0
| @@ -163,7 +163,9 @@ | ||
| 163 | 163 | "warning": "on", |
| 164 | 164 | "startup": "on", |
| 165 | 165 | "buy": "on", |
| 166 | + "buy_fill": "on", | |
| 166 | 167 | "sell": "on", |
| 168 | + "sell_fill": "on", | |
| 167 | 169 | "buy_cancel": "on", |
| 168 | 170 | "sell_cancel": "on" |
| 169 | 171 | } |
modified docs/telegram-usage.md +8 −1
| @@ -82,12 +82,19 @@ | ||
| 82 | 82 | "buy": "silent", |
| 83 | 83 | "sell": "on", |
| 84 | 84 | "buy_cancel": "silent", |
| 85 | - "sell_cancel": "on" | |
| 85 | + "sell_cancel": "on", | |
| 86 | + "buy_fill": "off", | |
| 87 | + "sell_fill": "off" | |
| 86 | 88 | }, |
| 87 | 89 | "balance_dust_level": 0.01 |
| 88 | 90 | }, |
| 89 | 91 | ``` |
| 90 | 92 | |
| 93 | +`buy` notifications are sent when the order is placed, while `buy_fill` notifications are sent when the order is filled on the exchange. | |
| 94 | +`sell` notifications are sent when the order is placed, while `sell_fill` notifications are sent when the order is filled on the exchange. | |
| 95 | +`*_fill` notifications are off by default and must be explicitly enabled. | |
| 96 | + | |
| 97 | + | |
| 91 | 98 | `balance_dust_level` will define what the `/balance` command takes as "dust" - Currencies with a balance below this will be shown. |
| 92 | 99 | |
| 93 | 100 | ## Create a custom keyboard (command shortcut buttons) |
modified docs/webhook-config.md +46 −0
| @@ -19,6 +19,11 @@ | ||
| 19 | 19 | "value1": "Cancelling Open Buy Order for {pair}", |
| 20 | 20 | "value2": "limit {limit:8f}", |
| 21 | 21 | "value3": "{stake_amount:8f} {stake_currency}" |
| 22 | + }, | |
| 23 | + "webhookbuyfill": { | |
| 24 | + "value1": "Buy Order for {pair} filled", | |
| 25 | + "value2": "at {open_rate:8f}", | |
| 26 | + "value3": "" | |
| 22 | 27 | }, |
| 23 | 28 | "webhooksell": { |
| 24 | 29 | "value1": "Selling {pair}", |
| @@ -30,6 +35,11 @@ | ||
| 30 | 35 | "value2": "limit {limit:8f}", |
| 31 | 36 | "value3": "profit: {profit_amount:8f} {stake_currency} ({profit_ratio})" |
| 32 | 37 | }, |
| 38 | + "webhooksellfill": { | |
| 39 | + "value1": "Sell Order for {pair} filled", | |
| 40 | + "value2": "at {close_rate:8f}.", | |
| 41 | + "value3": "" | |
| 42 | + }, | |
| 33 | 43 | "webhookstatus": { |
| 34 | 44 | "value1": "Status: {status}", |
| 35 | 45 | "value2": "", |
| @@ -91,6 +101,21 @@ | ||
| 91 | 101 | * `order_type` |
| 92 | 102 | * `current_rate` |
| 93 | 103 | |
| 104 | +### Webhookbuyfill | |
| 105 | + | |
| 106 | +The fields in `webhook.webhookbuyfill` are filled when the bot filled a buy order. Parameters are filled using string.format. | |
| 107 | +Possible parameters are: | |
| 108 | + | |
| 109 | +* `trade_id` | |
| 110 | +* `exchange` | |
| 111 | +* `pair` | |
| 112 | +* `open_rate` | |
| 113 | +* `amount` | |
| 114 | +* `open_date` | |
| 115 | +* `stake_amount` | |
| 116 | +* `stake_currency` | |
| 117 | +* `fiat_currency` | |
| 118 | + | |
| 94 | 119 | ### Webhooksell |
| 95 | 120 | |
| 96 | 121 | The fields in `webhook.webhooksell` are filled when the bot sells a trade. Parameters are filled using string.format. |
| @@ -103,6 +128,27 @@ | ||
| 103 | 128 | * `limit` |
| 104 | 129 | * `amount` |
| 105 | 130 | * `open_rate` |
| 131 | +* `profit_amount` | |
| 132 | +* `profit_ratio` | |
| 133 | +* `stake_currency` | |
| 134 | +* `fiat_currency` | |
| 135 | +* `sell_reason` | |
| 136 | +* `order_type` | |
| 137 | +* `open_date` | |
| 138 | +* `close_date` | |
| 139 | + | |
| 140 | +### Webhooksellfill | |
| 141 | + | |
| 142 | +The fields in `webhook.webhooksellfill` are filled when the bot fills a sell order (closes a Trae). Parameters are filled using string.format. | |
| 143 | +Possible parameters are: | |
| 144 | + | |
| 145 | +* `trade_id` | |
| 146 | +* `exchange` | |
| 147 | +* `pair` | |
| 148 | +* `gain` | |
| 149 | +* `close_rate` | |
| 150 | +* `amount` | |
| 151 | +* `open_rate` | |
| 106 | 152 | * `current_rate` |
| 107 | 153 | * `profit_amount` |
| 108 | 154 | * `profit_ratio` |
modified freqtrade/constants.py +12 −2
| @@ -246,14 +246,24 @@ | ||
| 246 | 246 | 'balance_dust_level': {'type': 'number', 'minimum': 0.0}, |
| 247 | 247 | 'notification_settings': { |
| 248 | 248 | 'type': 'object', |
| 249 | + 'default': {}, | |
| 249 | 250 | 'properties': { |
| 250 | 251 | 'status': {'type': 'string', 'enum': TELEGRAM_SETTING_OPTIONS}, |
| 251 | 252 | 'warning': {'type': 'string', 'enum': TELEGRAM_SETTING_OPTIONS}, |
| 252 | 253 | 'startup': {'type': 'string', 'enum': TELEGRAM_SETTING_OPTIONS}, |
| 253 | 254 | 'buy': {'type': 'string', 'enum': TELEGRAM_SETTING_OPTIONS}, |
| 254 | - 'sell': {'type': 'string', 'enum': TELEGRAM_SETTING_OPTIONS}, | |
| 255 | 255 | 'buy_cancel': {'type': 'string', 'enum': TELEGRAM_SETTING_OPTIONS}, |
| 256 | - 'sell_cancel': {'type': 'string', 'enum': TELEGRAM_SETTING_OPTIONS} | |
| 256 | + 'buy_fill': {'type': 'string', | |
| 257 | + 'enum': TELEGRAM_SETTING_OPTIONS, | |
| 258 | + 'default': 'off' | |
| 259 | + }, | |
| 260 | + 'sell': {'type': 'string', 'enum': TELEGRAM_SETTING_OPTIONS}, | |
| 261 | + 'sell_cancel': {'type': 'string', 'enum': TELEGRAM_SETTING_OPTIONS}, | |
| 262 | + 'sell_fill': { | |
| 263 | + 'type': 'string', | |
| 264 | + 'enum': TELEGRAM_SETTING_OPTIONS, | |
| 265 | + 'default': 'off' | |
| 266 | + }, | |
| 257 | 267 | } |
| 258 | 268 | } |
| 259 | 269 | }, |
modified freqtrade/freqtradebot.py +31 −8
| @@ -113,7 +113,7 @@ | ||
| 113 | 113 | via RPC about changes in the bot status. |
| 114 | 114 | """ |
| 115 | 115 | self.rpc.send_msg({ |
| 116 | - 'type': RPCMessageType.STATUS_NOTIFICATION, | |
| 116 | + 'type': RPCMessageType.STATUS, | |
| 117 | 117 | 'status': msg |
| 118 | 118 | }) |
| 119 | 119 | |
| @@ -205,7 +205,7 @@ | ||
| 205 | 205 | |
| 206 | 206 | if len(open_trades) != 0: |
| 207 | 207 | msg = { |
| 208 | - 'type': RPCMessageType.WARNING_NOTIFICATION, | |
| 208 | + 'type': RPCMessageType.WARNING, | |
| 209 | 209 | 'status': f"{len(open_trades)} open trades active.\n\n" |
| 210 | 210 | f"Handle these trades manually on {self.exchange.name}, " |
| 211 | 211 | f"or '/start' the bot again and use '/stopbuy' " |
| @@ -634,7 +634,7 @@ | ||
| 634 | 634 | """ |
| 635 | 635 | msg = { |
| 636 | 636 | 'trade_id': trade.id, |
| 637 | - 'type': RPCMessageType.BUY_NOTIFICATION, | |
| 637 | + 'type': RPCMessageType.BUY, | |
| 638 | 638 | 'exchange': self.exchange.name.capitalize(), |
| 639 | 639 | 'pair': trade.pair, |
| 640 | 640 | 'limit': trade.open_rate, |
| @@ -658,7 +658,7 @@ | ||
| 658 | 658 | |
| 659 | 659 | msg = { |
| 660 | 660 | 'trade_id': trade.id, |
| 661 | - 'type': RPCMessageType.BUY_CANCEL_NOTIFICATION, | |
| 661 | + 'type': RPCMessageType.BUY_CANCEL, | |
| 662 | 662 | 'exchange': self.exchange.name.capitalize(), |
| 663 | 663 | 'pair': trade.pair, |
| 664 | 664 | 'limit': trade.open_rate, |
| @@ -675,6 +675,21 @@ | ||
| 675 | 675 | # Send the message |
| 676 | 676 | self.rpc.send_msg(msg) |
| 677 | 677 | |
| 678 | + def _notify_buy_fill(self, trade: Trade) -> None: | |
| 679 | + msg = { | |
| 680 | + 'trade_id': trade.id, | |
| 681 | + 'type': RPCMessageType.BUY_FILL, | |
| 682 | + 'exchange': self.exchange.name.capitalize(), | |
| 683 | + 'pair': trade.pair, | |
| 684 | + 'open_rate': trade.open_rate, | |
| 685 | + 'stake_amount': trade.stake_amount, | |
| 686 | + 'stake_currency': self.config['stake_currency'], | |
| 687 | + 'fiat_currency': self.config.get('fiat_display_currency', None), | |
| 688 | + 'amount': trade.amount, | |
| 689 | + 'open_date': trade.open_date, | |
| 690 | + } | |
| 691 | + self.rpc.send_msg(msg) | |
| 692 | + | |
| 678 | 693 | # |
| 679 | 694 | # SELL / exit positions / close trades logic and methods |
| 680 | 695 | # |
| @@ -1212,19 +1227,20 @@ | ||
| 1212 | 1227 | |
| 1213 | 1228 | return True |
| 1214 | 1229 | |
| 1215 | - def _notify_sell(self, trade: Trade, order_type: str) -> None: | |
| 1230 | + def _notify_sell(self, trade: Trade, order_type: str, fill: bool = False) -> None: | |
| 1216 | 1231 | """ |
| 1217 | 1232 | Sends rpc notification when a sell occured. |
| 1218 | 1233 | """ |
| 1219 | 1234 | profit_rate = trade.close_rate if trade.close_rate else trade.close_rate_requested |
| 1220 | 1235 | profit_trade = trade.calc_profit(rate=profit_rate) |
| 1221 | 1236 | # Use cached rates here - it was updated seconds ago. |
| 1222 | - current_rate = self.get_sell_rate(trade.pair, False) | |
| 1237 | + current_rate = self.get_sell_rate(trade.pair, False) if not fill else None | |
| 1223 | 1238 | profit_ratio = trade.calc_profit_ratio(profit_rate) |
| 1224 | 1239 | gain = "profit" if profit_ratio > 0 else "loss" |
| 1225 | 1240 | |
| 1226 | 1241 | msg = { |
| 1227 | - 'type': RPCMessageType.SELL_NOTIFICATION, | |
| 1242 | + 'type': (RPCMessageType.SELL_FILL if fill | |
| 1243 | + else RPCMessageType.SELL), | |
| 1228 | 1244 | 'trade_id': trade.id, |
| 1229 | 1245 | 'exchange': trade.exchange.capitalize(), |
| 1230 | 1246 | 'pair': trade.pair, |
| @@ -1233,6 +1249,7 @@ | ||
| 1233 | 1249 | 'order_type': order_type, |
| 1234 | 1250 | 'amount': trade.amount, |
| 1235 | 1251 | 'open_rate': trade.open_rate, |
| 1252 | + 'close_rate': trade.close_rate, | |
| 1236 | 1253 | 'current_rate': current_rate, |
| 1237 | 1254 | 'profit_amount': profit_trade, |
| 1238 | 1255 | 'profit_ratio': profit_ratio, |
| @@ -1267,7 +1284,7 @@ | ||
| 1267 | 1284 | gain = "profit" if profit_ratio > 0 else "loss" |
| 1268 | 1285 | |
| 1269 | 1286 | msg = { |
| 1270 | - 'type': RPCMessageType.SELL_CANCEL_NOTIFICATION, | |
| 1287 | + 'type': RPCMessageType.SELL_CANCEL, | |
| 1271 | 1288 | 'trade_id': trade.id, |
| 1272 | 1289 | 'exchange': trade.exchange.capitalize(), |
| 1273 | 1290 | 'pair': trade.pair, |
| @@ -1344,9 +1361,15 @@ | ||
| 1344 | 1361 | |
| 1345 | 1362 | # Updating wallets when order is closed |
| 1346 | 1363 | if not trade.is_open: |
| 1364 | + if not stoploss_order and not trade.open_order_id: | |
| 1365 | + self._notify_sell(trade, '', True) | |
| 1347 | 1366 | self.protections.stop_per_pair(trade.pair) |
| 1348 | 1367 | self.protections.global_stop() |
| 1349 | 1368 | self.wallets.update() |
| 1369 | + elif not trade.open_order_id: | |
| 1370 | + # Buy fill | |
| 1371 | + self._notify_buy_fill(trade) | |
| 1372 | + | |
| 1350 | 1373 | return False |
| 1351 | 1374 | |
| 1352 | 1375 | def apply_fee_conditional(self, trade: Trade, trade_base_currency: str, |
modified freqtrade/rpc/rpc.py +9 −7
| @@ -31,13 +31,15 @@ | ||
| 31 | 31 | |
| 32 | 32 | |
| 33 | 33 | class RPCMessageType(Enum): |
| 34 | - STATUS_NOTIFICATION = 'status' | |
| 35 | - WARNING_NOTIFICATION = 'warning' | |
| 36 | - STARTUP_NOTIFICATION = 'startup' | |
| 37 | - BUY_NOTIFICATION = 'buy' | |
| 38 | - BUY_CANCEL_NOTIFICATION = 'buy_cancel' | |
| 39 | - SELL_NOTIFICATION = 'sell' | |
| 40 | - SELL_CANCEL_NOTIFICATION = 'sell_cancel' | |
| 34 | + STATUS = 'status' | |
| 35 | + WARNING = 'warning' | |
| 36 | + STARTUP = 'startup' | |
| 37 | + BUY = 'buy' | |
| 38 | + BUY_FILL = 'buy_fill' | |
| 39 | + BUY_CANCEL = 'buy_cancel' | |
| 40 | + SELL = 'sell' | |
| 41 | + SELL_FILL = 'sell_fill' | |
| 42 | + SELL_CANCEL = 'sell_cancel' | |
| 41 | 43 | |
| 42 | 44 | def __repr__(self): |
| 43 | 45 | return self.value |
modified freqtrade/rpc/rpc_manager.py +4 −4
| @@ -67,7 +67,7 @@ | ||
| 67 | 67 | def startup_messages(self, config: Dict[str, Any], pairlist, protections) -> None: |
| 68 | 68 | if config['dry_run']: |
| 69 | 69 | self.send_msg({ |
| 70 | - 'type': RPCMessageType.WARNING_NOTIFICATION, | |
| 70 | + 'type': RPCMessageType.WARNING, | |
| 71 | 71 | 'status': 'Dry run is enabled. All trades are simulated.' |
| 72 | 72 | }) |
| 73 | 73 | stake_currency = config['stake_currency'] |
| @@ -79,7 +79,7 @@ | ||
| 79 | 79 | exchange_name = config['exchange']['name'] |
| 80 | 80 | strategy_name = config.get('strategy', '') |
| 81 | 81 | self.send_msg({ |
| 82 | - 'type': RPCMessageType.STARTUP_NOTIFICATION, | |
| 82 | + 'type': RPCMessageType.STARTUP, | |
| 83 | 83 | 'status': f'*Exchange:* `{exchange_name}`\n' |
| 84 | 84 | f'*Stake per trade:* `{stake_amount} {stake_currency}`\n' |
| 85 | 85 | f'*Minimum ROI:* `{minimal_roi}`\n' |
| @@ -88,13 +88,13 @@ | ||
| 88 | 88 | f'*Strategy:* `{strategy_name}`' |
| 89 | 89 | }) |
| 90 | 90 | self.send_msg({ |
| 91 | - 'type': RPCMessageType.STARTUP_NOTIFICATION, | |
| 91 | + 'type': RPCMessageType.STARTUP, | |
| 92 | 92 | 'status': f'Searching for {stake_currency} pairs to buy and sell ' |
| 93 | 93 | f'based on {pairlist.short_desc()}' |
| 94 | 94 | }) |
| 95 | 95 | if len(protections.name_list) > 0: |
| 96 | 96 | prots = '\n'.join([p for prot in protections.short_desc() for k, p in prot.items()]) |
| 97 | 97 | self.send_msg({ |
| 98 | - 'type': RPCMessageType.STARTUP_NOTIFICATION, | |
| 98 | + 'type': RPCMessageType.STARTUP, | |
| 99 | 99 | 'status': f'Using Protections: \n{prots}' |
| 100 | 100 | }) |
modified freqtrade/rpc/telegram.py +65 −54
| @@ -176,6 +176,53 @@ | ||
| 176 | 176 | """ |
| 177 | 177 | self._updater.stop() |
| 178 | 178 | |
| 179 | + def _format_buy_msg(self, msg: Dict[str, Any]) -> str: | |
| 180 | + if self._rpc._fiat_converter: | |
| 181 | + msg['stake_amount_fiat'] = self._rpc._fiat_converter.convert_amount( | |
| 182 | + msg['stake_amount'], msg['stake_currency'], msg['fiat_currency']) | |
| 183 | + else: | |
| 184 | + msg['stake_amount_fiat'] = 0 | |
| 185 | + | |
| 186 | + message = (f"\N{LARGE BLUE CIRCLE} *{msg['exchange']}:* Buying {msg['pair']}" | |
| 187 | + f" (#{msg['trade_id']})\n" | |
| 188 | + f"*Amount:* `{msg['amount']:.8f}`\n" | |
| 189 | + f"*Open Rate:* `{msg['limit']:.8f}`\n" | |
| 190 | + f"*Current Rate:* `{msg['current_rate']:.8f}`\n" | |
| 191 | + f"*Total:* `({round_coin_value(msg['stake_amount'], msg['stake_currency'])}") | |
| 192 | + | |
| 193 | + if msg.get('fiat_currency', None): | |
| 194 | + message += f", {round_coin_value(msg['stake_amount_fiat'], msg['fiat_currency'])}" | |
| 195 | + message += ")`" | |
| 196 | + return message | |
| 197 | + | |
| 198 | + def _format_sell_msg(self, msg: Dict[str, Any]) -> str: | |
| 199 | + msg['amount'] = round(msg['amount'], 8) | |
| 200 | + msg['profit_percent'] = round(msg['profit_ratio'] * 100, 2) | |
| 201 | + msg['duration'] = msg['close_date'].replace( | |
| 202 | + microsecond=0) - msg['open_date'].replace(microsecond=0) | |
| 203 | + msg['duration_min'] = msg['duration'].total_seconds() / 60 | |
| 204 | + | |
| 205 | + msg['emoji'] = self._get_sell_emoji(msg) | |
| 206 | + | |
| 207 | + message = ("{emoji} *{exchange}:* Selling {pair} (#{trade_id})\n" | |
| 208 | + "*Amount:* `{amount:.8f}`\n" | |
| 209 | + "*Open Rate:* `{open_rate:.8f}`\n" | |
| 210 | + "*Current Rate:* `{current_rate:.8f}`\n" | |
| 211 | + "*Close Rate:* `{limit:.8f}`\n" | |
| 212 | + "*Sell Reason:* `{sell_reason}`\n" | |
| 213 | + "*Duration:* `{duration} ({duration_min:.1f} min)`\n" | |
| 214 | + "*Profit:* `{profit_percent:.2f}%`").format(**msg) | |
| 215 | + | |
| 216 | + # Check if all sell properties are available. | |
| 217 | + # This might not be the case if the message origin is triggered by /forcesell | |
| 218 | + if (all(prop in msg for prop in ['gain', 'fiat_currency', 'stake_currency']) | |
| 219 | + and self._rpc._fiat_converter): | |
| 220 | + msg['profit_fiat'] = self._rpc._fiat_converter.convert_amount( | |
| 221 | + msg['profit_amount'], msg['stake_currency'], msg['fiat_currency']) | |
| 222 | + message += (' `({gain}: {profit_amount:.8f} {stake_currency}' | |
| 223 | + ' / {profit_fiat:.3f} {fiat_currency})`').format(**msg) | |
| 224 | + return message | |
| 225 | + | |
| 179 | 226 | def send_msg(self, msg: Dict[str, Any]) -> None: |
| 180 | 227 | """ Send a message to telegram channel """ |
| 181 | 228 | |
| @@ -186,67 +233,31 @@ | ||
| 186 | 233 | # Notification disabled |
| 187 | 234 | return |
| 188 | 235 | |
| 189 | - if msg['type'] == RPCMessageType.BUY_NOTIFICATION: | |
| 190 | - if self._rpc._fiat_converter: | |
| 191 | - msg['stake_amount_fiat'] = self._rpc._fiat_converter.convert_amount( | |
| 192 | - msg['stake_amount'], msg['stake_currency'], msg['fiat_currency']) | |
| 193 | - else: | |
| 194 | - msg['stake_amount_fiat'] = 0 | |
| 195 | - | |
| 196 | - message = (f"\N{LARGE BLUE CIRCLE} *{msg['exchange']}:* Buying {msg['pair']}" | |
| 197 | - f" (#{msg['trade_id']})\n" | |
| 198 | - f"*Amount:* `{msg['amount']:.8f}`\n" | |
| 199 | - f"*Open Rate:* `{msg['limit']:.8f}`\n" | |
| 200 | - f"*Current Rate:* `{msg['current_rate']:.8f}`\n" | |
| 201 | - f"*Total:* `({round_coin_value(msg['stake_amount'], msg['stake_currency'])}") | |
| 202 | - | |
| 203 | - if msg.get('fiat_currency', None): | |
| 204 | - message += f", {round_coin_value(msg['stake_amount_fiat'], msg['fiat_currency'])}" | |
| 205 | - message += ")`" | |
| 206 | - | |
| 207 | - elif msg['type'] == RPCMessageType.BUY_CANCEL_NOTIFICATION: | |
| 236 | + if msg['type'] == RPCMessageType.BUY: | |
| 237 | + message = self._format_buy_msg(msg) | |
| 238 | + | |
| 239 | + elif msg['type'] in (RPCMessageType.BUY_CANCEL, RPCMessageType.SELL_CANCEL): | |
| 240 | + msg['message_side'] = 'buy' if msg['type'] == RPCMessageType.BUY_CANCEL else 'sell' | |
| 208 | 241 | message = ("\N{WARNING SIGN} *{exchange}:* " |
| 209 | - "Cancelling open buy Order for {pair} (#{trade_id}). " | |
| 242 | + "Cancelling open {message_side} Order for {pair} (#{trade_id}). " | |
| 210 | 243 | "Reason: {reason}.".format(**msg)) |
| 211 | 244 | |
| 212 | - elif msg['type'] == RPCMessageType.SELL_NOTIFICATION: | |
| 213 | - msg['amount'] = round(msg['amount'], 8) | |
| 214 | - msg['profit_percent'] = round(msg['profit_ratio'] * 100, 2) | |
| 215 | - msg['duration'] = msg['close_date'].replace( | |
| 216 | - microsecond=0) - msg['open_date'].replace(microsecond=0) | |
| 217 | - msg['duration_min'] = msg['duration'].total_seconds() / 60 | |
| 218 | - | |
| 219 | - msg['emoji'] = self._get_sell_emoji(msg) | |
| 220 | - | |
| 221 | - message = ("{emoji} *{exchange}:* Selling {pair} (#{trade_id})\n" | |
| 222 | - "*Amount:* `{amount:.8f}`\n" | |
| 223 | - "*Open Rate:* `{open_rate:.8f}`\n" | |
| 224 | - "*Current Rate:* `{current_rate:.8f}`\n" | |
| 225 | - "*Close Rate:* `{limit:.8f}`\n" | |
| 226 | - "*Sell Reason:* `{sell_reason}`\n" | |
| 227 | - "*Duration:* `{duration} ({duration_min:.1f} min)`\n" | |
| 228 | - "*Profit:* `{profit_percent:.2f}%`").format(**msg) | |
| 229 | - | |
| 230 | - # Check if all sell properties are available. | |
| 231 | - # This might not be the case if the message origin is triggered by /forcesell | |
| 232 | - if (all(prop in msg for prop in ['gain', 'fiat_currency', 'stake_currency']) | |
| 233 | - and self._rpc._fiat_converter): | |
| 234 | - msg['profit_fiat'] = self._rpc._fiat_converter.convert_amount( | |
| 235 | - msg['profit_amount'], msg['stake_currency'], msg['fiat_currency']) | |
| 236 | - message += (' `({gain}: {profit_amount:.8f} {stake_currency}' | |
| 237 | - ' / {profit_fiat:.3f} {fiat_currency})`').format(**msg) | |
| 238 | - | |
| 239 | - elif msg['type'] == RPCMessageType.SELL_CANCEL_NOTIFICATION: | |
| 240 | - message = ("\N{WARNING SIGN} *{exchange}:* Cancelling Open Sell Order " | |
| 241 | - "for {pair} (#{trade_id}). Reason: {reason}").format(**msg) | |
| 242 | - | |
| 243 | - elif msg['type'] == RPCMessageType.STATUS_NOTIFICATION: | |
| 245 | + elif msg['type'] in (RPCMessageType.BUY_FILL, RPCMessageType.SELL_FILL): | |
| 246 | + msg['message_side'] = 'Buy' if msg['type'] == RPCMessageType.BUY_FILL else 'Sell' | |
| 247 | + | |
| 248 | + message = ("\N{LARGE CIRCLE} *{exchange}:* " | |
| 249 | + "Buy order for {pair} (#{trade_id}) filled for {open_rate}.".format(**msg)) | |
| 250 | + | |
| 251 | + elif msg['type'] == RPCMessageType.SELL: | |
| 252 | + message = self._format_sell_msg(msg) | |
| 253 | + | |
| 254 | + elif msg['type'] == RPCMessageType.STATUS: | |
| 244 | 255 | message = '*Status:* `{status}`'.format(**msg) |
| 245 | 256 | |
| 246 | - elif msg['type'] == RPCMessageType.WARNING_NOTIFICATION: | |
| 257 | + elif msg['type'] == RPCMessageType.WARNING: | |
| 247 | 258 | message = '\N{WARNING SIGN} *Warning:* `{status}`'.format(**msg) |
| 248 | 259 | |
| 249 | - elif msg['type'] == RPCMessageType.STARTUP_NOTIFICATION: | |
| 260 | + elif msg['type'] == RPCMessageType.STARTUP: | |
| 250 | 261 | message = '{status}'.format(**msg) |
| 251 | 262 | |
| 252 | 263 | else: |
modified freqtrade/rpc/webhook.py +11 −7
| @@ -45,17 +45,21 @@ | ||
| 45 | 45 | """ Send a message to telegram channel """ |
| 46 | 46 | try: |
| 47 | 47 | |
| 48 | - if msg['type'] == RPCMessageType.BUY_NOTIFICATION: | |
| 48 | + if msg['type'] == RPCMessageType.BUY: | |
| 49 | 49 | valuedict = self._config['webhook'].get('webhookbuy', None) |
| 50 | - elif msg['type'] == RPCMessageType.BUY_CANCEL_NOTIFICATION: | |
| 50 | + elif msg['type'] == RPCMessageType.BUY_CANCEL: | |
| 51 | 51 | valuedict = self._config['webhook'].get('webhookbuycancel', None) |
| 52 | - elif msg['type'] == RPCMessageType.SELL_NOTIFICATION: | |
| 52 | + elif msg['type'] == RPCMessageType.BUY_FILL: | |
| 53 | + valuedict = self._config['webhook'].get('webhookbuyfill', None) | |
| 54 | + elif msg['type'] == RPCMessageType.SELL: | |
| 53 | 55 | valuedict = self._config['webhook'].get('webhooksell', None) |
| 54 | - elif msg['type'] == RPCMessageType.SELL_CANCEL_NOTIFICATION: | |
| 56 | + elif msg['type'] == RPCMessageType.SELL_FILL: | |
| 57 | + valuedict = self._config['webhook'].get('webhooksellfill', None) | |
| 58 | + elif msg['type'] == RPCMessageType.SELL_CANCEL: | |
| 55 | 59 | valuedict = self._config['webhook'].get('webhooksellcancel', None) |
| 56 | - elif msg['type'] in (RPCMessageType.STATUS_NOTIFICATION, | |
| 57 | - RPCMessageType.STARTUP_NOTIFICATION, | |
| 58 | - RPCMessageType.WARNING_NOTIFICATION): | |
| 60 | + elif msg['type'] in (RPCMessageType.STATUS, | |
| 61 | + RPCMessageType.STARTUP, | |
| 62 | + RPCMessageType.WARNING): | |
| 59 | 63 | valuedict = self._config['webhook'].get('webhookstatus', None) |
| 60 | 64 | else: |
| 61 | 65 | raise NotImplementedError('Unknown message type: {}'.format(msg['type'])) |
modified tests/conftest.py +2 −1
| @@ -314,7 +314,8 @@ | ||
| 314 | 314 | "telegram": { |
| 315 | 315 | "enabled": True, |
| 316 | 316 | "token": "token", |
| 317 | - "chat_id": "0" | |
| 317 | + "chat_id": "0", | |
| 318 | + "notification_settings": {}, | |
| 318 | 319 | }, |
| 319 | 320 | "datadir": str(testdatadir), |
| 320 | 321 | "initial_state": "running", |
modified tests/rpc/test_rpc_manager.py +3 −3
| @@ -71,7 +71,7 @@ | ||
| 71 | 71 | freqtradebot = get_patched_freqtradebot(mocker, default_conf) |
| 72 | 72 | rpc_manager = RPCManager(freqtradebot) |
| 73 | 73 | rpc_manager.send_msg({ |
| 74 | - 'type': RPCMessageType.STATUS_NOTIFICATION, | |
| 74 | + 'type': RPCMessageType.STATUS, | |
| 75 | 75 | 'status': 'test' |
| 76 | 76 | }) |
| 77 | 77 | |
| @@ -86,7 +86,7 @@ | ||
| 86 | 86 | freqtradebot = get_patched_freqtradebot(mocker, default_conf) |
| 87 | 87 | rpc_manager = RPCManager(freqtradebot) |
| 88 | 88 | rpc_manager.send_msg({ |
| 89 | - 'type': RPCMessageType.STATUS_NOTIFICATION, | |
| 89 | + 'type': RPCMessageType.STATUS, | |
| 90 | 90 | 'status': 'test' |
| 91 | 91 | }) |
| 92 | 92 | |
| @@ -124,7 +124,7 @@ | ||
| 124 | 124 | rpc_manager = RPCManager(get_patched_freqtradebot(mocker, default_conf)) |
| 125 | 125 | |
| 126 | 126 | assert 'webhook' in [mod.name for mod in rpc_manager.registered_modules] |
| 127 | - rpc_manager.send_msg({'type': RPCMessageType.STARTUP_NOTIFICATION, | |
| 127 | + rpc_manager.send_msg({'type': RPCMessageType.STARTUP, | |
| 128 | 128 | 'status': 'TestMessage'}) |
| 129 | 129 | assert log_has( |
| 130 | 130 | "Message type 'startup' not implemented by handler webhook.", |
modified tests/rpc/test_rpc_telegram.py +45 −23
| @@ -683,10 +683,10 @@ | ||
| 683 | 683 | context.args = ["1"] |
| 684 | 684 | telegram._forcesell(update=update, context=context) |
| 685 | 685 | |
| 686 | - assert msg_mock.call_count == 3 | |
| 686 | + assert msg_mock.call_count == 4 | |
| 687 | 687 | last_msg = msg_mock.call_args_list[-1][0][0] |
| 688 | 688 | assert { |
| 689 | - 'type': RPCMessageType.SELL_NOTIFICATION, | |
| 689 | + 'type': RPCMessageType.SELL, | |
| 690 | 690 | 'trade_id': 1, |
| 691 | 691 | 'exchange': 'Binance', |
| 692 | 692 | 'pair': 'ETH/BTC', |
| @@ -703,6 +703,7 @@ | ||
| 703 | 703 | 'sell_reason': SellType.FORCE_SELL.value, |
| 704 | 704 | 'open_date': ANY, |
| 705 | 705 | 'close_date': ANY, |
| 706 | + 'close_rate': ANY, | |
| 706 | 707 | } == last_msg |
| 707 | 708 | |
| 708 | 709 | |
| @@ -743,11 +744,11 @@ | ||
| 743 | 744 | context.args = ["1"] |
| 744 | 745 | telegram._forcesell(update=update, context=context) |
| 745 | 746 | |
| 746 | - assert msg_mock.call_count == 3 | |
| 747 | + assert msg_mock.call_count == 4 | |
| 747 | 748 | |
| 748 | 749 | last_msg = msg_mock.call_args_list[-1][0][0] |
| 749 | 750 | assert { |
| 750 | - 'type': RPCMessageType.SELL_NOTIFICATION, | |
| 751 | + 'type': RPCMessageType.SELL, | |
| 751 | 752 | 'trade_id': 1, |
| 752 | 753 | 'exchange': 'Binance', |
| 753 | 754 | 'pair': 'ETH/BTC', |
| @@ -764,6 +765,7 @@ | ||
| 764 | 765 | 'sell_reason': SellType.FORCE_SELL.value, |
| 765 | 766 | 'open_date': ANY, |
| 766 | 767 | 'close_date': ANY, |
| 768 | + 'close_rate': ANY, | |
| 767 | 769 | } == last_msg |
| 768 | 770 | |
| 769 | 771 | |
| @@ -794,11 +796,11 @@ | ||
| 794 | 796 | context.args = ["all"] |
| 795 | 797 | telegram._forcesell(update=update, context=context) |
| 796 | 798 | |
| 797 | - # Called for each trade 3 times | |
| 798 | - assert msg_mock.call_count == 8 | |
| 799 | - msg = msg_mock.call_args_list[1][0][0] | |
| 799 | + # Called for each trade 4 times | |
| 800 | + assert msg_mock.call_count == 12 | |
| 801 | + msg = msg_mock.call_args_list[2][0][0] | |
| 800 | 802 | assert { |
| 801 | - 'type': RPCMessageType.SELL_NOTIFICATION, | |
| 803 | + 'type': RPCMessageType.SELL, | |
| 802 | 804 | 'trade_id': 1, |
| 803 | 805 | 'exchange': 'Binance', |
| 804 | 806 | 'pair': 'ETH/BTC', |
| @@ -815,6 +817,7 @@ | ||
| 815 | 817 | 'sell_reason': SellType.FORCE_SELL.value, |
| 816 | 818 | 'open_date': ANY, |
| 817 | 819 | 'close_date': ANY, |
| 820 | + 'close_rate': ANY, | |
| 818 | 821 | } == msg |
| 819 | 822 | |
| 820 | 823 | |
| @@ -1195,7 +1198,7 @@ | ||
| 1195 | 1198 | def test_send_msg_buy_notification(default_conf, mocker, caplog) -> None: |
| 1196 | 1199 | |
| 1197 | 1200 | msg = { |
| 1198 | - 'type': RPCMessageType.BUY_NOTIFICATION, | |
| 1201 | + 'type': RPCMessageType.BUY, | |
| 1199 | 1202 | 'trade_id': 1, |
| 1200 | 1203 | 'exchange': 'Binance', |
| 1201 | 1204 | 'pair': 'ETH/BTC', |
| @@ -1240,7 +1243,7 @@ | ||
| 1240 | 1243 | telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf) |
| 1241 | 1244 | |
| 1242 | 1245 | telegram.send_msg({ |
| 1243 | - 'type': RPCMessageType.BUY_CANCEL_NOTIFICATION, | |
| 1246 | + 'type': RPCMessageType.BUY_CANCEL, | |
| 1244 | 1247 | 'trade_id': 1, |
| 1245 | 1248 | 'exchange': 'Binance', |
| 1246 | 1249 | 'pair': 'ETH/BTC', |
| @@ -1251,6 +1254,25 @@ | ||
| 1251 | 1254 | 'Reason: cancelled due to timeout.') |
| 1252 | 1255 | |
| 1253 | 1256 | |
| 1257 | +def test_send_msg_buy_fill_notification(default_conf, mocker) -> None: | |
| 1258 | + | |
| 1259 | + default_conf['telegram']['notification_settings']['buy_fill'] = 'on' | |
| 1260 | + telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf) | |
| 1261 | + | |
| 1262 | + telegram.send_msg({ | |
| 1263 | + 'type': RPCMessageType.BUY_FILL, | |
| 1264 | + 'trade_id': 1, | |
| 1265 | + 'exchange': 'Binance', | |
| 1266 | + 'pair': 'ETH/USDT', | |
| 1267 | + 'open_rate': 200, | |
| 1268 | + 'stake_amount': 100, | |
| 1269 | + 'amount': 0.5, | |
| 1270 | + 'open_date': arrow.utcnow().datetime | |
| 1271 | + }) | |
| 1272 | + assert (msg_mock.call_args[0][0] == '\N{LARGE CIRCLE} *Binance:* ' | |
| 1273 | + 'Buy order for ETH/USDT (#1) filled for 200.') | |
| 1274 | + | |
| 1275 | + | |
| 1254 | 1276 | def test_send_msg_sell_notification(default_conf, mocker) -> None: |
| 1255 | 1277 | |
| 1256 | 1278 | telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf) |
| @@ -1258,7 +1280,7 @@ | ||
| 1258 | 1280 | old_convamount = telegram._rpc._fiat_converter.convert_amount |
| 1259 | 1281 | telegram._rpc._fiat_converter.convert_amount = lambda a, b, c: -24.812 |
| 1260 | 1282 | telegram.send_msg({ |
| 1261 | - 'type': RPCMessageType.SELL_NOTIFICATION, | |
| 1283 | + 'type': RPCMessageType.SELL, | |
| 1262 | 1284 | 'trade_id': 1, |
| 1263 | 1285 | 'exchange': 'Binance', |
| 1264 | 1286 | 'pair': 'KEY/ETH', |
| @@ -1288,7 +1310,7 @@ | ||
| 1288 | 1310 | |
| 1289 | 1311 | msg_mock.reset_mock() |
| 1290 | 1312 | telegram.send_msg({ |
| 1291 | - 'type': RPCMessageType.SELL_NOTIFICATION, | |
| 1313 | + 'type': RPCMessageType.SELL, | |
| 1292 | 1314 | 'trade_id': 1, |
| 1293 | 1315 | 'exchange': 'Binance', |
| 1294 | 1316 | 'pair': 'KEY/ETH', |
| @@ -1325,27 +1347,27 @@ | ||
| 1325 | 1347 | old_convamount = telegram._rpc._fiat_converter.convert_amount |
| 1326 | 1348 | telegram._rpc._fiat_converter.convert_amount = lambda a, b, c: -24.812 |
| 1327 | 1349 | telegram.send_msg({ |
| 1328 | - 'type': RPCMessageType.SELL_CANCEL_NOTIFICATION, | |
| 1350 | + 'type': RPCMessageType.SELL_CANCEL, | |
| 1329 | 1351 | 'trade_id': 1, |
| 1330 | 1352 | 'exchange': 'Binance', |
| 1331 | 1353 | 'pair': 'KEY/ETH', |
| 1332 | 1354 | 'reason': 'Cancelled on exchange' |
| 1333 | 1355 | }) |
| 1334 | 1356 | assert msg_mock.call_args[0][0] \ |
| 1335 | - == ('\N{WARNING SIGN} *Binance:* Cancelling Open Sell Order for KEY/ETH (#1).' | |
| 1336 | - ' Reason: Cancelled on exchange') | |
| 1357 | + == ('\N{WARNING SIGN} *Binance:* Cancelling open sell Order for KEY/ETH (#1).' | |
| 1358 | + ' Reason: Cancelled on exchange.') | |
| 1337 | 1359 | |
| 1338 | 1360 | msg_mock.reset_mock() |
| 1339 | 1361 | telegram.send_msg({ |
| 1340 | - 'type': RPCMessageType.SELL_CANCEL_NOTIFICATION, | |
| 1362 | + 'type': RPCMessageType.SELL_CANCEL, | |
| 1341 | 1363 | 'trade_id': 1, |
| 1342 | 1364 | 'exchange': 'Binance', |
| 1343 | 1365 | 'pair': 'KEY/ETH', |
| 1344 | 1366 | 'reason': 'timeout' |
| 1345 | 1367 | }) |
| 1346 | 1368 | assert msg_mock.call_args[0][0] \ |
| 1347 | - == ('\N{WARNING SIGN} *Binance:* Cancelling Open Sell Order for KEY/ETH (#1).' | |
| 1348 | - ' Reason: timeout') | |
| 1369 | + == ('\N{WARNING SIGN} *Binance:* Cancelling open sell Order for KEY/ETH (#1).' | |
| 1370 | + ' Reason: timeout.') | |
| 1349 | 1371 | # Reset singleton function to avoid random breaks |
| 1350 | 1372 | telegram._rpc._fiat_converter.convert_amount = old_convamount |
| 1351 | 1373 | |
| @@ -1354,7 +1376,7 @@ | ||
| 1354 | 1376 | |
| 1355 | 1377 | telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf) |
| 1356 | 1378 | telegram.send_msg({ |
| 1357 | - 'type': RPCMessageType.STATUS_NOTIFICATION, | |
| 1379 | + 'type': RPCMessageType.STATUS, | |
| 1358 | 1380 | 'status': 'running' |
| 1359 | 1381 | }) |
| 1360 | 1382 | assert msg_mock.call_args[0][0] == '*Status:* `running`' |
| @@ -1363,7 +1385,7 @@ | ||
| 1363 | 1385 | def test_warning_notification(default_conf, mocker) -> None: |
| 1364 | 1386 | telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf) |
| 1365 | 1387 | telegram.send_msg({ |
| 1366 | - 'type': RPCMessageType.WARNING_NOTIFICATION, | |
| 1388 | + 'type': RPCMessageType.WARNING, | |
| 1367 | 1389 | 'status': 'message' |
| 1368 | 1390 | }) |
| 1369 | 1391 | assert msg_mock.call_args[0][0] == '\N{WARNING SIGN} *Warning:* `message`' |
| @@ -1372,7 +1394,7 @@ | ||
| 1372 | 1394 | def test_startup_notification(default_conf, mocker) -> None: |
| 1373 | 1395 | telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf) |
| 1374 | 1396 | telegram.send_msg({ |
| 1375 | - 'type': RPCMessageType.STARTUP_NOTIFICATION, | |
| 1397 | + 'type': RPCMessageType.STARTUP, | |
| 1376 | 1398 | 'status': '*Custom:* `Hello World`' |
| 1377 | 1399 | }) |
| 1378 | 1400 | assert msg_mock.call_args[0][0] == '*Custom:* `Hello World`' |
| @@ -1391,7 +1413,7 @@ | ||
| 1391 | 1413 | telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf) |
| 1392 | 1414 | |
| 1393 | 1415 | telegram.send_msg({ |
| 1394 | - 'type': RPCMessageType.BUY_NOTIFICATION, | |
| 1416 | + 'type': RPCMessageType.BUY, | |
| 1395 | 1417 | 'trade_id': 1, |
| 1396 | 1418 | 'exchange': 'Binance', |
| 1397 | 1419 | 'pair': 'ETH/BTC', |
| @@ -1417,7 +1439,7 @@ | ||
| 1417 | 1439 | telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf) |
| 1418 | 1440 | |
| 1419 | 1441 | telegram.send_msg({ |
| 1420 | - 'type': RPCMessageType.SELL_NOTIFICATION, | |
| 1442 | + 'type': RPCMessageType.SELL, | |
| 1421 | 1443 | 'trade_id': 1, |
| 1422 | 1444 | 'exchange': 'Binance', |
| 1423 | 1445 | 'pair': 'KEY/ETH', |
modified tests/rpc/test_rpc_webhook.py +72 −17
| @@ -25,6 +25,11 @@ | ||
| 25 | 25 | "value2": "limit {limit:8f}", |
| 26 | 26 | "value3": "{stake_amount:8f} {stake_currency}" |
| 27 | 27 | }, |
| 28 | + "webhookbuyfill": { | |
| 29 | + "value1": "Buy Order for {pair} filled", | |
| 30 | + "value2": "at {open_rate:8f}", | |
| 31 | + "value3": "{stake_amount:8f} {stake_currency}" | |
| 32 | + }, | |
| 28 | 33 | "webhooksell": { |
| 29 | 34 | "value1": "Selling {pair}", |
| 30 | 35 | "value2": "limit {limit:8f}", |
| @@ -35,6 +40,11 @@ | ||
| 35 | 40 | "value2": "limit {limit:8f}", |
| 36 | 41 | "value3": "profit: {profit_amount:8f} {stake_currency} ({profit_ratio})" |
| 37 | 42 | }, |
| 43 | + "webhooksellfill": { | |
| 44 | + "value1": "Sell Order for {pair} filled", | |
| 45 | + "value2": "at {close_rate:8f}", | |
| 46 | + "value3": "" | |
| 47 | + }, | |
| 38 | 48 | "webhookstatus": { |
| 39 | 49 | "value1": "Status: {status}", |
| 40 | 50 | "value2": "", |
| @@ -49,7 +59,7 @@ | ||
| 49 | 59 | assert webhook._config == default_conf |
| 50 | 60 | |
| 51 | 61 | |
| 52 | -def test_send_msg(default_conf, mocker): | |
| 62 | +def test_send_msg_webhook(default_conf, mocker): | |
| 53 | 63 | default_conf["webhook"] = get_webhook_dict() |
| 54 | 64 | msg_mock = MagicMock() |
| 55 | 65 | mocker.patch("freqtrade.rpc.webhook.Webhook._send_msg", msg_mock) |
| @@ -58,7 +68,7 @@ | ||
| 58 | 68 | msg_mock = MagicMock() |
| 59 | 69 | mocker.patch("freqtrade.rpc.webhook.Webhook._send_msg", msg_mock) |
| 60 | 70 | msg = { |
| 61 | - 'type': RPCMessageType.BUY_NOTIFICATION, | |
| 71 | + 'type': RPCMessageType.BUY, | |
| 62 | 72 | 'exchange': 'Binance', |
| 63 | 73 | 'pair': 'ETH/BTC', |
| 64 | 74 | 'limit': 0.005, |
| @@ -76,10 +86,10 @@ | ||
| 76 | 86 | assert (msg_mock.call_args[0][0]["value3"] == |
| 77 | 87 | default_conf["webhook"]["webhookbuy"]["value3"].format(**msg)) |
| 78 | 88 | # Test buy cancel |
| 79 | - msg_mock = MagicMock() | |
| 80 | - mocker.patch("freqtrade.rpc.webhook.Webhook._send_msg", msg_mock) | |
| 89 | + msg_mock.reset_mock() | |
| 90 | + | |
| 81 | 91 | msg = { |
| 82 | - 'type': RPCMessageType.BUY_CANCEL_NOTIFICATION, | |
| 92 | + 'type': RPCMessageType.BUY_CANCEL, | |
| 83 | 93 | 'exchange': 'Binance', |
| 84 | 94 | 'pair': 'ETH/BTC', |
| 85 | 95 | 'limit': 0.005, |
| @@ -96,11 +106,31 @@ | ||
| 96 | 106 | default_conf["webhook"]["webhookbuycancel"]["value2"].format(**msg)) |
| 97 | 107 | assert (msg_mock.call_args[0][0]["value3"] == |
| 98 | 108 | default_conf["webhook"]["webhookbuycancel"]["value3"].format(**msg)) |
| 109 | + # Test buy fill | |
| 110 | + msg_mock.reset_mock() | |
| 111 | + | |
| 112 | + msg = { | |
| 113 | + 'type': RPCMessageType.BUY_FILL, | |
| 114 | + 'exchange': 'Binance', | |
| 115 | + 'pair': 'ETH/BTC', | |
| 116 | + 'open_rate': 0.005, | |
| 117 | + 'stake_amount': 0.8, | |
| 118 | + 'stake_amount_fiat': 500, | |
| 119 | + 'stake_currency': 'BTC', | |
| 120 | + 'fiat_currency': 'EUR' | |
| 121 | + } | |
| 122 | + webhook.send_msg(msg=msg) | |
| 123 | + assert msg_mock.call_count == 1 | |
| 124 | + assert (msg_mock.call_args[0][0]["value1"] == | |
| 125 | + default_conf["webhook"]["webhookbuyfill"]["value1"].format(**msg)) | |
| 126 | + assert (msg_mock.call_args[0][0]["value2"] == | |
| 127 | + default_conf["webhook"]["webhookbuyfill"]["value2"].format(**msg)) | |
| 128 | + assert (msg_mock.call_args[0][0]["value3"] == | |
| 129 | + default_conf["webhook"]["webhookbuyfill"]["value3"].format(**msg)) | |
| 99 | 130 | # Test sell |
| 100 | - msg_mock = MagicMock() | |
| 101 | - mocker.patch("freqtrade.rpc.webhook.Webhook._send_msg", msg_mock) | |
| 131 | + msg_mock.reset_mock() | |
| 102 | 132 | msg = { |
| 103 | - 'type': RPCMessageType.SELL_NOTIFICATION, | |
| 133 | + 'type': RPCMessageType.SELL, | |
| 104 | 134 | 'exchange': 'Binance', |
| 105 | 135 | 'pair': 'ETH/BTC', |
| 106 | 136 | 'gain': "profit", |
| @@ -123,10 +153,9 @@ | ||
| 123 | 153 | assert (msg_mock.call_args[0][0]["value3"] == |
| 124 | 154 | default_conf["webhook"]["webhooksell"]["value3"].format(**msg)) |
| 125 | 155 | # Test sell cancel |
| 126 | - msg_mock = MagicMock() | |
| 127 | - mocker.patch("freqtrade.rpc.webhook.Webhook._send_msg", msg_mock) | |
| 156 | + msg_mock.reset_mock() | |
| 128 | 157 | msg = { |
| 129 | - 'type': RPCMessageType.SELL_CANCEL_NOTIFICATION, | |
| 158 | + 'type': RPCMessageType.SELL_CANCEL, | |
| 130 | 159 | 'exchange': 'Binance', |
| 131 | 160 | 'pair': 'ETH/BTC', |
| 132 | 161 | 'gain': "profit", |
| @@ -148,9 +177,35 @@ | ||
| 148 | 177 | default_conf["webhook"]["webhooksellcancel"]["value2"].format(**msg)) |
| 149 | 178 | assert (msg_mock.call_args[0][0]["value3"] == |
| 150 | 179 | default_conf["webhook"]["webhooksellcancel"]["value3"].format(**msg)) |
| 151 | - for msgtype in [RPCMessageType.STATUS_NOTIFICATION, | |
| 152 | - RPCMessageType.WARNING_NOTIFICATION, | |
| 153 | - RPCMessageType.STARTUP_NOTIFICATION]: | |
| 180 | + # Test Sell fill | |
| 181 | + msg_mock.reset_mock() | |
| 182 | + msg = { | |
| 183 | + 'type': RPCMessageType.SELL_FILL, | |
| 184 | + 'exchange': 'Binance', | |
| 185 | + 'pair': 'ETH/BTC', | |
| 186 | + 'gain': "profit", | |
| 187 | + 'close_rate': 0.005, | |
| 188 | + 'amount': 0.8, | |
| 189 | + 'order_type': 'limit', | |
| 190 | + 'open_rate': 0.004, | |
| 191 | + 'current_rate': 0.005, | |
| 192 | + 'profit_amount': 0.001, | |
| 193 | + 'profit_ratio': 0.20, | |
| 194 | + 'stake_currency': 'BTC', | |
| 195 | + 'sell_reason': SellType.STOP_LOSS.value | |
| 196 | + } | |
| 197 | + webhook.send_msg(msg=msg) | |
| 198 | + assert msg_mock.call_count == 1 | |
| 199 | + assert (msg_mock.call_args[0][0]["value1"] == | |
| 200 | + default_conf["webhook"]["webhooksellfill"]["value1"].format(**msg)) | |
| 201 | + assert (msg_mock.call_args[0][0]["value2"] == | |
| 202 | + default_conf["webhook"]["webhooksellfill"]["value2"].format(**msg)) | |
| 203 | + assert (msg_mock.call_args[0][0]["value3"] == | |
| 204 | + default_conf["webhook"]["webhooksellfill"]["value3"].format(**msg)) | |
| 205 | + | |
| 206 | + for msgtype in [RPCMessageType.STATUS, | |
| 207 | + RPCMessageType.WARNING, | |
| 208 | + RPCMessageType.STARTUP]: | |
| 154 | 209 | # Test notification |
| 155 | 210 | msg = { |
| 156 | 211 | 'type': msgtype, |
| @@ -173,8 +228,8 @@ | ||
| 173 | 228 | del default_conf["webhook"]["webhookbuy"] |
| 174 | 229 | |
| 175 | 230 | webhook = Webhook(RPC(get_patched_freqtradebot(mocker, default_conf)), default_conf) |
| 176 | - webhook.send_msg({'type': RPCMessageType.BUY_NOTIFICATION}) | |
| 177 | - assert log_has(f"Message type '{RPCMessageType.BUY_NOTIFICATION}' not configured for webhooks", | |
| 231 | + webhook.send_msg({'type': RPCMessageType.BUY}) | |
| 232 | + assert log_has(f"Message type '{RPCMessageType.BUY}' not configured for webhooks", | |
| 178 | 233 | caplog) |
| 179 | 234 | |
| 180 | 235 | default_conf["webhook"] = get_webhook_dict() |
| @@ -183,7 +238,7 @@ | ||
| 183 | 238 | mocker.patch("freqtrade.rpc.webhook.Webhook._send_msg", msg_mock) |
| 184 | 239 | webhook = Webhook(RPC(get_patched_freqtradebot(mocker, default_conf)), default_conf) |
| 185 | 240 | msg = { |
| 186 | - 'type': RPCMessageType.BUY_NOTIFICATION, | |
| 241 | + 'type': RPCMessageType.BUY, | |
| 187 | 242 | 'exchange': 'Binance', |
| 188 | 243 | 'pair': 'ETH/BTC', |
| 189 | 244 | 'limit': 0.005, |
modified tests/test_freqtradebot.py +18 −11
| @@ -1710,6 +1710,7 @@ | ||
| 1710 | 1710 | open_rate=0.01, |
| 1711 | 1711 | open_date=arrow.utcnow().datetime, |
| 1712 | 1712 | amount=11, |
| 1713 | + exchange="binance", | |
| 1713 | 1714 | ) |
| 1714 | 1715 | assert not freqtrade.update_trade_state(trade, None) |
| 1715 | 1716 | assert log_has_re(r'Orderid for trade .* is empty.', caplog) |
| @@ -2319,7 +2320,7 @@ | ||
| 2319 | 2320 | # note this is for a partially-complete buy order |
| 2320 | 2321 | freqtrade.check_handle_timedout() |
| 2321 | 2322 | assert cancel_order_mock.call_count == 1 |
| 2322 | - assert rpc_mock.call_count == 1 | |
| 2323 | + assert rpc_mock.call_count == 2 | |
| 2323 | 2324 | trades = Trade.query.filter(Trade.open_order_id.is_(open_trade.open_order_id)).all() |
| 2324 | 2325 | assert len(trades) == 1 |
| 2325 | 2326 | assert trades[0].amount == 23.0 |
| @@ -2354,7 +2355,7 @@ | ||
| 2354 | 2355 | assert log_has_re(r"Applying fee on amount for Trade.*", caplog) |
| 2355 | 2356 | |
| 2356 | 2357 | assert cancel_order_mock.call_count == 1 |
| 2357 | - assert rpc_mock.call_count == 1 | |
| 2358 | + assert rpc_mock.call_count == 2 | |
| 2358 | 2359 | trades = Trade.query.filter(Trade.open_order_id.is_(open_trade.open_order_id)).all() |
| 2359 | 2360 | assert len(trades) == 1 |
| 2360 | 2361 | # Verify that trade has been updated |
| @@ -2394,7 +2395,7 @@ | ||
| 2394 | 2395 | assert log_has_re(r"Could not update trade amount: .*", caplog) |
| 2395 | 2396 | |
| 2396 | 2397 | assert cancel_order_mock.call_count == 1 |
| 2397 | - assert rpc_mock.call_count == 1 | |
| 2398 | + assert rpc_mock.call_count == 2 | |
| 2398 | 2399 | trades = Trade.query.filter(Trade.open_order_id.is_(open_trade.open_order_id)).all() |
| 2399 | 2400 | assert len(trades) == 1 |
| 2400 | 2401 | # Verify that trade has been updated |
| @@ -2623,7 +2624,7 @@ | ||
| 2623 | 2624 | last_msg = rpc_mock.call_args_list[-1][0][0] |
| 2624 | 2625 | assert { |
| 2625 | 2626 | 'trade_id': 1, |
| 2626 | - 'type': RPCMessageType.SELL_NOTIFICATION, | |
| 2627 | + 'type': RPCMessageType.SELL, | |
| 2627 | 2628 | 'exchange': 'Binance', |
| 2628 | 2629 | 'pair': 'ETH/BTC', |
| 2629 | 2630 | 'gain': 'profit', |
| @@ -2639,6 +2640,7 @@ | ||
| 2639 | 2640 | 'sell_reason': SellType.ROI.value, |
| 2640 | 2641 | 'open_date': ANY, |
| 2641 | 2642 | 'close_date': ANY, |
| 2643 | + 'close_rate': ANY, | |
| 2642 | 2644 | } == last_msg |
| 2643 | 2645 | |
| 2644 | 2646 | |
| @@ -2672,7 +2674,7 @@ | ||
| 2672 | 2674 | assert rpc_mock.call_count == 2 |
| 2673 | 2675 | last_msg = rpc_mock.call_args_list[-1][0][0] |
| 2674 | 2676 | assert { |
| 2675 | - 'type': RPCMessageType.SELL_NOTIFICATION, | |
| 2677 | + 'type': RPCMessageType.SELL, | |
| 2676 | 2678 | 'trade_id': 1, |
| 2677 | 2679 | 'exchange': 'Binance', |
| 2678 | 2680 | 'pair': 'ETH/BTC', |
| @@ -2689,6 +2691,7 @@ | ||
| 2689 | 2691 | 'sell_reason': SellType.STOP_LOSS.value, |
| 2690 | 2692 | 'open_date': ANY, |
| 2691 | 2693 | 'close_date': ANY, |
| 2694 | + 'close_rate': ANY, | |
| 2692 | 2695 | } == last_msg |
| 2693 | 2696 | |
| 2694 | 2697 | |
| @@ -2729,7 +2732,7 @@ | ||
| 2729 | 2732 | last_msg = rpc_mock.call_args_list[-1][0][0] |
| 2730 | 2733 | |
| 2731 | 2734 | assert { |
| 2732 | - 'type': RPCMessageType.SELL_NOTIFICATION, | |
| 2735 | + 'type': RPCMessageType.SELL, | |
| 2733 | 2736 | 'trade_id': 1, |
| 2734 | 2737 | 'exchange': 'Binance', |
| 2735 | 2738 | 'pair': 'ETH/BTC', |
| @@ -2746,7 +2749,7 @@ | ||
| 2746 | 2749 | 'sell_reason': SellType.STOP_LOSS.value, |
| 2747 | 2750 | 'open_date': ANY, |
| 2748 | 2751 | 'close_date': ANY, |
| 2749 | - | |
| 2752 | + 'close_rate': ANY, | |
| 2750 | 2753 | } == last_msg |
| 2751 | 2754 | |
| 2752 | 2755 | |
| @@ -2830,7 +2833,7 @@ | ||
| 2830 | 2833 | trade = Trade.query.first() |
| 2831 | 2834 | assert trade |
| 2832 | 2835 | assert cancel_order.call_count == 1 |
| 2833 | - assert rpc_mock.call_count == 2 | |
| 2836 | + assert rpc_mock.call_count == 3 | |
| 2834 | 2837 | |
| 2835 | 2838 | |
| 2836 | 2839 | def test_may_execute_sell_after_stoploss_on_exchange_hit(default_conf, ticker, fee, |
| @@ -2898,7 +2901,10 @@ | ||
| 2898 | 2901 | assert trade.stoploss_order_id is None |
| 2899 | 2902 | assert trade.is_open is False |
| 2900 | 2903 | assert trade.sell_reason == SellType.STOPLOSS_ON_EXCHANGE.value |
| 2901 | - assert rpc_mock.call_count == 2 | |
| 2904 | + assert rpc_mock.call_count == 3 | |
| 2905 | + assert rpc_mock.call_args_list[0][0][0]['type'] == RPCMessageType.BUY | |
| 2906 | + assert rpc_mock.call_args_list[1][0][0]['type'] == RPCMessageType.BUY_FILL | |
| 2907 | + assert rpc_mock.call_args_list[2][0][0]['type'] == RPCMessageType.SELL | |
| 2902 | 2908 | |
| 2903 | 2909 | |
| 2904 | 2910 | def test_execute_sell_market_order(default_conf, ticker, fee, |
| @@ -2932,10 +2938,10 @@ | ||
| 2932 | 2938 | assert not trade.is_open |
| 2933 | 2939 | assert trade.close_profit == 0.0620716 |
| 2934 | 2940 | |
| 2935 | - assert rpc_mock.call_count == 2 | |
| 2941 | + assert rpc_mock.call_count == 3 | |
| 2936 | 2942 | last_msg = rpc_mock.call_args_list[-1][0][0] |
| 2937 | 2943 | assert { |
| 2938 | - 'type': RPCMessageType.SELL_NOTIFICATION, | |
| 2944 | + 'type': RPCMessageType.SELL, | |
| 2939 | 2945 | 'trade_id': 1, |
| 2940 | 2946 | 'exchange': 'Binance', |
| 2941 | 2947 | 'pair': 'ETH/BTC', |
| @@ -2952,6 +2958,7 @@ | ||
| 2952 | 2958 | 'sell_reason': SellType.ROI.value, |
| 2953 | 2959 | 'open_date': ANY, |
| 2954 | 2960 | 'close_date': ANY, |
| 2961 | + 'close_rate': ANY, | |
| 2955 | 2962 | |
| 2956 | 2963 | } == last_msg |
| 2957 | 2964 |