私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?
$ w1 ?* M3 b% K. O7 V在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。
2 ^5 \( T* i* W8 g5 Z我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进2 z( p; G( [/ {+ i/ W/ r  X
//|                                             https://www.mql5.com |
6 ~6 h; x( [3 R0 U3 ?9 A//+------------------------------------------------------------------+
+ c' n$ }+ F# w/ H2 C//--- price movement prediction
( d- }2 m; _- e3 J; B#define PRICE_UP   0
' o; Y0 x; i7 V, j4 p#define PRICE_SAME 1
$ }) ]- o, T( F# s3 M, u( |- c7 N#define PRICE_DOWN 2$ H/ i3 x2 A, Z, N
//+------------------------------------------------------------------+
( U$ V$ s, F% P2 b//| Base class for models based on trained symbol and period         |& n8 k( }7 [& I6 {! t2 G# Z: u
//+------------------------------------------------------------------+
( t1 W; {, c# S, u* r! N, Cclass CModelSymbolPeriod* [& P+ G( P# {$ T& x
{
) K  s2 K$ G& J4 Q! P" B/ z2 G' ?+ zprotected:. ?% T% C. A; Q- t
long              m_handle;           // created model session handle5 e: `: L( X, ?
string            m_symbol;           // symbol of trained data
  {" G3 F/ i2 t4 J0 J  F1 l2 r, LENUM_TIMEFRAMES   m_period;           // timeframe of trained data: g1 S/ j9 ]1 B7 ?
datetime          m_next_bar;         // time of next bar (we work at bar begin only)
' P2 b# W4 M' Q. O! a/ \double            m_class_delta;      // delta to recognize "price the same" in regression models
( w  u3 P# S0 _/ B; n: c  B7 Apublic:
1 G- a, h) k5 k6 u* Y2 z: D//+------------------------------------------------------------------+) T& b0 H+ s" ~: n: [+ _
//| Constructor                                                      |7 R, m7 Z! ~; G; ]
//+------------------------------------------------------------------++ t' g& j! d+ T( v$ ?& b
CModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)
7 c6 w8 q9 p/ O+ I6 Y{
* r  ]1 ?/ o, f1 ^- fm_handle=INVALID_HANDLE;5 |9 n. R8 n! F: A6 ~
m_symbol=symbol;& F/ X* o7 w% n/ s- z
m_period=period;' b) B3 A7 w' z& m( a$ `) s
m_next_bar=0;
5 M; h8 B0 Y! _8 ^1 P* u+ sm_class_delta=class_delta;% n* o' j( |6 B4 D* V, I! {
}
! F! ?. d7 }8 r, [//+------------------------------------------------------------------+
' g0 c8 K6 U6 X//| Destructor                                                       |
, L' F9 w3 l5 N9 }( K$ w//| Check for initialization, create model                           |
; V! W" t7 T* K5 {: K% ^//+------------------------------------------------------------------+/ u% F2 q6 P  y1 f  J6 Q
bool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])
5 B, v+ q  X: r! F* X& e{1 @$ a4 g0 P" l! e4 k- x% ^: J7 X
//--- check symbol, period
6 Q7 X+ P* t8 }if(symbol!=m_symbol || period!=m_period)' L( Y: o5 I& k% t+ l# f
{! t9 f# H- K+ Y. E+ \, f4 ^3 r  {/ |
PrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));
2 H7 S- e/ P( }! `& D. jreturn(false);
- r; @& Y! _: B}
0 F) Q7 d( h$ O4 Q0 V- @//--- create a model from static buffer
) ~4 p: e; H2 B( t/ Z8 j, U7 V' E2 P: ]m_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);, s& H: U: @0 U/ z9 {* S" Y
if(m_handle==INVALID_HANDLE)
6 r8 U' e. C/ p8 x3 s- w6 ~{# Q6 W2 _+ t- z" M# B! j
Print("OnnxCreateFromBuffer error ",GetLastError());
8 s7 }( E* ~9 ^* K: breturn(false);+ Y" c% W) Z% d
}: ~3 p- M& E2 a) J6 C) L; j, ]6 J
//--- ok
, Y& T1 o* y' B) K9 preturn(true);
" z/ ?" @" d  T' q}
' F7 j2 U/ a- i3 r8 e; J3 I//+------------------------------------------------------------------+
) @3 n2 g. X. h% ^m_next_bar=TimeCurrent();
  D( X  [9 _* T, m( \1 v  E+ jm_next_bar-=m_next_bar%PeriodSeconds(m_period);
( U+ U% L2 X1 E- S  c) ?m_next_bar+=PeriodSeconds(m_period);( J+ ^& n: t6 K5 q
//--- work on new day bar
; K/ |* I" [. e9 o6 M' Jreturn(true);8 `/ l" z! t0 g1 }4 a1 u
}6 w0 F! p4 @# a4 @/ D4 M
//+------------------------------------------------------------------++ E* o) H- h) O) D$ {
//| virtual stub for PredictPrice (regression model)                 |3 a9 w6 B( x: `# c% ?0 f- v0 ]
//+------------------------------------------------------------------+
$ w1 C2 z" F* H1 d7 k) v* `% rvirtual double PredictPrice(void)
# I, m+ Q9 m: B0 O6 Q, _{1 ]1 X2 @+ e8 I2 Q: {" P
return(DBL_MAX);6 D9 O) v8 _9 v5 W) B$ B' R
}
2 y6 @# O1 O; ]; U2 q//+------------------------------------------------------------------+4 A6 ?$ Z/ |+ {( @. y' }" j
//| Predict class (regression -> classification)                     |
. P$ l2 d( G* ?# s' r; I- O//+------------------------------------------------------------------+$ n& z% u8 @$ x1 E/ V
virtual int PredictClass(void)
$ p# L9 b) U$ q# \" G8 {{
* y7 R" t( f; O2 [. P) S+ udouble predicted_price=PredictPrice();
# b6 k, g4 F; D7 O# N  zif(predicted_price==DBL_MAX); m; W7 t% H- h; t8 ^) L# f" o
return(-1);
3 b6 w6 m( Y) r+ l: c! S9 z3 Eint    predicted_class=-1;' t0 o' f, G6 Z8 y
double last_close=iClose(m_symbol,m_period,1);
/ S4 {6 |, O$ X# w# `//--- classify predicted price movement
1 l; i0 Q" l" udouble delta=last_close-predicted_price;( I2 j% C  {  }! a) |3 T4 r
if(fabs(delta)<=m_class_delta)+ I+ K. j1 S9 V
predicted_class=PRICE_SAME;
1 V$ v) j5 _5 B& b. u* ~: Y7 ]+ Eelse
& x$ x* _2 O6 F* d; A% ?" lprivate:5 K* y+ c. K9 T# }6 j
int               m_sample_size;
4 N$ p8 C: y/ j9 ~! x/ @0 Q//+------------------------------------------------------------------+) \8 C" a. L+ f$ d, L0 T( z9 Y
virtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)6 @% b$ H7 j) O# d: L
{, g8 Y. o5 z) F, p3 o
//--- check symbol, period, create model$ W- y! i5 c8 m0 @; J! j7 t
if(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class))6 [/ j( ~8 t5 m- J: Q1 M0 a9 x
{& r, d. z( b- O# l* @9 d
Print("model_eurusd_D1_10_class : initialization error");6 Z) B0 ~' [  z9 {0 R- \. a
return(false);
& e) Z( w; C% d& j+ N1 o' e- T}+ A$ \+ [! O: ]  v% z- R" \# G
//--- since not all sizes defined in the input tensor we must set them explicitly: k; V9 P% f- m0 w
//--- first index - batch size, second index - series size, third index - number of series (OHLC)3 w' h* A2 G' `# J  \
const long input_shape[] = {1,m_sample_size,4};
2 f, z, V8 y# _/ d( @' fif(!OnnxSetInputShape(m_handle,0,input_shape)): s% f% i, g) N- {- W+ o! J/ Z
{( f" f% n8 p2 ~4 s& S  `
Print("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());$ u& [- a! W1 ]+ M
return(false);* C. M! G5 ^+ J- ?6 G
}
$ D, J6 K; j0 e' Q2 j//--- since not all sizes defined in the output tensor we must set them explicitly
6 @$ f/ y- N& M- s) [+ X- H8 }" K3 t4 o//--- first index - batch size, must match the batch size of the input tensor
0 l5 i* B. D# s% ]# R//--- second index - number of classes (up, same or down)3 q6 z; p) `5 T# z  e
const long output_shape[] = {1,3};; B5 \; p7 J0 s9 w* v/ r
if(!OnnxSetOutputShape(m_handle,0,output_shape))
; @) w. L3 S/ h$ A# ]2 o{$ [% `4 F8 G9 z  H! {6 K
Print("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());" u) v8 _2 Q1 H
return(false);7 Q1 M/ Q; f: T" F& N- A6 _1 r1 A
}
9 Q9 `( A; c7 n$ Z! R//--- ok" I) B# J0 I: B" B& c
return(true);% v" f: y) D' w, Y
}
) i' `; K: H9 O6 t//+------------------------------------------------------------------+
; _* J/ _+ D6 Y8 ?; w//| Predict class                                                    |
. J8 q. V. P2 M//+------------------------------------------------------------------+
) Q3 B! m( Y1 A! v- ivirtual int PredictClass(void)
. d" E6 u: F( n0 ]6 |7 \/ {1 u{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-7 09:23 , Processed in 0.456149 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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