1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
| /*
基于低通滤波器和带通滤波器的趋势反转策略。
*/
#include <profile.c>
#include <utils.c>
#include <myindicators.c>
function tradeStrategy() {
// ---------------------------------------------------------
// 参数设置
// ---------------------------------------------------------
int HFTrendPeriod = round(optimize(800,500,1200,50));
var FuzzyFactor = 0.15;
var EntryProbThreshold = 0.6;
int BandpassPeriod = round(optimize(40,10,50,5));
var BandpassWidth = 0.3;
var AtrStopFactor = optimize(0,5,10,0.5);
var AtrTrailFactor = optimize(0,5,10,0.5);
var ShortProfitThreshold = optimize(0,10,40,2);
var FixedCapital = 10000;
var FixedRisk = 0;
if(Live) FixedRisk = slider(1) / 1000. / 100.;
// ---------------------------------------------------------
// 指标计算
// ---------------------------------------------------------
TimeFrame = frameSync(4);
FuzzyRange = FuzzyFactor*ATR(100);
vars HFPrices = series(price());
vars HFTrends = series(LowPass(HFPrices, HFTrendPeriod));
var RisingProb = risingF(HFTrends);
var FallingProb = fallingF(HFTrends);
bool Bullish = RisingProb >= EntryProbThreshold;
bool Bearish = FallingProb >= EntryProbThreshold;
bool BullishPeak = peak(HFTrends);
bool BearishValley = valley(HFTrends);
TimeFrame = 1;
vars Prices = series(price());
vars Cycles = series(bandpass(Prices, BandpassPeriod, BandpassWidth));
var ATR100 = ATR(100);
var LongNormalizedProfit = getLongNormalizedProfit(ATR100);
var ShortNormalizedProfit = getShortNormalizedProfit(ATR100);
// ---------------------------------------------------------
// 交易信号
// ---------------------------------------------------------
bool BuySignal = crossOver(Cycles, Cycles+1) && Cycles[0] < 0;
bool SellSignal = crossUnder(Cycles, Cycles+1) && Cycles[0] > 0;
bool LongEntry = Bullish && BuySignal;
bool LongExit = BullishPeak;
bool ShortEntry = Bearish && SellSignal;
bool ShortExit = BearishValley;
// ---------------------------------------------------------
// 状态管理:再进场限制 (AlgoVar[0])
// ---------------------------------------------------------
// 触发部分止盈后,开始跟踪跟踪剩余头寸,并进入观望期
if(AlgoVar[0] == 1) {
// 限制进场
ShortEntry = false;
// 管理剩余持仓:锁定 50% 利润
for(current_trades) {
if(TradeIsOpen && TradeIsShort) {
TradeTrailLock = 0.5;
}
}
// 解锁条件:空仓且趋势反转
if(NumOpenShort == 0) {
if(BullishPeak || BearishValley) {
AlgoVar[0] = 0; // 允许下一波行情进场
}
}
}
// ---------------------------------------------------------
// 风控
// ---------------------------------------------------------
Stop = AtrStopFactor*ATR100;
Trail = AtrTrailFactor*ATR100;
TrailLock = 10;
TrailSlope = 0;
// ---------------------------------------------------------
// 资金管理
// ---------------------------------------------------------
Lots = 100;
var MinLots = getMinLots();
if(!Train && FixedCapital > 0)
Lots = floor(FixedCapital/priceC()/LotAmount);
if(!Train && Capital > 0 && FixedRisk > 0 && AtrStopFactor > 0)
Lots = fixedRiskLots(FixedRisk);
if(Lots < MinLots) Lots = MinLots;
// ---------------------------------------------------------
// 订单管理
// ---------------------------------------------------------
if(NumOpenLong > 0 && LongExit)
exitLong();
if(NumOpenShort > 0 && ShortExit)
exitShort();
if(NumOpenLong == 0 && LongEntry)
enterLong();
if(NumOpenShort == 0 && ShortEntry) {
if(ShortProfitThreshold > 0)
enterShortMultipleOrders(2, MinLots);
else
enterShort();
}
// 空头部分止盈逻辑
if(ShortProfitThreshold > 0 && NumOpenShort > 0 && AlgoVar[0] == 0) {
if(ShortNormalizedProfit >= ShortProfitThreshold) {
// 平仓50%
exitProfitShortOrders(1);
// 标记进入剩余头寸管理模式
AlgoVar[0] = 1;
}
}
// ---------------------------------------------------------
// 绘图
// ---------------------------------------------------------
if((Test && !is(LOOKBACK)) || Live) {
plot("HFTrend", HFTrends, MAIN, BLUE);
plot("RisingProb", RisingProb, NEW|LINE, BLUE);
plot("ProbT", EntryProbThreshold, 0, GREY);
plot("ProbT2", 1-EntryProbThreshold, 0, GREY);
var BullishState = ifelse(Bullish, 1, 0);
var BearishState = ifelse(Bearish, 1, 0);
plot("BullishState", BullishState, 0|BARS, GREEN+TRANSP);
plot("BearishState", BearishState, 0|BARS, RED+TRANSP);
plot("Bandpass", Cycles, NEW, RED);
plot("BandpassLag", Cycles+1, 0, GREY);
plot("Bandpass+", 0.8, 0, GREY);
plot("Bandpass-", -0.8, 0, GREY);
if(ShortProfitThreshold > 0) {
plot("LongProfit", LongNormalizedProfit, NEW|BARS, GREEN+TRANSP);
plot("ShortProfit", ShortNormalizedProfit, 0|BARS, RED+TRANSP);
plot("ShortProfitT", ShortProfitThreshold, 0, RED);
}
if(AtrStopFactor > 0 && NumOpenTotal > 0) {
var CurrentStop = 0;
for(current_trades) {
CurrentStop = (var)TradeStopLimit;
break_trades;
}
if(CurrentStop > 0) plot("Stop", CurrentStop, MAIN|DOT, LIGHTBLUE);
}
// if(AlgoVar[0] > 0) plot("LockMode", 10, NEW|BARS, BLUE);
}
}
function run() {
// --------------------------------------------------------- //
// Zorro 设置
// --------------------------------------------------------- //
// 日志
set(LOGFILE);
Verbose = 3;
// 图表
set(PLOTNOW);
setf(PlotMode,PL_DIFF);
PlotScale = 8;
PlotHeight2 = 320;
if(Live) PlotBars = -250;
// 训练
set(PARAMETERS);
setf(TrainMode,TRADES);
DataSplit = 85;
NumWFOCycles = 10;
NumCores = 4;
// K线生成规则,7*24交易
resf(BarMode,BR_WEEKEND);
StartWeek = 0;
EndWeek = 62359;
StartMarket = 0;
EndMarket = 2359;
BarPeriod = 15;
BarZone = UTC;
BarOffset = 0;
TickFix = 60000;
if(Live) TickFix = 0;
// 测试样本
StartDate = 20180101;
EndDate = 20251230;
LookBack = 5000;
// Initial Capital
if(Train) Capital = 0;
if(Test) Capital = 0;
if(Live) Capital = 0;
// 下一根k线开盘时进场/平仓
Fill = 3;
// --------------------------------------------------------- //
// Strategy logic
// --------------------------------------------------------- //
while(asset(loop("BTCUSDT","ETHUSDT"))) {
Leverage = 10;
MarginCost = priceClose()*LotAmount/Leverage;
tradeStrategy();
}
}
|