私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?% z4 h+ i5 I$ Y2 m
在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。
( }. g1 f7 B3 l# ^( T% u8 k7 }我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进: a/ A7 |6 e. S% M  e
//|                                             https://www.mql5.com |
& `) c' I+ I$ }3 R//+------------------------------------------------------------------+# N2 p; I  _8 h8 Q! A* N
//--- price movement prediction
- E4 }0 |3 [& [6 M! T4 L#define PRICE_UP   0
- b8 U0 l4 y: l! H#define PRICE_SAME 1+ z' `9 L7 ]/ l: w
#define PRICE_DOWN 2
8 @4 t2 \9 \- Y8 l. l. ^//+------------------------------------------------------------------+
; Y4 q$ y& l6 C/ O( Z//| Base class for models based on trained symbol and period         |6 x! O& L) h" m) X
//+------------------------------------------------------------------+
% Z4 l/ S. \. |  o- sclass CModelSymbolPeriod2 y, v+ p, |. A0 o/ b% D: Q+ B
{- c. N' n5 B  M! E+ v5 b  Z7 f' k
protected:2 {4 Z+ M5 N- m$ y
long              m_handle;           // created model session handle
/ w( [9 g; k& wstring            m_symbol;           // symbol of trained data' L% A8 t. t+ N! w& ~
ENUM_TIMEFRAMES   m_period;           // timeframe of trained data% P; Y6 M1 t6 J8 Y5 W- n2 b, T
datetime          m_next_bar;         // time of next bar (we work at bar begin only)
) h/ y' _3 p7 H% l% Qdouble            m_class_delta;      // delta to recognize "price the same" in regression models
+ Q: L2 Z" _/ s1 {5 d! I! x- g9 E: b5 epublic:% a& t2 k7 c0 F$ p+ s" `3 U
//+------------------------------------------------------------------+
- G% x9 p$ K6 m$ H//| Constructor                                                      |
/ |+ B5 O% F" S, o, s1 h0 T: S9 |//+------------------------------------------------------------------+' Z  t0 t% z5 Q7 q8 t8 j& S
CModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)
4 e/ K8 u5 ]5 O, Z* t# G{* k& `8 O& ~6 ?4 I( T0 i; ]5 U
m_handle=INVALID_HANDLE;9 f- y- P! i4 P, |
m_symbol=symbol;3 J" H1 p6 y3 n. {: k' ^
m_period=period;
* Q6 E- B% P- r" v' v" Nm_next_bar=0;
3 P4 _5 f2 D0 I6 B7 Zm_class_delta=class_delta;
* k5 v; H+ x2 p}" w+ A/ Q9 v3 F6 w6 S) S7 ?
//+------------------------------------------------------------------+' Y/ ^' \& v& I9 K2 x7 h, u
//| Destructor                                                       |! u( w0 r* m5 q  G- j. a! K4 O7 U
//| Check for initialization, create model                           |7 i3 ~/ e1 r0 l; ~# O% J
//+------------------------------------------------------------------+
9 X6 C9 J1 e( p  ?* _bool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])0 a$ Q* j* L; C! M; q4 x" @' z
{8 L& ]* O  H" C9 I" M' B+ m: n
//--- check symbol, period7 P% a. W/ ]  a. w, x
if(symbol!=m_symbol || period!=m_period)' [8 g9 i4 b* u- d. x
{
8 J( Z0 W+ q5 r% Y+ E/ v. e1 {PrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));
4 p- z3 w6 l, K* d& o' Kreturn(false);- M9 L3 J1 R8 c4 W
}
8 h; L3 F4 a5 b( H//--- create a model from static buffer
- i; l- P! m: D" t3 Bm_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);
1 P& A; q( O: G9 k6 Rif(m_handle==INVALID_HANDLE)& Z2 c( p6 d) G! s
{/ e3 o! |1 |. \& N6 q: W7 H' `
Print("OnnxCreateFromBuffer error ",GetLastError());( N+ b: ?! N7 w7 P- M& h# q
return(false);$ K7 @! y, @# ]2 |1 l  x' ^
}0 p8 f) z8 R- f
//--- ok
5 y5 F7 B0 j1 }# z7 Dreturn(true);: a1 C4 X' c6 \9 X4 |, |4 t
}
: u: A: X/ X  `# I7 E" q5 D//+------------------------------------------------------------------+/ P) T! E5 M& L4 c
m_next_bar=TimeCurrent();. a& t& B. H% r# T) ]0 Q% L
m_next_bar-=m_next_bar%PeriodSeconds(m_period);+ j! L  j4 d$ n! j" q
m_next_bar+=PeriodSeconds(m_period);
) J/ m0 |# M( ?! n0 s6 T! {//--- work on new day bar7 y8 K* i2 X! {/ {- {2 R( q
return(true);6 a) J# L4 A0 i# P
}+ ]8 H- K& w  r- n6 d: d% e
//+------------------------------------------------------------------+
9 S* k( P6 F  G# \5 S, ^5 P//| virtual stub for PredictPrice (regression model)                 |  R  J7 F6 m4 g$ L
//+------------------------------------------------------------------++ s* V+ r  t1 O; C
virtual double PredictPrice(void)' W7 ], i" g: \* Z. t
{5 l! F! `6 I. \- n* j
return(DBL_MAX);% [/ h; J: b& |) e6 a! {) v
}
3 {4 C* z1 d0 j' B4 d//+------------------------------------------------------------------+& J0 m: B" S1 G, ~0 I) s. h8 ^+ ?- Q
//| Predict class (regression -> classification)                     |/ [: z# ~; W0 a1 H8 G; V2 j1 Z
//+------------------------------------------------------------------+
/ `) e9 y( A6 d' |" Fvirtual int PredictClass(void)
( k! [! _/ w- p# I{
0 Y7 ~% d3 s+ N2 T0 ldouble predicted_price=PredictPrice();7 x# D5 E1 A) t3 L
if(predicted_price==DBL_MAX)
5 r/ N. v: b# f( ereturn(-1);' E# P# V1 ]0 I  U8 w4 F
int    predicted_class=-1;
' J5 G4 Q* @; @, \/ d4 Pdouble last_close=iClose(m_symbol,m_period,1);
9 V" z* N  R  c2 L* k//--- classify predicted price movement
4 A6 d' `, e0 @  ^  L; ~double delta=last_close-predicted_price;# P9 R8 ~7 r) n: D: k. Q( ~0 _
if(fabs(delta)<=m_class_delta)% C( x. F; @2 D* }: l, z
predicted_class=PRICE_SAME;
1 e6 Q' O1 l! J- Y. @$ z, ]else2 M0 P  H0 s9 _2 x/ T6 ~0 b
private:8 h4 {& I0 _$ z( Z# A9 E
int               m_sample_size;1 k4 w* y: s6 L& ^/ p: T
//+------------------------------------------------------------------+
4 c) Z5 O9 |. [% V( r% x+ uvirtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)  w5 s- `7 ~6 E+ J; k
{
" `* [( ^  P. ]4 m//--- check symbol, period, create model# x$ F' B2 N) h  i2 z
if(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class))* W+ I. Q" y8 |" _7 D3 K
{
6 [/ ^) \6 Y9 n2 F) qPrint("model_eurusd_D1_10_class : initialization error");
; _8 D* b$ I. i. ?, [return(false);
2 j+ d) I1 U. n  [$ j. H, D" b& D}7 J2 r1 n1 U# _2 W5 T
//--- since not all sizes defined in the input tensor we must set them explicitly/ Q$ Z$ g. U0 n3 ]' a' G) e) J
//--- first index - batch size, second index - series size, third index - number of series (OHLC)! W  s1 H+ z; ]; `0 E6 w' |
const long input_shape[] = {1,m_sample_size,4};
$ I/ r! k* {/ ^: n, A& U0 `4 p( ]  pif(!OnnxSetInputShape(m_handle,0,input_shape))- j8 N  ?3 t! _# V9 j  T
{
7 c, o! m1 q* u7 W! Y# }" @& `Print("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());7 ~- o* i0 T# m/ m+ Y# w- V
return(false);
" J! E* s+ z# L( N8 H4 ^}
' ~6 l' ]( J  h1 S//--- since not all sizes defined in the output tensor we must set them explicitly9 Z( Y. H+ W* C9 G* \
//--- first index - batch size, must match the batch size of the input tensor
/ A% C8 z2 K! X' Z* M9 y//--- second index - number of classes (up, same or down)
. L0 k  @) p2 G7 A# h/ |: b0 nconst long output_shape[] = {1,3};. x5 P0 u. F4 l; ?+ {( E+ C
if(!OnnxSetOutputShape(m_handle,0,output_shape))
7 T5 c! K9 N+ \5 e8 `{! K; _) v' B' o6 X+ B
Print("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());
* n! U) n+ \: F  R0 {return(false);5 U( h6 K1 i* B
}
% p5 k7 L1 g  v0 ]) A//--- ok! _' o9 a( [5 {5 k6 D' ~* q& i0 X+ N
return(true);- P+ s- K, M! u! P! g5 R
}, {: w0 L6 K4 Y2 }: E8 x$ U1 i
//+------------------------------------------------------------------+
! }/ Z8 p/ I$ c7 T/ m//| Predict class                                                    |
7 p% Z  q) w' F& g//+------------------------------------------------------------------+
8 ~  i& B5 i. O: {, s/ i) }' b: f; lvirtual int PredictClass(void)
  c: h8 M  T) C, O' a# ~7 x{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-22 23:46 , Processed in 0.913231 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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