私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?6 l7 A& X, @: U! @/ V* ~
在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。
/ q" ]3 H" Q1 ?) z5 k3 M我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进* L3 B3 ]1 Z6 {2 B2 S/ h
//|                                             https://www.mql5.com |
- L( Q+ Y) S) w5 W8 p//+------------------------------------------------------------------+
) ?1 U% Y% x  _//--- price movement prediction8 Z' }) H2 M# {% t* ~
#define PRICE_UP   0# }) Y- l$ e; S2 W& c1 L$ a
#define PRICE_SAME 1
) [: I/ i& i) B6 X$ E/ _+ Y#define PRICE_DOWN 2
) e% x# e3 a- b- K' ~//+------------------------------------------------------------------+
4 X( n9 Y+ Y) J9 E! A; g0 H//| Base class for models based on trained symbol and period         |9 l& |0 @. l$ M% i
//+------------------------------------------------------------------+- m7 `2 d; |. T& N& q$ ~. p
class CModelSymbolPeriod0 _$ F: r7 p- f* Z* q+ V8 h
{' B; ?: @2 j0 p5 k  J' A$ P
protected:1 D$ W/ E+ N* v
long              m_handle;           // created model session handle
7 }5 O6 {1 u- J1 v0 N/ B6 h) Qstring            m_symbol;           // symbol of trained data0 N  K" T& a+ N' D0 r4 S2 H
ENUM_TIMEFRAMES   m_period;           // timeframe of trained data
- n" F) F6 ]2 S2 u' f& Ydatetime          m_next_bar;         // time of next bar (we work at bar begin only)7 h. z5 s5 C+ a; q! l$ u
double            m_class_delta;      // delta to recognize "price the same" in regression models
! _9 G3 n  B9 E  d. spublic:
: M2 @2 h) Z# l( u# `5 l; g, [//+------------------------------------------------------------------+
1 s# E, p  D  w% K! Z0 B//| Constructor                                                      |5 T* V& {" X5 E2 G; |
//+------------------------------------------------------------------+& a$ t, J& I1 p% q9 u
CModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)
! O8 X; M8 z1 E0 y5 O& v{
  I- F1 M" f2 [) d3 B4 _9 xm_handle=INVALID_HANDLE;
, t5 v6 L. T; Y2 [/ O- Sm_symbol=symbol;
0 l4 O; c% |. |0 V. h& m  S' [m_period=period;0 e/ Z1 C( v+ B
m_next_bar=0;
  p; }% q0 d7 K/ pm_class_delta=class_delta;
( |/ h: v  [2 e6 V+ z+ h}: w. {7 y3 q  M$ i
//+------------------------------------------------------------------+
) W6 m9 F6 u% g5 E5 d& s$ C//| Destructor                                                       |
6 h6 ]* G0 ?3 b//| Check for initialization, create model                           |$ n1 U  d/ G8 \# O, R: k! u
//+------------------------------------------------------------------+
! D; z! A5 Z- j; B" U% h- Q  r8 j/ v2 hbool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])
3 r# ^( t9 \; f+ t{( I/ Z* J8 R" p. H/ m" f
//--- check symbol, period, ^9 j+ q: N8 Q2 M
if(symbol!=m_symbol || period!=m_period)
: v/ a: R' E) C0 I9 }5 o{
: S* I' @" g, e& ]$ N4 G$ }PrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));
7 E% v  R3 y. s6 M2 mreturn(false);2 c9 M& I/ J  F9 L
}
- f) f# n5 f/ M. s//--- create a model from static buffer
9 k- \1 C) F$ im_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);' J2 C# s' F  }
if(m_handle==INVALID_HANDLE)
% ^) x# f! A; z" w( Y{
5 m0 A  ]9 h8 o$ w/ V7 ^1 gPrint("OnnxCreateFromBuffer error ",GetLastError());
8 o6 ^9 s, I, c: I/ [) Z0 Vreturn(false);4 e) _- Y) ^7 H  Q  C1 C2 @' ~- M
}
5 W4 N# v8 E- u2 ]& _//--- ok
* m* ^- a" b8 u0 Qreturn(true);
  J7 N* c  I9 R. V  k7 n- ?6 u$ H}
  U3 v8 z; D0 d//+------------------------------------------------------------------+
