私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?. e1 p) c; Q2 g
在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。
3 d7 m1 q$ b: c我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进# j" L# B6 F. v" S' }: l
//|                                             https://www.mql5.com |/ U/ Y$ L! D0 ^. J& ]
//+------------------------------------------------------------------+
  M4 X6 P* I0 O$ Y//--- price movement prediction0 J% b+ X( K; d) `
#define PRICE_UP   0
9 d$ r) A' Y9 @% N  S! u+ X#define PRICE_SAME 1
+ I' c' O+ U4 u1 j6 J; m' {#define PRICE_DOWN 2
4 C( s# D9 D' M4 T* B. w0 |+ G- @//+------------------------------------------------------------------+
' g* b! c" j& d/ m' n4 |. ^) Y//| Base class for models based on trained symbol and period         |" R5 {" z+ D: b
//+------------------------------------------------------------------+. `* w3 x* ]* d# C, I
class CModelSymbolPeriod2 W4 D+ S' t4 }2 Y: t6 E
{5 L( T# T& N: W9 F2 O) ~
protected:$ `2 ^0 ^: {, Y# _
long              m_handle;           // created model session handle! S5 {+ `9 Y: G) P' b
string            m_symbol;           // symbol of trained data
# u1 X* \* W; hENUM_TIMEFRAMES   m_period;           // timeframe of trained data
) S7 f* v9 T+ Z2 M( y4 jdatetime          m_next_bar;         // time of next bar (we work at bar begin only); X7 ?* c, u8 h' ~) v6 b
double            m_class_delta;      // delta to recognize "price the same" in regression models
- q' q- [+ {* F: S8 opublic:
. m9 g% T$ O& k  w- q% A# q//+------------------------------------------------------------------+; h) o2 A/ [7 R, S. l; Y
//| Constructor                                                      |7 I4 x% F) E3 ?# I" l
//+------------------------------------------------------------------+
! @7 ~* f0 z' P& ~2 \CModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)
+ b4 Q7 K4 I" ~{. V) _% w& g9 [
m_handle=INVALID_HANDLE;) U7 P; x5 U1 O( ]. n9 @
m_symbol=symbol;
! [+ t5 m- J& g) Q# D6 Km_period=period;
* d- t, A$ L. v, bm_next_bar=0;
% B7 X/ A; B8 G- p# n2 x. f2 km_class_delta=class_delta;$ B% \1 W( m7 y( {4 P
}9 e6 u  X( B% R3 |( J7 Z6 x% E5 \
//+------------------------------------------------------------------+
( M. A& q: D& W2 C2 h. {  K; p, o//| Destructor                                                       |/ {5 }  ~5 v0 D: u# F
//| Check for initialization, create model                           |0 T+ U' f& P" j1 n& T' o; i
//+------------------------------------------------------------------++ z6 A+ x; G  ?3 }5 [
bool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])) z, `8 ^* t' c$ c' X. S
{- i# F# e* u; J" T0 }8 ~0 F
//--- check symbol, period
. a, q6 t. S% Jif(symbol!=m_symbol || period!=m_period)
- x0 g3 ~' o3 J/ ~3 E& C{
2 ~; d" B0 _; B8 Z" J0 g, L7 ]$ M, PPrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));6 @" N5 x3 r, P. F& a$ \5 X
return(false);) A0 a; B5 I6 s( `2 {
}
. q# Z8 E  Z% x0 u, F& B7 U//--- create a model from static buffer$ A: l$ \! Z( Q+ P4 v3 I) d0 i
m_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);0 J: V- F% H) \$ N) @3 X
if(m_handle==INVALID_HANDLE), K0 L: m5 f5 D% a- h6 q2 M
{; K( [  `( s9 t" A" j$ F1 S+ O5 U& A
Print("OnnxCreateFromBuffer error ",GetLastError());
% w5 b3 M  f9 @3 w% U0 S# R  Ureturn(false);: D1 G& w- a2 {
}
; j5 m! E# E5 m% [# J" H+ J) U' i//--- ok$ S1 ?' n0 O) y2 [
return(true);: {, g+ G7 y1 b# C1 H; @+ S
}4 p# P/ P# d2 K6 ^) ]% \9 a
//+------------------------------------------------------------------+. n' i+ _6 o; m! p9 [
m_next_bar=TimeCurrent();
: i; s8 Q2 F& N. w& b1 j) |m_next_bar-=m_next_bar%PeriodSeconds(m_period);
) Y# l6 s& J) A* v- im_next_bar+=PeriodSeconds(m_period);. B  D( w1 M& O
//--- work on new day bar
: ?9 o% m- s  ~$ O: Kreturn(true);
, T+ h7 \# b( p. L! [+ _}: [6 w& T0 e$ I
//+------------------------------------------------------------------+
( t$ C) V% L) l- L5 g- Z: T5 d//| virtual stub for PredictPrice (regression model)                 |
  F, e! k* v1 a8 I# [5 \//+------------------------------------------------------------------+
4 [8 b, N4 c$ w& x8 Zvirtual double PredictPrice(void)
$ C' u7 p: s/ l4 @* B{
: a1 W: f4 E$ W# O. u" Nreturn(DBL_MAX);
: S; B6 x9 U! }. ^4 l}. V/ C, H) d7 P- ?' Y7 l/ n
//+------------------------------------------------------------------+
% u+ G+ ?9 C, W- A//| Predict class (regression -> classification)                     |
+ T# p/ Q( v8 s. S//+------------------------------------------------------------------+! D0 c  I4 o9 C5 c. ]
virtual int PredictClass(void)( y, h' W! |2 B
{
" F7 }) ^# w7 K8 i: }( b$ Ydouble predicted_price=PredictPrice();
1 V! p, }5 |, R5 y3 vif(predicted_price==DBL_MAX)/ B" G6 s& W+ ~9 E
return(-1);
% }8 s% ]. d1 ]  ?+ g) G1 Tint    predicted_class=-1;
) K5 ]% w2 J, f# v1 `7 a' rdouble last_close=iClose(m_symbol,m_period,1);6 P" n% E& m  f+ `9 A$ b
//--- classify predicted price movement
. k* R( v. [& R: z# [3 [double delta=last_close-predicted_price;! }& B. D8 }% L
if(fabs(delta)<=m_class_delta)
+ g& _. O& Q6 \& \$ n+ t+ @( Hpredicted_class=PRICE_SAME;% [  X; ]& z  M: |
else' ?- y" T+ |5 Y+ R" S0 j) ^; {) q
private:
# L8 K7 l( I9 p/ f4 nint               m_sample_size;
/ J  d3 x: w: \+ Q2 M; ^# @//+------------------------------------------------------------------+: W5 a' w! Q2 I5 ~3 |/ Y; p. s- c0 V
virtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)
  E+ h: u& L5 t" H: z{" `' J0 r3 _. }
//--- check symbol, period, create model" N% m) N% _5 _6 z  J
if(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class))
  D$ S/ T9 C7 U0 W4 N( m7 }9 a{
7 N# {* c. d- ~2 t! x/ m0 lPrint("model_eurusd_D1_10_class : initialization error");0 q) j8 @- L; J6 U6 T( O: s7 M
return(false);3 c5 c! N( V/ P% q
}7 _6 E6 E; J% O5 M
//--- since not all sizes defined in the input tensor we must set them explicitly
! [- L: c9 A. M$ X* S, ?; E//--- first index - batch size, second index - series size, third index - number of series (OHLC)
4 q/ P( t( k$ Dconst long input_shape[] = {1,m_sample_size,4};8 G2 x! ]* D- a9 w+ l% {$ n1 F
if(!OnnxSetInputShape(m_handle,0,input_shape)), h5 _5 G2 m( A& M
{# a7 e6 D  Q5 F+ p
Print("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());1 H/ p6 l" Z( @  i$ l
return(false);$ ~% a0 ~5 Q4 A6 S& j
}; z6 ]# r2 N; }8 l) K" a. b
//--- since not all sizes defined in the output tensor we must set them explicitly
5 S: b0 i  W2 W//--- first index - batch size, must match the batch size of the input tensor
$ A( J& m& N2 g0 [' }& z4 [" N# v& ~//--- second index - number of classes (up, same or down)6 F9 S3 [5 ~0 o( m( |# d
const long output_shape[] = {1,3};4 X( Z8 x" \1 b, F, x  Q
if(!OnnxSetOutputShape(m_handle,0,output_shape))
$ n# z: p# C  P* m- t  d8 `. c{
- O( V( U5 Q; \7 u; Y* qPrint("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());
0 E5 ?2 S( q0 [* Wreturn(false);0 r: Q2 X8 d. V' U2 A" W4 g( ]" w
}% p$ U& u% y! C' p
//--- ok
: m. {& K8 B' o; b/ F) qreturn(true);5 M% ]" q, c4 [. q2 M8 x
}, R5 E( X3 e: P5 x9 s0 V
//+------------------------------------------------------------------+# Y8 s; o1 q2 A" y, C2 U4 U( A
//| Predict class                                                    |
: {4 I- s: \3 I. l5 A  K; Y+ D//+------------------------------------------------------------------+
2 x  d9 ~/ X8 L) O& X6 ]) P7 Bvirtual int PredictClass(void)
+ I1 F  i& V( r1 j! [( r+ X& b{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-5 21:36 , Processed in 0.547450 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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