Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import asyncio
|
| 4 |
+
import json
|
| 5 |
+
import threading
|
| 6 |
+
import time
|
| 7 |
+
from datetime import datetime
|
| 8 |
+
|
| 9 |
+
# 全局控制
|
| 10 |
+
运行线程 = None
|
| 11 |
+
运行状态 = {"运行中": False, "净值": 1.0, "回撤": 0.0, "日志": []}
|
| 12 |
+
|
| 13 |
+
def 启动模拟(合约, 周期, 参数json):
|
| 14 |
+
global 运行线程, 运行状态
|
| 15 |
+
|
| 16 |
+
if 运行状态["运行中"]:
|
| 17 |
+
return "已有任务运行中", "", ""
|
| 18 |
+
|
| 19 |
+
# 解析参数
|
| 20 |
+
try:
|
| 21 |
+
参数 = json.loads(参数json) if 参数json else {}
|
| 22 |
+
except:
|
| 23 |
+
return "参数JSON格式错误", "", ""
|
| 24 |
+
|
| 25 |
+
# 启动异步任务
|
| 26 |
+
def 运行任务():
|
| 27 |
+
from 插件_引擎.多策略引擎 import 多策略引擎
|
| 28 |
+
from 插件_交易.策略_自适应 import 自适应策略
|
| 29 |
+
|
| 30 |
+
async def 主循环():
|
| 31 |
+
运行状态["运行中"] = True
|
| 32 |
+
运行状态["日志"] = []
|
| 33 |
+
|
| 34 |
+
引擎 = 多策略引擎()
|
| 35 |
+
策略 = 自适应策略(f"{合约}_{周期}分", 参数)
|
| 36 |
+
引擎.注册策略(f"{合约}_{周期}", 策略, 合约, int(周期))
|
| 37 |
+
|
| 38 |
+
try:
|
| 39 |
+
await 引擎.启动([合约])
|
| 40 |
+
except Exception as e:
|
| 41 |
+
运行状态["日志"].append(f"错误: {e}")
|
| 42 |
+
finally:
|
| 43 |
+
运行状态["运行中"] = False
|
| 44 |
+
|
| 45 |
+
asyncio.run(主循环())
|
| 46 |
+
|
| 47 |
+
运行线程 = threading.Thread(target=运行任务)
|
| 48 |
+
运行线程.start()
|
| 49 |
+
|
| 50 |
+
return "模拟启动成功", "", ""
|
| 51 |
+
|
| 52 |
+
def 停止模拟():
|
| 53 |
+
global 运行状态
|
| 54 |
+
运行状态["运行中"] = False
|
| 55 |
+
return "已发送停止信号"
|
| 56 |
+
|
| 57 |
+
def 获取状态():
|
| 58 |
+
日志文本 = "\n".join(运行状态["日志"][-50:]) # 最近50条
|
| 59 |
+
状态文本 = f"运行中: {运行状态['运行中']} | 净值: {运行状态['净值']:.4f} | 回撤: {运行状态['回撤']:.2%}"
|
| 60 |
+
return 状态文本, 日志文本
|
| 61 |
+
|
| 62 |
+
# 默认参数(你的60分钟V1参数)
|
| 63 |
+
默认参数 = {
|
| 64 |
+
"ver": "v1",
|
| 65 |
+
"adx_thr": 25,
|
| 66 |
+
"atr_buf_tr": 0.05,
|
| 67 |
+
"rsi_low": 30,
|
| 68 |
+
"rsi_high": 70,
|
| 69 |
+
"mr_stop_atr": 2.0,
|
| 70 |
+
"trend_logic": "adx",
|
| 71 |
+
"alloc_mode": "fixed"
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
# Gradio界面
|
| 75 |
+
with gr.Blocks(title="模拟实盘") as demo:
|
| 76 |
+
gr.Markdown("# 📈 加密货币模拟实盘交易")
|
| 77 |
+
|
| 78 |
+
with gr.Row():
|
| 79 |
+
with gr.Column():
|
| 80 |
+
合约输入 = gr.Textbox(
|
| 81 |
+
label="合约ID",
|
| 82 |
+
value="UXLINK-USDT-SWAP",
|
| 83 |
+
placeholder="例: BTC-USDT-SWAP"
|
| 84 |
+
)
|
| 85 |
+
周期选择 = gr.Radio(
|
| 86 |
+
label="周期(分钟)",
|
| 87 |
+
choices=["30", "60", "120", "240"],
|
| 88 |
+
value="60"
|
| 89 |
+
)
|
| 90 |
+
参数输入 = gr.Textbox(
|
| 91 |
+
label="策略参数(JSON)",
|
| 92 |
+
value=json.dumps(默认参数, indent=2),
|
| 93 |
+
lines=10
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
with gr.Row():
|
| 97 |
+
启动按钮 = gr.Button("🚀 启动模拟", variant="primary")
|
| 98 |
+
停止按钮 = gr.Button("⏹️ 停止模拟", variant="stop")
|
| 99 |
+
|
| 100 |
+
with gr.Column():
|
| 101 |
+
状态显示 = gr.Textbox(label="运行状态", lines=1)
|
| 102 |
+
日志显示 = gr.Textbox(label="实时日志", lines=20)
|
| 103 |
+
刷新按钮 = gr.Button("🔄 刷新状态")
|
| 104 |
+
|
| 105 |
+
# 事件绑定
|
| 106 |
+
启动按钮.click(
|
| 107 |
+
fn=启动模拟,
|
| 108 |
+
inputs=[合约输入, 周期选择, 参数输入],
|
| 109 |
+
outputs=[状态显示, 日志显示, gr.Textbox(visible=False)]
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
停止按钮.click(
|
| 113 |
+
fn=停止模拟,
|
| 114 |
+
outputs=状态显示
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
刷新按钮.click(
|
| 118 |
+
fn=获取状态,
|
| 119 |
+
outputs=[状态显示, 日志显示]
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
# 自动刷新
|
| 123 |
+
demo.load(获取状态, outputs=[状态显示, 日志显示], every=2)
|
| 124 |
+
|
| 125 |
+
if __name__ == "__main__":
|
| 126 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|