私募

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz

期货量化软件:赫兹量化中包装 ONNX 模型

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?9 v0 n. Y( s3 m5 K
在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。, |0 _+ Y! e$ }' m- B0 b
我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进
9 q$ z  A1 h; E2 l$ y//|                                             https://www.mql5.com |
% V. K" @& @: {; {# C/ Y//+------------------------------------------------------------------+
. b& B/ d: m% z4 B//--- price movement prediction2 |* B; c7 X7 T( w8 N. y* \
#define PRICE_UP   0
: z" {5 {6 f9 v0 N#define PRICE_SAME 1
% p/ X; F! Q  W, w" }6 G3 K#define PRICE_DOWN 26 u7 M( f4 Y! ~2 l+ o* r* }
//+------------------------------------------------------------------+. ^. P7 Y0 X' n7 o
//| Base class for models based on trained symbol and period         |
, V9 `5 L4 p; u0 O# J+ J//+------------------------------------------------------------------+
. C2 P, P/ }3 S; m. Tclass CModelSymbolPeriod
& j+ n+ i/ w4 U; h# w{
0 ~( {: u. F6 |' uprotected:
% \/ r* |" p; e; ilong              m_handle;           // created model session handle
' [0 M2 A* B# h; J) T- \3 Vstring            m_symbol;           // symbol of trained data# v' W/ D; n9 V6 n3 D2 K9 _: `0 y
ENUM_TIMEFRAMES   m_period;           // timeframe of trained data
9 N3 ]( R- y9 i5 v6 ldatetime          m_next_bar;         // time of next bar (we work at bar begin only)7 J* B# B; d8 h: G( f# T, R, }. R
double            m_class_delta;      // delta to recognize "price the same" in regression models  s2 k6 J7 t+ u4 Z3 \
public:3 H$ t" G: J% s$ c( i
//+------------------------------------------------------------------+1 g( `# x$ n0 l- d7 x
//| Constructor                                                      |1 j$ P. _, B9 r, ^  X/ _
//+------------------------------------------------------------------+
/ @8 s1 E9 `) X# y' r; ]1 oCModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)# O) C8 {; ]' a% \
{
7 ]9 T5 v' O7 Z. I& ?  h0 P. Sm_handle=INVALID_HANDLE;6 U8 l: s$ T. G! D! X5 x2 j4 d1 k
m_symbol=symbol;
+ {7 C1 q2 g, G" B$ L$ Am_period=period;
/ o1 V: b  \' L8 R8 ~$ H8 Dm_next_bar=0;
' a% L7 c, ~( w) }m_class_delta=class_delta;
" F1 _' @* q# f7 [( |# f}
3 h/ v. Q, t: C! o0 T//+------------------------------------------------------------------+  [2 A2 F2 N( P1 \2 E
//| Destructor                                                       |# }% B4 @- y; w+ L
//| Check for initialization, create model                           |) c( I( x; F+ F- F- M% Q# r
//+------------------------------------------------------------------+
, N6 I( P3 C& o7 jbool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])
2 m/ h; S. D, P3 `' t7 v- m{) \  b/ {* u, L1 Q+ b- f* Q
//--- check symbol, period
3 R6 Z9 p4 _" q; ]) Iif(symbol!=m_symbol || period!=m_period)6 R4 E& G4 i3 x6 ]
{/ G5 y3 C: Q, D' J& M% R
PrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));
' ^# Q5 |1 s* O( Areturn(false);. B& F1 y4 L/ C  d# _+ I7 g/ [. s
}
; i: t% O% m5 q7 \( Z//--- create a model from static buffer" n8 \( o: {: R
m_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);
2 o$ G8 X# X; B. Gif(m_handle==INVALID_HANDLE)
8 O8 v  V) p( }: E0 `5 X7 N{
2 Z: @3 K# X0 V/ ^9 o& JPrint("OnnxCreateFromBuffer error ",GetLastError());8 m: ~6 L! u7 u8 u7 ^) s6 D* M' e
return(false);# i( I/ m" H3 B/ y" o) F4 m
}
7 u0 c+ s# R# i1 u- C& P3 q//--- ok: L+ j' a+ x; E3 t/ v
return(true);* ~2 X- E. a: I2 s" Q. y
}
/ U4 N2 n' }5 ]' s% O  e//+------------------------------------------------------------------+
. Y2 R$ r  v2 q7 _$ c5 Nm_next_bar=TimeCurrent();- }0 b6 C: x1 p  u
m_next_bar-=m_next_bar%PeriodSeconds(m_period);
# P( h% A% i0 J0 G( [m_next_bar+=PeriodSeconds(m_period);% P& O6 N1 ]# F( t- ^' j
//--- work on new day bar3 W& u8 }4 Q& s4 [
return(true);4 u$ g, J0 Z" w0 B( C  Y+ ^9 e
}
& Y! B  D0 ?* }//+------------------------------------------------------------------+1 c1 C' o; V1 @- \$ {
//| virtual stub for PredictPrice (regression model)                 |
3 A, _8 v% ?" a//+------------------------------------------------------------------+, z5 R% d+ ?7 B/ ?
virtual double PredictPrice(void)" i2 O6 A+ S4 D$ }) U; d) H2 t; ?/ Z
{& V  }+ Q% J) j: y* W6 b: x
return(DBL_MAX);/ |+ w" {! k( P' K
}4 w. m% s& k. j; s
//+------------------------------------------------------------------+! z) i. s  K3 J7 `# h+ p
//| Predict class (regression -> classification)                     |
' P- X. C" i% M" `, s# d) P2 G( Q//+------------------------------------------------------------------+
- I; Q& ~5 F1 ?4 Q, A1 Svirtual int PredictClass(void)
6 }) t8 Z5 n) o5 q( |) E{
7 U* n4 u: J" `! M& v# pdouble predicted_price=PredictPrice();  O4 j. w9 b4 X6 z% a* m& k1 O
if(predicted_price==DBL_MAX)3 w+ T- z9 Q( \* R4 b$ W
return(-1);$ u6 a- F8 _9 ]" G" _9 L; e- V& {
int    predicted_class=-1;( R  q1 U6 w: N5 O! _' D8 S0 X- C4 ]3 q
double last_close=iClose(m_symbol,m_period,1);
( _: m2 v! d% P; I6 a) |  d+ M; s//--- classify predicted price movement: b( d5 o8 Z' v6 b  w" @1 K1 ]
double delta=last_close-predicted_price;* h7 E' d- q' J  Z7 S& h
if(fabs(delta)<=m_class_delta)
/ e: r: G- z1 Q/ b8 q! ipredicted_class=PRICE_SAME;' P- b9 [: G; b% `; Z7 z
else
2 I' \5 o) q3 k% Sprivate:  {# `: k# J% w# h
int               m_sample_size;
* [( Y& ~1 _  E+ |//+------------------------------------------------------------------+: Q/ r, S* s) h% K! s
virtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)4 e+ y/ l, K9 u' ?1 l2 }
{4 I$ Q4 H2 X0 ]6 A
//--- check symbol, period, create model
( d4 i; b0 e( p8 n+ Wif(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class))/ l; C3 ~# K8 D$ x- P# }
{7 K- t6 `$ H) X9 z4 @
Print("model_eurusd_D1_10_class : initialization error");
: u: c( Y3 i2 L0 g1 @return(false);2 b# Q8 r' c# [
}
  J/ d/ @8 Q" M$ V2 U. W//--- since not all sizes defined in the input tensor we must set them explicitly
1 U4 d3 r+ o" r4 }! K* X/ `8 X( S//--- first index - batch size, second index - series size, third index - number of series (OHLC)
* v* o0 \0 G) n  Zconst long input_shape[] = {1,m_sample_size,4};
) v" c- P$ U. H: A: rif(!OnnxSetInputShape(m_handle,0,input_shape))
/ Q* y! Z+ L) s6 z{
6 e  ]2 O0 c2 V# K. A# g% C) nPrint("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());
+ l4 |8 ^/ x! i* {6 Areturn(false);* T7 R  k+ H; u
}
" N$ ], i, X% {# G# X/ E//--- since not all sizes defined in the output tensor we must set them explicitly" l& a$ i' y( ^" G
//--- first index - batch size, must match the batch size of the input tensor
# _' R* O  R6 k) R7 i//--- second index - number of classes (up, same or down)* U/ a6 @4 ]9 H
const long output_shape[] = {1,3};
. j6 c$ Q$ R, i0 Nif(!OnnxSetOutputShape(m_handle,0,output_shape))8 C, O9 ?7 g: h; I5 K! Q+ m$ q8 h
{
/ a* r  s, h4 S; B7 T2 kPrint("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());
% W" p7 _( |0 p& Mreturn(false);
5 q/ r' K& R7 a/ _}
" Q7 r. P5 ]: r7 q# \//--- ok( }6 k5 L  e8 M& M# x
return(true);; {9 f& P, ?: w( H: O3 r- u6 U
}
. N5 v! P7 @% V- `7 W& }( [//+------------------------------------------------------------------+
2 [1 p8 H1 m$ U4 m  M" q//| Predict class                                                    |9 V- c( \' m" G$ ]- X
//+------------------------------------------------------------------+
" f+ j# W; ]! K. xvirtual int PredictClass(void)
1 J/ R2 ~; u$ E9 J( W. t{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|Archiver| ( 桂ICP备12001440号-3 )|网站地图

GMT+8, 2025-6-20 03:16 , Processed in 0.394308 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表