私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?
( b% Q8 V& a: L& v# E2 j) ?在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。( r& }4 E- j2 ~0 a) M! z+ U
我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进  N6 A; ?' O' w5 c* e0 C5 [: q; O
//|                                             https://www.mql5.com |
; S& A2 `1 K+ Z) J5 X" R//+------------------------------------------------------------------+' f* x) d$ g; |" j+ G! Z
//--- price movement prediction7 O0 \+ O, T& j7 J* B" A7 A" n/ H
#define PRICE_UP   0
" d4 @. ^" F0 g, }1 `3 q#define PRICE_SAME 19 {$ |; O5 Y- {9 g
#define PRICE_DOWN 2
9 B% t4 E4 s! |4 n7 `//+------------------------------------------------------------------+( j2 [8 {/ Q' I3 g# W# [  p
//| Base class for models based on trained symbol and period         |
7 G% I% d/ E9 h# O4 o//+------------------------------------------------------------------+6 W/ T) L2 @& x$ w2 _
class CModelSymbolPeriod0 }' E7 T% F% T2 z4 m7 w6 j( H
{5 Y8 T# j5 j8 U6 m! @$ [, Q
protected:; O3 W4 Y* B7 B+ r- o" y
long              m_handle;           // created model session handle
2 z" k5 A$ q! P0 `4 `string            m_symbol;           // symbol of trained data# \5 I$ k/ y: t2 {) q' r9 S( b. x
ENUM_TIMEFRAMES   m_period;           // timeframe of trained data
8 q, ^/ a, \! m4 J& [datetime          m_next_bar;         // time of next bar (we work at bar begin only)- A: b8 z  U6 t8 P
double            m_class_delta;      // delta to recognize "price the same" in regression models
' D' X6 }" y9 ^public:
% `9 @1 c6 h& P4 |" B7 Y7 K//+------------------------------------------------------------------+
0 j5 S8 d3 m7 k0 s' L5 Z+ P//| Constructor                                                      |: W6 J# w& G& p1 s
//+------------------------------------------------------------------+" S7 ?1 ~& ?1 B, q& O
CModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)
, E7 J7 }7 G, _$ S: a3 `/ [. a{
/ H8 G8 M1 q# B! pm_handle=INVALID_HANDLE;
& \, Q/ ]1 v( j+ `2 Im_symbol=symbol;$ G9 O5 p5 l5 {& i* j
m_period=period;
2 }! c6 L4 |3 J: Dm_next_bar=0;& c+ w4 Z5 n+ M3 K
m_class_delta=class_delta;! f1 d+ d. P& ~& v# {1 n
}
5 L! Z+ R/ g/ V9 N% M//+------------------------------------------------------------------+
/ ~7 Y' ~: z8 x: _9 R! @4 k//| Destructor                                                       |8 I4 R) _, q5 |0 u2 V4 R
//| Check for initialization, create model                           |
/ ]4 v1 ]/ }& {6 u: `) A( k' x9 Z, _6 N//+------------------------------------------------------------------+
2 Z4 S2 h8 n! hbool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])
# j" m/ H  k9 S4 q{
* V' U6 H9 d6 S- P//--- check symbol, period
' K: b& x+ C: s8 c8 }% m/ rif(symbol!=m_symbol || period!=m_period)9 q/ I* }( `( x1 {( u
{5 Y9 F4 `- D( _' u% `& G2 E
PrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));
- ~8 v; f" h* w# R( v" [8 e  l) Sreturn(false);
+ \8 `: i! x. ^}! Y% Z& A. m$ ^9 r, T
//--- create a model from static buffer
4 V: l5 W4 B' A1 K8 Tm_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);
7 c' B2 A+ t9 A) Oif(m_handle==INVALID_HANDLE)/ B7 q$ H( R, V9 s/ D; J+ U' l! g
{
  X5 x' N' P3 J; l! RPrint("OnnxCreateFromBuffer error ",GetLastError());
7 x' S6 ?+ y3 p% Yreturn(false);
0 }1 E: t& `- s3 k}/ D: W( _, m2 _+ H( r* T- Q
//--- ok0 V! L& A3 A) u: I9 |- ^
return(true);# r" U0 v& r5 |* f& ]0 a% |2 Q
}
2 L" o8 ?; K3 o//+------------------------------------------------------------------+
6 b2 C- t3 V! O) |* T3 @! Sm_next_bar=TimeCurrent();* H' d* W6 s0 ~3 L- t
m_next_bar-=m_next_bar%PeriodSeconds(m_period);6 B5 u& v/ o: [, r5 s5 \) ]6 D( v
m_next_bar+=PeriodSeconds(m_period);$ b& {4 w. i# M0 ^
//--- work on new day bar
$ I0 [6 r/ h" G4 O: v  D9 \( t3 ereturn(true);& u& A- p- a- j
}
3 u6 e  ]8 {. l/ L5 b- S$ r# x//+------------------------------------------------------------------+
% V$ U' \" m3 r+ r2 A//| virtual stub for PredictPrice (regression model)                 |+ `! S% A' n3 G- O2 o) e
//+------------------------------------------------------------------+: x# N; Q' X2 q) k$ M. n; i8 ~
virtual double PredictPrice(void)& {8 u* M# |$ k, L/ u
{
0 K/ o- k( O" M6 y& Ureturn(DBL_MAX);
( D0 F% U5 R0 d: n}
* f' r: J3 {1 ]: y; l8 R* d//+------------------------------------------------------------------+- r7 u& ~& |: ?. o) S
//| Predict class (regression -> classification)                     |' N$ A% z: M! x% x- j9 Q
//+------------------------------------------------------------------+1 d/ E1 P0 k$ O: y
virtual int PredictClass(void)
3 P; A. e7 h! `& ~{
1 m- \' s, [8 ^, adouble predicted_price=PredictPrice();
0 `7 }/ v. K& A) Eif(predicted_price==DBL_MAX)/ z* X  ]8 c5 N* K% c
return(-1);
# R% K: ?# u; h$ Y0 Rint    predicted_class=-1;
- p- x* }$ V; }, z$ O0 {# Kdouble last_close=iClose(m_symbol,m_period,1);, j! |# r1 ~0 E4 D# i
//--- classify predicted price movement
2 D7 h5 d$ f8 a' `+ P4 Z+ Udouble delta=last_close-predicted_price;
) K  r" E! r, bif(fabs(delta)<=m_class_delta)
8 R( n9 t% L7 H) E. V5 Cpredicted_class=PRICE_SAME;" N" k0 W4 ^! }" j6 v$ |
else# }0 L2 W2 b% C2 P
private:
& K5 n/ K/ q" @( P# }! ?) E/ Rint               m_sample_size;
( w2 x2 o9 e& ^+ R6 t  P1 t8 x//+------------------------------------------------------------------+* {4 F; N& t2 n; g( }& {
virtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)
9 S+ W: j2 N8 q% |  ?0 n' y{
7 E: Q8 x  [& G9 ?. S, P//--- check symbol, period, create model) m% l& G2 V1 W) \
if(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class)), `7 o1 y. _# n) U3 O5 Z& X. e
{
" @" j# W9 b0 H: i* DPrint("model_eurusd_D1_10_class : initialization error");; ^8 }( M+ K6 V
return(false);
3 c2 u; l; Q4 \! G" g}
0 B+ l& l0 J# H, n* H//--- since not all sizes defined in the input tensor we must set them explicitly
: }7 [3 L, v" V2 v. I//--- first index - batch size, second index - series size, third index - number of series (OHLC)
# y% K8 @3 `3 z+ n+ Zconst long input_shape[] = {1,m_sample_size,4};
! O8 a, _) C9 [. a! Q1 t& Q; a+ ]$ Fif(!OnnxSetInputShape(m_handle,0,input_shape))+ u) o+ t6 |( J" B6 {0 Z% a+ q5 b
{0 d+ G5 v; [) c- x0 n
Print("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());' R) j0 y( `) i3 W* C+ l6 t
return(false);
( j- p9 i. U  ]}2 g% L0 r  g! K- Y
//--- since not all sizes defined in the output tensor we must set them explicitly
) e# `0 O4 j- D  z# T* k3 K//--- first index - batch size, must match the batch size of the input tensor- \2 S( _( g! t  O. U) d2 t  i
//--- second index - number of classes (up, same or down)
& t9 ^1 \4 J9 F4 n3 D  P4 uconst long output_shape[] = {1,3};
1 ~2 [. }. [- p3 V! g6 W) A1 m: X$ sif(!OnnxSetOutputShape(m_handle,0,output_shape))5 n, A+ L  I! p( U# I5 m& {  N, ?
{) R+ h9 f" y2 B2 r
Print("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());
7 f3 P  d; {$ H3 @/ Y6 freturn(false);, V& Q. K+ q2 z/ P6 p
}( d. l) C/ w6 u3 ?8 J
//--- ok4 H! C  q7 \7 H8 o+ H# \
return(true);
  [3 g: L. I+ t}/ y3 m" n6 o. _; y
//+------------------------------------------------------------------+4 L% p7 X. y; h
//| Predict class                                                    |. d- V) k. \* x$ X
//+------------------------------------------------------------------+5 ?3 f$ I$ V6 ?9 S% N% T0 K  ~4 b2 U
virtual int PredictClass(void)
4 [+ p3 ?* Z; H0 |' g. s{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-7 00:41 , Processed in 0.617110 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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