私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?
* ~6 C8 R9 i/ [* H在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。
" f3 Y. U' i! D4 B* C2 o$ H我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进- p7 o) g( t7 m' o
//|                                             https://www.mql5.com |9 H- J0 c3 d4 m8 Z4 |
//+------------------------------------------------------------------+
9 W: P6 ?9 M5 d6 C! d& D) Y( i//--- price movement prediction
# u3 S; J/ @+ i* R#define PRICE_UP   0# X% N/ v5 O/ P; d) w0 U; r) G
#define PRICE_SAME 1/ j. ?  k# q3 K
#define PRICE_DOWN 2
  d/ b5 Y7 I0 j. G2 m) i8 d, t4 m//+------------------------------------------------------------------+
0 h6 A" G0 W0 o7 ^//| Base class for models based on trained symbol and period         |
( _1 ]0 q' X' O3 S4 P7 W//+------------------------------------------------------------------+8 g! ^; L3 \; ^* d3 |, b& j3 v* P
class CModelSymbolPeriod
9 L- J, Y  O/ X( x% e9 F{
0 [3 H4 }: s0 n  E* u' ~protected:( y! j. G+ k, V( W  @+ A1 {
long              m_handle;           // created model session handle
% k- w! {1 d8 Q# m9 H/ ~0 t7 B3 Cstring            m_symbol;           // symbol of trained data& Q4 E* m. [5 q2 b) t4 {
ENUM_TIMEFRAMES   m_period;           // timeframe of trained data1 b" v. y2 H9 c5 V( y
datetime          m_next_bar;         // time of next bar (we work at bar begin only); V9 g/ K  E* b( W5 E
double            m_class_delta;      // delta to recognize "price the same" in regression models9 q  H6 D: e! O0 i' U
public:" m, N5 o& Y; ?" D+ r9 i
//+------------------------------------------------------------------+
9 l5 R/ J7 r4 y; B& r) [% k//| Constructor                                                      |
: P9 h& i" |" l) S- A2 T//+------------------------------------------------------------------+
: U5 k- F3 c, s7 ~( vCModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)3 B& Z# K# G7 J- g. S# `( {
{
1 l" q# ]- s8 m5 R) l, Z$ p' b$ Im_handle=INVALID_HANDLE;
. g) _7 d' F. A$ m' p% hm_symbol=symbol;7 t! ~" r0 Q6 W$ [
m_period=period;3 y/ \, R9 \" g4 D( c/ P- \5 {
m_next_bar=0;5 R% v# a3 f2 j! B1 f& [6 Q
m_class_delta=class_delta;5 \0 F& {9 V8 |: c
}* B2 F& P) Y! j& _$ u/ L
//+------------------------------------------------------------------+: w& C6 I' I9 ~+ y4 ^. X
//| Destructor                                                       |
" I+ W+ j6 P; x+ z//| Check for initialization, create model                           |0 c. N( q$ P$ e0 G( A( k
//+------------------------------------------------------------------+1 j4 }2 x* X# @; [
bool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])
. [0 Y8 S9 ?. `- g{+ X- W( M+ D. ~0 m
//--- check symbol, period
$ `8 O  M# s4 R6 xif(symbol!=m_symbol || period!=m_period)$ e8 z% Y# Q  G+ W
{8 r+ }8 o1 I3 S0 T! _  {
PrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));4 I6 Q6 _  c/ O- ]" T
return(false);
$ y7 \; q. ^' s) S}" t" T1 ~+ k( B, |2 `; Z
//--- create a model from static buffer
* _! e. _' C7 Z$ i# Gm_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);1 p) s, m" [' s6 a, P2 |4 q: P$ v$ F
if(m_handle==INVALID_HANDLE)1 D9 n4 m. ~2 ]" O* l( i
{
0 {' s7 X! \, E3 IPrint("OnnxCreateFromBuffer error ",GetLastError());$ t1 k) o  z* u0 G( d' q
return(false);
1 k" \" S* Z8 }% a( C3 q}
  S5 f- l+ N& W//--- ok
9 {4 r& X7 A. B6 Freturn(true);
  e; ^3 N; h4 x5 _, [}3 c( f) Q9 H: f4 o/ k' N! M
//+------------------------------------------------------------------+6 R# P/ z8 O4 a6 E5 j
m_next_bar=TimeCurrent();+ {2 N# ~1 Q- G+ I
m_next_bar-=m_next_bar%PeriodSeconds(m_period);
7 Y& d  Z7 ~% C6 Q1 K6 fm_next_bar+=PeriodSeconds(m_period);
7 ~$ N" n' p8 {* @* m  I, z  ^//--- work on new day bar! h  q1 l& @4 x
return(true);, B: Q$ g. {% Y1 v
}
8 R' H) i- ^! p//+------------------------------------------------------------------+
) N" @! A6 {) W0 r9 u) _//| virtual stub for PredictPrice (regression model)                 |
  A  G6 l- l/ m9 U1 @$ `//+------------------------------------------------------------------+: K4 g# d( Q% g, X8 p
virtual double PredictPrice(void)
. `; U, K2 m. r# N/ e{$ L# q7 a' f  G/ [* d; \
return(DBL_MAX);
3 {/ t. @9 h, s8 K}5 i/ ?" z. s' B! }: t) W# ~
//+------------------------------------------------------------------+
  l1 M" ?+ k( r//| Predict class (regression -> classification)                     |& I+ i4 B* M$ z8 q4 P# q
//+------------------------------------------------------------------+
, }) A5 j! n# z1 Z5 Ivirtual int PredictClass(void)
* M5 I  R9 }8 `+ }8 n5 ]{
4 g! _& h' X* {% b9 K; _double predicted_price=PredictPrice();
' \3 F4 h* T- z4 R: f9 @if(predicted_price==DBL_MAX), y4 ~' d& K0 H& U
return(-1);8 [) I9 A( C4 {, l% d, g# _+ ]
int    predicted_class=-1;
9 q) `) E; @' v7 ldouble last_close=iClose(m_symbol,m_period,1);' h$ K( t0 G* l7 P  D9 ]
//--- classify predicted price movement
; H* p3 q, S! e; J8 Z+ T' f4 e1 Qdouble delta=last_close-predicted_price;( m# e4 |2 O! U2 i! X% @
if(fabs(delta)<=m_class_delta)
, N0 c( R; L3 @2 ?# D4 Y9 Upredicted_class=PRICE_SAME;& x) t/ M5 D% c7 E- C' N8 C
else% Y" e" G. H2 Z9 L
private:- P% Q5 S3 p2 q, V
int               m_sample_size;
1 u. ^" p" r. z2 L//+------------------------------------------------------------------+9 Q- U4 b1 z2 c  w9 ^% b
virtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)
$ i& F7 z8 M, M- a) ~# \/ n. i{
5 p$ [' q1 J& I5 K% t! g0 Z//--- check symbol, period, create model
2 y' e2 X% H: q+ j+ w! @$ zif(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class))
! K# `! l5 m2 S) ?0 a; v) @{
9 f; M) t  k0 I: u1 T1 S- UPrint("model_eurusd_D1_10_class : initialization error");1 }  l) Q# j* Z9 B
return(false);
, h8 e) @7 U! [/ Z}! ?" F1 Y& x/ T; N& s5 t
//--- since not all sizes defined in the input tensor we must set them explicitly
$ x, \& ^- s% l$ z//--- first index - batch size, second index - series size, third index - number of series (OHLC)
- o  s! u" M; z3 nconst long input_shape[] = {1,m_sample_size,4};
- t  g( ~, x/ K8 |% t1 Zif(!OnnxSetInputShape(m_handle,0,input_shape))1 `# a7 |2 a$ A
{
3 w6 u& d  q9 g4 k! RPrint("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());4 t, Q* H) p. t- s6 l
return(false);
. V1 s4 ]$ Q" V% r}+ i3 ?$ D% x7 ]& f2 G! A
//--- since not all sizes defined in the output tensor we must set them explicitly
% A. e- o7 t- \6 n0 c+ e+ i//--- first index - batch size, must match the batch size of the input tensor% r7 ~$ g% J/ A
//--- second index - number of classes (up, same or down)
: d- y+ Q4 K4 o; ~8 \const long output_shape[] = {1,3};
; G. M2 w* C( q: ?4 Y+ aif(!OnnxSetOutputShape(m_handle,0,output_shape))
' J7 O# D" }+ F{" A( P0 z$ v3 v6 W
Print("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());
" R9 t" b! V2 ^5 y0 e8 x. Zreturn(false);
% b& S1 N% j+ q1 v}( r+ I/ L7 P) M
//--- ok
: r1 e% J! Y  H2 E0 O+ a- ~$ Z& Hreturn(true);
0 G, n9 h+ {6 O( R$ K9 U2 g}! ~8 T. n! B2 ~
//+------------------------------------------------------------------+
- o4 ?0 ]' a. `+ _0 S& {; R- m//| Predict class                                                    |5 W% U0 e9 s6 O# ?" t" s& \
//+------------------------------------------------------------------+
- T  g( @7 p/ W: B" x. ]6 C* D" Xvirtual int PredictClass(void)
, s7 ?$ P% m3 ?* _3 b4 B$ }8 d{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-6 17:27 , Processed in 2.695216 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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