69 lines
2.5 KiB
Python
69 lines
2.5 KiB
Python
|
||
from cryptoai.api.binance_api import BinanceAPI
|
||
from cryptoai.utils.config_loader import ConfigLoader
|
||
import logging
|
||
from datetime import datetime, timedelta
|
||
from cryptoai.models.data_processor import DataProcessor
|
||
from cryptoai.utils.discord_bot import DiscordBot
|
||
import time
|
||
class VolumeGrowupMonitor:
|
||
"""
|
||
成交量增长监控
|
||
"""
|
||
def __init__(self):
|
||
self.config_loader = ConfigLoader()
|
||
self.binance_config = self.config_loader.get_binance_config()
|
||
self.discord_config = self.config_loader.get_discord_config()
|
||
self.binance_api = BinanceAPI(
|
||
api_key=self.binance_config['api_key'],
|
||
api_secret=self.binance_config['api_secret'],
|
||
test_mode=self.binance_config['test_mode']
|
||
)
|
||
|
||
self.discord_bot = DiscordBot(
|
||
webhook_url=self.discord_config['volume_growup_webhook_url']
|
||
)
|
||
|
||
def run(self, time_interval: str = "5m"):
|
||
binance_symbols = self.binance_api.get_all_symbols()
|
||
|
||
# 计算开始时间
|
||
start_time = datetime.now() - timedelta(days=1) # 3天前
|
||
start_str = start_time.strftime("%Y-%m-%d")
|
||
|
||
# 获取所有symbol的klines 数据
|
||
for symbol in binance_symbols:
|
||
data = self.binance_api.get_historical_klines(symbol, time_interval, start_str)
|
||
|
||
data_processor = DataProcessor()
|
||
processed_data = data_processor.preprocess_market_data(symbol, data)
|
||
|
||
# 计算过去 30 根 K 线的平均交易量
|
||
average_volume = processed_data['volume'].tail(30).mean()
|
||
|
||
# 用上一根 k 线和过去 30 根 k 线的平均交易量计算增长率
|
||
volume_growth = processed_data['volume'].iloc[-2] / average_volume
|
||
|
||
print(f"{symbol} 过去 30 根 K 线的平均交易量为 {average_volume:.2f},当前交易量为 {processed_data['volume'].iloc[-1]:.2f},增长率为 {volume_growth:.2%}")
|
||
# 如果增加 5倍以上,则发送消息
|
||
if volume_growth >= 5:
|
||
# markdown 格式,带上emoji
|
||
message = f"""🚀交易量暴涨提醒🚀
|
||
|
||
监控交易对:*** {symbol} ***
|
||
时间周期:***{time_interval}***
|
||
30根K线平均交易量:***{average_volume:.2f}***
|
||
当前交易量:***{processed_data['volume'].iloc[-2]:.2f}***
|
||
增长率为 ***{volume_growth:.2%}***
|
||
|
||
"""
|
||
|
||
self.discord_bot.send_message(message)
|
||
print(f"发送交易量上涨提醒消息到discord")
|
||
|
||
time.sleep(1)
|
||
|
||
|
||
|
||
|