def split_and_send(self, dst, msg): MAX_MSG_SIZE = 2000 - 6 - 25 rows = msg.split('\n') i = 0 while i < len(rows): buf = "" while (i < len(rows)) and (len(buf) + len(rows[i]) + 1 < MAX_MSG_SIZE): buf += rows[i] + '\n' i += 1 if buf: send_to_discord(dst, 'SiiS', '```' + buf + '```')
def update(self): count = 0 while self._signals: signal = self._signals.popleft() label = "" message = "" icon = "contact-new" now = time.time() audio_alert = None if signal.signal_type == Signal.SIGNAL_SOCIAL_ENTER: # here we only assume that because of what 1broker return to us but should be timestamp in the model entry_date = signal.data.entry_date + timedelta(hours=2) position_timestamp = time.mktime(entry_date.timetuple()) audio_alert = DesktopNotifier.AUDIO_ALERT_SIMPLE if now - position_timestamp > 120 * 60: continue label = "Entry position on %s" % (signal.data.symbol, ) message = "Trader %s enter %s on %s at %s (x%s)" % ( signal.data.author.name if signal.data.author is not None else "???", "long" if signal.data.direction == Position.LONG else "short", signal.data.symbol, signal.data.entry_price, signal.data.leverage) elif signal.signal_type == Signal.SIGNAL_SOCIAL_EXIT: # here we only assume that because of what 1broker return to us but should be timestamp in the model exit_date = signal.data.exit_date + timedelta(hours=2) position_timestamp = time.mktime(exit_date.timetuple()) audio_alert = DesktopNotifier.AUDIO_ALERT_SIMPLE if now - position_timestamp > 120 * 60: continue label = "Exit position on %s" % (signal.data.symbol, ) message = "Trader %s exit %s on %s at %s" % ( signal.data.author.name, "long" if signal.data.direction == Position.LONG else "short", signal.data.symbol, signal.data.exit_price) # # @todo a threshold... or a timelimit # elif signal.signal_type == Signal.SIGNAL_TRADE_ALERT: # icon = "go-down" # label = "Position loss on %s" % (signal.data.symbol,) # audio_alert = DesktopNotifier.AUDIO_ALERT_WARNING # message = "Position %s %s of %s on %s start at %s %s is in regretable loss %s (%s%%) :$" % ( # signal.data.position_id, # "long" if signal.data.direction == Position.LONG else "short", # signal.data.author.name if signal.data.author is not None else "???", # signal.data.trader.name, # signal.data.entry_price, # signal.data.symbol, # signal.data.profit_loss, # signal.data.profit_loss_rate * 100.0) # elif signal.signal_type == Signal.SIGNAL_TRADE_ENJOY: # icon = "go-up" # label = "Position profit on %s" % (signal.data.symbol,) # audio_alert = DesktopNotifier.AUDIO_ALERT_SIMPLE # message = "Position %s %s of %s on %s start at %s %s is in enjoyable profit %s (%s%%) :)" % ( # signal.data.position_id, # "long" if signal.data.direction == Position.LONG else "short", # signal.data.author.name if signal.data.author is not None else "???", # signal.data.trader.name, # signal.data.entry_price, # signal.data.symbol, # signal.data.profit_loss, # signal.data.profit_loss_rate * 100.0) elif signal.signal_type == Signal.SIGNAL_STRATEGY_ENTRY_EXIT: # @todo in addition of entry/exit, modification and a reason of the exit/modification icon = "contact-new" direction = "long" if signal.data[ 'direction'] == Position.LONG else "short" audio_alert = DesktopNotifier.AUDIO_ALERT_SIMPLE if signal.data['action'] == 'stop': audio_alert = DesktopNotifier.AUDIO_ALERT_WARNING ldatetime = datetime.fromtimestamp( signal.data['timestamp']).strftime('%Y-%m-%d %H:%M:%S') label = "Signal %s %s on %s" % ( signal.data['action'], direction, signal.data['symbol'], ) message = "%s@%s (%s) %s %s at %s - #%s in %s" % ( signal.data['symbol'], signal.data['price'], signal.data['trader-name'], signal.data['action'], direction, ldatetime, signal.data['trade-id'], timeframe_to_str(signal.data['timeframe'])) if signal.data['stop-loss']: message += " SL@%s" % (signal.data['stop-loss'], ) if signal.data['take-profit']: message += " TP@%s" % (signal.data['take-profit'], ) if signal.data['rate'] is not None: message += " (%.2f%%)" % ((signal.data['rate'] * 100), ) if self.discord: if signal.data['identifier'] in self._discord_webhook: send_to_discord( self._discord_webhook[signal.data['identifier']], 'CryptoBot', '```' + message + '```') else: send_to_discord(self._discord_webhook['signals'], 'CryptoBot', '```' + message + '```') # log them to the signal view Terminal.inst().notice(message, view="signal") # process sound if not self.backtesting and self.audible and audio_alert: # if repeat for i in range(0, self._alerts[audio_alert[1]]): subprocess.Popen([ 'aplay', '-D', self._audio_device, self._alerts[audio_alert[0]] ], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) # duration = 1 # second # freq = 440 # Hz # os.system('play --no-show-progress --null --channels 1 synth %s sine %f' % (duration, freq)) if not self.backtesting and self.popups and message: n = notify2.Notification(label, message, icon) n.show() count += 1 if count > 10: # no more than per loop break