私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?
& w: c3 J8 I5 F- d2 y1 U$ O在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。/ T4 w0 a5 U% }# [
我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进* ^- ~, p) M0 F: p* p  s
//|                                             https://www.mql5.com |8 H$ X7 Y9 u" P. w8 B
//+------------------------------------------------------------------++ l( l# l  B5 `, _* V% y
//--- price movement prediction. Q, j( J# V! \6 u
#define PRICE_UP   0* v1 R' g8 N. V2 D5 j
#define PRICE_SAME 1
+ w/ E* U# Q" C* Y. a7 g" J8 A#define PRICE_DOWN 2( f6 b: X+ ]$ k+ h" k! p
//+------------------------------------------------------------------+
. X1 i* e3 U/ L7 f  \0 w8 }//| Base class for models based on trained symbol and period         |: n+ `% J& d4 C7 V) H8 Y+ x
//+------------------------------------------------------------------+
3 q% H* K7 ?- w+ Z' _- g% @3 a3 B1 Nclass CModelSymbolPeriod7 q7 @  L. B$ O6 W  u" h# d9 e
{
5 @' Q) d$ G. I4 V% N/ Oprotected:' ?) e  s  I, a7 j  _
long              m_handle;           // created model session handle
2 x; k" i& J( }5 e8 {* V* W9 @string            m_symbol;           // symbol of trained data' ~. V& l. }( B4 t  G4 M
ENUM_TIMEFRAMES   m_period;           // timeframe of trained data. }4 X6 ~0 a  Z$ U" h; `& k
datetime          m_next_bar;         // time of next bar (we work at bar begin only)! L7 b% N3 X# u/ U7 E5 q
double            m_class_delta;      // delta to recognize "price the same" in regression models
6 ]' o, z3 @8 A- f6 `public:
6 Z/ a' o* A, ?: D& ^% D//+------------------------------------------------------------------+2 e) ?  U/ a. D( t
//| Constructor                                                      |
! W# T! L7 V3 ~- {3 D, K* j& S! `! o//+------------------------------------------------------------------+" t2 c& v, P3 G8 P2 t) V
CModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)
' N' B. I7 X. R2 T) N7 ]2 L8 L( c5 P) [{
; k- W: r$ N5 _m_handle=INVALID_HANDLE;- B) p% H- a* q- ?
m_symbol=symbol;; B% m9 A% @2 a) @- V: ^9 b. a" L
m_period=period;
+ g1 d! \) u- E; B. hm_next_bar=0;, g( p1 R6 ?1 D* w% n5 ^2 R
m_class_delta=class_delta;: n4 ^5 t+ F  i& c) u. L
}  ]: `& \+ i3 _! b; n" k) n
//+------------------------------------------------------------------+" W) K& j; T7 Y0 Z& T) s
//| Destructor                                                       |
1 I6 ]7 g0 f3 j$ l+ m* w; p9 f5 L//| Check for initialization, create model                           |
3 ?; _! M. E# _//+------------------------------------------------------------------+
9 e: h1 n, D; N" a5 v. }$ d/ Ybool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[]). ?0 K  H: j0 a6 ?6 O; A" l, |
{
: j! G5 K& |- G3 Y7 E//--- check symbol, period
1 m6 ?) g1 ]; Z9 }4 q+ ]: B0 u0 Mif(symbol!=m_symbol || period!=m_period)
; L- }; g& o- t6 O( L{0 K+ A+ x" S; v9 ~1 f) s
PrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));
4 |$ I0 c4 Q( ureturn(false);5 P! [9 W1 a/ ^* D3 v9 G, ]
}
9 i- I/ n7 a4 v; H' T//--- create a model from static buffer
3 `5 ^5 m+ v& z# gm_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);& U! i7 F$ f: t1 H' P
if(m_handle==INVALID_HANDLE)
- ~$ r9 ]5 X9 K, J/ }/ T9 u* w{( Q7 o+ W: k. D
Print("OnnxCreateFromBuffer error ",GetLastError());3 C: |( a' }0 P/ [5 q# S
return(false);8 a% G; W. A, M( Y$ j
}
( Q) b0 t! {0 A( \+ u3 P; Z, F//--- ok
* S. B2 L4 e6 K8 `: H* q$ Ereturn(true);% r* G/ D. c$ k/ v
}# |2 A+ p! N9 z0 W# w# K& C7 K
//+------------------------------------------------------------------+  r* o0 F; B( l  r! Z7 w
m_next_bar=TimeCurrent();
3 p0 s( H1 j5 l$ o6 K9 a5 r+ Cm_next_bar-=m_next_bar%PeriodSeconds(m_period);# Y# a: _' L& @( P9 W
m_next_bar+=PeriodSeconds(m_period);$ w/ G" \+ h% h: |- J5 V! ^2 n
//--- work on new day bar
3 _% @+ @, n' F6 ^3 L) [; M* k$ Q9 jreturn(true);, S( J& k' k- L9 H% J9 c5 I7 x
}
5 o; q3 T$ P, a9 r- g! X# ?//+------------------------------------------------------------------+
! J3 ~2 ~) h4 F, g; l* ~//| virtual stub for PredictPrice (regression model)                 |. I3 b; Q- `* [7 `0 y8 j$ Y
//+------------------------------------------------------------------+
: V; J$ [) j" \' xvirtual double PredictPrice(void)1 o$ Z+ t9 W* k3 a
{; [* N9 j7 X/ Z; H1 s
return(DBL_MAX);
+ ?% y( E: D) x7 D. [% Y- _& u}& J1 A% p$ ?: d6 a0 N) o
//+------------------------------------------------------------------+7 v9 F' k  q0 z7 `5 Y+ J/ K
//| Predict class (regression -> classification)                     |  S+ k; t1 G6 H  c
//+------------------------------------------------------------------+
) {. y" S* B4 ], g& F# F& X' Jvirtual int PredictClass(void)# c3 E7 j1 |  b* m. }# \# e
{
$ T7 e+ |! w* y2 N! N; Edouble predicted_price=PredictPrice();
- ]2 C, {& g9 R" z2 B, t: _if(predicted_price==DBL_MAX)+ {% v* Q/ h! ^& W3 }) X
return(-1);* E7 I4 S2 `) a: ~4 K
int    predicted_class=-1;
- D' S4 c& y% }( Z$ Rdouble last_close=iClose(m_symbol,m_period,1);% \+ p0 e, x9 q/ j4 `4 m" V
//--- classify predicted price movement
* {' h% z5 k- ~/ sdouble delta=last_close-predicted_price;$ t+ a: n. h* n' ?; F2 B: U% M
if(fabs(delta)<=m_class_delta)
; M1 a: i& h' u6 ~8 Xpredicted_class=PRICE_SAME;
$ Z, Z  C: f; }5 H: [3 F4 felse: K, y, R7 _' T
private:
; C+ C, B2 A' `& n6 `9 rint               m_sample_size;
% k) j1 N. S1 z//+------------------------------------------------------------------+
) X5 j! u. j% b: _0 i! N) W- l0 _$ N+ Lvirtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)  W  S0 q; I/ B5 M, D4 I) b
{" j4 l: s: g# @0 n9 G- x: [( B
//--- check symbol, period, create model& E' G( O9 F* \% j  u
if(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class)): J: x* y/ X# K% a
{  n' ~6 a. T8 _* v
Print("model_eurusd_D1_10_class : initialization error");) R' K$ e& E$ ]1 Q! H5 i1 r$ X
return(false);
. Y' s& `6 H2 m8 T+ _, m& i}9 h$ q. b5 ?9 e1 }) r
//--- since not all sizes defined in the input tensor we must set them explicitly
# W/ i* T1 [0 r. l; y5 \//--- first index - batch size, second index - series size, third index - number of series (OHLC)* t, Y9 M" ~' |- f2 u4 H( F( E! b7 k& @& j
const long input_shape[] = {1,m_sample_size,4};2 R9 f/ y/ b0 O2 X/ A3 e  d* X
if(!OnnxSetInputShape(m_handle,0,input_shape))  a0 W9 u1 E; g" H# c
{% }6 ^* n) s9 z4 S
Print("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());8 H2 O+ e& x1 E$ x2 Z8 P2 m: d) J
return(false);& C/ X2 z! {6 y, X( x
}& I0 D$ t5 q* i! {: }
//--- since not all sizes defined in the output tensor we must set them explicitly
8 m7 G4 l: J/ u9 J2 M2 c! ]: S  v) B//--- first index - batch size, must match the batch size of the input tensor: q3 G5 X% E8 b3 k$ c+ m% j. m  c
//--- second index - number of classes (up, same or down)1 S$ p8 P. S: y5 {3 \2 O6 }
const long output_shape[] = {1,3};& x' y5 Y" h1 i- U; r9 b7 L
if(!OnnxSetOutputShape(m_handle,0,output_shape))
1 f8 R2 K8 s8 k( A5 A% e  t! _6 l{
3 J8 Y$ T1 r+ l% ~) yPrint("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());. v3 I; y% R& U! v7 b' }. y' I
return(false);
7 a0 d) P5 G0 b" G$ r}6 U+ D1 E1 O% k6 W
//--- ok6 O7 P  f) A$ o  }6 F
return(true);$ J3 ?- q5 v5 ^4 o5 x! B
}7 c) A2 i* _7 l6 U
//+------------------------------------------------------------------+
0 D4 V4 `# L  D4 j  {//| Predict class                                                    |
# c% N* [6 R4 l) E0 z% ?* ~//+------------------------------------------------------------------+
+ ?4 p' _- \- A5 @2 Svirtual int PredictClass(void): V& v) @  |  N. t8 B; h, h
{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-6 09:43 , Processed in 0.408715 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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