私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?
, f) R5 c6 ]: }: e  H3 b在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。
6 Z3 P8 B+ W/ n+ ^我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进8 e- e9 ]4 v: Y: o% s( M9 r
//|                                             https://www.mql5.com |# _0 x& X" `: V
//+------------------------------------------------------------------+
) z/ [+ X4 Y' U; n//--- price movement prediction: r9 B+ M' ]! k: v; C4 C  y
#define PRICE_UP   01 E, l+ u: x; w
#define PRICE_SAME 1
( e) c8 J$ z; c3 n#define PRICE_DOWN 22 ~# b; t# B) p- r8 h8 A
//+------------------------------------------------------------------+0 R, b' d5 ~+ ~4 q, S
//| Base class for models based on trained symbol and period         |+ Q3 C: K1 r# w
//+------------------------------------------------------------------+
& x2 Q0 ^6 h, \% |' ~class CModelSymbolPeriod
6 c5 r* L5 v' b, h. s, u{
( _3 Y3 H6 R; d. ]. Eprotected:  L" g( H$ J; ~( R0 e
long              m_handle;           // created model session handle/ t) I5 Q# [- X0 Z  g5 t
string            m_symbol;           // symbol of trained data
$ ?( v6 y: F# [4 ^- o6 @. GENUM_TIMEFRAMES   m_period;           // timeframe of trained data9 v# Z* H, B: X0 _0 z8 X8 `5 {' E
datetime          m_next_bar;         // time of next bar (we work at bar begin only). N6 m! r  P: o: v) G$ Z8 N
double            m_class_delta;      // delta to recognize "price the same" in regression models
0 C. I& B: m* ^! ?& Epublic:, t' C: w! K' o3 w& V; F# |
//+------------------------------------------------------------------+
5 P8 r! W4 z; f5 G+ e. U$ d* x//| Constructor                                                      |
, K+ B+ }. B) m0 }# N8 g4 P7 p! z% H//+------------------------------------------------------------------+
- z! l# I: ?% {0 [" n  zCModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)
4 q6 I5 j3 D5 q3 h{
/ g. o" h/ T2 t, O' }m_handle=INVALID_HANDLE;
# O. X8 ^  n5 }5 Z$ gm_symbol=symbol;) A7 E! B- A2 q0 r+ [  g; t9 Z
m_period=period;
5 Z+ K1 J" P8 c3 V# C* ^m_next_bar=0;# |& s0 X7 t/ E1 t1 ~* `
m_class_delta=class_delta;+ z" X% x. k$ U7 r# t7 c, G6 Q7 {# ~
}# r: U3 F) }9 x, Y; X
//+------------------------------------------------------------------+) ~5 ]) ]0 N+ y* C
//| Destructor                                                       |! a$ n7 e; q$ M% L
//| Check for initialization, create model                           |. @+ I4 ?9 `% u1 e2 Y& i( X
//+------------------------------------------------------------------+
0 G! J9 Z# c1 W) n( mbool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])' v* O$ X$ W' v
{8 x0 b( h: v7 M. K6 i2 T5 t, T
//--- check symbol, period( r" a, }+ _# L
if(symbol!=m_symbol || period!=m_period)
# A& b1 v5 j7 O% L: v) J{, v: }5 f  `0 F/ t' z! }
PrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));
" @6 n/ z4 `  Yreturn(false);+ R8 X5 i- X: H+ w( x
}" u6 D' Z! O- ~/ t8 d2 D2 M! V
//--- create a model from static buffer, `0 r5 G6 s3 C. z1 J
m_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);( e& p# p; K6 x% g# Y
if(m_handle==INVALID_HANDLE)" S) n/ i% m" X# g8 |/ C
{
8 h; d0 R: @( a0 TPrint("OnnxCreateFromBuffer error ",GetLastError());
1 v9 H) o5 K# X# D0 S, U+ i9 \return(false);5 M( l) E0 ]5 F9 @# |
}
3 i& R4 i5 ?# R5 [//--- ok
5 n& Z3 M  M/ x6 @return(true);
! p. j9 `& }# I; V; D/ Q4 a6 q}* E& ^$ K7 C& O
//+------------------------------------------------------------------+
1 k% R. A5 [5 n- r( K$ c& \m_next_bar=TimeCurrent();( t, I2 U! F) R% ]% F
m_next_bar-=m_next_bar%PeriodSeconds(m_period);
$ a8 |% ]- M$ k# J0 b- t/ Qm_next_bar+=PeriodSeconds(m_period);
) P6 p. R/ P4 s. ~0 A//--- work on new day bar
: E8 |) q' p8 M6 a5 a: Creturn(true);1 E* ~* ~  J# D
}
: C: f& H+ C- f& Z& M3 @2 W//+------------------------------------------------------------------+
1 i8 z% P# O5 N//| virtual stub for PredictPrice (regression model)                 |
) `" ?- N2 X$ R, ^* y* o//+------------------------------------------------------------------+' l- i; {# t+ C0 @( J3 ]1 o
virtual double PredictPrice(void)7 s$ G( u5 ^9 E: L- @# q# g6 T
{, g3 t$ {- y  p  _
return(DBL_MAX);
, V1 |0 M4 N* [# z/ F) N7 F7 T* `0 s* J}
& D/ s$ C& A7 E9 G$ Z+ G* M5 f' ^//+------------------------------------------------------------------+3 z1 h6 x* @- d9 q* j$ A
//| Predict class (regression -> classification)                     |1 {+ f0 N* {/ F% {. t
//+------------------------------------------------------------------+
1 d4 C4 y% H/ H# S# Bvirtual int PredictClass(void). e7 K- a6 u; l. E$ X% J( t% b1 E
{
/ P" T) i: t; O& jdouble predicted_price=PredictPrice();
7 R0 U+ v4 L$ S# vif(predicted_price==DBL_MAX)
0 U# s. d& \$ \; L& V& Creturn(-1);
4 U: B. g, J* b  q$ k  A& bint    predicted_class=-1;; p- O5 ~* V0 `- H  x
double last_close=iClose(m_symbol,m_period,1);, @! K1 h8 G* `0 S: o/ k
//--- classify predicted price movement4 f# @/ E! z; L3 S! m
double delta=last_close-predicted_price;$ n5 f. g+ j! m5 Z6 P  p
if(fabs(delta)<=m_class_delta)3 J* P5 k) K6 A3 F) J
predicted_class=PRICE_SAME;$ {, X; ]+ ^6 V) x& S3 S
else6 P4 E' i; V8 l% S
private:
9 p- H- A5 N7 I7 O/ xint               m_sample_size;3 ^" ~# A1 T! S" g- E* h
//+------------------------------------------------------------------+
1 j$ {7 {, z0 S1 x3 m+ Pvirtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)9 _6 j8 L* s: I
{
& h' ]& I" N9 Y/ \//--- check symbol, period, create model) U) k5 O  N4 I9 H9 D
if(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class))
; S4 s9 t1 o  Z# B  J4 O{( a( W* n8 ^6 b
Print("model_eurusd_D1_10_class : initialization error");2 F/ I9 P5 G; G# f( s( R( ~
return(false);4 y1 ^1 \' R8 R- l, f
}, W2 }$ F8 Y1 Z7 r
//--- since not all sizes defined in the input tensor we must set them explicitly
0 g9 D$ J. Y& E# v* S//--- first index - batch size, second index - series size, third index - number of series (OHLC)7 h7 v- V. ?# v3 M0 Z  P
const long input_shape[] = {1,m_sample_size,4};
9 P7 n! F2 U1 r# \+ zif(!OnnxSetInputShape(m_handle,0,input_shape))0 W5 t. t/ Y2 n1 y3 T
{
& A  a! E* }: E9 D) o2 EPrint("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());% x( U6 _8 T* B# z" }; N' \
return(false);
6 _+ x" H9 N. }+ i$ E}
. }5 V5 |$ E# b( }1 {//--- since not all sizes defined in the output tensor we must set them explicitly
6 |' M4 H6 U; h- D, p, {6 {& I//--- first index - batch size, must match the batch size of the input tensor
4 G- B* D  M8 Q$ {9 Q//--- second index - number of classes (up, same or down)4 K9 s, P4 O( U; i- X, V
const long output_shape[] = {1,3};
. f* q) w2 ?; x" w/ ?$ O( ^% }if(!OnnxSetOutputShape(m_handle,0,output_shape))5 j* {0 _0 n) A6 j$ G' W: b
{6 m/ n; C6 r! h& v/ v, f& J# e6 w4 X
Print("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());8 g$ \( ~9 C* L7 i, y( d/ |
return(false);( @1 ?. Z% I, x& J$ t
}& R# A8 ?, D$ S8 v  |
//--- ok5 M% R9 t! U; T# J" p' J
return(true);
0 y* |8 H1 I. e! _+ W$ h0 ]}; h, I1 e2 ~+ A0 H' F* u9 |
//+------------------------------------------------------------------+) \; k( g& d- p. e8 ?. N3 U
//| Predict class                                                    |8 y  a" U5 R  b8 J) h: j% w
//+------------------------------------------------------------------+
6 o! w  T6 q$ A7 F( Y( E2 `1 avirtual int PredictClass(void)
. {1 \# ]8 A: D! a{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-26 12:18 , Processed in 0.456148 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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