私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?  b' m0 q3 J2 N- {9 x
在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。; k6 Y! `& P4 ^( p4 [5 P
我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进4 j$ Z0 }" r! `( `& }; G# c4 G
//|                                             https://www.mql5.com |2 ~' G. j# f# Y- A
//+------------------------------------------------------------------+4 ?1 K/ g& A& B9 [% X7 k) {3 }
//--- price movement prediction7 k+ Z- ]5 j  l1 B5 D
#define PRICE_UP   0
: Y( ^9 _) {+ H5 `% u#define PRICE_SAME 1
) ?+ d3 y" x  Q#define PRICE_DOWN 2
$ u& n" n% L; [( H# n* z//+------------------------------------------------------------------+  O: G# h  }) E4 o' l6 y& m
//| Base class for models based on trained symbol and period         |& [/ w9 k5 z% Z, v! j
//+------------------------------------------------------------------+" Z6 _( k7 t: v* o# }$ S0 I
class CModelSymbolPeriod) w, m6 Q4 `! }! j- _
{3 g% p9 L* a: v) b8 _4 \5 o1 l0 C
protected:
% ?9 c% w, x; ^/ i7 j7 qlong              m_handle;           // created model session handle5 l/ V# j3 x6 Y: q0 `9 G# x8 T+ a
string            m_symbol;           // symbol of trained data3 r& ]  i# K. U; X$ @  [
ENUM_TIMEFRAMES   m_period;           // timeframe of trained data
" u2 q2 W, ]7 v- kdatetime          m_next_bar;         // time of next bar (we work at bar begin only)2 e/ R( @' K9 |, E6 G
double            m_class_delta;      // delta to recognize "price the same" in regression models
% N: ^' T5 O5 q& ?% Cpublic:
. I; f4 c/ r0 g7 t) r* C//+------------------------------------------------------------------+- b7 x/ \$ k( K' o
//| Constructor                                                      |
6 |. L1 n& I( I( r* w' I* L//+------------------------------------------------------------------+' [: k0 y/ U+ `
CModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)
  Z. e, t+ t3 \: O% s3 ~3 f" n{
4 y- y1 M% {. Rm_handle=INVALID_HANDLE;
: q* m  X! m& W; D; a0 @0 \% w! Nm_symbol=symbol;2 i' M2 B, C, N) o0 D
m_period=period;5 l3 E, g3 U4 [5 T. a* `6 g2 s7 V5 H  b$ [
m_next_bar=0;3 h  |; n7 `9 I  Y3 ]
m_class_delta=class_delta;* V4 ~2 l2 R" Z2 a, o1 [
}/ v3 q$ n' I7 ?; F" V* J
//+------------------------------------------------------------------+* [. y7 N( O7 z* N
//| Destructor                                                       |
, w- L' A- _: r7 O+ ~& u$ m& K//| Check for initialization, create model                           |
, }% a1 z( {! H4 n, y//+------------------------------------------------------------------+5 \2 ]& v+ x# c; u4 X
bool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])
: m2 ]! u" P$ ~/ C{
8 K! F; F. w! E5 {. L, n//--- check symbol, period
# |7 p2 v) w8 f: o. zif(symbol!=m_symbol || period!=m_period)
& M# l9 N9 x) H! r* w" {{
$ C- _3 M" P  ~  M% a% }PrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));; f' Z# {+ l, j& X2 I9 b7 y; R. c
return(false);
" f/ n5 z; t) t& |# N3 q}- F2 `/ f. i# M; }
//--- create a model from static buffer
$ `5 X& ]9 v7 S. ?m_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);) U& L8 d7 e& Y
if(m_handle==INVALID_HANDLE)
) M+ V* Y9 T  B$ f% J{6 u" q) [# @6 e2 p/ o/ m4 c8 p/ [' p
Print("OnnxCreateFromBuffer error ",GetLastError());
1 G& i0 \+ i, xreturn(false);
! T7 a) ]$ V7 l}
4 _8 a- [! @  Y, z8 k* t  Y1 B3 V; O//--- ok
, H' K: p& T, ^8 C- q2 A  B& freturn(true);
. R( u( a6 u: h0 m}  }+ a# l7 r4 U! ^9 Q3 w
//+------------------------------------------------------------------+: x! _0 C9 g9 _. P
m_next_bar=TimeCurrent();
$ P! P; o! ^9 cm_next_bar-=m_next_bar%PeriodSeconds(m_period);  G/ T1 A6 E; c
m_next_bar+=PeriodSeconds(m_period);+ a( ], ?4 U5 g5 e: Y7 Q# o* s0 |$ Y  `
//--- work on new day bar  b# p+ l2 i# w) Z/ g, i
return(true);+ C4 T7 u  N# N0 l: S0 x
}; a0 `: [" j9 s
//+------------------------------------------------------------------+
  n/ H. A9 Z) G  R$ B//| virtual stub for PredictPrice (regression model)                 |1 ^* W9 g0 N2 Q# v+ x3 P
//+------------------------------------------------------------------+
- O; p. n5 |, O, t# I7 I* ~$ qvirtual double PredictPrice(void)( q, B9 Z! C' k( M; E; F  @
{
- a/ m( |& C9 [$ l) O3 W2 s+ m$ o$ Freturn(DBL_MAX);; N6 U! N, d; c/ Z2 V9 W
}
3 N! b1 ^( J4 Y//+------------------------------------------------------------------+* I+ z  K- x' t
//| Predict class (regression -> classification)                     |2 I5 A' F/ [! }  J5 {
//+------------------------------------------------------------------+
+ N: y& @( A1 t& ]6 c, }' cvirtual int PredictClass(void)8 R$ B6 K5 V- t2 B8 J, u. A! E3 ~; K
{7 x$ g+ f& q: t3 |- R
double predicted_price=PredictPrice();
7 Z/ g, o: a9 ]/ ?5 G8 c- [if(predicted_price==DBL_MAX)1 F) p" k- x6 G2 q  i- i2 A! g
return(-1);
! u  q0 J8 N1 ^  Gint    predicted_class=-1;
5 k5 n, d0 g5 S8 t% vdouble last_close=iClose(m_symbol,m_period,1);
7 g- Q$ A+ }0 I7 Y& e' f//--- classify predicted price movement
: W; B: J/ @8 R: s% Edouble delta=last_close-predicted_price;
2 M* j* _8 `9 i8 A* \) vif(fabs(delta)<=m_class_delta)5 t9 y6 f5 t" y+ I
predicted_class=PRICE_SAME;
* _( F7 D3 M" R/ A) }9 K. _* velse
* O: G* U$ x. X1 k* F, lprivate:
. }8 Z4 ~8 S) [, [int               m_sample_size;
5 ~$ L+ \1 M) k# e5 E//+------------------------------------------------------------------+9 `  C3 {# z4 {; w
virtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)# X. G" a& s$ K- u
{5 t" F4 S, W+ v5 t0 v( S% O
//--- check symbol, period, create model& G+ s8 W) v: R# U
if(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class))' V5 E: k( C6 {; s. |
{: X" G4 T4 `: C+ ]
Print("model_eurusd_D1_10_class : initialization error");2 p' p  g& m) B, i: m
return(false);
( Q/ e* m7 {, T7 ~# G' G}
6 L9 R+ v2 e. _//--- since not all sizes defined in the input tensor we must set them explicitly
. `& o; U7 y" `$ T//--- first index - batch size, second index - series size, third index - number of series (OHLC)9 W  t! \+ {$ d; f: K2 O
const long input_shape[] = {1,m_sample_size,4};
9 i) O) }7 S% e8 tif(!OnnxSetInputShape(m_handle,0,input_shape))
6 }" U2 O; v# }9 e: x) R{; R6 _# n$ h7 u/ }+ ]6 z9 _# f: \
Print("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());3 k4 ~2 |9 Q4 w% N5 N1 q
return(false);( q/ X# @+ _+ a) J
}
/ D2 z+ l3 @$ \* u5 O4 x//--- since not all sizes defined in the output tensor we must set them explicitly$ q; |1 ^% V+ f( d! u+ I
//--- first index - batch size, must match the batch size of the input tensor6 ^: R" b% P. o7 @- p
//--- second index - number of classes (up, same or down)
+ n) k! G( ~) ~) M3 @& ~const long output_shape[] = {1,3};
1 P8 o' M4 x! o/ j+ e& w" c! Iif(!OnnxSetOutputShape(m_handle,0,output_shape))
8 ?: n  J1 l# o% |* t. S{
3 k" |4 h/ Q3 R) ?9 KPrint("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());
! \8 A: K  b5 w0 o+ mreturn(false);
! \6 l1 L, U9 U6 L& P) t}
( ]" T$ ]- X/ `; d6 T# t//--- ok
$ q  R- K/ M/ p2 [- x1 Mreturn(true);
( f  s! ?0 E6 Y  g+ g1 F}
# z. G. ~' J& |# y# b6 U+ e. K6 `//+------------------------------------------------------------------+
4 |$ |, c/ D8 }7 Y//| Predict class                                                    |( r# ?( r# e3 |& ]  c
//+------------------------------------------------------------------+
& l) x$ Y. T0 S8 O. s3 c- l* O6 zvirtual int PredictClass(void)9 z$ Y9 ~& J$ ]6 J+ i- j
{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-21 11:36 , Processed in 0.663830 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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