私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?
3 _$ h) m# @' s: S  m6 @在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。& l/ N4 l& P( `4 }" R
我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进
6 S7 l8 {4 o& ]* }* [3 j$ u//|                                             https://www.mql5.com |
# Q: V7 M7 y. b//+------------------------------------------------------------------++ o. W' U! |9 }* C
//--- price movement prediction% c2 p$ w7 |8 Q  f0 l6 f
#define PRICE_UP   06 }) k( Y+ [2 |1 e9 o/ q
#define PRICE_SAME 15 h# x2 q5 O% V9 B, `; h  `' S
#define PRICE_DOWN 29 V  u# a/ G$ \$ P
//+------------------------------------------------------------------+. g2 ]6 c9 t" J* t* S
//| Base class for models based on trained symbol and period         |8 ^0 V- K& |( Q
//+------------------------------------------------------------------+" ~$ R2 a$ E+ C5 y+ m
class CModelSymbolPeriod
& v/ @- _8 Y  d{
/ R# `$ k" p, v5 d3 i4 nprotected:
% w( W& J- W, m- h" w& rlong              m_handle;           // created model session handle
8 q! h( C6 ~  b. C% f9 ]string            m_symbol;           // symbol of trained data$ f8 Q: p( v; c
ENUM_TIMEFRAMES   m_period;           // timeframe of trained data
- ]1 D6 ~% i0 }' k9 B% v, ]3 ddatetime          m_next_bar;         // time of next bar (we work at bar begin only)4 ^) t+ H% d1 e6 d
double            m_class_delta;      // delta to recognize "price the same" in regression models5 [/ X: c6 A$ T  l' t
public:
$ T) d/ R8 h7 M//+------------------------------------------------------------------+  H6 g' ]8 h" O2 m) @# ?
//| Constructor                                                      |
% p9 g7 \9 u0 K# O//+------------------------------------------------------------------+
9 A% Z/ g2 I. ?6 ]- ICModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)3 q/ V7 p. V7 S9 A. s0 K
{
0 M9 s' v. b- B* j' Km_handle=INVALID_HANDLE;
. v; n6 {: D8 q* _% Dm_symbol=symbol;
; w  M, c7 ]& V( \% b$ Um_period=period;
# h  }( ~  z* @4 n& q. a, I  n# R1 cm_next_bar=0;
% z7 w+ T* R/ n* S4 H4 o7 n% qm_class_delta=class_delta;
2 D; B) {* B# A9 c}  G) `, Z$ p" ?3 ^
//+------------------------------------------------------------------+
1 a; I% G" a  X//| Destructor                                                       |1 I. g! v2 M, o- n* p3 D
//| Check for initialization, create model                           |- h( X: W' `0 D4 N, T
//+------------------------------------------------------------------+
. ^  j! n" H, o: M$ L3 k$ \: obool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[]); w% X4 |- }/ u8 N& c
{
) q) S% Q5 G* A. X, r//--- check symbol, period
/ f7 E4 |' i* ^( J9 v9 w) ]* Y. dif(symbol!=m_symbol || period!=m_period)8 C) h' R+ R* P5 A0 i' t1 |- U8 {
{
6 ]: j# H3 _% XPrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));
) B: }) m) k$ P4 ]3 _& N2 P2 Kreturn(false);
% ^1 I# k" _+ u% ^' j' e}
& m  r1 E, {. _0 q8 j9 j9 y//--- create a model from static buffer
& g4 {1 ^& |. w  o- r# Gm_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);& }% j4 a% F+ u5 `% W) @
if(m_handle==INVALID_HANDLE)5 a6 b2 p8 b) k
{, M8 o! Q) V! s
Print("OnnxCreateFromBuffer error ",GetLastError());% Z. O! t* g/ @  k( `
return(false);5 @+ f; _. t- r0 u' V/ g0 g' D
}& a* v0 @! A1 `9 ~/ Z/ f
//--- ok! ?8 I+ S* d4 r3 Q0 W1 M) {- g+ s: a
return(true);
& B$ j  ^0 h' A% X  {6 u1 Y}- R( R7 A: h8 }2 B- Z( g3 P
//+------------------------------------------------------------------++ X0 g% D6 M" F, T5 E- d6 b
m_next_bar=TimeCurrent();
% _. u2 b3 l/ i2 b3 cm_next_bar-=m_next_bar%PeriodSeconds(m_period);7 |! l: e5 S3 c
m_next_bar+=PeriodSeconds(m_period);
9 c# c' U' {, v0 x  W4 l//--- work on new day bar
1 S; H! t0 t5 ireturn(true);
! o' J1 x5 e0 Z) F. c}
( r! D: V- d8 r9 }  Q//+------------------------------------------------------------------+' F9 K6 N5 w! T2 p1 h5 P
//| virtual stub for PredictPrice (regression model)                 |
1 {+ O' W$ d) W1 E7 \) n//+------------------------------------------------------------------++ v, d* B* n# w; m
virtual double PredictPrice(void)
0 N0 z' F' y) t4 C# n/ X{, q- _- R; _& t& y
return(DBL_MAX);
& C+ H4 U, L/ o* ~( M0 Q}
6 H+ H, k  B$ A3 G" F//+------------------------------------------------------------------+6 g6 S7 m# G- e; @
//| Predict class (regression -> classification)                     |2 ?$ W2 y3 e# @8 I
//+------------------------------------------------------------------+
- F5 A( [, k0 t/ L! ]3 Ivirtual int PredictClass(void)) i, V' R+ e. a* }8 i7 N) ^. ]5 M
{8 s' y( ]/ |, m
double predicted_price=PredictPrice();
6 ?  U6 y6 |% B# G: t- D" mif(predicted_price==DBL_MAX)  s4 _7 Y3 L) A  B4 ~4 @2 J
return(-1);3 l" f8 r- F) S" B
int    predicted_class=-1;4 Y2 V- H( A" w- Z1 L% V
double last_close=iClose(m_symbol,m_period,1);, J  b' w6 {3 W
//--- classify predicted price movement
' R$ O6 Z& ]2 D" d  ~double delta=last_close-predicted_price;
/ e: w* t$ c- i6 Wif(fabs(delta)<=m_class_delta)
8 C/ a& M+ _: ~# t* e0 J" |1 `predicted_class=PRICE_SAME;8 }# @/ p% A3 A0 O8 t& t8 r  A
else
* C  ~5 _2 l- y0 k5 ^, `  v! pprivate:( V" V5 Y: m: W8 W! c( z! ~6 B  c
int               m_sample_size;
( q* W6 o  ?+ U1 U0 U& D//+------------------------------------------------------------------+8 ?& l) ?" m7 g* e9 w; X
virtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)0 L. K# l& R$ v5 \, l
{
4 a6 P4 @* v2 f. m* D2 ?$ M" @//--- check symbol, period, create model, m% k) k1 u6 p/ a1 \% s, x
if(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class))! t; u7 x2 L; X$ U. J- s
{
  E3 z4 J& H) I7 j; YPrint("model_eurusd_D1_10_class : initialization error");
! }$ e8 h  S% \5 ]0 Q0 w, rreturn(false);
" Z  R7 r# m! j' h3 L}( v( _3 P3 G. s
//--- since not all sizes defined in the input tensor we must set them explicitly
  o5 \0 Y; L  C  M; B1 k" ~' g//--- first index - batch size, second index - series size, third index - number of series (OHLC)
8 ]3 T. @; m4 v% z/ hconst long input_shape[] = {1,m_sample_size,4};- Y, R. r1 @8 m0 O
if(!OnnxSetInputShape(m_handle,0,input_shape))
/ y/ a6 _; r1 y& T% e- n, g{
5 K: ?  D- H# N( D% o) K, S& pPrint("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());7 j! v9 I: M+ z* T7 k
return(false);/ j0 ]3 H. ?8 F8 L* L
}( ~( [) h. e* C( S8 W
//--- since not all sizes defined in the output tensor we must set them explicitly
6 s4 I& }, ]: E* x: r- G/ s//--- first index - batch size, must match the batch size of the input tensor
1 ~5 @$ i7 B4 D, h4 w//--- second index - number of classes (up, same or down)
. {9 c' {! j$ D9 _const long output_shape[] = {1,3};+ o5 S; R' E7 g
if(!OnnxSetOutputShape(m_handle,0,output_shape))
* N* K* D- m  M. W" f& x{
6 B5 b2 Y( o( q7 I- qPrint("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());
1 J* B' \# h1 d# yreturn(false);
; I& c7 R0 e, E+ S8 b  \}
# k/ \; H- J" _: X! f$ H/ {0 W//--- ok- I+ ^  l% O/ j# r. W$ d8 @; W' s
return(true);' }. O3 i7 e: T  ~( r& X( u: n! q
}8 ~9 j: I5 L8 M" L
//+------------------------------------------------------------------+; m* r, \% ^# }: |; ^) F
//| Predict class                                                    |* O  a8 R5 g* e) E) \6 L3 o& Z
//+------------------------------------------------------------------+
+ b- w. I5 `. a1 y! a/ T( v) uvirtual int PredictClass(void)
" d- V7 N; \2 s  g{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-10-21 07:30 , Processed in 0.374244 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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