私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?( x  q3 I! Z" U: o
在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。
" G8 m( r3 R- A5 y% a5 H) l我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进4 v3 @# _" L6 A# e+ S0 W! q) u
//|                                             https://www.mql5.com |9 o6 ]2 V+ v2 q" e! o- a% j
//+------------------------------------------------------------------+
, H  ]( L& D3 n* O//--- price movement prediction
  R6 U* e  @; P+ q- M4 f#define PRICE_UP   0/ n" T9 K  L$ `" B$ o& R
#define PRICE_SAME 1' O9 ]4 l( K3 K
#define PRICE_DOWN 2
8 U2 V9 P4 Q& B6 ^2 |* d- T//+------------------------------------------------------------------+
; w  D# S" i4 B: \7 t% o9 X//| Base class for models based on trained symbol and period         |  U' B, Q9 f* F* A+ @- X
//+------------------------------------------------------------------+# E: W  r) {7 K7 X& P; Y' M9 U/ k
class CModelSymbolPeriod  s3 @7 A) U+ l0 v8 x* a9 R
{
/ g! i/ L! Z, K# Nprotected:4 x) \6 D) c& e9 o( d; [; r
long              m_handle;           // created model session handle2 \/ v5 U* Y  `' c0 E
string            m_symbol;           // symbol of trained data+ ~# h- T) }8 J: r' @
ENUM_TIMEFRAMES   m_period;           // timeframe of trained data) G3 K) u6 E# a) }' R, v# i
datetime          m_next_bar;         // time of next bar (we work at bar begin only)
. K4 a1 @8 ~" f( t/ {- N9 o& y# E. [double            m_class_delta;      // delta to recognize "price the same" in regression models
9 l9 C/ \) Z: c* `9 bpublic:) F9 N; Z6 l+ W1 c& J& \+ i
//+------------------------------------------------------------------+
3 u- z6 ^: B) l* s3 J( k: T( n//| Constructor                                                      |
! K# o6 j3 \) b5 `# F! M$ Z//+------------------------------------------------------------------+* U5 A/ e4 Z4 \- W1 k
CModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)
& U" C# o- K, F  [# b# R{4 e( N- d0 r8 {( T. p
m_handle=INVALID_HANDLE;) \9 X, n5 w) @$ x* t! _
m_symbol=symbol;
; f, D1 F( \/ g" m9 a+ R$ A. nm_period=period;
# a" O: \( r" B8 j8 O( [m_next_bar=0;
1 o: K$ K; X, U; Z+ ?2 Hm_class_delta=class_delta;
( [1 w" g8 ~' }! t6 X- c}
$ K% k$ r4 l, C1 U6 }//+------------------------------------------------------------------+
5 C, e/ q' ?& Q+ a) Z( `//| Destructor                                                       |5 d) J, N) {& K2 C/ ~
//| Check for initialization, create model                           |5 I+ Y7 k- m# ^+ s- v" v
//+------------------------------------------------------------------+
% x7 o* j4 s$ X- W+ [: R4 Cbool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])1 u: V. n0 X9 W* C! K
{- i% s) O- m/ g  a7 j, _& g2 J
//--- check symbol, period
$ j/ k: ]1 [1 }% k3 }+ j6 ]& Oif(symbol!=m_symbol || period!=m_period)
: E; \- Q+ K3 o+ \# \- E{1 \& f2 B# C* O# N( H; E- ~, q# a
PrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));
8 Q) y' k4 c# c& f# b2 g& {return(false);
+ t6 D8 s0 S( R; n  Z; T+ Z0 T}% m- x# T6 F1 q. U& L0 ^
//--- create a model from static buffer
1 H0 @7 K1 A0 [$ }m_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);
0 d* p$ W" U9 _. {if(m_handle==INVALID_HANDLE)
. q% J7 R$ k" l8 n/ W" r{
  X! N; g. D# F; h, l; \2 GPrint("OnnxCreateFromBuffer error ",GetLastError());
9 n8 J) N1 v6 N: s7 @. O) W& Xreturn(false);2 ^6 E: ?7 M& y/ q
}
( a$ a& Y! R4 ^1 u4 ~# u//--- ok/ k- a5 T; ~" q1 Q
return(true);: H& l" M! h/ M/ V/ j9 z
}7 d2 J( j' D8 r8 u2 O' p
//+------------------------------------------------------------------+
/ }/ S) _: _' \' w  u/ e5 s+ ~8 `m_next_bar=TimeCurrent();
& j' ~. G. J6 Y7 ]$ q0 nm_next_bar-=m_next_bar%PeriodSeconds(m_period);
3 R4 ~& }$ O- r9 sm_next_bar+=PeriodSeconds(m_period);1 u! o& Y$ J+ F# @+ ]; @
//--- work on new day bar1 l; e$ o7 \' v" v* F2 w4 x
return(true);/ n6 t# c$ k: f  L" ]9 {
}9 _+ [/ H' b& X3 V$ X4 J
//+------------------------------------------------------------------+
$ a; f6 M" x1 I; m//| virtual stub for PredictPrice (regression model)                 |7 E, n: |; R# U8 y3 j
//+------------------------------------------------------------------+3 B7 s. {! b7 I
virtual double PredictPrice(void)' L% e2 n0 k5 b! h1 C, j
{! M! Q8 L8 Y3 n, K3 R* W
return(DBL_MAX);5 H0 x* ]0 N4 i0 `8 b" }' Q/ j
}# A0 e3 j% N9 d1 |! s$ v! d
//+------------------------------------------------------------------+
7 c5 s7 p9 _; Y" w! x) a/ t//| Predict class (regression -> classification)                     |4 U5 `9 {# S) S* O
//+------------------------------------------------------------------+
2 u8 {+ M. U4 ?( Xvirtual int PredictClass(void)
; H& M' s2 L3 a0 g' r$ }1 f' f: L! O{
0 ?4 y; _1 n$ ?" X, N3 S/ Ldouble predicted_price=PredictPrice();5 e' w9 f# \" d
if(predicted_price==DBL_MAX)
; ]2 B) g% b+ w# P, Dreturn(-1);
- K8 T/ V- S: t* f1 A: F+ I/ f, ^# Lint    predicted_class=-1;" {* H8 u( N" b$ ?( z* r
double last_close=iClose(m_symbol,m_period,1);" |, g2 a5 a* J& X4 x0 r
//--- classify predicted price movement
( @) Z$ t' y$ x# F5 ]1 J+ cdouble delta=last_close-predicted_price;
' o) Z% k& y9 W' fif(fabs(delta)<=m_class_delta)
$ V, e2 F! l6 m2 n, o1 Dpredicted_class=PRICE_SAME;4 ]" X9 K9 L  k. Y( U  D  m
else
/ E9 M- B6 t0 {+ h% s* W' Q  Rprivate:
* P5 ?$ X8 D! l% B/ w6 T9 {int               m_sample_size;
$ p, v0 J  W8 j0 L//+------------------------------------------------------------------+; q8 c5 s4 S) p
virtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)6 m" @8 H$ O: F8 K+ K' D* y
{, ^8 Y7 K1 e& e+ {
//--- check symbol, period, create model4 ]3 Q1 Z8 ?9 J7 j" B7 y( c
if(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class))
+ P& U8 p4 A( i6 b0 L6 c{5 M& A; ^! I2 f9 ^- o+ `+ v2 B) r
Print("model_eurusd_D1_10_class : initialization error");2 u6 e, x9 Z3 o6 C" ?
return(false);
7 R- N- B( X8 S5 }: |}
# F1 W$ v. K& z& j2 K//--- since not all sizes defined in the input tensor we must set them explicitly
, ~6 u! e+ _- C. [0 E& p' V5 F//--- first index - batch size, second index - series size, third index - number of series (OHLC)
* Y9 |5 n" w% econst long input_shape[] = {1,m_sample_size,4};* ]; [. w3 {% }8 ^. F; Q
if(!OnnxSetInputShape(m_handle,0,input_shape))
! d8 N; B2 R) ^/ q' C0 ~{0 Q& z& n' t8 e6 P
Print("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());
# b# f6 G6 v- q! V" G$ L& qreturn(false);
; h9 A6 F6 x: V9 n5 p2 Z}# X7 W+ I$ M1 U
//--- since not all sizes defined in the output tensor we must set them explicitly8 U; S9 u, f' H6 d; c4 k
//--- first index - batch size, must match the batch size of the input tensor( i9 q. W+ z" `6 G4 w! t, y
//--- second index - number of classes (up, same or down)9 r) @+ q$ _' h7 J
const long output_shape[] = {1,3};
; e% Z: f" z1 }" T1 S: ]if(!OnnxSetOutputShape(m_handle,0,output_shape))
1 ]; y( G, E8 u! \& T{
; Y: u; a% d4 z) ?8 oPrint("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());
5 Y. l9 @1 ^  G) C$ Rreturn(false);
- h4 y% k2 p5 B' C" g0 L}8 j& `$ s. J$ ~
//--- ok
$ m3 H* n* z+ B# _return(true);! \' r% V6 K7 }+ g1 A# M
}
. }; H  U3 K% i& ?& I. ?  Q2 Y//+------------------------------------------------------------------+  o) S4 e- i# h8 ?+ q( W: b' Z7 L
//| Predict class                                                    |3 `8 Z- r, i/ X3 h
//+------------------------------------------------------------------+
7 d, ~& M; U  l; j  @% Mvirtual int PredictClass(void)
$ z: L% `4 l7 t& M- `- H& R2 s{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-7 23:51 , Processed in 0.396021 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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