私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?
+ }* V9 l1 k; d: D在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。
- J$ N0 X0 ]* a) W4 m& R2 Z. g我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进( L, p/ {0 C, N  x
//|                                             https://www.mql5.com |2 z8 h) j% @- f' _4 _
//+------------------------------------------------------------------+4 a) A$ I- ^: I8 s+ k# O
//--- price movement prediction
8 [# p/ u2 ~1 [& {& I5 L5 {#define PRICE_UP   0
6 H) r4 @2 O9 {( ]: i3 x! z#define PRICE_SAME 1
$ \' s. e% T) t3 V9 f. B0 [- v#define PRICE_DOWN 2
0 _- N8 w2 H$ T, \1 x3 }6 H  J4 K//+------------------------------------------------------------------+
2 b8 Y; L7 p8 I0 k: S//| Base class for models based on trained symbol and period         |
  Z& S+ m# l8 A  ]2 p//+------------------------------------------------------------------+$ }5 U+ X5 X  Z, z/ n
class CModelSymbolPeriod  L. c. b0 A# w, W. P
{0 m2 R) ?" C( N  R$ j
protected:) o& [2 Q# N  h: e
long              m_handle;           // created model session handle2 N- B% l# I8 J  G: u. Z/ D! f' {
string            m_symbol;           // symbol of trained data
& ]0 ?0 a" ?1 g' S+ _; a" F: Z/ }ENUM_TIMEFRAMES   m_period;           // timeframe of trained data8 }* F5 r+ n2 e
datetime          m_next_bar;         // time of next bar (we work at bar begin only)7 P' M. Q; h  X
double            m_class_delta;      // delta to recognize "price the same" in regression models1 a4 J. v8 @& N/ |
public:
! M4 a) _. s$ |$ d3 U- s$ d% u8 M//+------------------------------------------------------------------+
3 G2 H5 n' j; {; Y/ V//| Constructor                                                      |
3 W+ {! h$ i- G) ^) o2 J//+------------------------------------------------------------------+
7 S" f9 o5 z6 D1 M4 L1 OCModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)* \0 ]# n2 a, I3 G6 @
{
- u  ?$ N& u8 [: x# A6 L  Pm_handle=INVALID_HANDLE;2 t+ B7 g# I6 h" L% H2 ^
m_symbol=symbol;0 P% B' F  w4 I- O- t$ _: f
m_period=period;
( f$ Q/ c7 u9 c4 im_next_bar=0;/ Y: \5 H1 h) ?; v. n( a- h
m_class_delta=class_delta;
3 E3 {7 V! S4 i5 _6 L0 A}
6 s! M6 t5 k" u& Q//+------------------------------------------------------------------+
: s" E% X9 r; b" p; W3 d) p& k: R//| Destructor                                                       |
& e" o( R4 t1 ?5 K6 ?3 Y//| Check for initialization, create model                           |
* d: |# R* \; d, z0 q0 K3 t//+------------------------------------------------------------------+
7 X& G, G) i5 d* p+ K2 }- P9 \bool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])
4 d. b/ ]5 {" e{/ Q; F+ Y6 {2 h. F; C0 Q
//--- check symbol, period9 S6 G9 v& R1 v* h9 d4 ^; Q
if(symbol!=m_symbol || period!=m_period), X8 _6 G/ h: k
{/ s+ l! I& |, n; y1 j
PrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));7 O- S  F! U0 M
return(false);- X  V" u5 s' @* E/ N( ?
}
* Y2 D9 \2 Y: K+ E: N8 {% t//--- create a model from static buffer# a2 l5 W% t  ~9 I
m_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);. I1 |- c. h$ N* v$ d2 n8 U5 `  \
if(m_handle==INVALID_HANDLE)7 ], ~6 i! r) I% ~/ ~/ h
{) w) Z  J. Z# T8 V
Print("OnnxCreateFromBuffer error ",GetLastError());, q# K# a) g1 t1 Z
return(false);
# C4 u( ?! Y+ X8 ~5 S}
4 S" w* l4 |3 T6 }" D//--- ok. o4 M1 H1 o. }! E
return(true);) R2 x( @1 d  r6 j
}2 t& q7 X& Z$ Y6 ^# Q+ y6 s8 l$ M
//+------------------------------------------------------------------+
6 x+ I6 s- l$ ?1 l" I' km_next_bar=TimeCurrent();
  K' K* l) A! b0 f5 q) ^. {m_next_bar-=m_next_bar%PeriodSeconds(m_period);
- I5 `1 V1 Z* i: Y: U3 X. N3 Bm_next_bar+=PeriodSeconds(m_period);
) S, B9 W  e. L5 d) n; \" h8 ^/ o//--- work on new day bar+ ?8 ?4 F/ _, G) ]% w% t8 K' b
return(true);
1 V( N: A- ?  S( B6 a}
5 [* r7 y1 E9 u$ _. F7 \//+------------------------------------------------------------------+
: W/ Z& d& o1 }6 s: O//| virtual stub for PredictPrice (regression model)                 |6 H2 t7 R* R7 f
//+------------------------------------------------------------------+# L( r( K4 G# V7 W7 \
virtual double PredictPrice(void)0 [. U& I  F: r* p
{
/ @' z, w9 Y/ Areturn(DBL_MAX);- D- c5 r* i4 c, M
}0 H1 ?( e. X, o8 p- u: ]
//+------------------------------------------------------------------+
* ?& `; M9 v/ ?! Z//| Predict class (regression -> classification)                     |
9 z0 ?. l; t! q) V: P+ M/ O8 a- T& w//+------------------------------------------------------------------+
" K& z, b2 v5 }0 vvirtual int PredictClass(void)
1 ]& ]" U) w5 o6 h% C{
/ k$ p& S/ @. k! _double predicted_price=PredictPrice();6 y. k9 k, v6 n, ^& e9 Q
if(predicted_price==DBL_MAX); z1 k5 f3 f( S2 j8 A" B0 _
return(-1);
% R$ S' D4 L5 }int    predicted_class=-1;# T# H, X5 j! j* h2 ~0 d0 F
double last_close=iClose(m_symbol,m_period,1);( d+ U5 g6 l" a
//--- classify predicted price movement
, _) K6 r6 F* ?# L9 p" T% b( edouble delta=last_close-predicted_price;# {. G/ k7 e0 s6 w( w7 u
if(fabs(delta)<=m_class_delta)
+ J& R+ |5 S$ opredicted_class=PRICE_SAME;
  q7 v" @9 b; A7 gelse
# x5 j/ K9 z8 a* x/ ]: d& Oprivate:- c- [# G# ~- c0 u( ?
int               m_sample_size;
, @- B8 ^; ?2 _//+------------------------------------------------------------------+
4 r1 a2 e# i9 i6 l" fvirtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)0 y+ p" Q* g8 ~$ f: g6 S) d/ k
{
! y/ [& }( D) h! Z* }. y1 h  P//--- check symbol, period, create model
# p4 S: G/ d7 I. |if(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class)); Y8 F5 n) \9 f. {" b
{
+ J- N, P: f3 r5 YPrint("model_eurusd_D1_10_class : initialization error");0 \4 R) J& T2 s7 ~
return(false);. n& C# G& Z2 Q  d& D6 [" E
}
3 ?( S) m: ?1 _1 g//--- since not all sizes defined in the input tensor we must set them explicitly8 K5 B7 s) @: e7 i' F. a4 o
//--- first index - batch size, second index - series size, third index - number of series (OHLC)
5 S9 b; I# d: c2 _2 Z$ i" Oconst long input_shape[] = {1,m_sample_size,4};
* u! Q  V: H8 m- }& ^4 Wif(!OnnxSetInputShape(m_handle,0,input_shape))
6 A& M6 B5 c! W8 A{& [1 h, s" E8 R& ]
Print("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());; n" H2 M- u" g) R0 e+ r9 w1 \
return(false);+ B4 M' O. |1 ]% P
}
  `+ A/ ?# |  X) c, Y//--- since not all sizes defined in the output tensor we must set them explicitly( S' e  F" e! F2 H1 l
//--- first index - batch size, must match the batch size of the input tensor% V8 q6 @( G* ^" Q1 P4 D9 c
//--- second index - number of classes (up, same or down)
. N% z  u5 t) a6 i9 L/ l7 jconst long output_shape[] = {1,3};8 F) {7 O/ N/ u) `' N# W9 V& ?( y
if(!OnnxSetOutputShape(m_handle,0,output_shape))) d4 y* y9 k7 g0 X
{4 h9 H8 V) ~; p) H: b+ }
Print("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());
, X, J, w9 C3 jreturn(false);
! M. e% ?3 I4 R0 @! w}: z6 {7 [7 @  b& c
//--- ok
" w& \  g# o1 |return(true);. P5 b5 ], f# K: H. Q" A* D( R
}& f  z" E2 @$ M
//+------------------------------------------------------------------+
4 g* E' X$ B9 Y' p( o# d$ o- t! O" b//| Predict class                                                    |" i. V5 X: ^8 V- i: A7 V
//+------------------------------------------------------------------+3 p  {4 E) |. h
virtual int PredictClass(void)/ {! x0 o4 }, j5 |
{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-17 06:11 , Processed in 0.433539 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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