私募网

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?
8 a5 V5 Q* x( j% I+ o在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。( \' I' U$ j0 P( u# E8 k$ W8 x
我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进1 O5 J7 R( r- f# K! b
//|                                             https://www.mql5.com |
, L2 n5 O( x# y//+------------------------------------------------------------------+; ~$ f" P$ H- v; F4 G0 A
//--- price movement prediction
1 E. e' z1 ]& C#define PRICE_UP   0" ~7 l. e( H5 c5 x
#define PRICE_SAME 1
$ R* j; e7 J# s7 B1 y#define PRICE_DOWN 26 Y0 b, I2 H0 m' z) V4 h+ C. H& c
//+------------------------------------------------------------------++ P. g9 i/ W2 ]* ^$ }1 u, ~
//| Base class for models based on trained symbol and period         |
6 ~7 x3 o1 e/ ]. e8 H4 ?# B$ F//+------------------------------------------------------------------+
9 D0 n/ \  k) ~' y: c) s  `class CModelSymbolPeriod
% _/ y- U% H$ |6 w{
, I5 l' }5 u) R% A1 b8 aprotected:
  D1 s% _3 a! a3 r3 `- F( Elong              m_handle;           // created model session handle
5 b* O/ G' I! i4 ~) Q1 l$ ^string            m_symbol;           // symbol of trained data/ a& |# Y2 `& Y. e4 l
ENUM_TIMEFRAMES   m_period;           // timeframe of trained data
& B: C- O; _; wdatetime          m_next_bar;         // time of next bar (we work at bar begin only)4 x, S; ]1 S0 j& f
double            m_class_delta;      // delta to recognize "price the same" in regression models
9 l/ q( z& ]; a7 t; n) F6 `' Apublic:* }; q4 {: u2 A9 J7 N$ I+ y2 S% J) _
//+------------------------------------------------------------------+; x7 _& l' v; ]
//| Constructor                                                      |; O. P: P$ X+ ?  e: h
//+------------------------------------------------------------------+" O4 m, T) m7 ^% c
CModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)0 `1 `- v4 I8 o! q7 D! |$ H  K
{, m) M- x& H- r9 r
m_handle=INVALID_HANDLE;4 l0 |; E$ s* j4 D# {7 }% S; ~2 q5 e
m_symbol=symbol;9 L+ n1 s3 ~) o# }! l6 Z  f
m_period=period;
/ m/ ]3 |6 U1 R3 q  v  fm_next_bar=0;9 {) Y% m. C9 L( l& }! `+ F9 z
m_class_delta=class_delta;+ l: C$ s5 H; k6 A+ p- }* S
}* y1 w. m* M) `8 S) s6 B8 x
//+------------------------------------------------------------------+0 ]/ k) O$ h8 g! F$ ^
//| Destructor                                                       |9 N5 W! p) r. m* M* B. d
//| Check for initialization, create model                           |5 C9 I7 o/ J- D6 I
//+------------------------------------------------------------------+3 D1 {7 u1 O! }: Z- t4 Y/ ?
bool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])
% t& w& p8 \: ?: e* c0 L, o7 r" u{
' J, _1 Q9 ]- Z//--- check symbol, period
8 t. ~6 K. s: t8 k8 [if(symbol!=m_symbol || period!=m_period)& }3 m- A" m1 Y( l
{
/ Y3 J8 y! A1 x& p7 J9 IPrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));! O  O, _) R/ C; h
return(false);
1 S- b- |2 v2 `5 P# M}
: N8 @  t/ {, H0 M* E' Q5 ^//--- create a model from static buffer2 _, ^' L, l$ `. a4 T9 m# f/ ^
m_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);1 ~& B* K* o2 G2 m' e8 `' p0 q
if(m_handle==INVALID_HANDLE)# F2 T7 J8 o; I& p1 A4 Z9 c) z) @
{
3 U- t. \" @! k" S0 pPrint("OnnxCreateFromBuffer error ",GetLastError());
7 f- I! T, H2 g/ j8 B5 q/ Xreturn(false);
7 Q$ t( s+ ?( c2 F}
7 d1 z- |7 B9 m2 _6 g//--- ok
' b2 L  m( Q3 P$ W6 |return(true);
8 ]8 B  j) k' Z+ A! I0 q4 T! ~}9 |3 B+ ?& l; Q, h- r) ?
//+------------------------------------------------------------------+
  B9 b9 N6 Y) Q; w% h! f9 E/ Gm_next_bar=TimeCurrent();/ t* z6 A0 m+ J' D
