Vars
Plot plt; // 补全你原代码中的绘图对象,无任何修改
Global String fd("幅度报警");
Global String fd_min("预警分钟数"); // 单独的分钟数事件
Global String fd_fudu("预警幅度%"); // 单独的幅度事件
Global Numeric custom_minutes(5); // 自定义分钟数(默认5分钟,可输入修改)
Global Numeric custom_fudu(0.5); // 自定义预警幅度(默认0.5%,可输入修改)
Global Bool fdkg(False);//幅度报警开关
Global Integer fz(-1);//分钟
Global Integer curr1m(-1); // 补全依赖的curr1m变量(原代码中存在,确保Range作用域有效)
Global Integer zld1(-1);
Global Integer zlm1(-1);
Global Integer czld1(-1);
Global Integer czlm1(-1);
Global Integer curr1d(-1);
Events
OnInit()
{
zld1 = SubscribeBar(MainSymbol, "1d", 8000);
zlm1 = SubscribeBar(MainSymbol, "1m", 8000);
curr1d = zld1;
curr1m = zlm1;
SubscribeEvent(fd);
SubscribeEvent(fd_min); // 订阅分钟数事件
SubscribeEvent(fd_fudu); // 订阅幅度事件
Data[zld1].Hide;
Data[0].Hide;
Data[curr1d].Hide;
}
OnReady()
{
Range[curr1m:curr1m]
{
Data[curr1d].Hide;
plt.toolBar(fd_min, "分钟", text(custom_minutes), "input");
plt.toolBar(fd_fudu, "幅度", text(custom_fudu), "input");
plt.toolBar(fd, "波幅预警", "开启/关闭", "button");
plt.setOption("分钟", "fix-width", 100);
plt.setOption("幅度", "fix-width", 100);
}
}
OnEvent(StringRef evtName, MapRef<String, String> evtValue)
{
if(evtName == fd)
{
fdkg = !fdkg;
Numeric show_min = custom_minutes;
Numeric show_fud = custom_fudu;
PushStatusMsg(IIFString(fdkg, "涨速报警开(" + text(show_min) + "分钟/" + text(show_fud) + "%)", "涨速报警关"));
}
// 独立处理:分钟数输入(只改分钟数,幅度不变)
if(evtName == fd_min)
{
Numeric input_min = Value(evtValue["分钟"]);
custom_minutes = input_min;
PushStatusMsg("预警分钟数:" + text(custom_minutes) + "分钟");
}
// 独立处理:幅度输入(只改幅度,分钟数不变)
if(evtName == fd_fudu)
{
Numeric input_fud = Value(evtValue["幅度"]);
// 双重判断:非NA + 合法值(≥0),否则保留原有值
custom_fudu = input_fud;
PushStatusMsg("预警幅度:" + text(custom_fudu) + "%");
}
}
OnBar(ArrayRef<Integer> indexs)
{
integer sjsj = Hour * 60 + Minute;
if( BarStatus == 2 And sjsj != fz)
{
Numeric custom_bar = custom_minutes; // 自定义分钟数(对应K线根数,1分钟K线即n分钟对应n根Bar)
Numeric target_fudu = custom_fudu; // 自定义预警幅度(%)
// 替换原有固定的5和0.5,使用自定义变量
if((Max(C , C[custom_bar]) - Min(C, C[custom_bar])) / Min(C, C[custom_bar]) * 100 >= target_fudu And fdkg == True)
{
Alert(SymbolName + IIFString(C > C[custom_bar], "涨速", "跌速") + Text((Max(C , C[custom_bar]) - Min(C, C[custom_bar])) / Min(C, C[custom_bar]) * 100, 2) + "%" );
fz = sjsj;
}
}
}分享给需要的朋友
Vars
Plot plt;
Global String fd_min("预警分钟数"); // 单独的分钟数事件
Global String fd_fudu("预警幅度%"); // 单独的幅度事件
Global Numeric custom_minutes(5); // 自定义分钟数(默认5分钟,可输入修改)
Global Numeric custom_fudu(0.5); // 自定义预警幅度(默认0.5%,可输入修改)
Global Bool fdkg(False);//幅度报警开关
Global Integer fz(-1);//分钟
Global Integer curr1m(-1); // 补全依赖的curr1m变量(原代码中存在,确保Range作用域有效)
Global Integer zld1(-1);
Global Integer zlm1(-1);
Global Integer czld1(-1);
Global Integer czlm1(-1);
Global Integer curr1d(-1);
Events
OnInit()
{
zld1 = SubscribeBar(MainSymbol, "1d", 8000);
zlm1 = SubscribeBar(MainSymbol, "1m", 8000);
curr1d = zld1;
curr1m = zlm1;
SubscribeEvent(fd);
SubscribeEvent(fd_min); // 订阅分钟数事件
SubscribeEvent(fd_fudu); // 订阅幅度事件
Data[zld1].Hide;
Data[0].Hide;
Data[curr1d].Hide;
}
OnReady()
{
Range[curr1m:curr1m]
{
Data[curr1d].Hide;
plt.toolBar(fd_min, "分钟", text(custom_minutes), "input");
plt.toolBar(fd_fudu, "幅度", text(custom_fudu), "input");
plt.setOption("分钟", "fix-width", 100);
plt.setOption("幅度", "fix-width", 100);
}
}
OnEvent(StringRef evtName, MapRef<String, String> evtValue)
{
if(evtName == fd_min)
{
Numeric input_min = Value(evtValue["分钟"]);
// 有效性判断:非NA、≥1(分钟数不能为0或负数)
if(input_min <> InvalidInteger && input_min >= 1)
{
// 输入有效:更新变量+自动开启幅速报警+提示
custom_minutes = input_min;
fdkg = true; // 自动开启幅速报警
PushStatusMsg("预警:" + Text(custom_minutes) + "分钟,"+"幅度:" + Text(custom_fudu, 2)+"幅速报警已自动开启");
}
else
{
// 输入无效:保留原有值+提示+不改变报警状态
PushStatusMsg("预警分钟数输入无效(需≥1的数字),保留原有值:" + Text(custom_minutes) + "分钟");PushStatusMsg("预警幅度更新为:" + Text(custom_fudu, 2) + "%,幅速报警已自动开启");
}
}
// 独立处理:幅度输入(只改幅度,分钟数不变)
if(evtName == fd_fudu)
{
Numeric input_fud = Value(evtValue["幅度"]);
// 有效性判断:非NA、≥0(幅度不能为负数)
if(input_fud <> InvalidInteger && input_fud >= 0)
{
// 输入有效:更新变量+自动开启幅速报警+提示
custom_fudu = input_fud;
fdkg = true; // 自动开启幅速报警
PushStatusMsg("预警:" + Text(custom_minutes) + "分钟,"+"幅度:" + Text(custom_fudu, 2)+"幅速报警已自动开启");
}
else
{
// 输入无效:保留原有值+提示+不改变报警状态
PushStatusMsg("预警幅度输入无效(需≥0的数字),保留原有值:" + Text(custom_fudu, 2) + "%");
}
}
OnBar(ArrayRef<Integer> indexs)
{
integer sjsj = Hour * 60 + Minute;
if( BarStatus == 2 And sjsj != fz)
{
Numeric custom_bar = custom_minutes; // 自定义分钟数(对应K线根数,1分钟K线即n分钟对应n根Bar)
Numeric target_fudu = custom_fudu; // 自定义预警幅度(%)
// 替换原有固定的5和0.5,使用自定义变量
if((Max(C , C[custom_bar]) - Min(C, C[custom_bar])) / Min(C, C[custom_bar]) * 100 >= target_fudu And fdkg == True)
{
Alert(SymbolName + IIFString(C > C[custom_bar], "涨速", "跌速") + Text((Max(C , C[custom_bar]) - Min(C, C[custom_bar])) / Min(C, C[custom_bar]) * 100, 2) + "%" );
fz = sjsj;
}
}
}这个版本就是去掉单独开关,只要输入有效数,就可以开启,不用单独去点开关