私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?
; y3 o) `- d% o# n& P. V  B在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。% x6 k" v0 c" E: i; y
我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进# t+ w& \2 A$ Y# O- Q; H
//|                                             https://www.mql5.com |4 T* j. {9 b+ \% x
//+------------------------------------------------------------------+) h, r" |) e' A/ }3 ?0 C* n: [9 {4 {6 R
//--- price movement prediction
( ?: |2 d* Q  M$ \#define PRICE_UP   0
7 f7 Q1 n3 v, i6 M#define PRICE_SAME 1, Q1 m5 D& e, ?
#define PRICE_DOWN 2& p! q; U% G0 n, _5 S' Z
//+------------------------------------------------------------------+/ G, Y8 c( z( E. B) c" b, w( L1 e$ r
//| Base class for models based on trained symbol and period         |( ^" T; ]9 x5 X5 C, k
//+------------------------------------------------------------------+/ }4 y7 l6 j' \2 y1 E: o
class CModelSymbolPeriod
: @$ @3 n/ n6 ]( r{
& O0 m( j# V% |& Q5 Y6 c1 Kprotected:
; G- A  Z6 Z4 J+ d6 nlong              m_handle;           // created model session handle" `7 _4 p/ f! `* K* I
string            m_symbol;           // symbol of trained data
! Y2 W4 @, N- w3 F+ [7 h% MENUM_TIMEFRAMES   m_period;           // timeframe of trained data# y! l- G9 [" |3 M$ M6 t
datetime          m_next_bar;         // time of next bar (we work at bar begin only)4 T) Y3 ]/ i* T+ F
double            m_class_delta;      // delta to recognize "price the same" in regression models
, ?9 m$ f3 c8 S- Dpublic:
: [0 h. I7 c( }//+------------------------------------------------------------------+/ c3 D: |0 I& B
//| Constructor                                                      |7 Q5 K) {) t% [+ o6 U2 O0 n8 e, |- j
//+------------------------------------------------------------------+
; n. p7 l7 n- M3 s) e( k8 F' ACModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)
3 e, r9 w+ d* k# [9 t{
# g( m+ X* z( I  Zm_handle=INVALID_HANDLE;
" {; V: ^- o) t$ \) a: G4 Y9 om_symbol=symbol;
! P  C3 C3 X3 u+ n. `' tm_period=period;( Y- z) \# `* Q1 F
m_next_bar=0;
, u& b  _+ R1 i9 o7 e2 y6 d, Pm_class_delta=class_delta;
3 R' l; `; e5 y& \/ C7 k}
+ h% x0 q( p2 Z( O% I4 ~//+------------------------------------------------------------------+7 M- R6 T, N- ^# c. b# A
//| Destructor                                                       |
: X# J7 F) e7 C0 F//| Check for initialization, create model                           |9 F! I& ~4 [6 H' a  b3 o0 j
//+------------------------------------------------------------------+
4 [4 m: a6 [( M8 c* S5 cbool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])& a4 l* o1 E: P0 Z- f- K
{' E+ @$ l' s% r: e; Z7 |
//--- check symbol, period
; {2 e% W, \/ P9 {$ L' i3 oif(symbol!=m_symbol || period!=m_period)2 t( v8 K  q  m" G" X
{$ m7 h0 V6 J# Y# W/ n) b- i$ l
PrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));
. G8 k, u7 C" u/ Ereturn(false);/ I+ ?; N. ^9 g$ i% E# l
}7 r, E# V9 T* K# h, `) _% t1 H' v
//--- create a model from static buffer
2 a; b+ J9 r- [$ tm_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);
4 [& c. W& e( j( l, d  R/ nif(m_handle==INVALID_HANDLE)" E8 y. h8 P2 W# B
{
0 Z# Q1 j& d; J9 i+ U: {Print("OnnxCreateFromBuffer error ",GetLastError());
. [2 ?( u( V: [- ?) b3 T& ^return(false);
0 A4 I( v: b3 ]6 r, v" H}# B$ ~' A2 d& E9 q
//--- ok
# J) M$ U: G2 ?/ F# a2 V, Greturn(true);- {" A7 B- B! i4 ]/ b
}
7 n7 ?1 v; A6 n3 O) y" l//+------------------------------------------------------------------+
$ @% i& n, I6 d. m0 ~m_next_bar=TimeCurrent();' E1 [/ k! W2 d1 [$ v
m_next_bar-=m_next_bar%PeriodSeconds(m_period);
- d3 t: o' K/ U3 j5 q& A% g3 U" n' ]m_next_bar+=PeriodSeconds(m_period);
. Q8 H5 I- m: m1 o( x; K( S  W//--- work on new day bar
* z/ ?2 u: q. g) I  W5 y1 n( Z& Ereturn(true);
5 }: g1 ~( ?' Q$ z) a}
, L& X( H( l$ O! U  l% a0 A//+------------------------------------------------------------------+: N$ a6 P- j- D! A' C
//| virtual stub for PredictPrice (regression model)                 |% I- u" K8 C- f  K7 d
//+------------------------------------------------------------------+
, [" d; n7 R; e( g8 z7 R! @; cvirtual double PredictPrice(void)
. Y6 H# d! j. @  B/ n{
4 F2 c. P9 S1 t+ U1 Q0 p& ]return(DBL_MAX);
; g1 p! }+ N* d! @3 {, d! Z}4 t( N( ^' o) k( M# I3 j5 J3 l, ~
//+------------------------------------------------------------------+( v% U! _# [5 n/ ?% N' c
//| Predict class (regression -> classification)                     |
. T& Y" `  [7 k% K//+------------------------------------------------------------------+
2 I  s8 r' s% f9 gvirtual int PredictClass(void)
* i$ Z5 X" }) d9 r{
% {4 Z' o9 L; ^: ^: F# P' D* b4 ~double predicted_price=PredictPrice();+ Z8 C# k) ~( S* }- i) r( R3 F! A
if(predicted_price==DBL_MAX)8 z6 f! f  I  }& C
return(-1);
  u) t! v0 w$ \! L4 Eint    predicted_class=-1;. @1 K( L$ h# W
double last_close=iClose(m_symbol,m_period,1);
, x" @6 e$ r% D/ s% W- T//--- classify predicted price movement
- S- F$ A0 u: W' D7 u0 h  Vdouble delta=last_close-predicted_price;2 x* _2 p! L. f6 Q& P
if(fabs(delta)<=m_class_delta)0 ]: |+ f: H5 F) R2 b# V; q
predicted_class=PRICE_SAME;
; E" a" h3 ^* xelse& w$ c5 m, d3 {+ I" ~% _  t
private:
1 k+ g& D0 V7 p# r7 C; W: d9 jint               m_sample_size;  u0 V: p- s0 }) W4 @6 {
//+------------------------------------------------------------------+
( F2 R9 Z" \1 f. P: d! Ivirtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)
  V, g7 U- T4 X' |) l# E{: ?$ ~& _+ P7 Q9 d) w9 ^
//--- check symbol, period, create model" k  l# c# C: v# Q  Z
if(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class))
- \/ W1 O! c' l2 P. A8 K{
: a# _  V( s& @7 }9 xPrint("model_eurusd_D1_10_class : initialization error");
( v, [% Y" n6 \3 {' x3 Z5 M+ qreturn(false);2 T# @, H& h- \( Q$ T) \3 }
}
/ A+ u2 ^/ g  m//--- since not all sizes defined in the input tensor we must set them explicitly
. E+ ^2 w$ H/ V6 m//--- first index - batch size, second index - series size, third index - number of series (OHLC)* |9 ~& ?6 B- R# E- [& K7 ^
const long input_shape[] = {1,m_sample_size,4};
0 m8 D. m3 f( G; s4 G4 u1 nif(!OnnxSetInputShape(m_handle,0,input_shape))
  a6 ?9 e  b# K$ q2 p; l  T{% x5 M' }) G, m$ |
Print("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());
8 P. S. \6 o' k% S! n4 {6 }( }0 k3 Zreturn(false);
. Z6 ]: s: W4 c+ a5 L}0 ]" e; N0 S" j& f: o8 n  ]
//--- since not all sizes defined in the output tensor we must set them explicitly
2 b& o8 Z7 Y- \+ E5 J//--- first index - batch size, must match the batch size of the input tensor
4 t" ^, A( w, i- c+ r' ?//--- second index - number of classes (up, same or down)
% S  l. m: }1 @; B7 [1 i+ Bconst long output_shape[] = {1,3};1 f$ ~' X, k" S: `
if(!OnnxSetOutputShape(m_handle,0,output_shape))
9 M% X8 e* }2 c; z: L9 O/ g{* `+ F0 Z( B7 ]9 R' d3 P
Print("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());3 v4 r8 a3 `) y
return(false);
8 @" w( e; D: C2 O" o% E}1 K) ~# a4 ?" P4 C: M
//--- ok
7 z) R5 k, `9 [2 |( M$ e( Preturn(true);1 r/ y; ]3 t) k& u6 Q0 J( l
}
2 C) I1 f3 H. U3 l6 P; y& [& V* Y0 R//+------------------------------------------------------------------+
' g" Y- d& T6 T1 B1 t//| Predict class                                                    |
$ q- |% d+ q6 a( D% ?8 j. t3 Y& k//+------------------------------------------------------------------++ Y  X. n7 J# }% A! Z8 ?: ~
virtual int PredictClass(void)0 C3 v5 w% A3 L% E
{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-11 08:22 , Processed in 1.018195 second(s), 32 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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