私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?; P' G* I- `# {: g7 B& e
在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。
' Z! X/ D/ Z% {) v" d: f" N: {6 q我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进
% P% x! z& E' h7 c4 o//|                                             https://www.mql5.com |. ?9 J1 X) |0 U$ A7 N7 l: L/ j
//+------------------------------------------------------------------+* ]" P2 q, d3 O% N" I" E
//--- price movement prediction
2 D( g: \* j5 R, I; j8 ?) P3 y3 \7 [#define PRICE_UP   0
* w( `4 J6 _5 {$ w$ o3 K% f1 S/ \#define PRICE_SAME 16 v  \1 A# N" B% y: F7 O) ^
#define PRICE_DOWN 2
) E/ @$ x) b2 `, f& a//+------------------------------------------------------------------+
( c9 D4 Q8 w6 n4 [& W% y//| Base class for models based on trained symbol and period         |' Q5 H. H8 V! M& a* q4 U
//+------------------------------------------------------------------+
0 A4 \2 a: J+ i0 tclass CModelSymbolPeriod
1 v6 }) ~7 A6 R2 A{# K5 B2 [6 x9 L) \% Z* ~
protected:2 j4 j" B! O0 f  G6 H; `; _
long              m_handle;           // created model session handle
  ?/ q; w9 p+ r) F- [& q5 D9 sstring            m_symbol;           // symbol of trained data  A% U. i, m: n; U0 N3 B8 Z
ENUM_TIMEFRAMES   m_period;           // timeframe of trained data* w9 E4 ^0 H/ ~
datetime          m_next_bar;         // time of next bar (we work at bar begin only)
" t# T8 T1 k# k+ ^! {/ e0 Tdouble            m_class_delta;      // delta to recognize "price the same" in regression models4 Y6 Z$ S) Y5 E: b# M% n
public:8 m( e  n% i! z- f
//+------------------------------------------------------------------+( a& y) V* a3 B0 [; D; e
//| Constructor                                                      |; b/ Y5 p- F. D# G) Z
//+------------------------------------------------------------------+
3 w0 b1 e  p' r  ~, z/ xCModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)$ _  w% `7 ^# G: M
{: m5 k+ Q, e: w6 n) ~: j
m_handle=INVALID_HANDLE;& p$ a3 N1 R, F" F- v5 l
m_symbol=symbol;$ A" ^; b3 s* O( c6 @! C- B; ?
m_period=period;
& c0 M: B  A& O6 C* J, gm_next_bar=0;( }1 f' @9 y: `! g7 ~
m_class_delta=class_delta;; o4 G6 G5 C5 k4 @3 _- H
}( Z' K; I. t0 _+ }2 l
//+------------------------------------------------------------------+
8 @' t& P, r! i//| Destructor                                                       |
+ e$ {; G5 b5 L//| Check for initialization, create model                           |
8 J' S  K# M, x6 r8 H//+------------------------------------------------------------------+
# Q, S: l- W( Q* x$ u( }  f/ F7 N6 Xbool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[]), x3 E; d) E' \1 X
{2 R+ m" Q* e3 V- C6 ^& W9 O7 G7 D+ [
//--- check symbol, period
6 s* ?* m6 L$ n1 d* qif(symbol!=m_symbol || period!=m_period)8 `1 H' @* A5 a" c4 }* y, E" q
{' ~, w/ }, {9 U% t. a
PrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));
& r1 y1 i7 B0 q& A& Greturn(false);
* r$ r/ Q3 ^. r0 J, F}+ }4 `5 a% y# `
//--- create a model from static buffer
' d6 \: B+ @% n6 P, Zm_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);7 b& m; P- P9 C7 t
if(m_handle==INVALID_HANDLE)) O  j. N0 o; T& N( p# V6 b
{
& _2 z8 B! P$ APrint("OnnxCreateFromBuffer error ",GetLastError());0 S$ `) l3 g5 u# t7 U& D
return(false);4 Q/ c/ R  o9 f/ x
}
: x& P- W5 Z3 w/ p$ _7 o5 b//--- ok0 F/ j9 Q) u- I0 n' J
return(true);1 r/ P( a, u/ O# x
}/ A0 s0 u3 t) t7 p9 ^7 ]6 u
//+------------------------------------------------------------------+
" n+ M2 }- i4 Wm_next_bar=TimeCurrent();
$ k. C; m3 p& N9 Q: Mm_next_bar-=m_next_bar%PeriodSeconds(m_period);' k9 a4 b- T6 }8 M- ^9 D6 @% o) Q
m_next_bar+=PeriodSeconds(m_period);8 M( i) `0 p7 t9 g5 N
//--- work on new day bar. R  _8 x5 H8 ^& [( e; }4 q$ E
return(true);, f' A5 Y/ q. s+ }( k2 D* A. E4 l
}2 f, ?: c9 g7 w7 ]& g0 d  e/ B9 {
//+------------------------------------------------------------------+
7 X( u# T( W$ Z//| virtual stub for PredictPrice (regression model)                 |
* |# b$ k* W5 D4 c; `2 K//+------------------------------------------------------------------+
, [0 n  N; s% \  c  |# B) ]virtual double PredictPrice(void)" [2 x# O- Q  b/ I
{
) W4 N+ a8 A: R- ]3 Y8 W# \return(DBL_MAX);
0 R& o8 g! o, B  Y5 r, j' }' G}; I) h8 {0 K+ P; M# U% y* R
//+------------------------------------------------------------------+
; R2 X* m  s9 X2 r0 W% x//| Predict class (regression -> classification)                     |
) ^& R$ H1 J# l3 _5 Q2 t, u! F//+------------------------------------------------------------------+
' M! t! h8 G, V/ M; x2 pvirtual int PredictClass(void)
7 z6 a0 \5 q4 q( L{
( Y, W; P. w) m- J0 Q2 x  m2 Xdouble predicted_price=PredictPrice();! V1 t3 ^( c2 K: ]- z
if(predicted_price==DBL_MAX)
. U: P3 U& c: R; u! a; r' g+ \% rreturn(-1);
/ P7 p8 |# H9 ~) Jint    predicted_class=-1;
* I; O. j+ U, ?. z$ F" rdouble last_close=iClose(m_symbol,m_period,1);5 T" i; b  W* Z/ D+ e8 {9 G. N- H9 r
//--- classify predicted price movement
* G, ?* T1 J5 I; _& B1 x3 o# G/ \  Kdouble delta=last_close-predicted_price;! w+ y9 Z/ X+ u" c1 l
if(fabs(delta)<=m_class_delta)
: F+ |7 E' W+ q. spredicted_class=PRICE_SAME;, I! \; _' I" V* T# o
else& G% }& [2 t" y) {# z" x
private:6 t6 M% t0 o8 y" o, V8 Y- ?
int               m_sample_size;3 R0 T" V, Q3 d3 f; W& F) e- e9 w' R
//+------------------------------------------------------------------+( B4 V7 k  G/ s7 x0 r4 ~; N9 h
virtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)$ k% v9 z7 X7 Z9 x8 J
{
" W; G. y  R  g' H  P- ^- f1 f1 ]//--- check symbol, period, create model
5 F9 S: D# g4 A0 }if(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class))* I, H+ ~  F- [+ T  f0 Q* ~# f
{
' `% F3 G  H- E4 r& P5 Y% IPrint("model_eurusd_D1_10_class : initialization error");8 Y6 b+ p( @- ~  r6 |# b
return(false);
* h1 ]3 c6 Q( M}" u' y' I! o8 A. ^; z0 x0 L6 f
//--- since not all sizes defined in the input tensor we must set them explicitly
( C; s# e3 E! m9 g# N8 `, m//--- first index - batch size, second index - series size, third index - number of series (OHLC)* f$ h6 R, q9 K+ k: x7 l7 t% T% m$ u
const long input_shape[] = {1,m_sample_size,4};
* {9 z- j" m/ {3 ?* W5 eif(!OnnxSetInputShape(m_handle,0,input_shape))8 x8 O1 \5 M- @. B. F3 F
{
' J; E/ N0 g$ N$ tPrint("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());
& N  L; p4 \. d' V, _1 oreturn(false);
/ h# R& w$ |# Z) m6 E- q}2 V  Y- P7 W& P0 [" T
//--- since not all sizes defined in the output tensor we must set them explicitly
* _" E- N3 ?6 \4 T4 T& u//--- first index - batch size, must match the batch size of the input tensor& G: f! q3 M2 P  b; F- `' }+ B
//--- second index - number of classes (up, same or down)( Q) U: p$ x' R. U2 P6 h
const long output_shape[] = {1,3};: D7 H& d' {' u: m: I
if(!OnnxSetOutputShape(m_handle,0,output_shape))
" y( _- U, n# R2 q  B9 A) S7 Z{
( |% n" c1 a7 }9 W! D' o) EPrint("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());
4 q  g$ u" R/ f0 b* }; o' K5 K+ [return(false);
2 F3 [4 r. a/ J1 B* u' x% l  v}
9 c- s6 Q; J5 e( Y; n9 _. t//--- ok- K" _9 ]$ P2 R' e6 \5 D3 X3 c
return(true);! l: E; V& i  T' e
}5 d& U; ]4 t. Z
//+------------------------------------------------------------------+: i& R5 \( V9 J0 \: }
//| Predict class                                                    |
$ [- U* N6 F  h. B//+------------------------------------------------------------------+6 t3 m- }. J4 r% B+ T
virtual int PredictClass(void)
: C: Z1 v% K0 t6 \+ \7 S8 ]{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-3 05:20 , Processed in 1.958133 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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