私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?
6 G$ r% ~* k. j* C# A在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。$ c! K1 M! c4 ~: d) u0 [+ P2 q
我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进2 s. C6 @% s/ G/ ]
//|                                             https://www.mql5.com |# Z5 }2 G3 }6 c, q
//+------------------------------------------------------------------+8 H0 S- c1 t3 A; Y1 P9 u3 U" L
//--- price movement prediction8 f1 b" C* s+ ^  ^3 z- r
#define PRICE_UP   0
7 \  i, n" ~+ n& D) \: S8 ?#define PRICE_SAME 16 A: v3 h# s" F: U, K
#define PRICE_DOWN 2
6 O4 s+ s3 j+ T/ k1 l/ c( ?! s//+------------------------------------------------------------------+1 N+ k* o3 l: a; T4 o  B
//| Base class for models based on trained symbol and period         |
& S3 i9 h- d; T5 P, Q//+------------------------------------------------------------------+
; Z; p* E3 j$ B9 nclass CModelSymbolPeriod
2 x& D1 ]' x( J8 L{$ }, ?' z& S4 ]* @2 g. Z
protected:6 w, }- r7 o$ ]: q: `( a2 z
long              m_handle;           // created model session handle
/ T# p6 K& n0 Y; E1 Estring            m_symbol;           // symbol of trained data
7 c, W* C/ S- I* bENUM_TIMEFRAMES   m_period;           // timeframe of trained data# ~+ ]$ o; m1 ^7 {
datetime          m_next_bar;         // time of next bar (we work at bar begin only)
1 \) d% V; j6 E# o$ f: O6 r1 Hdouble            m_class_delta;      // delta to recognize "price the same" in regression models
1 \9 t! n+ J. s% q. n5 D2 {5 Xpublic:
& z8 A! [4 G' a: V! V, R8 R//+------------------------------------------------------------------+8 M, l3 M. g3 u) E7 J8 c; s7 L
//| Constructor                                                      |9 y0 V/ F. |! [" Q. y- `
//+------------------------------------------------------------------+
2 f" f0 V, x% J  H* [- H0 }CModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)+ B, p: d+ s. L- X; C1 W* q- e7 n
{1 p3 @& }/ Y' J: g$ c# r
m_handle=INVALID_HANDLE;" n+ T4 d+ K2 U6 ?6 P& ?5 k6 F( V% B
m_symbol=symbol;
! C! z" _2 Q: ^1 z" |) a. Mm_period=period;9 s. J2 b" O- s' t# H' Y/ v2 e: K
m_next_bar=0;2 s7 t( z; _7 `( d9 ~" k
m_class_delta=class_delta;
7 s: i  ]; r- a5 @. H- s}: W4 i% b* a* ?5 f; b3 `
//+------------------------------------------------------------------+
" w* n; w4 u$ j5 |, ]//| Destructor                                                       |- l6 D* a1 m/ s0 i
//| Check for initialization, create model                           |
& e( h( r6 D0 P- |  M& Y2 o//+------------------------------------------------------------------+! W% x6 l5 }$ H! |, u
bool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])
. W; r8 E1 j8 s( _& Z* g{
- I2 q3 h# p/ b/ J1 @8 I//--- check symbol, period0 G5 O6 O2 ]) D: j& Y4 y- n& Q
if(symbol!=m_symbol || period!=m_period)4 Q! m. b; ?$ O' I  m
{( S$ v! b3 S" \! U6 B, [
PrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));
# s. V  J3 R8 ]- Rreturn(false);; N  d! d# P4 P
}6 K6 A8 F1 w# x8 R6 ]
//--- create a model from static buffer
# E2 [3 d. C. I8 f# z6 d7 Rm_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);- P; p# n) J: u& \) U# H4 y
if(m_handle==INVALID_HANDLE)
6 h8 ]& E& R5 |, E/ M* [! w{
. r  H4 \3 r; \4 F6 G1 n% mPrint("OnnxCreateFromBuffer error ",GetLastError());0 m" f/ m. N1 S. J; n2 ~! _; V
return(false);$ r$ E, k9 E& [$ Q
}
9 k& ~3 E( q' P* }- @; Z, v//--- ok4 h2 E9 C1 v) n7 G
return(true);, a! ^1 y7 q" ]6 ^
}
# h6 T% \! k5 l//+------------------------------------------------------------------+9 ^" N' i% J% N
m_next_bar=TimeCurrent();% q4 v9 O- n; ~" F& ^0 T9 r
m_next_bar-=m_next_bar%PeriodSeconds(m_period);
2 V, x9 i4 {& E. {m_next_bar+=PeriodSeconds(m_period);4 r3 D- Y! i& g! g) h6 U" R
//--- work on new day bar& m0 s0 F% n% a3 n+ K
return(true);+ \, n6 B1 |( t: [4 Z
}1 @8 Y. E. Y6 y# Q
//+------------------------------------------------------------------+3 u* J3 B$ D$ \% Y
//| virtual stub for PredictPrice (regression model)                 |. Y4 R4 Z; \9 s' L
//+------------------------------------------------------------------+
$ R2 g) G2 R6 \. s. l9 D% n  ]- Mvirtual double PredictPrice(void)
; P2 \4 g' G2 N  Z* G4 }% M2 v+ S{
, h; u1 g- t9 I  W8 n5 G% T2 m( Freturn(DBL_MAX);  P( b! n7 R" b( ~/ R% P5 s
}
! E4 @( a3 N) J6 b; V  h+ p' G//+------------------------------------------------------------------+, }) s+ C( c0 x: Y
//| Predict class (regression -> classification)                     |
. z) F' j! }6 {; }4 \; L7 z4 [- ]* }//+------------------------------------------------------------------+
7 s3 |1 E; }, g8 qvirtual int PredictClass(void)$ f' z- f  v; M+ }2 `- A$ b
{  P2 z' \# a( \0 b+ T% Y
double predicted_price=PredictPrice();  T" y9 m# W8 H1 W; F6 y
if(predicted_price==DBL_MAX)- J% v: h( z& _0 t) w
return(-1);$ \+ S- j1 v5 Z0 c, g. C1 m
int    predicted_class=-1;8 l/ _- l& c/ N3 D! x5 x
double last_close=iClose(m_symbol,m_period,1);; o' M+ [" u2 Q
//--- classify predicted price movement
7 g2 |# X, R8 G/ G: j/ C4 }double delta=last_close-predicted_price;
% A1 s+ N; d2 m( d/ Hif(fabs(delta)<=m_class_delta)
2 ^9 A7 s: ~$ v' }predicted_class=PRICE_SAME;
$ l- U4 ]0 c) ?' J: R; b, Y2 selse
5 c' z. ^, h. M4 e2 M# b) A1 |private:  B0 A- J8 j, m$ X% Y# T9 F
int               m_sample_size;
9 j& R+ h  {# W4 }# H4 n7 _//+------------------------------------------------------------------+
* W6 h1 y$ B5 I4 _6 ]virtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)
# W$ w& B$ [9 Q  ~3 H8 o{
0 d: v) P6 \9 D. J1 R% ]! P//--- check symbol, period, create model& C9 \8 W# M" q
if(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class))' c' u# Y6 M, W* Q0 D, j- y
{
" Y2 t# ^. X6 L8 P# cPrint("model_eurusd_D1_10_class : initialization error");  {9 B6 \. d% j7 G# h! j' ?7 l" M
return(false);
$ F: W; |; Y1 O7 M1 i. b6 t}9 e* w( S& `  u! H% h' F! Y+ e+ G
//--- since not all sizes defined in the input tensor we must set them explicitly- M: l, f- Y: w' K
//--- first index - batch size, second index - series size, third index - number of series (OHLC)! F: b5 x6 s& ~6 w, ?& q. E
const long input_shape[] = {1,m_sample_size,4};+ P# z9 H% b; y1 U9 ^' P
if(!OnnxSetInputShape(m_handle,0,input_shape))6 Y. ^& Z+ Y% M; `1 ^# |& B6 n! r
{
$ p( z8 I4 e  z- k8 ePrint("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());% F0 G6 D7 ^( L- T9 c) g
return(false);
5 q% m! J- G; k% X  P2 }. q}
, r6 |& r1 r8 U5 M0 u/ Z+ h//--- since not all sizes defined in the output tensor we must set them explicitly2 S- _5 N9 n5 p( U' {
//--- first index - batch size, must match the batch size of the input tensor
) o5 l5 O7 }! w//--- second index - number of classes (up, same or down)
6 S, K2 f5 x1 ?" G9 E! a8 `const long output_shape[] = {1,3};4 D, |; @! }$ B: I- `. g
if(!OnnxSetOutputShape(m_handle,0,output_shape))
! _4 G0 [+ G6 p0 ^4 D{, j8 c7 w! f( j/ S
Print("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());8 U* H% U$ _% X& r
return(false);
& a/ D: a8 T( I- t: Z1 ]: }}9 [7 V; |; x5 [
//--- ok
6 R$ [6 k: p+ c& m: qreturn(true);. V" h( l* s5 y0 h; G- l
}
' D5 o. r! E6 Y" A! J2 s4 U//+------------------------------------------------------------------+
3 a, A5 H' G5 F" Y//| Predict class                                                    |  Z9 h* B  L% e: V# Q  \* @
//+------------------------------------------------------------------+( {$ X, E5 d2 O  S1 _/ q3 D
virtual int PredictClass(void)
, Y5 K$ ~9 C+ ~{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-5 14:07 , Processed in 0.451209 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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