私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?! r7 |5 t/ ~! T6 h- R
在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。
: u' C" ~# P& M! {4 G* L) K我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进0 j' u2 ]0 D/ P6 [6 i) a
//|                                             https://www.mql5.com |9 l0 _; P8 c) W/ G
//+------------------------------------------------------------------+9 `  n( Y- S8 p5 g
//--- price movement prediction" m5 `4 X! P7 C: a  M  V/ @
#define PRICE_UP   0& N- X2 |& y; p$ Z. X
#define PRICE_SAME 1
+ h" q9 p+ J+ O$ k' @8 n#define PRICE_DOWN 2
$ q6 T$ |2 \  f; F( D3 G- S//+------------------------------------------------------------------+
, }0 C4 f7 _5 v3 y//| Base class for models based on trained symbol and period         |8 Q. q9 Q0 j, l  [
//+------------------------------------------------------------------+
2 I' e* K. @' f3 v* gclass CModelSymbolPeriod& n6 N- T7 y" \. j2 {$ m& I
{
4 M# S7 g2 ]: S, _0 j8 ^2 Lprotected:% O# i6 p& F7 _7 w2 q
long              m_handle;           // created model session handle
' J5 c" I! a3 Bstring            m_symbol;           // symbol of trained data7 E* v/ J# |3 v6 f. V& G% S7 n
ENUM_TIMEFRAMES   m_period;           // timeframe of trained data
$ p% P  b3 T6 {5 D) Vdatetime          m_next_bar;         // time of next bar (we work at bar begin only)
: E3 I. G( P: P- Edouble            m_class_delta;      // delta to recognize "price the same" in regression models
) A6 Q1 C, o8 wpublic:
4 ]7 F0 K/ u1 @# Y3 x; G. i+ U//+------------------------------------------------------------------+0 A7 F2 S" y2 Q' a- D& @, i! t
//| Constructor                                                      |
1 D5 v+ W# d- h+ D/ i# I+ k) H//+------------------------------------------------------------------+
) v4 b4 g0 ~8 b' bCModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)
" f0 d3 C; \( m& R8 y5 y{
) E* I6 U% q6 Q( U7 I6 Gm_handle=INVALID_HANDLE;
! w9 Q" ?/ h, j) J! O. k9 qm_symbol=symbol;
$ G  \; K# U8 D; Cm_period=period;
* X9 ]4 y* q" `! Y: O& ~m_next_bar=0;% ~' g& P6 Q- V5 m" [; ]
m_class_delta=class_delta;
( E0 b% }4 ]" S1 I) R# `+ ~}
7 q! ?2 R$ D2 t# J  x//+------------------------------------------------------------------+
4 q$ ?# S/ _+ e. E+ e//| Destructor                                                       |
; P& J1 y5 @. w4 |& T- L//| Check for initialization, create model                           |
  j0 N% @" @4 X% r2 }//+------------------------------------------------------------------+9 K. T& j+ h+ M
