私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?; O8 g# V. X3 N& y3 X8 G$ B
在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。0 g6 @. y7 @9 e: w- G
我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进
% e( Q- ?) d& Q# u( y1 W- _& Q( a//|                                             https://www.mql5.com |
( [  z! b% ?: O//+------------------------------------------------------------------+
4 r% Y6 q0 C1 E1 M2 [% u//--- price movement prediction
$ A) Q1 |; X6 `) E) C1 K#define PRICE_UP   0
' ^2 m7 \" y, z1 Z3 f) r- E#define PRICE_SAME 1% ?$ z! D0 |7 S
#define PRICE_DOWN 2$ i7 D) H, K9 S' k- l. r1 Y* D
//+------------------------------------------------------------------+1 q+ K$ q8 X$ f+ r7 [
//| Base class for models based on trained symbol and period         |
9 E( S! R" E. m; i//+------------------------------------------------------------------+
8 @+ r& r% V" f* N% i, t& y9 yclass CModelSymbolPeriod
% _. `% p* T5 P9 T. p{
, g4 z- X" ?4 K. Z) F8 q4 _1 a5 Eprotected:
9 I4 d. q7 n( ]long              m_handle;           // created model session handle3 Q6 `1 L% C) h8 v/ Z; J/ @$ l
string            m_symbol;           // symbol of trained data& r* h; A" @; e: Z8 u+ j
ENUM_TIMEFRAMES   m_period;           // timeframe of trained data
! v% `" _. B1 D6 z) f: E4 fdatetime          m_next_bar;         // time of next bar (we work at bar begin only), F$ B  }1 e+ ~: u) m
double            m_class_delta;      // delta to recognize "price the same" in regression models5 d, \  ^3 f$ u5 O3 s8 v
public:
! a4 L- H% l9 o) j% z//+------------------------------------------------------------------+
  c5 k# G, K8 D# P//| Constructor                                                      |7 q( N, q2 q. U5 m
//+------------------------------------------------------------------+/ l1 d1 s* j: }3 x/ o+ t1 b
CModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)$ [* N4 \! c$ z# L: g0 T1 T7 j
{6 r: j/ |: F3 f  \, c+ @0 M+ O8 b0 V1 n
m_handle=INVALID_HANDLE;+ w0 Z% |3 Z+ B
m_symbol=symbol;
; _6 @5 |& w! D) B& ]' H! ~m_period=period;9 d7 }0 E# s2 F0 o% |0 c8 Y3 P
m_next_bar=0;
" m9 H, B$ @7 w' i4 am_class_delta=class_delta;
% b4 v# o9 J/ K5 b; h}3 Y* @4 t' R( ]
//+------------------------------------------------------------------+
7 t/ C, i2 A% e6 \//| Destructor                                                       |
. k: h0 B. u- t, i5 X//| Check for initialization, create model                           |& ?" P4 ?4 L. C; }6 J* m
//+------------------------------------------------------------------+% X9 d9 b% _/ t" \7 q
bool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])
8 g& X' X, V8 C% v6 R% B{) c2 k( e1 B7 k: r3 w
//--- check symbol, period
6 \- P% y2 Q% e- K/ ]3 N! Vif(symbol!=m_symbol || period!=m_period)
/ v: H% p# q" G8 {! @; i/ j0 h{
0 |$ S2 \2 S+ s6 r+ D& ^- NPrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));9 y0 u/ I! X* W7 Y; ~; F4 c
return(false);. K  w( v: m& i4 y- g
}& ~5 g% b: [% M8 y. w
//--- create a model from static buffer
" a+ w  `( e! \m_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);
! ~) z% ?) E; D$ i+ eif(m_handle==INVALID_HANDLE)
+ E. D2 b2 U, I5 k1 G' @{
, R$ E1 V& L9 `7 \, }2 g( `Print("OnnxCreateFromBuffer error ",GetLastError());
+ \' O$ F, k0 _+ i, d5 Creturn(false);9 [" A! w9 T8 D' x" B
}8 i: G7 n0 ~- P; K0 W4 V7 @
//--- ok
* J; P) K5 U4 lreturn(true);
2 b% I7 `0 A; W8 a5 z}0 [0 G0 O1 m) i' c# j1 R3 k
//+------------------------------------------------------------------+
- H; E% k! R# |3 r9 |8 Lm_next_bar=TimeCurrent();
& f2 w' d# ?) k$ I9 Z, L- Z( R( }m_next_bar-=m_next_bar%PeriodSeconds(m_period);
. ^0 s* X6 p( Nm_next_bar+=PeriodSeconds(m_period);
$ v& `+ W! @3 r8 m! C; ~  B$ c3 w//--- work on new day bar% G* {& p; E0 Z
return(true);
+ Z: m) j/ X1 ~- M, D}) f$ d* @+ T# v
//+------------------------------------------------------------------+
/ f* o+ i7 |$ w* Q& k+ g/ W* B  W//| virtual stub for PredictPrice (regression model)                 |" |& L% k- T+ X6 O
//+------------------------------------------------------------------+
6 J5 E. ?( _0 q" E' ^0 E! kvirtual double PredictPrice(void)# N& T  C: X2 F( i% ?! U) T% a
{
; f* ]6 [* R. j; {2 o' {return(DBL_MAX);
5 W8 L/ d3 @! R) b& S7 _) G- i}
* A$ t. ]$ K6 T$ j7 u//+------------------------------------------------------------------+0 h& p. P, Z$ f, ]1 t/ G
//| Predict class (regression -> classification)                     |
2 T% Q! ?8 K0 d$ H5 r//+------------------------------------------------------------------+: [/ k" b( j( k) {8 j6 ?
virtual int PredictClass(void)
  G' G7 G- K8 u% M{
