私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?% O- g/ S- l4 T* }1 R3 O9 |
在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。
9 U) L3 v4 |# o- Q$ |: u3 k$ c我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进# t! x; e3 v9 ~) |! s# g
//|                                             https://www.mql5.com |
! t7 o# H1 |6 l+ z9 E" _8 l//+------------------------------------------------------------------+) J0 t8 e2 `% B2 M! x0 j2 d* ]- ]8 a
//--- price movement prediction
' A* g+ z  q, K; i6 e( {#define PRICE_UP   0( |- _& v* E8 ^5 f: H* \
#define PRICE_SAME 1) _7 ~" h, R) r; Z- M3 ~8 i
#define PRICE_DOWN 2- s/ N9 A$ ?: o7 H: O
//+------------------------------------------------------------------+" t8 e+ q" d6 r0 |2 y
//| Base class for models based on trained symbol and period         |+ q& ~- p8 f) [! Y% G  }: ^4 h5 C+ `
//+------------------------------------------------------------------+0 C+ O! d1 G8 h6 \* s8 M. u! x
class CModelSymbolPeriod$ D4 M( D' Q- N/ F# ~% U3 p: s
{% u& |/ h7 |8 Z" n
protected:
+ J4 g! x/ ~$ q5 Dlong              m_handle;           // created model session handle2 ?* Q* O1 b+ t" m% l4 E& Q
string            m_symbol;           // symbol of trained data
4 N6 F, T. W* q6 E5 z2 DENUM_TIMEFRAMES   m_period;           // timeframe of trained data* L7 U' J: i; y- G  o  ?& k0 c4 Q
datetime          m_next_bar;         // time of next bar (we work at bar begin only)2 ]( w; y# X0 J7 p
double            m_class_delta;      // delta to recognize "price the same" in regression models$ [5 B( P2 |5 f2 x. ]; ~
public:
# {/ ~9 v7 u7 ~0 p8 c//+------------------------------------------------------------------+% E- L, T+ |/ f5 |1 _7 y! ]
//| Constructor                                                      |
- T7 V6 F! P2 c, _//+------------------------------------------------------------------+
5 T' [" X1 S( x; gCModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)- j3 H- m2 \6 m# {* w4 u
{
/ q' I$ H- i, N# Am_handle=INVALID_HANDLE;+ b( Y' N! M. {+ @! @+ T$ V
m_symbol=symbol;% A; V# |* |$ m- Z, `7 g& o/ X2 u
m_period=period;
8 m* y/ S+ ~! u$ {: sm_next_bar=0;
& J; s* |* u& l9 B" c1 |: Um_class_delta=class_delta;# `! w7 O5 |8 |$ n/ Q+ v
}
' L, s! ^6 [  K1 e0 F' C2 C//+------------------------------------------------------------------+: J" L7 L( l" a2 b4 h
//| Destructor                                                       |
) [4 w, B- O  \! K8 \- v//| Check for initialization, create model                           |* G6 [, T# i0 W
//+------------------------------------------------------------------+; V/ {6 H( e% I
bool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])) Y* V9 \. C" t! H$ s- @# d. U) V
{/ c# R+ K! a& ^
//--- check symbol, period# J, Y; @5 A$ s2 O/ _" ?
if(symbol!=m_symbol || period!=m_period)
" y( v) K$ m5 U1 \* V7 v2 ?7 ?{- H% @5 W% Y5 V- s  H: S: ~* [
PrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));0 L0 d* C3 m  a; z* h2 ~; l
return(false);0 w  e7 y& v3 Z5 c8 _* l0 V
}( b4 ^( ~) W. z" i$ W5 o
//--- create a model from static buffer
" i0 l) M; Y( F4 q1 o* u" fm_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);
. ^, r( B4 R7 l% o/ T+ u# U  ]  Rif(m_handle==INVALID_HANDLE): e1 N( i6 _( W9 E# h  r2 t
{1 s4 v: M1 H, M
Print("OnnxCreateFromBuffer error ",GetLastError());: x: l* N2 U* l* |8 y+ \
return(false);% [7 S, A1 Y, [* v4 M" H
}
6 S2 L0 I6 r! o% I' G//--- ok/ U# Y2 D% w* v; c! D2 H5 ~+ N
return(true);
( j. E7 W  q. u# d9 x0 }8 l}, e5 ?- G$ w3 n5 k  V- |- J& R
//+------------------------------------------------------------------+; Y$ S$ l. z5 s$ N; o
m_next_bar=TimeCurrent();3 p% E0 d9 p+ t1 u9 j+ s8 m2 H
m_next_bar-=m_next_bar%PeriodSeconds(m_period);* x7 {5 s6 L/ D) q1 T7 }- t
m_next_bar+=PeriodSeconds(m_period);
3 \1 M1 f! H- s" [//--- work on new day bar( Q! T+ y2 v# {& F
return(true);
5 n$ ?2 X' T  m" w2 f}# d: i' _3 P) i- `
//+------------------------------------------------------------------+
6 o+ g0 K& q  n. A- a//| virtual stub for PredictPrice (regression model)                 |( }* O$ a5 [5 R+ c
//+------------------------------------------------------------------+
  [' R2 Z: E+ T6 u, u' bvirtual double PredictPrice(void)
1 C5 m# O, [7 ]4 a/ j: B% q& G{
& H6 B  e; }2 I$ ?) I6 q; jreturn(DBL_MAX);/ o' o2 t6 x2 ^+ k
}
5 [. u5 M3 J, s* R$ L2 `//+------------------------------------------------------------------+$ ?2 d, ~% D+ A9 \
//| Predict class (regression -> classification)                     |
+ i+ e( L( q, h# C& p0 T//+------------------------------------------------------------------+! s- s4 \# `7 p! m' U3 [' E' K
virtual int PredictClass(void)
3 P" q1 j9 p; |" ]" b4 b{
, g1 J, P% n9 |( [# s3 w5 vdouble predicted_price=PredictPrice();
9 f3 K3 c4 _7 [if(predicted_price==DBL_MAX)
9 Y- z! M0 L. }1 Preturn(-1);
6 b/ h% g7 O6 u- [$ Z; G) Kint    predicted_class=-1;1 h! d5 z1 l6 ?9 |- t
double last_close=iClose(m_symbol,m_period,1);
( z5 E: y6 t, g- ]4 x* j, _//--- classify predicted price movement
9 i- x. t4 k6 g5 L* j& b2 f9 I( Idouble delta=last_close-predicted_price;
) G# o! d$ l4 D4 E7 |if(fabs(delta)<=m_class_delta)3 t! Q# P( t6 e! R7 [4 F1 `& O. ?+ \6 `
predicted_class=PRICE_SAME;) F6 h" y3 B8 ^, \
else3 a- j. }  W" k3 V' U6 N3 ~+ y
private:
) l8 S& |2 I8 q0 S3 ], |: Kint               m_sample_size;
, s" E* i: u, n# d9 H" h! {  W//+------------------------------------------------------------------+& |5 F* l% N8 ?. j
virtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)# m" f+ I4 J$ z1 A& R& u2 b
{
8 q; f5 g' |6 p: b+ A- q//--- check symbol, period, create model
- j' O) ?# a' b( Z% Zif(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class))
" M, w7 m+ H# a/ A: K{
1 h- X4 B7 @0 u, e( M9 `8 NPrint("model_eurusd_D1_10_class : initialization error");7 W/ f- ], y/ @( [5 B+ {6 G9 B
return(false);
2 w$ [9 C- r' ?$ x7 z9 c}
0 x3 i: N& X6 z; j$ n//--- since not all sizes defined in the input tensor we must set them explicitly# o' _" q) P2 M# b
//--- first index - batch size, second index - series size, third index - number of series (OHLC)% l$ N. E. J% A3 C/ W2 A
const long input_shape[] = {1,m_sample_size,4};* A- S# b  l" f, u) j9 U9 Z$ v
if(!OnnxSetInputShape(m_handle,0,input_shape))
4 T2 o  f: D) A' ^6 M: ^( E{
+ p! D9 N% P$ @! v" Z6 J, DPrint("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());
& q  Z- v) q! M% a& wreturn(false);! }  I, G3 V5 U* J6 \  U* J8 \
}
: \9 V+ T/ \0 Y4 O+ I//--- since not all sizes defined in the output tensor we must set them explicitly
/ \0 q' ^* a# ]! g/ p! C//--- first index - batch size, must match the batch size of the input tensor6 T, |" ~! p- e1 ]4 a. `2 f" o
//--- second index - number of classes (up, same or down)1 C) K# E! R6 ]8 f
const long output_shape[] = {1,3};
+ D" t4 T, r7 t5 z5 jif(!OnnxSetOutputShape(m_handle,0,output_shape))
" n1 h6 Q0 B0 f{
; ~6 u, r$ {# W1 W2 F  P% bPrint("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());5 y5 g  \/ Z6 }# V) W5 Q
return(false);, q( f% S6 I/ S2 Z' N# Y9 j
}5 t7 w6 N; K9 m. o; p* e
//--- ok. A- |) ~6 R3 `& S5 p% k
return(true);
  B1 E  [* o8 N% n, n8 c}
/ N: K7 B/ I' \) M% i: ~//+------------------------------------------------------------------+
8 p% u$ j: l, |3 q1 }//| Predict class                                                    |/ e& r0 m& y) i" E$ u# U  M( ^
//+------------------------------------------------------------------+' i! j4 v6 J9 [
virtual int PredictClass(void)
' |* N. _7 B1 g% f+ O; D{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-28 08:36 , Processed in 0.750474 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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