bool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])
2 i2 i9 f2 P) f& b! n{- I6 v2 w$ `9 W$ h
//--- check symbol, period
/ d" n# e; _$ K& r) y0 s( ~if(symbol!=m_symbol || period!=m_period)/ D* M2 S  e* b: f4 I- h
{+ L3 M& ^+ z/ w9 [7 U- i& I5 ?
PrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));
' p" r/ H/ E4 A% K: D( j" @& H/ creturn(false);8 \# P" `$ h/ W& M' Z8 l
}4 C( A3 f& L, `9 [* i( `
//--- create a model from static buffer. E" `; w# J$ t5 I+ N" _: ~, U
m_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);; N) _2 m. R( f) t1 m9 B
if(m_handle==INVALID_HANDLE)
1 Y1 ^: ?% P. }' R7 u. O% Z{
7 H/ t* \4 p/ d6 V% sPrint("OnnxCreateFromBuffer error ",GetLastError());4 i! ~/ V5 Y1 ?% g) ^$ |8 _
return(false);
) Z" }, V4 G/ M5 A6 m- w4 U4 E}
1 Q: ?! w) i! Y& @. y0 L5 E//--- ok  N3 D% o( S7 U
return(true);
0 S; y: e+ q- g: c' e}
, m; I( D3 F2 ~1 `3 d0 }+ `//+------------------------------------------------------------------+
% u3 W' S& d* R3 f3 V1 ym_next_bar=TimeCurrent();$ R7 f# r+ t/ V: I: m  U
m_next_bar-=m_next_bar%PeriodSeconds(m_period);' D- O4 u: O8 h% [
m_next_bar+=PeriodSeconds(m_period);/ G5 g5 [+ O4 o% _- N$ T3 n) I
//--- work on new day bar0 R9 ?! J. _/ Z7 R
return(true);
' T+ b  b: p( C  l. w}
3 f5 _5 {0 s  U) ^: |//+------------------------------------------------------------------+
% I% T1 O$ r+ ]$ N% g% U, C: X6 z//| virtual stub for PredictPrice (regression model)                 |- ?: z9 |; L  C' ^9 K
//+------------------------------------------------------------------+7 j) a- p* y2 ~9 f
virtual double PredictPrice(void)) K: I+ `& T' m& o
{
& c3 e8 W  ]2 X/ G- }return(DBL_MAX);2 t4 s: ^' s$ R' O* y6 Q, b% s
}
0 e- s2 A  b5 Y( v/ e//+------------------------------------------------------------------+1 w5 x5 |* L' e$ m1 p$ Z3 C# p
//| Predict class (regression -> classification)                     |3 K! _" @: W8 i9 @- z" W
//+------------------------------------------------------------------+
0 {4 P, m7 K# h) ?" g; dvirtual int PredictClass(void)3 v: t% ^( P+ N* [  D/ D
{: Z) B$ D) i3 |9 ~
double predicted_price=PredictPrice();
3 u1 P3 B" c& f, \' P2 Tif(predicted_price==DBL_MAX)
0 K2 ?# L1 g9 Ireturn(-1);& h# c4 e. m% |) e, h4 r: U" s: y
int    predicted_class=-1;
5 F9 L6 k" f+ K8 Q4 H; rdouble last_close=iClose(m_symbol,m_period,1);
  o6 P6 i+ R$ M% B/ P$ v//--- classify predicted price movement8 t! ?" L  \) W$ v# Q
double delta=last_close-predicted_price;
: Y4 w1 U7 z& E$ qif(fabs(delta)<=m_class_delta)
6 l4 S# Z+ }! }1 i' Xpredicted_class=PRICE_SAME;# h) ]6 I7 x, g0 U! X$ g, p
else7 E$ i" J1 U/ j+ S# {! w& Y# u
private:% O6 `) B6 K5 n3 ~, d7 ^9 s0 Y
int               m_sample_size;
, ~2 O  _4 M% w* p% N//+------------------------------------------------------------------+
; t2 P9 E# w- I( yvirtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)/ b7 Y( I: G* D  t' q; }; s- V
{* V- y5 @) F4 y3 t- [9 ?
//--- check symbol, period, create model
3 g5 }# \3 g3 mif(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class))% ^( z3 N% R2 f3 U+ R2 t
{
5 c- J$ p  M# J6 n* m: j$ V( GPrint("model_eurusd_D1_10_class : initialization error");' U! ]7 p  R* u& L* V
return(false);" m1 m. \5 j! J- P+ @
}7 m2 W# |* N5 Z7 H$ c* r5 m
//--- since not all sizes defined in the input tensor we must set them explicitly
5 `8 M+ F& L6 Z3 q9 i' v6 w//--- first index - batch size, second index - series size, third index - number of series (OHLC)8 g7 `2 X' K( w$ r" M6 E* p' ^0 i
const long input_shape[] = {1,m_sample_size,4};/ f! D/ R- D+ X
if(!OnnxSetInputShape(m_handle,0,input_shape))" {* N3 H) k  M4 Q, f
{
6 t8 I3 P6 r  }: sPrint("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());  |# Z5 h2 Q5 x" ^( t( L3 M% \+ v. s
return(false);
* @% _% e3 k7 S, s/ r! R5 o  e}6 p0 `+ g. w$ {6 p' g
//--- since not all sizes defined in the output tensor we must set them explicitly
& x/ }: d0 x1 f! a1 S! R. D. |& j//--- first index - batch size, must match the batch size of the input tensor- k# _! ~1 O/ G0 ^
//--- second index - number of classes (up, same or down)# N- P4 \0 u  e# m9 D
const long output_shape[] = {1,3};. |; o! `! D, a/ a9 z
if(!OnnxSetOutputShape(m_handle,0,output_shape))
& X& G8 J- G" p2 g5 \{" |8 A5 `) p( M( d- F4 P
Print("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());9 n0 z( L' F. V4 U# j
return(false);; m: }7 u( K0 q
}
: h; F: U' v8 B; x/ f//--- ok
( i, h+ u/ \5 u+ a& {4 qreturn(true);
4 F, L, ?2 C5 I$ q8 o}; k% }6 T6 Q6 G
//+------------------------------------------------------------------+7 e7 W% w$ Z/ a$ x; u, [( F2 k
//| Predict class                                                    |
2 M- ]% }. n0 q+ F3 Z7 j//+------------------------------------------------------------------+9 m8 m" d. A' d+ `$ F# O; n
virtual int PredictClass(void)7 T- M* h0 x& k6 {, V7 q- T
{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-3 02:36 , Processed in 0.417589 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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