! J( o! H; ~$ n" ~- R- @double predicted_price=PredictPrice();6 `4 N1 z0 O1 X/ A9 U1 O
if(predicted_price==DBL_MAX)
; M9 f7 X1 _& ureturn(-1);( {  @4 T$ Z' N
int    predicted_class=-1;  N" G- z" ?( S
double last_close=iClose(m_symbol,m_period,1);
6 e. O6 n# T5 e+ G# I//--- classify predicted price movement
& e6 g3 J8 r, P9 kdouble delta=last_close-predicted_price;
! p# w# g# @1 \if(fabs(delta)<=m_class_delta)
3 o+ J+ {6 `$ i. z5 }5 Ypredicted_class=PRICE_SAME;9 T- b( r2 _& B: n4 l' k# F* f! ]
else
8 h5 I/ \' }, ]) p% d/ ]3 B/ ]private:
, ^$ ]5 b' D9 X' [* W/ L/ Qint               m_sample_size;6 j8 C- I6 k2 G! s' a
//+------------------------------------------------------------------+. S6 {5 X1 p3 i8 j, e) Y- I4 ^
virtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)
* H) h) A- C1 d{6 P' H0 |: x' M
//--- check symbol, period, create model
. W  ], H" r' i+ Vif(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class))! \, c& @- D1 l% s: i
{; k% R4 P; i3 z- H* J4 Z$ G) ^2 I
Print("model_eurusd_D1_10_class : initialization error");
7 I' ~/ D) t% a  Hreturn(false);
% T9 {! L( [; D% c  C0 F2 E}
& Z" _' F9 R" b% x& ^! \! P% r//--- since not all sizes defined in the input tensor we must set them explicitly! J7 D. e8 G# T! @: V1 N' T
//--- first index - batch size, second index - series size, third index - number of series (OHLC)" g* \4 t; c( C
const long input_shape[] = {1,m_sample_size,4};
' l6 x9 ^3 j- E) l; Iif(!OnnxSetInputShape(m_handle,0,input_shape))6 J8 ~5 D3 v7 e2 N
{
/ \  l4 K2 W! _" _% f: x3 XPrint("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());+ T' K! P/ ~# x/ Z( @
return(false);7 o& N4 k* ]6 z" e) E6 x. n2 @
}
0 [; i9 u. ?5 Y1 _//--- since not all sizes defined in the output tensor we must set them explicitly) M8 ?# g% s4 w2 R
//--- first index - batch size, must match the batch size of the input tensor* D+ m/ ?% T2 E& a8 S. ~. B
//--- second index - number of classes (up, same or down)
. x8 v5 |! O' d9 I$ k5 V& p0 v1 j, wconst long output_shape[] = {1,3};
6 t( m  T& ?$ N8 }7 L( M; Pif(!OnnxSetOutputShape(m_handle,0,output_shape))( J: x% N4 u  @$ l! j
{
, m- m! n) Z7 k9 \" kPrint("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());
0 H+ n5 {6 E+ L2 p3 g6 `3 @return(false);* W; d6 W- h9 ~3 w/ f
}
2 }+ \9 N) U, S5 G2 @( x* c//--- ok
5 W; E  ]0 j. l* ]% p- hreturn(true);
) x: `. x( P  y/ u4 {& H/ H}; |% h6 H' M7 u5 {4 R* Z+ \- L
//+------------------------------------------------------------------+
7 ^* b! e: D, h, N//| Predict class                                                    |
. P& s. L/ w) W: O# ^4 N: u# w//+------------------------------------------------------------------+
1 q5 E: q! q6 A) [/ b0 Bvirtual int PredictClass(void)
$ c/ \+ u  [4 ]* Z/ Q{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-19 10:22 , Processed in 0.418542 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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