
等级: 新手上路
- 注册:
- 2025-4-10
- 曾用名:
|
//================ 全局变量声明 ================
VARIABLE: ZigPeriod_Global=9, Cycle_Global=15, Divisor_Global=6; // Zig指标周期、循环周期、除数参数全局变量
VARIABLE: StopLoss_Global=76, TickSize_Global=1, Position_Global=1; // 止损阈值、最小价格变动单位、持仓量全局变量
VARIABLE: HIST_PRICE_STR='', DataPointer=0; // 历史价格字符串存储及指针变量
VARIABLE: OptZigPeriod=0, OptCycle=0, OptDivisor=0, MaxProfit=-99999; // 最优参数及最大收益临时变量
VARIABLE: LastHigh=0, LastLow=0, Trend_Global=1, PreHigh=0, PreLow=0, SwingBar=0, SwingPrice=0; // 波动相关状态变量
// 参数优化范围设置
VARIABLE: ZigStart_OPT=1, ZigEnd_OPT=10, ZigStep_OPT=1; // Zig参数优化起始/结束/步长
VARIABLE: CycleStart_OPT=1, CycleEnd_OPT=20, CycleStep_OPT=1; // 循环周期优化参数
VARIABLE: DivisorStart_OPT=1, DivisorEnd_OPT=10, DivisorStep_OPT=1; // 除数参数优化范围
// 临时变量声明
VARIABLE: tempProfit_OPT=0, currentPointer_OPT=0, currentTrend_OPT=1, lastPivot_OPT=0, lastPrice_OPT=0, zigTemp_OPT=0; // 优化过程临时变量
VARIABLE: currentPrice=0, CurrentProfit=0, H1=0, L1=0, ZigPercent=0; // 当前价格及计算中间变量
// 新增的波动计算相关变量
VARIABLE: currentPrice_H=0, currentPrice_L=0, startPos=0, periodLen=0,highestPrice = 0,currentPosition = 0,startIndex = 0;
VARIABLE: ENTRYPRICE=0; // 入场价格全局变量
VARIABLE: lowestPrice = 999999, currentPosition_L = 0,startIndex_L = 0,currentPrice_L = 0;
//================ 初始化模块 ================
IF BARPOS=1 THEN BEGIN; // 当K线位置为第一个时执行初始化
HIST_PRICE_STR := '0000.00'; // 初始化历史价格字符串
FOR i := 2 TO 200 DO // 预填充200个价格数据位
HIST_PRICE_STR := HIST_PRICE_STR + ',0000.00';
DataPointer := 0; // 数据指针初始化为0
END;
//================ 数据更新模块 ================
BEGIN; // 每个新K线周期执行
IF STRLEN(HIST_PRICE_STR) > 1400 THEN // 防止字符串过长
HIST_PRICE_STR := STRMID(HIST_PRICE_STR, 7, 1393); // 截取字符串保持长度
HIST_PRICE_STR := HIST_PRICE_STR + NUMTOSTR(CLOSE,2) + ','; // 将当前收盘价追加到历史记录
DataPointer := DataPointer + 1; // 更新数据指针
END;
//================ 参数优化模块 ================
IF MOD(BARPOS,10) = 0 THEN BEGIN; // 每10个周期执行参数优化
MaxProfit = -99999; // 重置最大收益初始值
// 重置临时变量
tempProfit_OPT = 0;
currentTrend_OPT = 1;
lastPivot_OPT = 0;
lastPrice_OPT = 0;
zigTemp_OPT = 0;
// 初始化最优参数变量
OptZigPeriod = 0;
OptCycle = 0;
OptDivisor = 0;
FOR ZigVar := ZigStart_OPT TO ZigEnd_OPT DO BEGIN; // 遍历Zig参数
FOR CycleVar := CycleStart_OPT TO CycleEnd_OPT DO BEGIN; // 遍历循环周期参数
FOR DivisorVar := DivisorStart_OPT TO DivisorEnd_OPT DO BEGIN; // 遍历除数参数
// 重置当前参数组合的临时变量
tempProfit_OPT = 0;
currentPointer_OPT = DataPointer;
currentTrend_OPT = 1;
lastPivot_OPT = 0;
lastPrice_OPT = 0;
zigTemp_OPT = 0;
FOR i := 1 TO 100 DO BEGIN; // 回溯100个周期进行测试
currentPointer_OPT = currentPointer_OPT - 1; // 回溯指针
IF currentPointer_OPT < 1 THEN currentPointer_OPT = 1; // 边界处理
currentPrice = STRTONUM(STRMID(HIST_PRICE_STR, (currentPointer_OPT-1)*7+1, 6)); // 获取当前回溯价格
startPos = currentPointer_OPT; // 设置起始位置
periodLen = CycleVar; // 设置周期长度
highestPrice = 0; // 初始化最高价
currentPosition = 0;
startIndex = 0;
FOR j := 0 TO periodLen-1 DO BEGIN; // 计算当前周期最高价
currentPosition = startPos - j;
IF currentPosition < 1 THEN BREAK;
startIndex = (currentPosition - 1)*7 + 1;
currentPrice_H = STRTONUM(STRMID(HIST_PRICE_STR, startIndex, 6));
IF currentPrice_H > highestPrice THEN highestPrice = currentPrice_H;
END;
H1 = highestPrice; // 记录最高价
lowestPrice = 999999; // 初始化最低价
currentPosition_L = 0;
startIndex_L = 0;
currentPrice_L = 0;
FOR j := 0 TO periodLen-1 DO BEGIN; // 计算当前周期最低价
currentPosition_L = startPos - j;
IF currentPosition_L < 1 THEN BREAK;
startIndex_L = (currentPosition_L - 1)*7 + 1;
currentPrice_L = STRTONUM(STRMID(HIST_PRICE_STR, startIndex_L, 6));
IF currentPrice_L < lowestPrice THEN lowestPrice = currentPrice_L;
END;
L1 = lowestPrice; // 记录最低价
zigTemp_OPT = (H1 - L1)/L1 * 100 / DivisorVar; // 计算Zig波动百分比
// 趋势判断逻辑
IF currentTrend_OPT = 1 THEN BEGIN; // 当前趋势向上
IF currentPrice <= lastPrice_OPT*(1 - zigTemp_OPT/100) AND (i - lastPivot_OPT) >= ZigVar THEN BEGIN;
currentTrend_OPT = -1; // 转为向下趋势
lastPivot_OPT = i;
lastPrice_OPT = currentPrice;
END;
END ELSE BEGIN; // 当前趋势向下
IF currentPrice >= lastPrice_OPT*(1 + zigTemp_OPT/100) AND (i - lastPivot_OPT) >= ZigVar THEN BEGIN;
currentTrend_OPT = 1; // 转为向上趋势
lastPivot_OPT = i;
lastPrice_OPT = currentPrice;
END;
END;
// 收益计算逻辑
IF currentTrend_OPT = 1 THEN BEGIN;
tempProfit_OPT = tempProfit_OPT + (STRTONUM(STRMID(HIST_PRICE_STR, (currentPointer_OPT-1)*7+1, 6)) - currentPrice); // 多头收益计算
END ELSE BEGIN;
tempProfit_OPT = tempProfit_OPT - (STRTONUM(STRMID(HIST_PRICE_STR, (currentPointer_OPT-1)*7+1, 6)) - currentPrice); // 空头收益计算
END;
END;
CurrentProfit = tempProfit_OPT; // 记录当前参数组合收益
// 更新最优参数
IF CurrentProfit > MaxProfit THEN BEGIN;
MaxProfit = CurrentProfit;
OptZigPeriod = ZigVar;
OptCycle = CycleVar;
OptDivisor = DivisorVar;
END;
// 记录参数测试日志
DEBUGFILE('D:\log.txt', '测试参数 %s',
'Zig='&NUMTOSTR(ZigVar,0)&
' Cycle='&NUMTOSTR(CycleVar,0)&
' Div='&NUMTOSTR(DivisorVar,0)&
' Profit='&NUMTOSTR(tempProfit_OPT,2));
END;
END;
END;
// 更新全局最优参数
IF MaxProfit > -99999 THEN BEGIN;
ZigPeriod_Global = OptZigPeriod;
Cycle_Global = OptCycle;
Divisor_Global = OptDivisor;
// 绘制参数更新提示
DRAWTEXT(BARSTATUS=2, HIGH, '新参数:'+NUMTOSTR(ZigPeriod_Global,0)),COLORRED;
DRAWTEXT(BARSTATUS=2, SwingPrice + 2, 'Optimized ZigPeriod: ' + NUMTOSTR(OptZigPeriod, 0)), COLORYELLOW;
DRAWTEXT(BARSTATUS=2, SwingPrice + 4, 'Optimized Cycle: ' + NUMTOSTR(OptCycle, 0)), COLORYELLOW;
DRAWTEXT(BARSTATUS=2, SwingPrice + 6, 'Optimized Divisor: ' + NUMTOSTR(OptDivisor, 0)), COLORYELLOW;
DRAWTEXT(BARSTATUS=2 AND SwingPrice > 0, SwingPrice, 'SWING: ' & NUMTOSTR(SwingPrice,2)), COLORYELLOW;
END;
// 记录参数更新日志
IF ZigPeriod_Global <> OptZigPeriod THEN BEGIN;
DEBUGFILE('D:\update.log','参数更新 %s',
'Zig:'&NUMTOSTR(ZigPeriod_Global,0)&'→'&NUMTOSTR(OptZigPeriod,0)&
' Cycle:'&NUMTOSTR(Cycle_Global,0)&'→'&NUMTOSTR(OptCycle,0));
END;
END; |
|