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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
| // =========================================================
// Nobel Predictor - Multi-Timeframe Implementation
// Timeframes: M15, M30, H1, H4, D1
// =========================================================
// --- 常量定义 ---
#define PATTERN_LEN 10 // 模式长度
#define MAX_PATTERNS 50 // 每个周期的记忆库容量
#define MAX_SIMILAR 10 // 搜索相似模式数量
#define NUM_TFS 5 // 时间周期数量 (M15, M30, H1, H4, D1)
// --- 数据结构 ---
// 模式结构体
typedef struct {
var values[PATTERN_LEN]; // 归一化形态
var result; // 结果 (相对于起点的涨跌幅)
var time; // 发生时间
int confidence; // 置信度
bool validated; // 验证标记
} Pattern;
// 预测结果结构体
typedef struct {
var predicted_change_pct; // 预测下一根K线的涨跌幅 (用于显示)
var raw_weighted_change; // 结构性趋势强度 (核心指标)
var predicted_price; // 预测绝对价格
var confidence; // 置信度
bool is_bullish; // 结构是否看涨
int found_count; // 找到的相似模式数
} PredictionResult;
// --- 全局变量 ---
// 周期定义 (单位: 分钟)
// Base Period 将设为 15 分钟
int g_tfPeriods[NUM_TFS] = {15, 30, 60, 240, 1440};
string g_tfNames[NUM_TFS];
// 记忆库:第一维是周期索引,第二维是模式索引
Pattern g_patterns[NUM_TFS][MAX_PATTERNS];
// 计数器:每个周期独立计数
int g_patternCounts[NUM_TFS];
// 预测结果:每个周期独立的预测
PredictionResult g_predictions[NUM_TFS];
// --- 核心函数 ---
// 提取模式
void extract_pattern(Pattern* p, int offset)
{
int base_idx = offset + PATTERN_LEN - 1;
var base_price = priceClose(base_idx);
if(base_price <= 0) return;
int i;
for(i = 0; i < PATTERN_LEN; i++)
{
int price_idx = offset + (PATTERN_LEN - 1) - i;
var current_price = priceClose(price_idx);
p->values[i] = (current_price - base_price) / base_price * 100.0;
}
}
// 计算模式相似度
var calculate_similarity(var* p1, var* p2)
{
var sum1 = 0, sum2 = 0, sumProd = 0;
var sumSq1 = 0, sumSq2 = 0;
int i;
for(i = 0; i < PATTERN_LEN; i++) {
var v1 = p1[i]; var v2 = p2[i];
sum1 += v1; sum2 += v2;
sumSq1 += v1*v1; sumSq2 += v2*v2;
sumProd += v1*v2;
}
var n = (var)PATTERN_LEN;
var numerator = n * sumProd - sum1 * sum2;
var denom = sqrt((n * sumSq1 - sum1 * sum1) * (n * sumSq2 - sum2 * sum2));
if(denom < 0.0001) return 0;
return (numerator / denom + 1.0) / 2.0;
}
// 清理旧模式,防止内存溢出
void cleanup_patterns(int tf_idx)
{
int keep_count = MAX_PATTERNS / 2;
int remove_count = g_patternCounts[tf_idx] - keep_count;
if(remove_count <= 0) return;
int i;
for(i = 0; i < keep_count; i++)
{
int src_idx = remove_count + i;
// 注意:访问二维数组
if(src_idx < g_patternCounts[tf_idx])
memcpy(&g_patterns[tf_idx][i], &g_patterns[tf_idx][src_idx], sizeof(Pattern));
}
g_patternCounts[tf_idx] = keep_count;
}
// 更新指定周期的模式库
void update_patterns(int tf_idx)
{
// 访问特定周期的计数器
if(g_patternCounts[tf_idx] >= MAX_PATTERNS - 5) {
cleanup_patterns(tf_idx);
}
int learn_offset = 1;
if(LookBack < PATTERN_LEN + learn_offset + 5) return;
Pattern newPat;
extract_pattern(&newPat, learn_offset);
int base_idx = learn_offset + PATTERN_LEN - 1;
var base_price = priceClose(base_idx);
var current_close = priceClose(0);
if(base_price <= 0) return;
newPat.result = (current_close - base_price) / base_price * 100.0;
newPat.time = wdate(learn_offset);
newPat.confidence = 0;
newPat.validated = false;
if(g_patternCounts[tf_idx] < MAX_PATTERNS) {
// 存入二维数组
memcpy(&g_patterns[tf_idx][g_patternCounts[tf_idx]], &newPat, sizeof(Pattern));
g_patternCounts[tf_idx]++;
}
}
// 预测指定周期的下一根K线
void predict_next_bar(int tf_idx)
{
// 重置当前周期的预测结果
g_predictions[tf_idx].predicted_change_pct = 0;
g_predictions[tf_idx].confidence = 0;
g_predictions[tf_idx].is_bullish = false;
g_predictions[tf_idx].found_count = 0;
g_predictions[tf_idx].raw_weighted_change = 0;
Pattern currentPat;
extract_pattern(¤tPat, 0);
int indices[MAX_SIMILAR];
var similarities[MAX_SIMILAR];
int foundCount = 0;
// 遍历当前周期的模式库
int i;
for(i = g_patternCounts[tf_idx] - 1; i >= 0; i--)
{
if(foundCount >= MAX_SIMILAR) break;
var sim = calculate_similarity(currentPat.values, g_patterns[tf_idx][i].values);
if(sim > 0.7) {
indices[foundCount] = i;
similarities[foundCount] = sim;
foundCount++;
}
}
g_predictions[tf_idx].found_count = foundCount;
if(foundCount < 3) return;
var totalWeightedChange = 0;
var totalWeight = 0;
int countPositive = 0;
int countNegative = 0;
for(i = 0; i < foundCount; i++)
{
int idx = indices[i];
var weight = similarities[i];
var result = g_patterns[tf_idx][idx].result;
totalWeightedChange += result * weight;
totalWeight += weight;
if(result > 0.01) countPositive++;
else if(result < -0.01) countNegative++;
}
if(totalWeight > 0)
g_predictions[tf_idx].raw_weighted_change = totalWeightedChange / totalWeight;
g_predictions[tf_idx].is_bullish = (countPositive > countNegative) && (g_predictions[tf_idx].raw_weighted_change > 0.01);
var consensus = 0;
int stronger_side = max(countPositive, countNegative);
if(foundCount > 0) consensus = (var)stronger_side / foundCount * 100.0;
var coverage = (var)foundCount / MAX_SIMILAR * 100.0;
g_predictions[tf_idx].confidence = (consensus * 0.7 + coverage * 0.3);
// 计算隐含的下一根K线涨跌
var current_base = priceClose(PATTERN_LEN - 1);
g_predictions[tf_idx].predicted_price = current_base * (1.0 + g_predictions[tf_idx].raw_weighted_change / 100.0);
var current_price = priceClose(0);
if(current_price > 0)
g_predictions[tf_idx].predicted_change_pct = (g_predictions[tf_idx].predicted_price - current_price) / current_price * 100.0;
}
void print_dashboard()
{
if(!is(LOOKBACK)) {
print(TO_WINDOW, "\n\n=== NOBEL MULTI-TF DASHBOARD ===");
print(TO_WINDOW, "\nTF | DIR | CONF | STRUCT | NEXT");
print(TO_WINDOW, "\n-----------------------------------");
int i;
for(i=0; i<NUM_TFS; i++) {
string dir = "---";
if(g_predictions[i].confidence > 0)
dir = ifelse(g_predictions[i].is_bullish, "BULL", "BEAR");
print(TO_WINDOW, "\n%s | %s | %3.0f%% | %+5.2f%% | %+5.2f%%",
g_tfNames[i],
dir,
g_predictions[i].confidence,
g_predictions[i].raw_weighted_change,
g_predictions[i].predicted_change_pct
);
}
}
}
// --------------------------------------------------------- //
// 主程序
// --------------------------------------------------------- //
function run()
{
// 日志
set(LOGFILE, PLOTNOW);
Verbose = 0;
PlotScale = 8;
PlotHeight2 = 320;
// k线生成规则,7*24交易
resf(BarMode,BR_WEEKEND);
StartWeek = 0;
EndWeek = 62359;
StartMarket = 0;
EndMarket = 2359;
BarPeriod = 15; // 最小周期设为15分钟
BarZone = UTC;
BarOffset = 0;
TickFix = 60000;
// 训练
set(PARAMETERS);
setf(TrainMode,TRADES);
DataSplit = 85;
NumWFOCycles = 10;
NumCores = 4;
// 样本
StartDate = 20210101;
EndDate = 20251230;
LookBack = 5000; // 至少需要2000根15分钟k线才能生成日图信号
// 设置资产
asset("BTCUSDT");
// 初始化设置
if(is(INITRUN)) {
g_tfNames[0] = "M15";
g_tfNames[1] = "M30";
g_tfNames[2] = "H1";
g_tfNames[3] = "H4";
g_tfNames[4] = "D1";
}
// --- 多周期循环逻辑 ---
int i;
for(i = 0; i < NUM_TFS; i++)
{
// 计算当前周期相对于 BarPeriod (15m) 的倍数
// M15=1, M30=2, H1=4, H4=16, D1=96
int frame_multiplier = g_tfPeriods[i] / BarPeriod;
// 切换时间帧
TimeFrame = frameSync(frame_multiplier);
// 在当前 TimeFrame 下运行逻辑
if(TimeFrame) {
update_patterns(i);
predict_next_bar(i);
}
}
// 恢复到主周期 (M15)
TimeFrame = 1;
// 核心参数
var score_threshold = 4;
// 计算共识分
var consensus_score = 0;
for(i = 0; i < NUM_TFS; i++) {
// 过滤掉置信度太低的信号
if(g_predictions[i].confidence > 70) {
if(g_predictions[i].is_bullish)
consensus_score += 1;
else
consensus_score -= 1;
}
}
// 信号
bool LongEntry = consensus_score >= score_threshold;
// bool LongEntry = consensus_score >= score_threshold && g_predictions[4].raw_weighted_change > 0;
bool LongExit = consensus_score <= 0;
bool ShortEntry = consensus_score <= -score_threshold;
// bool ShortEntry = consensus_score <= -score_threshold && g_predictions[4].raw_weighted_change < 0;
bool ShortExit = consensus_score >= 0;
// 订单
Lots = 100;
if(NumOpenLong > 0 && LongExit)
exitLong();
if(NumOpenShort > 0 && ShortExit)
exitShort();
if(NumOpenLong == 0 && LongEntry)
enterLong();
if(NumOpenShort == 0 && ShortEntry)
enterShort();
// 绘图
plot("Score", consensus_score, NEW|BARS, BLUE+TRANSP);
plot("Score+", score_threshold, 0, GREEN);
plot("Score-", -score_threshold, 0, RED);
}
|