m_next_bar-=m_next_bar%PeriodSeconds(m_period);9 N- s0 p2 ?8 k
m_next_bar+=PeriodSeconds(m_period);7 Q. w. S" U# D7 ~" F
//--- work on new day bar
$ `' e% q1 Q- e  Preturn(true);% s) Y% T+ P0 p2 i* I+ Z& e) g2 B# M
}
" M" |4 K$ R# H2 R; u//+------------------------------------------------------------------+
  ^& Y" e" R( {9 n8 q: |//| virtual stub for PredictPrice (regression model)                 |
4 K% F* R( E* i: I% L/ H//+------------------------------------------------------------------+( o& {- N' W! X+ v# S: k
virtual double PredictPrice(void)' v. |; f1 r6 N& f* l; t$ `* ?( Y2 A8 v
{; q& n" E) t5 ^" N4 r
return(DBL_MAX);5 I& U( B7 K* A3 u/ u  V. ?5 w' v
}
$ }" ]8 [$ ^) c2 [- A//+------------------------------------------------------------------+
) l4 ]! z# a9 N7 s//| Predict class (regression -> classification)                     |2 H; `' B7 |' W0 F1 W. k& H
//+------------------------------------------------------------------+
7 d1 @- L) }, \) u+ V. o  m" qvirtual int PredictClass(void)
/ i# E9 F% Y  _/ F- ?% p6 Y* {{; B! N: A# \) d; M) D2 _
double predicted_price=PredictPrice();
- P2 ], a. y# N. v3 a- p. Nif(predicted_price==DBL_MAX)  \, E* \6 r  V% v7 r
return(-1);
6 c- _. [1 X* J* v' xint    predicted_class=-1;( @$ q9 \9 Y8 ^; i. u5 B3 w. p9 S
double last_close=iClose(m_symbol,m_period,1);
' C% o. R6 B, j//--- classify predicted price movement
5 g8 g7 y) v3 E9 ?4 Odouble delta=last_close-predicted_price;/ X0 L- G; u" O/ r& M1 O1 h1 J( D* u
if(fabs(delta)<=m_class_delta)3 v7 a5 l' |% Y# w
predicted_class=PRICE_SAME;
; K9 r/ a8 ?$ ?; x" Velse, c' J5 G  O7 S  Y% G( S6 G
private:
& V* H3 A- L4 V9 ?8 u4 v8 Xint               m_sample_size;+ S: L2 U) A+ j
//+------------------------------------------------------------------+  w( C% \& o2 P, {+ ]
virtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)4 I9 U0 T! r( J8 ]" ?# e2 W
{7 x1 l1 s  c: Q+ F% G+ e
//--- check symbol, period, create model1 ]; T* B$ y) q$ w4 L
if(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class))
3 I6 Y) Q& {3 k: n, e/ |{
& J, D/ s) _( z/ w3 @2 a/ u4 YPrint("model_eurusd_D1_10_class : initialization error");% F$ |5 L: k/ z/ ^
return(false);
& C3 Z/ j" a+ v4 o; Z1 C}; Q2 u$ b8 ]3 g5 l$ v/ N& Q3 t
//--- since not all sizes defined in the input tensor we must set them explicitly
0 R4 X* z  `! A# n. |//--- first index - batch size, second index - series size, third index - number of series (OHLC)
  O* ?3 N' ]- sconst long input_shape[] = {1,m_sample_size,4};
  p8 P7 E/ R' \# N$ F& Y- a( Z2 Rif(!OnnxSetInputShape(m_handle,0,input_shape))
0 v2 w( G- n) _2 k8 x3 S{
- J. M. G' Z  q1 _: H; K% nPrint("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());( p; r, O' V  x* i/ U
return(false);* W' ^5 E% p5 ^- \1 l) b
}7 H* Z, {8 @2 v- L
//--- since not all sizes defined in the output tensor we must set them explicitly1 E3 l  `/ _, b" i
//--- first index - batch size, must match the batch size of the input tensor
* ^, E6 j3 O' v  u* S//--- second index - number of classes (up, same or down)- _3 y0 T6 j7 z7 [/ o- j5 r9 }
const long output_shape[] = {1,3};0 t8 G% l; q( X
if(!OnnxSetOutputShape(m_handle,0,output_shape))
& w4 l" ]2 ]7 k% E+ ^( M$ W% H{
- s) }( B7 c& `3 w# E! |) [7 ]Print("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());
, z, j6 N$ U9 c2 O5 u" [return(false);& O: ?- l- F" ~0 K
}9 X/ \' ~5 J$ r
//--- ok
. M$ [. ]. y0 `0 o7 A& |9 g4 X8 q- vreturn(true);/ X7 a7 D# G% u$ F2 d- `
}+ d1 Q( d; O4 a0 \/ |% t2 l7 A
//+------------------------------------------------------------------+) C7 Z5 R( A: j# t: t" _* Z5 `) H4 d
//| Predict class                                                    |
7 e0 |0 \" L: W- l. ]& r7 m; H0 k//+------------------------------------------------------------------+
3 y! ]6 l  a" x' `virtual int PredictClass(void)4 |1 M, X' D3 \  m* _' z
{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-26 21:15 , Processed in 1.204651 second(s), 32 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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