私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?
8 D% B0 r8 ^+ {& b在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。5 X5 F, V9 W/ H+ B# c; @
我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进
/ O' q% k) E2 R% W2 R//|                                             https://www.mql5.com |- X7 e9 k1 g) ?3 S. l# k, Y/ p/ r& q
//+------------------------------------------------------------------+
: _- q9 P7 d- ]6 @//--- price movement prediction1 T1 U8 Q' J/ ]2 `9 x1 |. |
#define PRICE_UP   02 O' c- m: ^( ^4 J& `
#define PRICE_SAME 1  R, |8 a" L* w& U- E
#define PRICE_DOWN 2  u& E* @/ @4 @' O
//+------------------------------------------------------------------+0 n& O) `  g7 {/ f
//| Base class for models based on trained symbol and period         |5 y" T( c4 g' g( J
//+------------------------------------------------------------------+
7 D6 K( ^. E/ I# T# kclass CModelSymbolPeriod; J5 o/ G6 `) \3 b
{
0 R3 G2 {1 `0 O9 y5 @) B! n7 ~protected:2 Y: M4 t; T" k, m6 N: \  R/ C
long              m_handle;           // created model session handle, x( y' r5 G4 r3 _& D" b0 ?9 J
string            m_symbol;           // symbol of trained data
$ k. O+ t$ n3 O" y, l/ Y  h% yENUM_TIMEFRAMES   m_period;           // timeframe of trained data
6 n* o9 r- B! x+ |! bdatetime          m_next_bar;         // time of next bar (we work at bar begin only)
" J+ U/ Q/ D: Z8 cdouble            m_class_delta;      // delta to recognize "price the same" in regression models2 j6 n- Y9 n6 B! L0 ~
public:
8 o/ I  y1 e1 E) n5 R//+------------------------------------------------------------------+1 _5 x! l' D- K( O' [8 p
//| Constructor                                                      |( [3 ?! n% ?5 Z8 @( \
//+------------------------------------------------------------------+
2 T7 q& S- M$ B+ [5 A* o; f5 iCModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)
8 R0 G0 U# m' X! Y9 u+ d5 k; z; {{
0 u& v. n0 Q9 [. `m_handle=INVALID_HANDLE;- E, A9 A1 b- v; H
m_symbol=symbol;
; W4 _# G  P! e! ]m_period=period;
. Q1 u9 P2 u7 B4 ]$ lm_next_bar=0;
+ ^0 z  _- [* mm_class_delta=class_delta;) s6 M9 j5 U# \4 I) y$ M) c1 I
}
/ j& \. F6 s. F: l//+------------------------------------------------------------------+9 h2 _7 t7 }6 w: I$ U: e
//| Destructor                                                       |
) m0 \: L" D3 {: _$ S//| Check for initialization, create model                           |
; G% V. r8 r% u2 b//+------------------------------------------------------------------+4 g2 k. s" m% n, h4 V9 A
bool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])
: J& Y4 O8 ^$ K* \) C- `' X{9 a: H1 e# Y4 j: M- l
//--- check symbol, period
  R7 _- I7 o  h' J, ]% j" N3 Oif(symbol!=m_symbol || period!=m_period)' T) h4 }7 P$ i6 R
{) c, M; c7 f# D: J, Z3 g  i
PrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));; I" y" ]  z( o& m( o  e
return(false);1 d& p: f) R) i8 N! i
}& V' a6 h) o: D$ {+ {
//--- create a model from static buffer. v; S0 n/ {, f. J' R
m_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);
/ N' L3 K, ?3 D9 u, s" qif(m_handle==INVALID_HANDLE)" T1 m% u6 [: ?9 T: u0 n
{0 T, i4 w$ f+ f/ W: r7 J- b
Print("OnnxCreateFromBuffer error ",GetLastError());+ l3 w( d. l! U# m
return(false);% i3 I6 I5 h: {
}
) c- p% s+ }9 P8 f( F: h  K//--- ok
: `; r/ ~! }4 {return(true);9 ?- F6 A. m7 {; O* f5 }$ g
}
# U! J6 j5 e. \. S6 i4 |5 K5 G, t//+------------------------------------------------------------------+, c7 p8 O3 m" V; r1 L5 y
m_next_bar=TimeCurrent();6 }# S+ ]- o0 Q  g8 t% d
m_next_bar-=m_next_bar%PeriodSeconds(m_period);
/ q8 S$ _# `$ o5 q6 J2 P& u+ Sm_next_bar+=PeriodSeconds(m_period);
3 @7 R, ^4 c: t  @- _) x//--- work on new day bar9 U- b1 ^% w" j( v
return(true);
# Y! x  s3 z+ V' D: r4 W) x* r}
: h- F; T/ p5 T; [1 S//+------------------------------------------------------------------+
  e* [4 B4 C+ n* m& G//| virtual stub for PredictPrice (regression model)                 |
$ P6 R0 h6 `2 h/ x1 m/ f//+------------------------------------------------------------------+
$ m) h" g; n; O; @, P% Vvirtual double PredictPrice(void)+ b) {* |" X4 E' k. `
{
9 z' S5 P* W/ D) F' B2 ~return(DBL_MAX);0 N* ]! A4 ], `6 A. @9 y; a! J# ]
}
7 j6 d! u' B  e6 ]. Z" T//+------------------------------------------------------------------+: M, ^: `4 V8 N
//| Predict class (regression -> classification)                     |" o, V* Q. w/ B" |8 ^! j$ C
//+------------------------------------------------------------------+
( S" w! M, X5 x! f, [  nvirtual int PredictClass(void)0 U5 s% Y; H' x% ]+ Y: r( f
{
4 P# }! N/ a+ h( O4 i# |double predicted_price=PredictPrice();
  @9 N9 H- d* Q$ i  E8 R, O7 s  Rif(predicted_price==DBL_MAX)
0 p# r9 p( L+ l" d" }5 Z: Lreturn(-1);) {, Q* B; h& S# v; s3 R- u
int    predicted_class=-1;1 C0 a+ J' w! M  p
double last_close=iClose(m_symbol,m_period,1);
5 g5 W4 O5 k% s; g1 h# O//--- classify predicted price movement
' p8 W3 b9 A; i% m9 h' pdouble delta=last_close-predicted_price;
* w9 F; _. u$ T  |' t( a' I, Qif(fabs(delta)<=m_class_delta)9 H9 c7 j, D, n$ O+ [
predicted_class=PRICE_SAME;3 z) I: j$ B" }) x  }3 K& N
else' b6 `, W9 Q6 ]; H) r
private:
6 G' M7 I* _. F, G; ?8 Gint               m_sample_size;
5 p8 e4 a1 ^9 Q! c" I//+------------------------------------------------------------------+
2 n+ t4 ~7 b- R- d7 ~% Y2 Vvirtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)
( B( y: R- d  B8 t{
% b0 s: ^$ F( q. [//--- check symbol, period, create model
5 W& c! G3 x2 ]4 H, u$ `7 S- I4 Kif(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class))
) M2 b3 ~7 ]3 }$ m{
3 T4 [% D2 \' x( oPrint("model_eurusd_D1_10_class : initialization error");
/ O* n/ I+ P6 [9 X* ?. \8 i( L' Oreturn(false);2 K9 L  \% q& t
}
; z. Z) E+ C" J7 C  |//--- since not all sizes defined in the input tensor we must set them explicitly2 e% _+ J9 @! j7 D3 G
//--- first index - batch size, second index - series size, third index - number of series (OHLC)! k( ]4 s; T; }' ?8 V% K
const long input_shape[] = {1,m_sample_size,4};
" ]% P, t1 ^) x) y7 O7 [/ Bif(!OnnxSetInputShape(m_handle,0,input_shape))
9 }& `  R* z$ w$ {{
3 D" l$ |+ h- v: G; D: U" U  gPrint("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());
& A+ A, i7 G/ n# I0 xreturn(false);
! N& z8 p0 |1 F' Z7 T0 H7 O}& ^8 [6 z( p! I* ?
//--- since not all sizes defined in the output tensor we must set them explicitly
6 m; n  c0 \. M//--- first index - batch size, must match the batch size of the input tensor) U7 Q8 K' w0 W% @6 w8 {+ q
//--- second index - number of classes (up, same or down)
. K, @5 x, e  I! B9 p  R0 Dconst long output_shape[] = {1,3};' C- C" g) y) `: N6 P# `
if(!OnnxSetOutputShape(m_handle,0,output_shape))
0 w5 o" @% K: g6 L4 K  ]+ f{: Z6 o2 K" v; I+ Y7 y) @' m5 l; [
Print("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());
, |% X; H5 d3 R! V1 ^3 P5 C, areturn(false);
1 M6 I5 s+ X8 Q}
  Y$ e  ?" h  H9 G: M//--- ok( \0 \+ K, O, {" g6 ~
return(true);  p/ K" R! m! Z+ E, D* P# x. y0 u
}0 Y  o2 s* L4 t8 \' m. h
//+------------------------------------------------------------------+
' Q3 H& M5 ^1 B6 ^//| Predict class                                                    |
9 t$ `( b4 J- `1 s  C0 R- D//+------------------------------------------------------------------+
, @8 x; g6 S3 X$ o* o, z3 R/ {virtual int PredictClass(void)
9 |# ]  U9 |( b{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-18 07:28 , Processed in 0.419981 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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