update
This commit is contained in:
parent
21af2978eb
commit
06c178f6e7
Binary file not shown.
@ -30,6 +30,24 @@ class BinanceAPI:
|
||||
except BinanceAPIException as e:
|
||||
print(f"Binance API连接失败: {e}")
|
||||
|
||||
def get_all_symbols(self) -> List[str]:
|
||||
"""
|
||||
获取所有合约交易对
|
||||
|
||||
Returns:
|
||||
List[str]: 所有合约交易对列表
|
||||
"""
|
||||
symbols = self.client.get_exchange_info()['symbols']
|
||||
|
||||
result = []
|
||||
# 选择USDT交易对以及 status 为 TRADING 的交易对
|
||||
for symbol in symbols:
|
||||
if symbol['quoteAsset'] == 'USDT' and symbol['status'] == 'TRADING':
|
||||
result.append(symbol['symbol'])
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def get_historical_klines(self, symbol: str, interval: str, start_str: str, end_str: Optional[str] = None) -> pd.DataFrame:
|
||||
"""
|
||||
获取历史K线数据
|
||||
@ -59,8 +77,8 @@ class BinanceAPI:
|
||||
])
|
||||
|
||||
# 转换数据类型
|
||||
df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms')
|
||||
df['close_time'] = pd.to_datetime(df['close_time'], unit='ms')
|
||||
df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms', utc=True).map(lambda x: x.tz_convert('Asia/Shanghai'))
|
||||
df['close_time'] = pd.to_datetime(df['close_time'], unit='ms', utc=True).map(lambda x: x.tz_convert('Asia/Shanghai'))
|
||||
|
||||
for col in ['open', 'high', 'low', 'close', 'volume', 'quote_asset_volume',
|
||||
'taker_buy_base_asset_volume', 'taker_buy_quote_asset_volume']:
|
||||
|
||||
@ -71,6 +71,7 @@ discord:
|
||||
enabled: true
|
||||
crypto_webhook_url: "https://discord.com/api/webhooks/1286585288986198048/iQ-yr26ViW45GM48ql8rPu70Iumqcmn_XXAmxAcKGeiQBmTQoDPeq-TmlChvIHkw_HJ-"
|
||||
gold_webhook_url: "https://discord.com/api/webhooks/1367341235987021914/XVcjs6ZAZad3ZezzuudyiK_KqNUowqz2o2NJPdvwWY_EvwZVJcnVHq5M0-RQhkKV-FEQ"
|
||||
volume_growup_webhook_url: "https://discord.com/api/webhooks/1368217200023961631/yl_zZK865YLNpq7F7ISwPa6ztXEjU8_V646XxL95RF7PIGEFoLCTa_dTiabkfUaUvme0"
|
||||
|
||||
|
||||
# 数据库配置
|
||||
|
||||
28
cryptoai/monitor_endpoint.py
Normal file
28
cryptoai/monitor_endpoint.py
Normal file
@ -0,0 +1,28 @@
|
||||
|
||||
from cryptoai.monitors.volume_growup import VolumeGrowupMonitor
|
||||
import schedule
|
||||
|
||||
class MonitorEndpoint:
|
||||
"""
|
||||
监控端点
|
||||
"""
|
||||
def __init__(self):
|
||||
self.volume_growup_monitor = VolumeGrowupMonitor()
|
||||
|
||||
def run(self):
|
||||
try:
|
||||
print("☕️ 加密货币监控程序已启动")
|
||||
times = [":00", ":05", ":10", ":15", ":20", ":25", ":30", ":35", ":40", ":45", ":50", ":55"]
|
||||
for time in times:
|
||||
schedule.every().hour.at(time).do(self.volume_growup_monitor.run, time_interval="5m")
|
||||
|
||||
while True:
|
||||
schedule.run_pending()
|
||||
import time
|
||||
time.sleep(1)
|
||||
except Exception as e:
|
||||
print(f"程序运行出错: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
|
||||
|
||||
68
cryptoai/monitors/volume_growup.py
Normal file
68
cryptoai/monitors/volume_growup.py
Normal file
@ -0,0 +1,68 @@
|
||||
|
||||
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)
|
||||
|
||||
|
||||
|
||||
|
||||
@ -16,14 +16,14 @@ from cryptoai.agents.crypto_agent import CryptoAgent
|
||||
from cryptoai.agents.gold_agent import GoldAgent
|
||||
from cryptoai.utils.config_loader import ConfigLoader
|
||||
|
||||
def main():
|
||||
def task_start():
|
||||
try:
|
||||
|
||||
# GoldAgent().start_agent()
|
||||
# # CryptoAgent().start_agent()
|
||||
# return
|
||||
|
||||
print("定时程序启动")
|
||||
print("🚀 加密货币Agent程序已启动")
|
||||
# 设置 08:00, 20:00 运行一次
|
||||
schedule.every().day.at("00:00").do(CryptoAgent().start_agent)
|
||||
schedule.every().day.at("08:00").do(CryptoAgent().start_agent)
|
||||
@ -41,14 +41,7 @@ def main():
|
||||
schedule.run_pending()
|
||||
time.sleep(1)
|
||||
|
||||
except KeyboardInterrupt:
|
||||
print("\n程序已退出")
|
||||
|
||||
except Exception as e:
|
||||
print(f"程序运行出错: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@ -15,6 +15,17 @@ services:
|
||||
networks:
|
||||
- app-network
|
||||
|
||||
cryptoai-monitor:
|
||||
build: .
|
||||
container_name: cryptoai-monitor
|
||||
image: cryptoai-monitor:0.0.1
|
||||
restart: always
|
||||
command: python monitor_endpoint.py
|
||||
environment:
|
||||
- TZ=Asia/Shanghai
|
||||
networks:
|
||||
- app-network
|
||||
|
||||
cryptoai-api:
|
||||
build: .
|
||||
container_name: cryptoai-api
|
||||
|
||||
4
run.py
4
run.py
@ -7,7 +7,7 @@ CryptoAI 启动脚本
|
||||
"""
|
||||
|
||||
import sys
|
||||
from cryptoai.main import main
|
||||
from cryptoai.task_endpoint import task_start
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
sys.exit(task_start())
|
||||
4
run_monitor.py
Normal file
4
run_monitor.py
Normal file
@ -0,0 +1,4 @@
|
||||
from cryptoai.monitor_endpoint import MonitorEndpoint
|
||||
|
||||
if __name__ == "__main__":
|
||||
MonitorEndpoint().run()
|
||||
Loading…
Reference in New Issue
Block a user