
等级: 新手上路
- 注册:
- 2025-4-10
- 曾用名:
|
//================ 全局变量声明 ================
VARIABLE: ZigPeriod=3, Cycle=10, Divisor=6;
VARIABLE: StopLoss=76, TickSize=1, Position=1;
VARIABLE: HIST_PRICE_STR='', DataPointer=0;
VARIABLE: OptZigPeriod=0, OptCycle=0, OptDivisor=0, MaxProfit=-99999;
VARIABLE: LastHigh=0, LastLow=0, Trend=1, PreHigh=0, PreLow=0, SwingBar=0, SwingPrice=0;
//================ 函数定义 ================
FUNCTION GetHistoricalHigh(startPos, periodLen);
LOCAL highestPrice=0, currentPosition=0, startIndex=0, currentPrice=0;
FOR i = 0 TO periodLen-1 DO
BEGIN
currentPosition := startPos - i;
IF currentPosition < 1 THEN BREAK;
startIndex := (currentPosition-1)*7 + 1;
currentPrice := STRTONUM(STRMID(HIST_PRICE_STR, startIndex, 6));
IF currentPrice > highestPrice THEN highestPrice := currentPrice;
END;
RETURN highestPrice;
END;
FUNCTION GetHistoricalLow(startPos, periodLen);
LOCAL lowestPrice=999999, currentPosition=0, startIndex=0, currentPrice=0;
FOR i = 0 TO periodLen-1 DO
BEGIN
currentPosition := startPos - i;
IF currentPosition < 1 THEN BREAK;
startIndex := (currentPosition-1)*7 + 1;
currentPrice := STRTONUM(STRMID(HIST_PRICE_STR, startIndex, 6));
IF currentPrice < lowestPrice THEN lowestPrice := currentPrice;
END;
RETURN lowestPrice;
END;
FUNCTION CalculateProfit(startPointer, zigPeriod, cycle, divisor);
LOCAL tempProfit=0, currentPointer=0, currentTrend=1, lastPivot=0, lastPrice=0, zigTemp=0;
currentPointer := startPointer;
FOR i = 1 TO 100 DO
BEGIN
currentPointer := currentPointer - 1;
IF currentPointer < 1 THEN currentPointer := 1;
currentPrice := STRTONUM(STRMID(HIST_PRICE_STR, (currentPointer-1)*7+1, 6));
H1 := GetHistoricalHigh(currentPointer, cycle);
L1 := GetHistoricalLow(currentPointer, cycle);
zigTemp := (H1 - L1)/L1*100/divisor;
// 趋势判断
IF currentTrend = 1 THEN
BEGIN
IF currentPrice <= lastPrice*(1 - zigTemp/100) AND (i - lastPivot) >= zigPeriod THEN
BEGIN
currentTrend = -1;
lastPivot = i;
lastPrice = currentPrice;
END;
END
ELSE
BEGIN
IF currentPrice >= lastPrice*(1 + zigTemp/100) AND (i - lastPivot) >= zigPeriod THEN
BEGIN
currentTrend = 1;
lastPivot = i;
lastPrice = currentPrice;
END;
END;
// 收益计算
IF currentTrend = 1 THEN
tempProfit += STRTONUM(STRMID(HIST_PRICE_STR, currentPointer*7+1, 6)) - currentPrice;
ELSE
tempProfit -= STRTONUM(STRMID(HIST_PRICE_STR, currentPointer*7+1, 6)) - currentPrice;
END;
RETURN tempProfit;
END;
//================ 初始化模块 ================
IF BARPOS=1 THEN
BEGIN
HIST_PRICE_STR := '0000.00';
FOR i = 2 TO 200;
HIST_PRICE_STR := HIST_PRICE_STR + ',0000.00';
DataPointer := 0;
END;
//================ 数据更新模块 ================
BEGIN
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 BARPOS MOD 100 = 0 THEN
BEGIN
ZigStart=2; ZigEnd=5; ZigStep=1;
CycleStart=5; CycleEnd=20; CycleStep=1;
DivisorStart=4; DivisorEnd=8; DivisorStep=1;
MaxProfit = -99999;
FOR ZigPeriodTemp = ZigStart TO ZigEnd STEP ZigStep;
BEGIN
FOR CycleTemp = CycleStart TO CycleEnd STEP CycleStep;
BEGIN
FOR DivisorTemp = DivisorStart TO DivisorEnd STEP DivisorStep;
BEGIN
CurrentProfit = CalculateProfit(DataPointer, ZigPeriodTemp, CycleTemp, DivisorTemp);
IF CurrentProfit > MaxProfit THEN
BEGIN
MaxProfit = CurrentProfit;
OptZigPeriod = ZigPeriodTemp;
OptCycle = CycleTemp;
OptDivisor = DivisorTemp;
END;
END;
END;
END;
ZigPeriod = OptZigPeriod;
Cycle = OptCycle;
Divisor = OptDivisor;
DRAWTEXT(BARSTATUS=2, HIGH, 'Optimal:Z='+NUMTOSTR(ZigPeriod,0)+' C='+NUMTOSTR(Cycle,0)+' D='+NUMTOSTR(Divisor,0)),COLORYELLOW;
END;
//================ 策略逻辑 ================
H1 = GetHistoricalHigh(BARPOS, Cycle);
L1 = GetHistoricalLow(BARPOS, Cycle);
ZigPercent = (H1-L1)/L1*100/Divisor;
T1 = TIME=145800;
T2 = TIME=022800;
T5 = TIME<022700 OR TIME>085500;
T6 = TIME<145000 OR TIME>185500;
IF BARPOS=1 THEN
BEGIN
LastHigh = HIGH;
LastLow = LOW;
PreHigh = HIGH;
PreLow = LOW;
SwingBar = 1;
END
ELSE
BEGIN
IF Trend=1 THEN
BEGIN
IF HIGH > PreHigh THEN PreHigh = HIGH;
IF LOW <= PreHigh*(1-ZigPercent/100) AND (BARPOS - SwingBar) >= ZigPeriod THEN
BEGIN
Trend = -1;
SwingPrice = PreHigh;
SwingBar = BARPOS;
PreLow = LOW;
END;
END
ELSE
BEGIN
IF LOW < PreLow THEN PreLow = LOW;
IF HIGH >= PreLow*(1+ZigPercent/100) AND (BARPOS - SwingBar) >= ZigPeriod THEN
BEGIN
Trend = 1;
SwingPrice = PreLow;
SwingBar = BARPOS;
PreHigh = HIGH;
END;
END;
END;
//================ 交易信号 ================
BuySignal = Trend=1 AND BARPOS>=SwingBar+1;
SellSignal = Trend=-1 AND BARPOS>=SwingBar+1;
BUY(BuySignal AND HOLDING=0 AND T6 AND T5, Position);
BUYSHORT(SellSignal AND HOLDING=0 AND T6 AND T5, Position);
SELL(SellSignal OR T1 OR T2, 0);
SELLSHORT(BuySignal OR T1 OR T2, 0);
//================ 风控模块 ================
IF HOLDING>0 AND LOW<=ENTRYPRICE-StopLoss*MINDIFF THEN SELL(1, Position);
IF HOLDING<0 AND HIGH>=ENTRYPRICE+StopLoss*MINDIFF THEN SELLSHORT(1, Position);
//================ 图形输出 ================
DRAWTEXT(BARSTATUS=2 AND BARPOS=SwingBar, SwingPrice, 'SWING:'+NUMTOSTR(SwingPrice,2)),COLORYELLOW; |
|