私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?) R6 e! }! f) `% |' U. ~$ }9 V
在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。! `& M% D: _4 v. Q  I; t3 u( O+ a
我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进
8 y/ }8 m0 }) H//|                                             https://www.mql5.com |
6 T; V* U2 n, A, h; n4 x//+------------------------------------------------------------------+. K. G9 p. U7 j: w' I9 N
//--- price movement prediction) L" E: P/ }) i7 C% }7 A
#define PRICE_UP   0. s  }7 S) n$ A3 v5 z0 x
#define PRICE_SAME 1. \9 @. [* [! ?' V; M
#define PRICE_DOWN 2
: D' a# c- f  Z! U; H3 T+ Z* X//+------------------------------------------------------------------+
( L3 Z$ \" {4 t- S( f  x//| Base class for models based on trained symbol and period         |: _8 N+ ]& f) X0 g1 O  Q% u
//+------------------------------------------------------------------+' U% C2 X0 j+ h1 R% K  T% f2 _- P" W
class CModelSymbolPeriod
" l0 {9 ]. _$ K2 d{
: [" e+ ^/ q4 K- wprotected:3 Z1 D& G% N: W$ x, u2 B5 j3 }
long              m_handle;           // created model session handle: H+ K, ?. P  o: w% c- r. A; a
string            m_symbol;           // symbol of trained data
5 H7 j& @2 E2 s( DENUM_TIMEFRAMES   m_period;           // timeframe of trained data+ N" E+ G4 J% z
datetime          m_next_bar;         // time of next bar (we work at bar begin only)2 C  V) K) S5 }1 u/ `* p8 D
double            m_class_delta;      // delta to recognize "price the same" in regression models
2 N1 C6 K5 R, r: A& M/ ~) e) t) Ipublic:  s+ p4 F2 k% Y; A
//+------------------------------------------------------------------+. @8 y* f- ^6 o
//| Constructor                                                      |
! X; {% M& x+ \/ Y: v//+------------------------------------------------------------------+
. b! t" c2 j- x5 qCModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)% ?* A; ^" U: I4 o! g6 R
{
4 V' H  V; [8 vm_handle=INVALID_HANDLE;' c: \/ U6 C* H. E4 a
m_symbol=symbol;- F& S+ j& Q9 E& R# z: d" [" ~" x
m_period=period;5 d, ^2 `% J& V9 O3 n0 v: s
m_next_bar=0;
' G+ z1 l) d! k7 ~# k2 p" tm_class_delta=class_delta;* a- S9 J0 ?9 |- z1 W8 U1 w
}9 Y$ p. N/ y: @2 y- v' Q3 u
//+------------------------------------------------------------------+( Y; y% o6 H+ p7 x$ K& V+ G7 i
//| Destructor                                                       |: B  e% V5 P6 k9 s
//| Check for initialization, create model                           |- m# S2 @8 s% w. A! g9 q
//+------------------------------------------------------------------+
* h5 ?( ?. f9 ^- L+ Q* q! Obool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])4 a# _8 |& `4 W* h4 l# @
{' {/ K, [3 `: p
//--- check symbol, period- T5 F: t) l, Q- }" q; O
if(symbol!=m_symbol || period!=m_period)
* I, x# C* \3 y{
" ]% ~8 X8 u1 e+ l! iPrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));
/ I" R# a; }% R0 v6 [return(false);( P* E& M3 u6 A; ]* x; i% R" X# V
}6 U  H# {' q. C- A" l* D( U& k
//--- create a model from static buffer/ c7 J" X8 T0 B0 U. u* K
m_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);( k3 v6 N6 O2 A: G) ~2 {' Z1 p
if(m_handle==INVALID_HANDLE)) f) d* j4 M* Z% C- o+ p. {
{4 o8 o9 m8 w" K* Y. ?2 e& A. n2 W
Print("OnnxCreateFromBuffer error ",GetLastError());6 O. N7 e4 I6 O7 H" d! [# y- H% M
return(false);
$ E& ?" k5 N4 x, d, N) e2 o}8 f! k! G' z' B6 `1 D
//--- ok/ d! v  g* l2 O3 M
return(true);
" E% t# ^% i! S% U$ S+ l" Z1 p9 u3 I}7 P( A/ Q* [. A; D+ N3 z% u" B' J
//+------------------------------------------------------------------+5 ^0 S5 R4 \$ J
m_next_bar=TimeCurrent();1 t2 R& j+ U7 i$ Y" Q
m_next_bar-=m_next_bar%PeriodSeconds(m_period);
4 ~+ ~6 ^! K! w; P, xm_next_bar+=PeriodSeconds(m_period);( L3 n0 ]) C6 n  L# C9 G  c# n) X
//--- work on new day bar, }+ L/ X) g% N) y; r8 [9 S
return(true);- [( c* t" ^# Y  y# `+ I
}
& B# Y7 c) O4 Y4 a//+------------------------------------------------------------------+
/ D, K0 v# s- Q& h. W+ ]//| virtual stub for PredictPrice (regression model)                 |
( D! k9 n2 V: ]9 f7 n( |//+------------------------------------------------------------------+
% e0 V9 w; i; P7 Yvirtual double PredictPrice(void)
) I1 O- r: F: y{: {6 D: j2 x) y; d+ A) A+ M
return(DBL_MAX);# P3 S' d+ z0 s5 [0 J
}
2 ?# w* j/ [, D& z# Y3 z- D' K* d//+------------------------------------------------------------------+) n2 e7 u* w7 a7 b1 ?  L
//| Predict class (regression -> classification)                     |2 I' S* l. l8 w! s
//+------------------------------------------------------------------+! `, G" l* h& x% D8 f0 M
virtual int PredictClass(void)
) C/ ~; H$ r; H: u0 @5 W- q{/ s; b4 g# ^5 L
double predicted_price=PredictPrice();% Q- R+ J0 H" @0 Y
if(predicted_price==DBL_MAX)
1 n! a& }0 [. wreturn(-1);
  k' V2 @- h9 b' g% z% h- O; Xint    predicted_class=-1;
* _+ D& T- r5 K. j, Z9 ^5 Ddouble last_close=iClose(m_symbol,m_period,1);7 L5 V1 w! w! Z: K& M
//--- classify predicted price movement
. S) c6 Y0 ~2 q$ h# ldouble delta=last_close-predicted_price;3 u  k  ~# C9 e$ e! y; x/ l
if(fabs(delta)<=m_class_delta)
- ~1 u2 Q" X) z+ W3 d/ {predicted_class=PRICE_SAME;
( L. N! ]! x7 Q1 m; y7 kelse) A/ {1 x- S% s9 V0 |
private:
8 c# w- J5 i: w% S: Z1 R* G6 \int               m_sample_size;) [6 k7 ?' d% ?) X7 R7 z
//+------------------------------------------------------------------+
9 O! j9 @4 i+ N; f) t8 ^% p) bvirtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)9 s* u  o6 B! S% h$ F7 b! z  A
{
' x3 \: N1 Y/ K' K$ T+ m, U* S//--- check symbol, period, create model
' b0 a! e3 N0 iif(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class))
8 w8 q( I+ n2 M( g4 Z. y{
2 n, g6 C) v2 |/ E. F9 \$ PPrint("model_eurusd_D1_10_class : initialization error");; m4 O' y2 k7 i) F
return(false);: C' B, w. y7 a  V! `6 G8 @, T4 W& Y
}. V/ R# _; U& \) R7 n
//--- since not all sizes defined in the input tensor we must set them explicitly
7 D# P% p  t' h2 p//--- first index - batch size, second index - series size, third index - number of series (OHLC)( S3 k1 {0 t( D5 `
const long input_shape[] = {1,m_sample_size,4};% k1 Y0 C' m' }' T
if(!OnnxSetInputShape(m_handle,0,input_shape))
: W. l# n3 W' ?' u  `% j& ^7 P{
  P) L  Q; ~- P8 WPrint("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());5 }8 r7 ]" O# ?
return(false);( j' d9 r% a/ T) B  ~5 T& M
}
' h) Q  s* A% B+ w9 Z//--- since not all sizes defined in the output tensor we must set them explicitly$ d8 X4 q* L# h
//--- first index - batch size, must match the batch size of the input tensor
* v# }8 [8 }# p//--- second index - number of classes (up, same or down)
* n  P1 ?  c* k: G# Wconst long output_shape[] = {1,3};
( X, U0 S: m$ I3 l6 ~/ g$ B& Hif(!OnnxSetOutputShape(m_handle,0,output_shape))
- b" ^6 z& \: i+ ^{
6 |# t2 e. c/ g# R- oPrint("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());
$ J: B) B, @- r7 Kreturn(false);
, ]% D) ?% i- u5 u$ i! J  I}# y8 X- W% n) G: e: G
//--- ok
" _  e2 k0 s2 P3 j* L  oreturn(true);; E) A8 g9 i4 c" a4 h
}; H+ _$ I8 S7 K: n
//+------------------------------------------------------------------+
! [5 w* X) i7 u//| Predict class                                                    |
8 W9 N! r3 I# \+ d//+------------------------------------------------------------------+
5 P  s. ~8 w9 W& t5 w, U( z7 Uvirtual int PredictClass(void)+ e2 ?. ?3 q! m; i
{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-16 08:35 , Processed in 0.446695 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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