python - 如何使用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 - 如何在 GITHUB 中获取 xlsx 文件的链接以作为熊猫数据框打开?
- sql - 如何计算一周中每天工作的平均收入/小时?
- prolog - Prolog - 数组数组,返回数组长度大于一的条目
- reactjs - 在 TS 中动态导出 React 组件
- python - 在 venv 的 pip list 时如何解决此导入错误?
- angular-cli - 为什么在构建 Angular 应用程序(npm run build)时需要添加“运行”参数?
- javascript - 如何添加所需的反应选择?
- python-3.x - 在请求中实现身份验证发送到 LDAP 服务器以在 python3 中进行连接
- asp.net-core - 脚手架身份时“运行所选代码生成器时出错”
- r - 如何在 r 中重定向系统命令及其输出?