私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?
1 E. P3 p8 x5 K" o. f, w0 u在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。; h1 G, k* h/ `( t" a1 y7 u
我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进
' n* o3 D: y' o. o+ X, m# X//|                                             https://www.mql5.com |
" m6 R$ h3 ]. q: `) u5 W8 m. l//+------------------------------------------------------------------+
/ w' k: C1 A9 ^  L* w//--- price movement prediction
" ^0 N4 W/ q# v% y7 z#define PRICE_UP   0
4 G0 s; v& b; i" b1 I1 `5 s. Q#define PRICE_SAME 1
; U4 }2 ~% a$ G1 _#define PRICE_DOWN 2' I* u& t* X, [: u9 Q+ [
//+------------------------------------------------------------------+
$ w5 ^0 \/ F: j. g& U//| Base class for models based on trained symbol and period         |6 X# e* s8 x6 A- r! y/ t. B0 z0 Z; p
//+------------------------------------------------------------------+
; |+ Y1 A2 V7 g) p, ~  Oclass CModelSymbolPeriod6 t3 y& e. B9 p+ w5 t5 S  T
{+ }( y" @' Z: s0 q/ Q! ]' e
protected:
* ]* b4 ~- u7 a! u) ]- b9 v1 b7 Dlong              m_handle;           // created model session handle
$ {# l' h' _: f' Fstring            m_symbol;           // symbol of trained data
$ |3 I# m0 R6 D8 G( x- K% `, tENUM_TIMEFRAMES   m_period;           // timeframe of trained data! l+ q% `. r) j" ^$ [$ ]
datetime          m_next_bar;         // time of next bar (we work at bar begin only)
6 D. C* b2 w" ~6 W  a! u* \double            m_class_delta;      // delta to recognize "price the same" in regression models
: l, m& K6 n- B1 m& Z/ R, A* J7 \  Lpublic:
' Z# W5 b  @( f9 h1 f; ]//+------------------------------------------------------------------+" A9 i/ r0 y( H8 }+ e+ \
//| Constructor                                                      |1 M& S# y  b: e/ u2 Q( a
//+------------------------------------------------------------------+
3 @) k5 s. u" K# s( BCModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)  Y1 V% `4 I  P! k6 ~6 I: F
{
6 {/ Q  |7 G; ~  i2 M6 im_handle=INVALID_HANDLE;/ c4 Q3 U' ~8 h- D' M& [
m_symbol=symbol;
. u! L5 ~: r1 ym_period=period;
1 _, w% L2 _1 \! v3 X4 d/ xm_next_bar=0;
& Z* @+ l* C7 D) t$ F' ^9 w9 Nm_class_delta=class_delta;0 K7 r) l5 I; _- G" n; i9 O
}
0 Q- w7 O: x3 ~! G//+------------------------------------------------------------------+
, G% p% y) f; W- ]/ Z8 f//| Destructor                                                       |
0 {% C  i5 S% O& }2 p//| Check for initialization, create model                           |
' _) c  [9 d# q1 R4 p6 |, ^+ T//+------------------------------------------------------------------++ B& L3 J/ }' q1 G0 c% U% L
bool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])' J4 w9 T9 O5 _6 u7 V4 A6 ]( _
{6 y) T- G$ t/ }6 b& g  V4 j2 P
//--- check symbol, period1 g: T7 G0 U" j4 v8 }! }6 v; j
if(symbol!=m_symbol || period!=m_period): k: \, Y1 ^2 z0 T' A) o0 Y
{) k- c0 l# Z7 z7 v4 D
PrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));
/ }* G8 m  ]) h( a, y; |return(false);
& b( p$ Z$ M6 X7 a$ z( G8 R) Q7 S2 v3 D; N}
- S4 a: O% c# j, I. ~//--- create a model from static buffer
4 U# k' }. f# z" O1 |  A+ v$ Mm_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);
/ ?% V9 n1 G; h' J! i( P- |) tif(m_handle==INVALID_HANDLE)- ?( ]/ M% [; ~  P7 m- E
{
3 w' p3 I7 _" E( V# X  w- K) z! vPrint("OnnxCreateFromBuffer error ",GetLastError());
  h( o) C4 r: ]5 d2 W4 |8 f. o* t" @return(false);
$ y6 a7 F; Z( \$ u}. w: V6 y, j+ X; c4 Z) X- c! ?; w
//--- ok
1 R' I' A1 G( |( J8 U) h6 mreturn(true);
! _* I% R: |8 x}+ d. R- u$ L% c. {
//+------------------------------------------------------------------+
) Y$ y$ H& F  ]0 am_next_bar=TimeCurrent();
# D% U1 i9 `1 Z& x' T( Jm_next_bar-=m_next_bar%PeriodSeconds(m_period);; Z1 L) L% M/ x3 ~
m_next_bar+=PeriodSeconds(m_period);
- t- d7 O  c& ^( \6 Q//--- work on new day bar
, h5 {2 s! k" C# }6 h" ^return(true);
$ U/ T; w/ @) D% |6 ?}
, a. ?. R+ l# {  v. M5 K$ E  x//+------------------------------------------------------------------+" c  |" ~7 K# e; [9 ]
//| virtual stub for PredictPrice (regression model)                 |/ r& S6 M$ ], S+ p; I& X) n( X
//+------------------------------------------------------------------+" v& D0 ^' E! u. S, A' W+ e
virtual double PredictPrice(void)0 b' T; e) t" J* F4 B, Q
{; w7 S* ^0 e% l: T& x
return(DBL_MAX);
( O) Q+ U% d( s" U}
0 S2 y$ C5 {5 K* p! Y$ B7 I//+------------------------------------------------------------------+
) m% o& H' F. T//| Predict class (regression -> classification)                     |
0 V5 O( a- q; L  n$ F//+------------------------------------------------------------------+& ~  @( M* y5 ]1 E3 t& f( c
virtual int PredictClass(void)' g2 u, [- G+ J
{+ P' u# k  V5 h; _2 Z0 P& W5 h
double predicted_price=PredictPrice();
  G1 ^1 [9 W7 k' E. Cif(predicted_price==DBL_MAX)
- [( ~  o! ?0 x4 }5 v: Y' W1 t) sreturn(-1);8 m  q1 c  i' w" }
int    predicted_class=-1;' e% `" p# I0 F2 P4 Z( e- \) s
double last_close=iClose(m_symbol,m_period,1);
  A: `4 |: m* o5 d. K//--- classify predicted price movement+ E+ ~# L& }' [0 k
double delta=last_close-predicted_price;0 ~; k! M. }- B
if(fabs(delta)<=m_class_delta)
5 {  E  q* f- T1 R0 k- Zpredicted_class=PRICE_SAME;* B/ M; C9 b+ p8 d/ v% ]
else
5 s0 Q7 `7 R/ f* o/ q& c, ?private:( B9 m- @' X$ f
int               m_sample_size;
+ F0 [) z! y% Y. p4 A8 s4 }2 g//+------------------------------------------------------------------+
6 q: l; n* A4 i; T" m* E% }( mvirtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)
( X0 _( E+ j* a6 ?{' M& b  [# W& T+ N' m0 J0 s
//--- check symbol, period, create model  X0 z/ k4 _3 m' S& C
if(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class))/ y7 B! B( l" V. c9 W- R
{
) f! @1 ?0 Y  H7 N& sPrint("model_eurusd_D1_10_class : initialization error");- W1 c+ O3 i: x* F; n
return(false);  f  J2 v! ]. ^. }. b+ }6 ~
}( v# s& y% \) R8 [0 C
//--- since not all sizes defined in the input tensor we must set them explicitly
5 ^! }! B& e$ m$ {% r% @  Q8 {//--- first index - batch size, second index - series size, third index - number of series (OHLC)
) H2 v& q! ]" T) ?# ~const long input_shape[] = {1,m_sample_size,4};
$ _% [1 `1 U, }- fif(!OnnxSetInputShape(m_handle,0,input_shape))
5 C0 ]2 ], T' l8 n- n. Q{
' P( r; k" Y. p9 n5 W, APrint("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());3 V! w3 j) A8 r! M9 h
return(false);
1 f: f( c/ m* k. f  b) U}
  P3 X- {! J; T9 F4 ]0 F//--- since not all sizes defined in the output tensor we must set them explicitly+ S4 [/ Q4 {6 K# B% a" n! d& ?) j& \
//--- first index - batch size, must match the batch size of the input tensor# i. @$ t2 t4 p3 x
//--- second index - number of classes (up, same or down)
. k9 p. ^, H3 G- o( ]3 Rconst long output_shape[] = {1,3};5 ~. A" o# M0 U: C
if(!OnnxSetOutputShape(m_handle,0,output_shape))
; ?; t: B3 E1 K: O' A5 \3 Z{
/ e; C! b! o0 Y% aPrint("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());" L3 _& _" r+ ]
return(false);% V  m1 c7 W' Q) X
}
! F; e% Q: U+ ?% Y: X//--- ok
" a8 T* T$ e6 U0 O9 q! Rreturn(true);
: n( ?3 F2 ^, t& @}
& `/ \4 M. g8 H6 X) W//+------------------------------------------------------------------+
: w2 G. I: a# r) `8 s3 [- ?# B//| Predict class                                                    |, r5 n3 P1 u* I" W9 `+ M; m) o  C/ c/ z/ ?
//+------------------------------------------------------------------+3 k1 ]# _- k6 G$ y0 o; p
virtual int PredictClass(void)
% z. q. C: k* `6 ?  L0 a{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-13 04:27 , Processed in 1.236898 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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