私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?5 O6 z9 R7 F9 F  h/ e
在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。
+ h" l3 m2 @4 o: u我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进
. t2 M  G2 |: m  x//|                                             https://www.mql5.com |/ n9 H4 y8 v1 h# O( n* H
//+------------------------------------------------------------------+
% _- H! e' q- A9 I' P% `: g! T# ?//--- price movement prediction
# G. q, Q6 ]1 B2 ?" V+ k+ F1 h#define PRICE_UP   0
- s+ ]  e1 l% b9 L9 E) B7 t# [#define PRICE_SAME 1
3 T3 G- s8 s* z6 l! X5 D#define PRICE_DOWN 2
, N1 @! Z# d$ T9 ^; n//+------------------------------------------------------------------+7 ~3 W/ D. y+ Y4 o2 s% q, `
//| Base class for models based on trained symbol and period         |$ D8 }. M1 s. `8 g
//+------------------------------------------------------------------+. f9 a: V  f, \" W, a( W
class CModelSymbolPeriod
: N! S- w# x& S4 o{8 o8 Z& \4 o4 X4 B9 o0 i
protected:
& _1 L2 P6 B% ^3 r; zlong              m_handle;           // created model session handle
6 j: V; l- P4 J  N9 }0 \2 W0 bstring            m_symbol;           // symbol of trained data
4 b% C$ X; a$ F( [4 B5 {. b) T; YENUM_TIMEFRAMES   m_period;           // timeframe of trained data, B" V5 B$ n9 D1 ^, z3 T
datetime          m_next_bar;         // time of next bar (we work at bar begin only)( G0 p5 ^" t; S5 i$ z+ e
double            m_class_delta;      // delta to recognize "price the same" in regression models
* j3 f! I! x5 D# i  B% a" Wpublic:
. q& }0 [0 t: }; ~+ g0 @//+------------------------------------------------------------------+
' B) V8 h5 O: I) p5 G9 W% u# \//| Constructor                                                      |- i9 ~1 y# w4 \0 c5 \+ F5 J
//+------------------------------------------------------------------+7 ?. L! v$ ]' g! h7 L$ l8 E9 i
CModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)0 n4 Y5 L! L- ^3 ]
{
# W+ R9 w# a  m6 p4 |" I- km_handle=INVALID_HANDLE;2 C0 q  k* U/ @' _% k$ G
m_symbol=symbol;
0 J7 d7 ]' ?( q% t7 ym_period=period;' L! O2 m8 L4 i' y7 o
m_next_bar=0;
9 F  y4 ^# g, ~- Q. i# x# hm_class_delta=class_delta;' `' r9 n+ \* V! h' p5 O
}
1 x6 }0 v8 C/ `7 J2 i$ r//+------------------------------------------------------------------+( P" `' m5 o; v, E& I6 ?
//| Destructor                                                       |
4 ]) a+ L  e7 K+ P/ W" |//| Check for initialization, create model                           |, P: V: M2 K' `8 j
//+------------------------------------------------------------------+& X% l5 r) s+ M' \1 R: l( h
bool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])
) ~5 w0 I8 [& _& I{' H4 K# l5 S: @- z3 k; H
//--- check symbol, period
7 ?- R% O* ?/ ?; x" b/ ^- D3 Fif(symbol!=m_symbol || period!=m_period)
$ \( Z4 ?  `* `% f2 o{/ }& q4 m4 A$ Y4 j
PrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));
" k5 @- k7 e% |return(false);+ }0 A- M( J: Y
}4 |" K# Q, J& y
//--- create a model from static buffer) C* Y, Q5 t) z5 ^' q; t3 e
m_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);
# I5 ?- C" ~; B+ C8 {( P* fif(m_handle==INVALID_HANDLE)
, Z8 `7 |% Z0 Y0 u{1 S6 S! i. B+ V; o8 K" M
Print("OnnxCreateFromBuffer error ",GetLastError());
# b/ j" H; a$ k% `7 o! Preturn(false);0 b. p7 c5 O5 i) k" z* T
}
! F9 x; M9 q1 `2 C2 {  C' \4 u//--- ok
" m8 g9 p4 Y' r$ S4 b- a! ]. q$ Vreturn(true);
! Y& Y8 d5 b+ J! z: q& w3 k4 A}# k7 w9 s. Y2 P1 v
//+------------------------------------------------------------------+! L. w7 ?# \; d8 d) ^
m_next_bar=TimeCurrent();
6 H2 Z% ^5 k0 K- Y  E* pm_next_bar-=m_next_bar%PeriodSeconds(m_period);
" |: k" i: r& dm_next_bar+=PeriodSeconds(m_period);
7 Q' ?- j4 S9 e7 P/ ^0 o//--- work on new day bar
  N+ Q6 b$ @" m/ n* w" qreturn(true);
/ C$ h2 J, v& f( \) E}# t+ l1 a3 J) T1 C  t
//+------------------------------------------------------------------+) M6 c- p1 E8 l7 p% b
//| virtual stub for PredictPrice (regression model)                 |! R" \' W. S- e" ^/ n" }
//+------------------------------------------------------------------+* g' h9 |. g8 {0 s& H: f& [
virtual double PredictPrice(void)
! m$ a$ p: F4 X( a3 t% ?6 b% k- W{- w5 e! s) g6 L  N
return(DBL_MAX);
: D4 w: }' z) P# X" K2 m# g; |% n: v/ i}
) w5 x' v; p: S! X# \3 e7 T//+------------------------------------------------------------------+% H% L0 l2 r, ?& }
//| Predict class (regression -> classification)                     |
, [* E6 Z& s1 Q5 Q//+------------------------------------------------------------------+8 z0 w/ U5 S' i
virtual int PredictClass(void)
- f' ]9 `2 b6 b6 J/ D{
6 e" _: `  E. W: [double predicted_price=PredictPrice();
- m5 J, @: @# Q6 z2 P# ], H- Zif(predicted_price==DBL_MAX)  a3 B4 ?" ^- x
return(-1);
, Y! _; w9 Y0 B  R2 \int    predicted_class=-1;0 e& b& K5 d8 v7 U2 ], P
double last_close=iClose(m_symbol,m_period,1);' m) C. k, ~0 i1 t
//--- classify predicted price movement0 i; a7 E$ e5 h  d# ?# \! w. [3 ?2 X
double delta=last_close-predicted_price;3 x) u: p/ g5 W# [; B* |
if(fabs(delta)<=m_class_delta)
5 W" [$ F! i! u: Z) f" E, j/ gpredicted_class=PRICE_SAME;
# U  l1 Y2 B( d. t3 x& s# d8 Helse
1 z0 ?: t; b! ?! j8 B8 e, O( Hprivate:
) Z$ r) N6 o& {' Q! Cint               m_sample_size;+ i/ A9 n0 L6 Y$ B
//+------------------------------------------------------------------+
9 i5 K; J4 J/ _virtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)1 R% N8 n5 M  d: Z/ Y3 y1 S
{
7 o% u* g% u4 F4 B//--- check symbol, period, create model
3 _6 ]! V9 a% Z; t& I# `if(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class))
  ?3 B( s1 E, j/ q* G5 f{: i% `+ ]0 Y( U2 z) u: T
Print("model_eurusd_D1_10_class : initialization error");
7 L% V7 z" \/ P+ t' C9 H$ hreturn(false);
  C$ |% r8 E' X& ?}# N3 `( n4 b' \- X0 V4 R6 a
//--- since not all sizes defined in the input tensor we must set them explicitly
$ u. d$ N% B3 F1 t8 u3 s3 k//--- first index - batch size, second index - series size, third index - number of series (OHLC)
) {7 b2 G. m- i9 m' B' Tconst long input_shape[] = {1,m_sample_size,4};/ [5 F& v8 M  T3 C
if(!OnnxSetInputShape(m_handle,0,input_shape))1 ~  ]* r' v7 O3 E* m3 z. T
{2 U; ?+ S: O* n  w) p0 ?
Print("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());/ B, t. @" T' t. @* o* F
return(false);. q, s5 W- \; A" W
}
" p+ N- v! T  c' y/ }8 f" h! c  ^//--- since not all sizes defined in the output tensor we must set them explicitly
3 j4 t, X+ G* ?* _9 N0 E//--- first index - batch size, must match the batch size of the input tensor* H% B0 }: A4 \. Q5 f( K/ K+ Z
//--- second index - number of classes (up, same or down)
$ A& M+ v& x2 k) f+ zconst long output_shape[] = {1,3};
" h/ H& o: Q- z8 aif(!OnnxSetOutputShape(m_handle,0,output_shape))
0 b$ e+ q/ |+ K+ n  `{$ v  o4 d6 y, z4 d% J% C/ N4 \
Print("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());! ~4 |5 M$ M6 G, Q4 i" M
return(false);
& Y/ B" {; D- E) k* {}
1 F3 a6 w: q9 r0 [' G- |" d//--- ok
% p7 y& x( S+ ~3 mreturn(true);+ w1 U! S- H) n+ }+ x/ |5 W9 g* ~
}( O4 p6 x' L; P
//+------------------------------------------------------------------+
5 R! Y8 d  J1 ]4 ]; n//| Predict class                                                    |- A8 z8 L1 y4 B
//+------------------------------------------------------------------+
! [* t# g  ?7 T* ^2 s) x% evirtual int PredictClass(void)
% j( h5 t: N+ t* L{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-6 11:29 , Processed in 0.396879 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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