首页 > 解决方案 > 如何使用python在while循环中仅发送一次电报机器人消息

问题描述

我已经使用 binance api 我的机器人实现了一个机器人。但是当算法工作时我想开发一些东西机器人向我的电报机器人发送多条消息有没有办法在代码工作时只发送一次消息当代码执行时它有时会发送数百条消息但我只想要一次或几条消息次如果有什么办法可以解决它请告诉我谢谢

        from binance.client import Client
    import talib as ta
    import numpy as np
    import matplotlib.pyplot as plt
    from datetime import datetime
    import sys
    import math
    
    from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, updater
    token = "TOKENNNNNNNNNNNN"
    CHAT_ID= "CHAT IDDDDDDD"
    updater = Updater(token, use_context=True)
    updater.start_polling()
    
    class BinanceConnection:
        def __init__(self, file):
            self.connect(file)
    
        """ Creates Binance client """
    
        def connect(self, file):
            lines = [line.rstrip('\n') for line in open(file)]
            key = lines[0]
            secret = lines[1]
            self.client = Client(key, secret)
    
    def generateTillsonT3(close_array, high_array, low_array, volume_factor, t3Length):
    
        ema_first_input = (high_array + low_array + 2 * close_array) / 4
    
        e1 = ta.EMA(ema_first_input, t3Length)
    
        e2 = ta.EMA(e1, t3Length)
    
        e3 = ta.EMA(e2, t3Length)
    
        e4 = ta.EMA(e3, t3Length)
    
        e5 = ta.EMA(e4, t3Length)
    
        e6 = ta.EMA(e5, t3Length)
    
        c1 = -1 * volume_factor * volume_factor * volume_factor
    
        c2 = 3 * volume_factor * volume_factor + 3 * volume_factor * volume_factor * volume_factor
    
        c3 = -6 * volume_factor * volume_factor - 3 * volume_factor - 3 * volume_factor * volume_factor * volume_factor
    
        c4 = 1 + 3 * volume_factor + volume_factor * volume_factor * volume_factor + 3 * volume_factor * volume_factor
    
        T3 = c1 * e6 + c2 * e5 + c3 * e4 + c4 * e3
    
        return T3
    
    if __name__ == '__main__':
        filename = 'credentials.txt'
        connection = BinanceConnection(filename)
        interval = '1d'
        limit = 500
    
        coinlist = ['XRPUSDT', 'ETHUSDT', 'BTCUSDT', 'DOGEUSDT', 'XLMUSDT', 'LTCUSDT', 'BNBUSDT', 'BTTUSDT']
    while True:
        for x in coinlist:
            pair = x
            klines = connection.client.get_klines(symbol=pair, interval=interval, limit=limit)
    
            open_time = [int(entry[0]) for entry in klines]
    
            open = [float(entry[1]) for entry in klines]
            high = [float(entry[2]) for entry in klines]
            low = [float(entry[3]) for entry in klines]
            close = [float(entry[4]) for entry in klines]
    
            close_array = np.asarray(close)
            high_array = np.asarray(high)
            low_array = np.asarray(low)
    
            new_time = [datetime.fromtimestamp(time / 1000) for time in open_time]
    
            new_time_x = [date.strftime("%y-%m-%d") for date in new_time]
    
            volume_factor = 0.7
    
            t3length = 8
            tillsont3 = generateTillsonT3(close_array, high_array, low_array, volume_factor=volume_factor, t3Length=t3length)
    
            ##plt.figure(figsize=(11, 6))
            #plt.plot(new_time_x[400:], close_array[400:], label='Price')
            #plt.plot(new_time_x[400:], tillsont3[400:], label='Tillson T3')
            #plt.xticks(rotation=90, fontsize=5)
            #plt.title("Tillson T3 Plot for BTC/USDT")
            #plt.xlabel("Open Time")
            #plt.ylabel("Value")
            #plt.legend()
            #plt.show()
    
            t3_last = tillsont3[-1]
            t3_previous = tillsont3[-2]
            t3_prev_previous = tillsont3[-3]
    
            print("Price For ", pair, "Last:", t3_last, "Previous:", t3_previous, "Prev Prevous:", t3_prev_previous)
    
    
            # kırmızıdan yeşile dönüyor
            if t3_last > t3_previous and t3_previous < t3_prev_previous:
                print('TILSOn t3 buy signal, from red to green')
                print(f'al sinyali {pair}')
                updater.dispatcher.bot.send_message(chat_id="CHAT_ID", text=f"TILSON T3 'BUY SIGNAL FOR {pair}'")
    
            # yeşilden kırmızıya dönüyor
            elif t3_last < t3_previous and t3_previous > t3_prev_previous:
                print('tillson t3 sell signal, from green to red')
                print(f'Sat sinyali {pair}')
                updater.dispatcher.bot.send_message(chat_id="CHAT_ID", text=f"TILSON T3 'SELL SIGNAL FOR {pair}'")
    
        #change the time interval
        #if __name__ == '__main__':
        #interval = '3m'
        #new_time_x = [date.strftime("%H:%M:%S") for date in new_time]

标签: python

解决方案


推荐阅读