私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?  I2 H/ P, ~- t# M. D. w
在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。
. A: E+ `" Z% \8 E, V我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进3 ?5 I, s, l( _
//|                                             https://www.mql5.com |
) j) _9 ?- s7 O/ l' x$ \//+------------------------------------------------------------------++ z4 B! @: a* D6 j- N! w, R1 }9 {
//--- price movement prediction
7 y% Z9 o/ }( }#define PRICE_UP   05 H# G8 U6 v! v& q( _
#define PRICE_SAME 1
  X1 e7 k: L2 ~, U6 Y+ O. b#define PRICE_DOWN 26 X$ ~6 S2 q" V. I* S9 Q
//+------------------------------------------------------------------+. X3 Y1 W9 t9 a7 {( {+ G/ R& P
//| Base class for models based on trained symbol and period         |
/ j) t7 z4 k/ X: N; z//+------------------------------------------------------------------+
. [; Q& l0 D: Oclass CModelSymbolPeriod$ R, n* x2 {' f! O7 t: z1 F
{7 ]) l; z! f4 @$ E; V9 l6 {3 g6 y+ a
protected:
$ b3 A8 y- v& t' E; plong              m_handle;           // created model session handle
' m0 j* {1 ~. C# Astring            m_symbol;           // symbol of trained data
" o4 Q  p4 D- CENUM_TIMEFRAMES   m_period;           // timeframe of trained data9 ?4 R8 N  P. f: M/ Q
datetime          m_next_bar;         // time of next bar (we work at bar begin only)) Q- n1 [( n2 r9 D8 V0 y2 B9 ?
double            m_class_delta;      // delta to recognize "price the same" in regression models
4 B- J) u1 ~$ M3 @public:
) ]5 v* @- y$ `8 C) c: A6 s//+------------------------------------------------------------------+9 i0 \( M; q  y3 d
//| Constructor                                                      |1 U, O% Z, ?) x: Z
//+------------------------------------------------------------------+8 y( @! D" f2 K& w- q" Y1 k. V
CModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)
7 b% @( ]  S% H7 U+ O( D{" H, c8 K9 i, S
m_handle=INVALID_HANDLE;
5 {/ Y* }" ]- i4 x0 [! Y  Nm_symbol=symbol;
- u& j4 k7 U+ }6 M9 w. jm_period=period;/ L0 ^2 h! H. F% u) Q1 A# ]
m_next_bar=0;
% M: K$ `/ Q# bm_class_delta=class_delta;
& H( w. R# U' t! l  n}
  }5 ~( [3 S5 L! @) w+ h//+------------------------------------------------------------------+
' j: F9 j- ?) X6 v( t//| Destructor                                                       |
3 Z( b5 Z; v; e0 Q3 M//| Check for initialization, create model                           |
! b6 r" M% y. I//+------------------------------------------------------------------+% q% [; W- D+ K. ~. t  ?
bool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])' l2 o! i$ X4 I  Z0 W
{8 q* ^( b* ]: ]- I$ y5 L1 q) X
//--- check symbol, period
( p" V: \" u! j: L' W4 V# W- hif(symbol!=m_symbol || period!=m_period)  [6 j, o, Q$ [
{2 b9 @- ]9 t9 U
PrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));5 a, W0 F7 r0 t0 G2 C% K2 M
return(false);: w% B0 F% e$ ~* d- Q' L- ?5 n
}+ p% F2 Y2 J6 y
//--- create a model from static buffer/ ~' P% }& |, J
m_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);4 s6 ]6 W: n. ~0 u% }2 _; q( E  W
if(m_handle==INVALID_HANDLE)
" F. p% ?4 f* W$ h  x+ s{0 c) |6 F2 N' {/ O6 c* E
Print("OnnxCreateFromBuffer error ",GetLastError());- B! I" p1 N1 f; f, B; |
return(false);- {9 T- S& \9 \7 P
}' t+ d" }! I) f# @7 e
//--- ok; ?! i& I+ i4 A& ^  ]6 d+ j
return(true);9 d/ ~% @# u# F9 `
}0 p2 P0 F- Q# |7 d1 C. o" J
//+------------------------------------------------------------------+5 G! l8 D- f5 U
m_next_bar=TimeCurrent();
( F% j$ \0 z% r% x; @2 Fm_next_bar-=m_next_bar%PeriodSeconds(m_period);
% R4 ^- z* T2 G. r6 p9 p3 km_next_bar+=PeriodSeconds(m_period);) r( ^) W/ ]0 B0 ~( A2 P
//--- work on new day bar3 u( R. z. J- [
return(true);
& Q! K  h& f# T" q1 x( V}
* K3 a& W9 c( y* k8 _- i( a//+------------------------------------------------------------------+$ \- \, g+ o$ j. B; M( ^2 z1 A8 d$ ~
//| virtual stub for PredictPrice (regression model)                 |' |- y" [: z. V3 r
//+------------------------------------------------------------------+5 k) x1 d4 b5 Z& q# |1 E6 \
virtual double PredictPrice(void)
$ p  N3 z7 Z; W4 |6 I# a{
! M  E, L: J2 ]* O1 J3 |return(DBL_MAX);
% S* F! x$ b! Q- O3 }1 ?}- D0 b* q% F2 B. |2 v+ }
//+------------------------------------------------------------------+/ f* P2 [% o7 R1 U- G3 D2 h8 V5 e1 _% w
//| Predict class (regression -> classification)                     |& U5 R/ }7 o6 x# k: _4 s
//+------------------------------------------------------------------++ ^% r$ u2 Q8 a0 G2 R' T+ R* G0 s
virtual int PredictClass(void): {( I8 j# `8 N+ @, l# m3 n7 e- Q4 |
{
2 e9 p. r3 D" ~. A+ H0 Adouble predicted_price=PredictPrice();7 h& Y; O% N0 o
if(predicted_price==DBL_MAX)4 N+ [8 a) p/ x( x0 `0 p3 N
return(-1);
  U" @0 d) h9 q  y. V+ m! vint    predicted_class=-1;
5 _* m' \; I( N1 X8 g" H0 `+ Cdouble last_close=iClose(m_symbol,m_period,1);
  r% j, \4 s& y9 b//--- classify predicted price movement/ V7 w  E3 D1 o3 a* ^. _- G0 B* ?
double delta=last_close-predicted_price;
  H# H* _8 s' Vif(fabs(delta)<=m_class_delta): U2 X  L- s6 v3 e8 ^' s# _/ v
predicted_class=PRICE_SAME;2 y+ X4 u3 y1 M8 |$ u& z0 q
else( N: C- w/ u6 h# k& e
private:
5 \  V$ D! s# cint               m_sample_size;
! i/ J* i, B1 q* F9 E4 B- k" e//+------------------------------------------------------------------+0 m* P. l/ d& d! n7 b: q
virtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)$ C5 C: j( I1 _; b" f; a1 M
{
% b7 |) M1 P4 S2 `9 h//--- check symbol, period, create model
' V9 N5 U& s. Oif(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class))
! ~1 W% c( [4 i4 u4 e6 A: |{
9 J4 B4 [; f" f7 z0 OPrint("model_eurusd_D1_10_class : initialization error");
- L$ i; k! ~6 @& J; b8 greturn(false);
4 z& `0 H, k8 J) J6 }& l3 y}
, c  g& y1 ~. N0 Q; J4 h//--- since not all sizes defined in the input tensor we must set them explicitly
" b5 R$ v6 R8 U+ `1 k7 J) K//--- first index - batch size, second index - series size, third index - number of series (OHLC)& T, a' p% P% s6 Y" d5 L
const long input_shape[] = {1,m_sample_size,4};( a9 v- a$ P: F* C* y3 q- a5 H
if(!OnnxSetInputShape(m_handle,0,input_shape))2 Q8 P" |& A; D
{
/ m7 b' @' v" k9 m5 CPrint("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());+ n3 P$ ]( g8 L# p( E6 H! r
return(false);
  C: I" i5 R3 P1 t$ w}+ P3 r! C5 P" W& F
//--- since not all sizes defined in the output tensor we must set them explicitly
4 g1 G6 E* T" B) D//--- first index - batch size, must match the batch size of the input tensor
9 n. {% N6 h% z2 X7 }//--- second index - number of classes (up, same or down)
9 ~+ f1 j9 e$ h" b' T& v: E% d% oconst long output_shape[] = {1,3};
5 o: p# I# k! F# v7 Zif(!OnnxSetOutputShape(m_handle,0,output_shape))! B0 B' p3 F# @0 b6 k% @) J" ?; V
{
, a2 o; p- @. m( CPrint("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());
2 b' Z; u) h9 n3 ~# [3 dreturn(false);+ Q$ s4 ^- {9 t) ^: z- ]# M3 u
}
% C- g2 I, _8 a7 @: ^: t//--- ok
! x+ c! D( Q; e# |) creturn(true);" @4 o, U6 S6 i$ z
}' M# r9 c2 {# {/ w$ V1 J9 A3 ~" A) b8 N; r
//+------------------------------------------------------------------+. V9 ]( _3 \" i" [& s7 S
//| Predict class                                                    |
. m+ _$ J/ |0 Y. D! l//+------------------------------------------------------------------+- Y  ~; x% Z, ~
virtual int PredictClass(void)
$ M9 r4 `' N5 w! \3 g2 D5 W+ d3 ~{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-10 19:49 , Processed in 2.060178 second(s), 32 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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