私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?
8 r4 q- V& A! U+ C' b$ j在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。- w& A7 o% d' k
我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进
. F# _' a+ O1 t3 Y, O//|                                             https://www.mql5.com |
/ X* q9 {) F7 e% ~7 O//+------------------------------------------------------------------+6 W5 S, o3 C5 [) ?7 j3 l7 L
//--- price movement prediction
0 w1 i. H1 U. Z& W' C0 X& ~#define PRICE_UP   0$ L5 d8 [# S2 y6 ~4 _0 R
#define PRICE_SAME 15 |1 Y! W0 a7 _- N
#define PRICE_DOWN 2+ e4 z2 y4 h; W% m
//+------------------------------------------------------------------+
3 K2 Z3 k. O. V( S//| Base class for models based on trained symbol and period         |  r. y6 i" L; C6 \
//+------------------------------------------------------------------+* X/ k3 T3 O; ^( I4 |: f
class CModelSymbolPeriod: a% J8 P. w- Q, H  W) {
{
1 n: Z) u' `7 U4 r; M' v: Jprotected:4 _( p- d! A: u! a
long              m_handle;           // created model session handle' L: ?) O: r, Z! F
string            m_symbol;           // symbol of trained data
3 R- t7 D# e* x( b  u2 [4 BENUM_TIMEFRAMES   m_period;           // timeframe of trained data  \9 p! ~! m% A( B8 s, \) G
datetime          m_next_bar;         // time of next bar (we work at bar begin only)6 X1 y  a% b+ E
double            m_class_delta;      // delta to recognize "price the same" in regression models
2 M  b- ^* w2 P, o; j6 p5 tpublic:$ v- {" ]- R# w- ~( ~
//+------------------------------------------------------------------+
5 Y. @6 |; M& p! c4 _  ^6 F' |  j//| Constructor                                                      |$ O- g- P9 ?$ Q% B% _1 n
//+------------------------------------------------------------------+: y. w! P5 E2 G+ M% d3 ]% F6 p
CModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)
- U) m7 t0 [3 p! B9 i. ~. @{, i7 a% O3 r6 I8 ^& Z1 O
m_handle=INVALID_HANDLE;
& t8 g% q4 D1 `* X0 Z, dm_symbol=symbol;- y2 h; h8 B% K* y/ S
m_period=period;$ _2 n1 P) Q8 v% H5 D; I
m_next_bar=0;9 X6 g  `7 F! c+ W) F
m_class_delta=class_delta;
7 v; I4 A4 K8 W9 s% ~0 @" v  \}- F' {- u, [3 X; t* a
//+------------------------------------------------------------------+
) s0 W) r8 Z& Z7 y3 L2 p//| Destructor                                                       |1 r( f5 [+ I: g0 M6 _* Y1 V  {$ K
//| Check for initialization, create model                           |8 I( m( Z4 Q! i+ `# X
//+------------------------------------------------------------------+
- w) p* Z7 n9 Dbool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])
7 X6 k6 d1 _" }{& z- B6 _- T! z/ M9 y# n) P
//--- check symbol, period9 q1 _2 X; H8 F! h$ g  m) z
if(symbol!=m_symbol || period!=m_period)
+ Y/ Y6 J7 O* B) N4 j{/ |5 T* X# d1 U: p
PrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));# u- |! [: S' Q. _% Q6 Y" ]
return(false);
1 |  C+ h7 \' ]# Z}7 f* D, J) G7 R
//--- create a model from static buffer
) q) Y+ E7 v. M; ]7 F9 o$ r, Im_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);& v. K" B+ P$ Q& s! s0 \( a
if(m_handle==INVALID_HANDLE)0 a' N8 m" h$ G. }
{1 a" V( @6 C) x2 u
Print("OnnxCreateFromBuffer error ",GetLastError());
0 W* e( ^( i: Y+ f. g5 B6 Greturn(false);) y: C4 o, _, o  i4 G
}
) O/ z6 u# l  @4 _$ ~+ C; E//--- ok1 j1 W* y7 r, `- C; e
return(true);
: H- q3 B6 c# s/ J) P; b3 X. w$ w4 {}
. g4 |, J5 x0 T; z: A! ?//+------------------------------------------------------------------+  n% R* I% A9 n. b
m_next_bar=TimeCurrent();
1 [' }' r2 D5 w# O. G5 F" v  Zm_next_bar-=m_next_bar%PeriodSeconds(m_period);
& \, O+ M7 m! q( Xm_next_bar+=PeriodSeconds(m_period);
. o* |+ F8 _( J; _$ y//--- work on new day bar
- C7 c* J  t4 Preturn(true);
2 k9 a2 R: B- ^" A}- i2 ?- T0 @; z! U6 ?% ?
//+------------------------------------------------------------------+$ g; s8 x2 z8 p/ g- Q$ f) g5 C
//| virtual stub for PredictPrice (regression model)                 |9 }8 q" U! d! u% l3 v% [  A
//+------------------------------------------------------------------+
/ \, S; Q/ ?! B1 Z2 `( L4 Hvirtual double PredictPrice(void)
) @8 q" X$ c' g0 ^- H1 A, M" f) q{3 x9 ^' D$ H+ ~( _; H7 `9 T
return(DBL_MAX);' Z# y0 B6 T3 G! u# {
}+ y" x0 g5 n0 G( ?' p' k$ w2 Y
//+------------------------------------------------------------------+" j& H9 u$ B1 g' _* D/ k
//| Predict class (regression -> classification)                     |1 z3 j) @* l7 ~* v! _
//+------------------------------------------------------------------+
1 l0 R4 i0 @# E  I& rvirtual int PredictClass(void)
7 }% e+ v- o1 q+ q' \{: {' a3 v. i5 {$ k3 r: H
double predicted_price=PredictPrice();% y9 ?, j1 b1 @" I( \3 E# N  }
if(predicted_price==DBL_MAX)1 u7 X; C' W+ N7 u  @
return(-1);' o+ G! v! x; d% k1 F$ F- f% z
int    predicted_class=-1;% l9 S9 O7 C( P% R3 B$ H# r
double last_close=iClose(m_symbol,m_period,1);6 U' J' S- J5 Z4 }& ~+ J9 X
//--- classify predicted price movement
: l/ D/ I/ }3 s. K$ q! bdouble delta=last_close-predicted_price;
# a; R( v  H8 L* o6 z% S: vif(fabs(delta)<=m_class_delta)
& r* u7 i. @! x/ P6 d! V. w% qpredicted_class=PRICE_SAME;6 |, H% [$ F; [6 Z5 {' P+ C' l( @
else
1 n! P( ], h* P& ]" Pprivate:
6 G% s* l( s$ {* _! \int               m_sample_size;
+ E6 }: j, Y* g( s. a$ z. o//+------------------------------------------------------------------+9 w2 i! D0 A! }% ]2 S$ |$ k
virtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)
. a# x  Q. z4 a# ?6 j& D2 \1 _{4 Z( E/ N, ?1 A) w
//--- check symbol, period, create model
" n. G9 {" g' _2 \6 Hif(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class))
7 z  h) ~# U: d( l/ o4 r' n{5 }- h2 V# \5 v3 s6 O
Print("model_eurusd_D1_10_class : initialization error");
* k7 O& r5 o. e" M; Z/ _+ Y( f1 dreturn(false);4 }2 |+ o( M0 l! e
}6 R: n% U6 ~& T  n- G+ E7 C. J
//--- since not all sizes defined in the input tensor we must set them explicitly% J% N  z' q  W# j
//--- first index - batch size, second index - series size, third index - number of series (OHLC)
: N1 J" ]" u1 ?1 E* o6 l; iconst long input_shape[] = {1,m_sample_size,4};
/ W" u* `/ _' U" V7 S; t8 uif(!OnnxSetInputShape(m_handle,0,input_shape))5 N0 B& y- S4 Y. {' D8 G
{( ?" f# v4 ]( }+ l
Print("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());
* d9 O. M  K  u: K5 D3 x1 Lreturn(false);
- }0 ?: @1 A8 e2 b' J; `}. {- H5 }( b: |: f
//--- since not all sizes defined in the output tensor we must set them explicitly* b" Q' Q" c, @2 M; E
//--- first index - batch size, must match the batch size of the input tensor
* l' `- e: Y* U0 E6 t& Z//--- second index - number of classes (up, same or down)4 ]3 a) _3 b: H& i0 v. y
const long output_shape[] = {1,3};
2 M. D- \& m5 J! m$ ?if(!OnnxSetOutputShape(m_handle,0,output_shape))  V9 `4 N( `( u" _5 c, j9 `- g
{: L$ I" C# J+ R9 ~
Print("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());
7 y+ J' j7 S" H. L& W5 a& Preturn(false);* d/ |+ `  O4 ?5 K7 k4 L6 l& b
}$ h& u; K' c* S* |
//--- ok+ x' U  u/ ]) M% }2 y1 g  i
return(true);; x3 s* O4 f% q1 |
}0 t) j* C! [  ?9 @9 @
//+------------------------------------------------------------------+3 \8 c+ r4 _" h4 m
//| Predict class                                                    |
- U) N1 j$ U/ ^% V+ I% Q  T//+------------------------------------------------------------------+
5 D0 |7 a* l! b( Qvirtual int PredictClass(void)* B" m/ Y. O% {- V" F) J4 h1 ^+ j. j
{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-3 23:23 , Processed in 2.920717 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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