私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?
' S1 b' F1 r) D3 l1 u# W, X* D在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。7 M6 J) K# x+ W; y' _* w6 h: Y
我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进
; h$ c7 l' j" o  r, s/ i6 e$ y//|                                             https://www.mql5.com |" ?8 \: R/ ]* m8 f5 n4 A
//+------------------------------------------------------------------+
8 W) j& F5 q: Z% S  |/ b//--- price movement prediction
! @9 i% J6 w0 `8 g8 v% s#define PRICE_UP   08 C0 g: j( @$ `/ [" J
#define PRICE_SAME 1
9 q8 v1 T( c4 f/ I+ A4 G#define PRICE_DOWN 27 Q3 _6 P0 S+ U1 W9 P. d: s9 C' c
//+------------------------------------------------------------------+% Z! M6 I$ G* e5 b6 _9 g
//| Base class for models based on trained symbol and period         |$ W6 m) f1 ]5 ^& G. v, _! J% c. z
//+------------------------------------------------------------------+
7 n( ]% ^5 n, b5 R" W" P2 u" Vclass CModelSymbolPeriod  l' Q) ~* p; M2 l9 _- l! p
{
. Q" R' q8 H0 U/ ^" w! kprotected:
  q: r- T3 {! N. |1 Zlong              m_handle;           // created model session handle
$ e4 }  K) v2 L4 a" f+ Ostring            m_symbol;           // symbol of trained data& V3 \$ p* {! w6 R) E5 Y# I) X
ENUM_TIMEFRAMES   m_period;           // timeframe of trained data$ z/ K' W9 \! t4 A5 ]: ~; s' F! {6 C; @: Q
datetime          m_next_bar;         // time of next bar (we work at bar begin only)
! |; \0 j) g7 o6 Adouble            m_class_delta;      // delta to recognize "price the same" in regression models: J% W# d0 {" b: j" `
public:
; ]/ o& C. H* H//+------------------------------------------------------------------+% G( y! }$ O$ |
//| Constructor                                                      |- f3 M) ^+ [) c2 z
//+------------------------------------------------------------------+2 o0 Q# @( u! K+ m/ A/ |8 v3 b
CModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)
0 `8 |  f0 x, Z. N7 i; x$ e6 W5 e{
3 y4 `! a! l4 U: e0 Y( Lm_handle=INVALID_HANDLE;. L  z  ^* F  p% X% l; h4 a
m_symbol=symbol;3 p, X- Q  q* E, s/ n' W, @
m_period=period;
8 L( B- ^  u8 P7 V1 s% p4 bm_next_bar=0;
* l" A9 `! s/ c0 Lm_class_delta=class_delta;
5 Z$ }. q, S) j9 k# `}# W( V; @5 S# a# J7 L3 H  S
//+------------------------------------------------------------------+
1 p$ i: G% E0 v* G& k//| Destructor                                                       |
- b0 g7 ^6 g7 X  \4 A//| Check for initialization, create model                           |7 [7 O9 n9 T4 s+ J% G
//+------------------------------------------------------------------+
4 Y) \% \% @* u* Z+ n: F7 x( L! Kbool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])( c8 X! i% ~* Q/ E
{
' m/ r+ Y9 e, {6 e//--- check symbol, period
' B5 M2 |$ n3 j9 H( d) f4 k0 rif(symbol!=m_symbol || period!=m_period)8 b4 |5 `! P0 y3 v7 y
{
- k+ L; `. O5 H+ Z* P( I9 O' p5 WPrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));& }, z- M8 z1 }. g4 B' A9 g6 Q% Q
return(false);
) A! Q1 z2 d. b& F1 i4 `}  P5 |8 p8 c' Y: j: Z5 R; ^
//--- create a model from static buffer" H  Y( N$ y3 E! q1 t9 }- x
m_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);' z) @( P) ?7 B, b" w
if(m_handle==INVALID_HANDLE)( K( g# o' k% z$ {$ @( k
{
* N3 u# |9 Z, N" E6 w2 d3 L7 f; xPrint("OnnxCreateFromBuffer error ",GetLastError());
7 o( I' I" X( p) G9 e6 Treturn(false);1 e$ p8 w! \2 b# a( v
}
! ^" {+ ?$ r8 f7 I6 s//--- ok
! a4 u+ h) L. G/ ?6 }# Mreturn(true);$ j2 _$ h! L, Q7 f6 h) x) W
}/ o# \8 X( m: E" A2 o, z. L
//+------------------------------------------------------------------+6 [  _* y! B4 Z: n
m_next_bar=TimeCurrent();
$ N. \; f& ^0 ^! O% Sm_next_bar-=m_next_bar%PeriodSeconds(m_period);+ ?8 P& T+ }! Q+ r# f
m_next_bar+=PeriodSeconds(m_period);
5 u+ X$ u9 B( M+ @* p6 g//--- work on new day bar
5 T" |$ s: @! P1 o; breturn(true);
. c: U- u6 o: d6 e" t/ ?}
5 A8 C) c, a- y8 e: O//+------------------------------------------------------------------+4 e) Y6 k7 E6 d, \: U. t
//| virtual stub for PredictPrice (regression model)                 |/ n% Z4 _% F2 H7 Y3 X& H7 i
//+------------------------------------------------------------------+
3 g9 V. N4 k: D% B. U& h0 q+ {virtual double PredictPrice(void)
+ f0 p: r- h: q+ {1 J{( W7 p  [# m  _: I# Z% R9 D3 \
return(DBL_MAX);
: ^  {0 R5 q/ t. t8 l}
) z5 x0 @: f0 k- T! Z4 v//+------------------------------------------------------------------+! r! p% e2 D2 P/ D; y2 f
//| Predict class (regression -> classification)                     |! `$ W+ n5 g- N1 E1 c: u* m, `
//+------------------------------------------------------------------+
& n0 G$ s+ f4 ]! Dvirtual int PredictClass(void)
* k6 ?' Y; g: S/ j& U{: L* Q3 h" P! p9 j# ~
double predicted_price=PredictPrice();
# s7 }; o; q1 ~9 T2 U- |if(predicted_price==DBL_MAX)
! P2 k$ S7 Z8 Treturn(-1);0 _9 i) I/ o3 ^1 L% G& A0 B
int    predicted_class=-1;# z8 f9 c4 R0 ?& C
double last_close=iClose(m_symbol,m_period,1);3 y; d" Y2 l! p; ?4 Q3 O# u8 \
//--- classify predicted price movement+ L+ L0 N* S  l: u* S! D
double delta=last_close-predicted_price;" [& P- J; e( b9 O' ~
if(fabs(delta)<=m_class_delta)
; S$ i# K+ H3 E( e1 G) apredicted_class=PRICE_SAME;
1 G( O+ |9 r1 h- C& t7 T; _else
  U4 G7 Y6 q% z* Rprivate:
9 Y. ~* T" t. D7 Dint               m_sample_size;$ _1 l, i7 L1 A3 J" H# s
//+------------------------------------------------------------------+) \, v1 w5 \- v3 x6 d% A- ?7 w$ e
virtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)
: x8 [. {3 H3 {! ~8 Y{; j2 b4 X, b6 S8 a8 z' [6 o& b
//--- check symbol, period, create model
& b0 X: H% x/ [2 fif(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class))/ |8 R, t# I: G5 ?4 S! ]
{
. X, U' k$ d0 L7 Y) |0 U- fPrint("model_eurusd_D1_10_class : initialization error");
8 c. s% Q- Y5 P/ Wreturn(false);, T8 \# |& l3 f
}
$ d4 l( A. ^+ v//--- since not all sizes defined in the input tensor we must set them explicitly0 e9 U8 C) m% W4 I* R4 S8 g
//--- first index - batch size, second index - series size, third index - number of series (OHLC)
5 y! O# {# y9 x9 J3 Mconst long input_shape[] = {1,m_sample_size,4};4 N* g+ v/ P- H& i; a* `
if(!OnnxSetInputShape(m_handle,0,input_shape))( x7 c7 G2 A6 E6 a% A6 i; z+ n5 e
{
2 j  ?& j( i3 p& DPrint("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());
3 f$ U; h+ g; |2 Z* e! ~return(false);6 k/ o6 g* G! y: \9 u) x
}, g9 N1 R; a$ `2 I/ {
//--- since not all sizes defined in the output tensor we must set them explicitly  C" Y6 Z( n8 q) o
//--- first index - batch size, must match the batch size of the input tensor
" u8 c9 P$ h! X  m+ a( D+ t7 u% \//--- second index - number of classes (up, same or down)4 D" n- S6 _5 [6 b5 C
const long output_shape[] = {1,3};0 `8 T/ }8 N/ {& w7 ?. l
if(!OnnxSetOutputShape(m_handle,0,output_shape))
/ b, e2 Z6 Z5 g. \{
% \/ {1 V4 B6 k. x; DPrint("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());
; q0 G- c# g0 m# O. |return(false);" [- i: o8 s$ I( m" p* Y' I* K
}4 G$ l3 h+ h  f+ D+ x$ R4 T5 R' w$ `
//--- ok
1 X5 c6 y6 H) b5 _0 m% oreturn(true);3 B$ {0 D: z! a  E5 z6 b7 F
}% @' t7 h! O9 U1 l; m$ c& g
//+------------------------------------------------------------------+' @0 h0 t$ {: m1 e
//| Predict class                                                    |
$ e0 o$ F- K; |' m7 Y9 y; l, W//+------------------------------------------------------------------+: k7 u2 c' F2 i+ A
virtual int PredictClass(void)6 Q4 l% \& a. J
{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-10 13:41 , Processed in 0.523210 second(s), 32 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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