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
224
225
226
227
228
229
230
231
232
233
234
235
236
| /*
基于控制论振荡器的交易策略
*/
#include <profile.c>
#include <utils.c>
#include <myindicators.c>
var CyberOsc(vars Data, int HPLength, int LPLength) {
vars HP = series(HighPass2(Data,HPLength));
vars LP = series(Smooth(HP,LPLength));
var RMS = sqrt(SumSq(LP,100)/100);
return LP[0]/fix0(RMS);
}
function tradeStrategy() {
// ---------------------------------------------------------
// 参数设置
// ---------------------------------------------------------
int TrendPeriod = round(optimize(800,400,1000,50));
var FuzzyFactor = 0.1;
var EntryProbThreshold = 0.6;
int HPLength = round(optimize(200,200,800,50));
int LPLength = 20;
var VolaThreshold = optimize(0,0.5,1.2,0.05);
var AtrStopFactor = optimize(0,3,8,0.5);
var AtrTrailFactor = 0;
var ShortProfitThreshold = optimize(0,3,15,1);
var FixedCapital = 0;
var FixedRisk = 0.015;
// ---------------------------------------------------------
// 指标计算
// ---------------------------------------------------------
vars Prices = series(price());
vars Trends = series(LowPass(Prices, TrendPeriod));
vars Moms = series(CyberOsc(Prices, HPLength, LPLength));
var ATR100 = ATR(100);
var VolaRatio = ATR(20)/ATR100;
FuzzyRange = FuzzyFactor*ATR100;
vars RisingProb = series(risingF(Trends));
vars FallingProb = series(fallingF(Trends));
var LongNormalizedProfit = getLongNormalizedProfit(ATR100);
var ShortNormalizedProfit = getShortNormalizedProfit(ATR100);
// ---------------------------------------------------------
// 交易信号
// ---------------------------------------------------------
bool Bullish = RisingProb[0] >= EntryProbThreshold;
bool Bearish = FallingProb[0] >= EntryProbThreshold;
bool MomentumBuy = Moms[0] > 0 && Moms[0] < 2 && rising(Moms);
bool PullbackBuy = Moms[0] < -2 && valley(Moms);
bool MomentumSell = Moms[0] < 0 && Moms[0] > -2 && falling(Moms);
bool PullbackSell = Moms[0] > 2 && peak(Moms);
bool LongEntry = Bullish && (MomentumBuy || PullbackBuy);
bool LongExit = crossUnder(Moms, 0);
bool ShortEntry = Bearish && (MomentumSell || PullbackSell);
bool ShortExit = crossOver(Moms, 0);
if(VolaThreshold > 0) {
bool LowVolatility = VolaRatio <= VolaThreshold;
LongEntry = LongEntry && !LowVolatility;
ShortEntry = ShortEntry && !LowVolatility;
}
// ---------------------------------------------------------
// 状态管理:再进场限制 (AlgoVar[0])
// ---------------------------------------------------------
// 触发部分止盈后,开始跟踪跟踪剩余头寸,并进入观望期
if(AlgoVar[0] == 1) {
// 限制进场
ShortEntry = false;
// 管理剩余持仓:锁定 50% 利润
for(current_trades) {
if(TradeIsOpen && TradeIsShort) {
TradeStopLimit = min(TradeStopLimit, TradePriceOpen + (priceC() - TradePriceOpen)*0.5);
}
}
// 解锁条件:空仓且短期趋势反转向上
if(NumOpenShort == 0) {
if(PullbackSell || peak(Trends) || crossOver(Prices,Trends)) {
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("Mom", Moms, NEW, RED);
plot("Mom+", 0, 0, GREY);
plot("Mom++", 2, 0, GREY);
plot("Mom--", -2, 0, GREY);
if(TrendPeriod) plot2("Trend", Trends[0], MAIN, rising(Trends), GREEN, RED);
var BullishState = ifelse(RisingProb[0] >= EntryProbThreshold, 1, 0);
var BearishState = ifelse(FallingProb[0] >= EntryProbThreshold, 1, 0);
plot("BullishState", BullishState, NEW|BARS, GREEN+TRANSP);
plot("BearishState", BearishState, 0|BARS, RED+TRANSP);
plot("RisingProb", RisingProb[0], 0|LINE, BLUE);
plot("BullT", EntryProbThreshold, 0, GREY);
plot("BearT", 1-EntryProbThreshold, 0, GREY);
plot("VolaRatio", VolaRatio, NEW, RED);
plot("Vola+", VolaThreshold, 0, GREY);
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(ShortProfitThreshold > 0) {
plot("LongProfit", LongNormalizedProfit, NEW|BARS, GREEN+TRANSP);
plot("ShortProfit", ShortNormalizedProfit, 0|BARS, RED+TRANSP);
plot("ProfitT",ShortProfitThreshold,0,GREY);
}
// if(AlgoVar[0] > 0) plot("LockMode", 10, NEW|BARS, BLUE);
}
}
function run() {
// --------------------------------------------------------- //
// 回溯检验设置
// --------------------------------------------------------- //
// 日志
set(LOGFILE);
Verbose = 3;
// 图表
set(PLOTNOW);
setf(PlotMode,PL_DIFF);
PlotScale = 8;
PlotHeight2 = 320;
// 训练
set(PARAMETERS);
setf(TrainMode,TRADES);
DataSplit = 85;
NumWFOCycles = 15;
NumCores = 4;
// k线生成
resf(BarMode,BR_WEEKEND);
StartWeek = 0;
EndWeek = 62359;
StartMarket = 0;
EndMarket = 2359;
BarPeriod = 60;
BarZone = UTC;
BarOffset = 0;
TickFix = 60000;
if(Live) TickFix = 0;
// 样本
StartDate = 20180101;
EndDate = 20251230;
LookBack = 5000;
// 初始资本
if(Train) Capital = 0;
if(Test) Capital = 10000;
if(Live) Capital = 0;
// 下一根k线开盘进场/平仓
Fill = 3;
// --------------------------------------------------------- //
// 策略逻辑
// --------------------------------------------------------- //
while(asset(loop("BTCUSDT","ETHUSDT"))) {
Leverage = 10;
MarginCost = priceClose()*LotAmount/Leverage;
tradeStrategy();
}
}
|