四目观天下

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?
/ }, }. M3 ~4 I# O$ o) k: Z: a4 ]在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。* f0 P) |2 N& u3 l+ g
我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进
9 c$ r2 i' v9 r3 ]; j//|                                             https://www.mql5.com |
3 B. }0 |9 k8 F$ o. ]7 W4 l6 K- G//+------------------------------------------------------------------+
+ D% Y8 R( j, r0 q" n//--- price movement prediction
3 w) L6 G) ^. f$ }. A/ R  _; c  ^9 ~#define PRICE_UP   0
7 ?) ^0 n5 N3 X/ ?: z1 n3 ~  h#define PRICE_SAME 1$ \$ Q# E! B/ \1 n( Y; X! x
#define PRICE_DOWN 2! g- q' ^. i  u% w8 }9 @. w
//+------------------------------------------------------------------+
; _' N5 I% N) y3 R8 R) B//| Base class for models based on trained symbol and period         |
' d- v, O% U: l//+------------------------------------------------------------------+
8 `1 b0 @" K  w( \. M; sclass CModelSymbolPeriod. V) X% R: T- Y, m+ Y( F* J
{
# Z& d4 u% D& q+ `. ]% tprotected:$ t2 r) [. U8 C9 q' _! e$ G+ Q* `
long              m_handle;           // created model session handle
  I# l9 o! ]5 X7 r  v7 h8 I9 Nstring            m_symbol;           // symbol of trained data  N4 b. p$ W. O) F. r# L& O  O1 e
ENUM_TIMEFRAMES   m_period;           // timeframe of trained data$ h# B- w; m1 {) t" z6 P
datetime          m_next_bar;         // time of next bar (we work at bar begin only)
2 l8 T8 b" d  b$ f1 [& tdouble            m_class_delta;      // delta to recognize "price the same" in regression models" N4 ~! d6 W. z8 q8 h: n8 M% z
public:- r6 I/ i8 z  f' }5 J$ E
//+------------------------------------------------------------------+2 b" r( |* T1 O" I* ?  B
//| Constructor                                                      |- ]4 G2 |7 \; {" A
//+------------------------------------------------------------------+; }+ m; u' ?8 M
CModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)
- b5 ?  n/ k6 }/ a; |$ R( o{
* g: V- h3 K. a7 e! c+ ~m_handle=INVALID_HANDLE;
* N6 N* B4 I8 r: _+ \, Em_symbol=symbol;
- \: }1 t: J# I2 G* [m_period=period;
8 h, N5 P1 a3 I, f8 O2 hm_next_bar=0;
  J% z7 k& V: g3 r6 W$ hm_class_delta=class_delta;! _& F" O+ K: c: R: @
}
3 W5 s& y- `1 h8 A9 e3 k//+------------------------------------------------------------------+
7 {$ L$ U" j6 R6 c' L2 P. V//| Destructor                                                       |2 f; H( p9 O9 s: B7 r+ ]
//| Check for initialization, create model                           |0 @/ r1 |, @! s4 Y" ~4 x+ S) N
//+------------------------------------------------------------------+  A, S  a$ ]. b0 j+ V
bool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])% W3 `) {! Y/ v" V+ U
{
. a( [3 W% P/ X* g' O5 m//--- check symbol, period9 r( h! u6 _, c/ z( y  y
if(symbol!=m_symbol || period!=m_period)
4 ?. Z& A, C0 p4 q$ Y{9 M( W3 S' C( r) C' M
PrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));! D% w6 T' a5 y# p: g6 O9 L
return(false);* W' i8 t9 ]5 Y, Z3 r" b
}+ I; N1 F0 |  C
//--- create a model from static buffer5 K; k. \' r# n3 ?& Z2 d
m_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);, Q$ ^# @% p% T+ e; X- N
if(m_handle==INVALID_HANDLE)& A" |8 l. k9 n3 r3 E7 s% W
{8 k6 p2 T# F5 B1 L
Print("OnnxCreateFromBuffer error ",GetLastError());
0 `6 ^8 z0 I. m2 X' X' [1 Rreturn(false);
1 p( ~  b& g) ^# d6 W& X( p% A}
8 Q, |8 D2 l' ]" u, u9 S% e//--- ok
' e) `0 c% G9 j1 Z6 ?2 Ireturn(true);
8 A2 T8 J: A0 A}7 i4 C. ^9 p5 H  ?9 n2 O8 N
//+------------------------------------------------------------------+
: F( [8 h# q. ^1 E, }* Mm_next_bar=TimeCurrent();/ X- b8 l# `: \
m_next_bar-=m_next_bar%PeriodSeconds(m_period);
) q- u7 E' Y  P! |* z% Jm_next_bar+=PeriodSeconds(m_period);. v9 K7 J9 A5 X8 B1 b4 c) x
//--- work on new day bar. ^4 f2 {( }+ f( n: v
return(true);3 T& p5 y% G3 [. ]/ U( G' P2 O
}
6 x; S" K/ D5 [- b% V3 r) W//+------------------------------------------------------------------+
1 m' O. W7 C* y# n- n  v//| virtual stub for PredictPrice (regression model)                 |
/ I; T/ I9 U! y8 `//+------------------------------------------------------------------+* h* S  `  c- Y/ o
virtual double PredictPrice(void)
( B5 a$ |' d$ y. S5 }7 |{
" u! G1 S/ x. r% ereturn(DBL_MAX);
& O4 V+ A: V  A9 j+ m}& |3 j6 [8 |3 S4 R% E
//+------------------------------------------------------------------++ z; n$ e$ G! `" I8 C" P
//| Predict class (regression -> classification)                     |3 c0 h1 S; V0 D* V
//+------------------------------------------------------------------+7 C2 L5 d" M+ Q6 v0 C* Q+ g
virtual int PredictClass(void)  y% F' d( ^9 x9 L$ }0 B3 z: ^: x+ M
{
6 g4 }7 X: R+ U0 @- U' T3 ~% zdouble predicted_price=PredictPrice();& E, T8 b1 X( P4 d
if(predicted_price==DBL_MAX)
) w) J7 P8 C: g$ e9 t2 ?8 vreturn(-1);: N+ B5 i# K4 u6 S. F' E% u
int    predicted_class=-1;
1 @1 t, X* y0 p: G' r4 l$ U/ y2 Ydouble last_close=iClose(m_symbol,m_period,1);
3 Q: p# \+ `9 [7 e( U1 J3 X7 a! i//--- classify predicted price movement0 I6 s" `! }3 Q$ M
double delta=last_close-predicted_price;+ p3 T0 j5 B3 T, i: K3 R0 Z
if(fabs(delta)<=m_class_delta)
* q2 D' [  r. N( j( G' ]  i4 Vpredicted_class=PRICE_SAME;
1 y; H' e" V5 C0 \' ^else
3 E# D* d  Q: j4 rprivate:
% {6 r  ~1 M, i. s+ A" N3 dint               m_sample_size;1 G0 H, f" A9 H# }
//+------------------------------------------------------------------+
2 `$ y3 Y2 q, a0 J$ Y; Qvirtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)( `2 B( p! G. Y, G
{+ x! u' d4 |1 K. D
//--- check symbol, period, create model' b6 |! x; b% \+ e
if(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class))7 a% K  |1 e) S1 E) s
{
! R# R) h7 B! v/ Z& k' t8 L6 xPrint("model_eurusd_D1_10_class : initialization error");' B0 G; S6 e! p; H
return(false);; `# D+ h( k5 i7 G4 V4 U9 t) Y
}
0 M7 r5 g% E8 X5 S* E, D//--- since not all sizes defined in the input tensor we must set them explicitly
/ w/ r7 X1 I5 j6 V* N//--- first index - batch size, second index - series size, third index - number of series (OHLC): u. D& g" t/ y% ?& D* x
const long input_shape[] = {1,m_sample_size,4};! e3 Z5 b& d$ u
if(!OnnxSetInputShape(m_handle,0,input_shape))
) O& _) J7 u8 @2 F{
. p7 _' o+ O$ N( V0 @1 JPrint("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());
% {" U4 A7 |& G& B& s7 S- Z3 x+ |return(false);
+ |/ k/ u  q. c- i, r}6 \' ]; u/ J8 W
//--- since not all sizes defined in the output tensor we must set them explicitly
4 U. ~9 ^( N3 k- ^- j5 g/ S! P  P//--- first index - batch size, must match the batch size of the input tensor
( s- R8 t  u, S//--- second index - number of classes (up, same or down)( N8 O' W0 l% `' M# Z) F
const long output_shape[] = {1,3};2 B; p/ b3 V- d
if(!OnnxSetOutputShape(m_handle,0,output_shape)): q+ a2 p4 [' y; U6 Q2 G: Y" D$ c5 q
{
8 _! i/ V: I1 i4 D2 Q+ C) bPrint("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());
9 U% ^* L. k2 Y8 H8 t9 Sreturn(false);$ Q$ v2 O, O7 t  Q) m' E
}
& H2 \; H0 c$ x. }1 q$ o//--- ok, E9 n. a1 y! q* `) X2 {* K
return(true);: z) _7 R% L: _8 i4 D6 ^9 v  x
}% j2 f: x5 E% r$ U: d! q
//+------------------------------------------------------------------+
! y( \  ]8 ^) k: [9 r//| Predict class                                                    |5 I& p2 |. o7 z+ o3 n$ g2 _
//+------------------------------------------------------------------+
3 |/ L7 y4 E1 L% i7 g& W, [virtual int PredictClass(void)
. t8 _: H  `& j2 ^- U- f: o{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-8-16 23:07 , Processed in 1.530041 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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