! ?" }) |9 g4 i4 m9 ^% Gm_next_bar=TimeCurrent();( A( p$ j# [6 ^! r( U8 e
m_next_bar-=m_next_bar%PeriodSeconds(m_period);
9 t9 u. u: c+ i$ \: R  F, e& D3 Dm_next_bar+=PeriodSeconds(m_period);
5 d, p+ m3 j! s4 p//--- work on new day bar
9 L" ?* l2 A$ f9 oreturn(true);' B) o: `' Z" B: Y
}. I& I. f6 Q3 ]" w0 R! t3 w8 @
//+------------------------------------------------------------------+7 Z) c7 s% ?: y7 w
//| virtual stub for PredictPrice (regression model)                 |5 G1 D5 W% `# o% _  ~- X6 ^
//+------------------------------------------------------------------+
/ M$ u/ F" i* E) `5 P7 Cvirtual double PredictPrice(void)
9 X9 i) W7 j9 z  g{
0 G4 ^" z: N* Y: N' Treturn(DBL_MAX);- y" R8 o- V; D- H% j2 `
}
7 G4 K* z8 ?3 l  Q* i/ I//+------------------------------------------------------------------+2 y* `/ ?: ]2 P* T$ ?
//| Predict class (regression -> classification)                     |
1 v0 F( M0 z! V" _2 S9 `+ m//+------------------------------------------------------------------+
' X' L4 U$ V: y% F# ]2 Q( E# Yvirtual int PredictClass(void)
, g' H6 s) u* y7 J{
6 q6 w) K' H$ y6 \9 x1 t! pdouble predicted_price=PredictPrice();, s- A4 P) `, y. y, ]
if(predicted_price==DBL_MAX)
# Z) N3 |- r/ C+ h  r8 p. q. Freturn(-1);
% C5 e" W/ b6 j4 S' A/ w: [& [int    predicted_class=-1;
' E0 K8 a* Z  `, w; Kdouble last_close=iClose(m_symbol,m_period,1);
! L+ t7 e( b: u, P5 i//--- classify predicted price movement
' y, E, a( e* n3 Mdouble delta=last_close-predicted_price;, H; X0 Z6 b+ i) _
if(fabs(delta)<=m_class_delta)
2 A  J6 b) y0 W: V6 Lpredicted_class=PRICE_SAME;
0 l4 t3 K& E; M5 Q; lelse4 ?; G. B# ?8 T: U6 \
private:" ]3 j/ k/ l8 T3 P2 g6 j3 }+ ?6 ]- O
int               m_sample_size;
+ `% F$ U( k, C//+------------------------------------------------------------------+) K. S5 _  A  P, J9 m) ]9 v
virtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)
( W- i' k& u+ o; a, K{
. j3 J9 {/ p1 v! Q  @" l4 t//--- check symbol, period, create model! u7 U1 Y. _, N. r! }) c- Q, R
if(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class))
4 a# Z8 D8 X" C: w. I. e{
+ a1 ]) c4 p+ J% ePrint("model_eurusd_D1_10_class : initialization error");
( [9 _3 w4 z$ }5 {, M% a% oreturn(false);
+ {8 S9 q6 E3 g4 S. _% q}
4 K* R1 O. C0 |5 a//--- since not all sizes defined in the input tensor we must set them explicitly
% m1 x. W1 t: X; p7 S//--- first index - batch size, second index - series size, third index - number of series (OHLC)7 T1 P# Q% S% b- q
const long input_shape[] = {1,m_sample_size,4};$ n( D& ^- _7 G
if(!OnnxSetInputShape(m_handle,0,input_shape)), |7 B/ \" P; z
{/ j1 ^2 r7 q+ e7 \: N. H
Print("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());
+ v9 |  H8 ~" @return(false);
$ U5 t% a/ U1 I}7 Y, S- O  E9 ?
//--- since not all sizes defined in the output tensor we must set them explicitly- m6 d+ D* K+ V9 Z# y% n9 g0 }
//--- first index - batch size, must match the batch size of the input tensor
- \2 E4 m: t  s8 X7 s( R6 L//--- second index - number of classes (up, same or down)
) f1 C* F: w7 ?+ H8 r9 s' P1 Jconst long output_shape[] = {1,3};
! t3 ~7 @  _& p- J  ^* C& A# zif(!OnnxSetOutputShape(m_handle,0,output_shape))' F8 g; J) \- n# W( p
{& k% ~0 x/ i9 f
Print("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());  @/ _; [. t( k9 _
return(false);) P# f1 r8 w% G5 q- l# P8 O8 K
}
! E7 Q8 n3 I% q4 w3 Q//--- ok
; A3 M1 q! j' X7 dreturn(true);
3 t0 H6 ^9 B# O4 A2 E2 r}
0 F/ G: ^, _2 H- Z+ S% L//+------------------------------------------------------------------+& V3 w3 U2 q" p- \( U
//| Predict class                                                    |
5 p" k4 c0 Z! P//+------------------------------------------------------------------+
& O4 B9 h4 L, F7 s8 J- zvirtual int PredictClass(void)  R6 {0 b+ Y3 O3 p5 q
{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-12 06:35 , Processed in 0.398341 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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