私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?0 h& `2 P% m: e. T0 w
在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。
1 J; Z' _0 B8 U我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进( Y$ D9 e* r- U/ t7 K' K( _' e3 S( c
//|                                             https://www.mql5.com |
* Y& L% I2 x/ V7 g0 f  r//+------------------------------------------------------------------+
( d/ {, F5 [8 x2 J2 U/ N# B//--- price movement prediction
0 y( J, a7 {; B* s#define PRICE_UP   0
9 H9 u; K0 p3 R% ]9 u* S( K/ Y#define PRICE_SAME 1. z6 o" E0 j8 q2 h
#define PRICE_DOWN 2& n5 X) U3 s0 `
//+------------------------------------------------------------------+
* W/ N! H5 ^4 r5 R//| Base class for models based on trained symbol and period         |* L. M* I  {+ W9 t7 t: f- a5 i
//+------------------------------------------------------------------+9 G$ O( q  y/ [/ _& S+ X
class CModelSymbolPeriod
/ y6 o+ U3 D0 _# l8 W{! ~- G! A" v! y" I: f4 z- G% X
protected:
: c$ O  q. V3 F7 b; \. olong              m_handle;           // created model session handle0 e, V$ E2 w. ]) S& _/ H
string            m_symbol;           // symbol of trained data
5 x( b; `3 W' \/ xENUM_TIMEFRAMES   m_period;           // timeframe of trained data
$ `! r! |' }- r% F( W& k( Rdatetime          m_next_bar;         // time of next bar (we work at bar begin only)
; m2 Y: q4 l7 v! L: r& \double            m_class_delta;      // delta to recognize "price the same" in regression models
8 N) p0 F8 s1 N4 ^5 ^- O9 fpublic:
  V" i8 e( N" v7 X  G6 B' w5 g. c//+------------------------------------------------------------------+9 x; K* z9 R+ n0 _
//| Constructor                                                      |8 L/ r% J* P1 t+ p% N
//+------------------------------------------------------------------+
8 P4 E/ O" U4 SCModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)8 p. ~2 _8 z2 K) O% [# G
{
2 u1 Q- A  r. O/ R( Cm_handle=INVALID_HANDLE;
7 n3 r" x5 P& R) hm_symbol=symbol;* s% s/ W& W/ M, c9 D* |4 z5 S3 s
m_period=period;
, w! f+ k# \$ N) V/ qm_next_bar=0;
" E: e& \$ ~! W1 V! P/ l5 vm_class_delta=class_delta;
* Q6 h: W0 N6 R/ G}# `9 t4 T  @, }% `/ k
//+------------------------------------------------------------------+
# u( ]! g6 ?- R# E% |9 R" Q//| Destructor                                                       |
( p1 L" A+ N5 [2 d' w4 o7 t//| Check for initialization, create model                           |4 I8 w; o0 L/ G* a& y) n
//+------------------------------------------------------------------+
* Q7 ^9 C1 y1 g" kbool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])
" F% n; x. n" T1 n* m4 A{& M% j* h1 C& z" w7 }- B
//--- check symbol, period0 O. u. h. u& W1 A! i
if(symbol!=m_symbol || period!=m_period)
$ `/ p5 l) o4 p1 f{% a4 L8 V8 p6 v
PrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));8 O- E" _' L# X- `9 Q+ w2 s9 ]
return(false);# s% P7 _8 b6 |& x- c& ]
}% R! ~% ^0 X3 y" n& i! S6 f2 N0 @( N
//--- create a model from static buffer
- Z+ w! A9 |/ Pm_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);
- ~. p! r# \2 u5 d; D. tif(m_handle==INVALID_HANDLE)7 U9 H/ p4 L, o) `: S5 U
{
$ b- C; J2 p- g) uPrint("OnnxCreateFromBuffer error ",GetLastError());# E  j3 Z5 k/ |: F: Y) [0 u5 h
return(false);9 ^$ u% h, o7 j+ M5 `! X4 \0 _
}
" M9 ]8 _3 B* I' @  q, r! ?//--- ok- O1 t" E4 P$ G( v- z+ L
return(true);4 P9 |" k% j' @+ a
}! h6 a4 T1 v  ]; F& s
//+------------------------------------------------------------------+
8 M7 b& v' _/ L0 |/ em_next_bar=TimeCurrent();+ z) c5 W. X0 e& q7 ^5 z1 X
m_next_bar-=m_next_bar%PeriodSeconds(m_period);7 n; W! Y5 z1 E3 ]/ ]' E$ i6 P
m_next_bar+=PeriodSeconds(m_period);
6 ~; Z1 ?5 [- |& R, W1 E//--- work on new day bar
  i: I. r+ H) ]( }$ preturn(true);1 x4 h9 n' a" s1 A
}
, u6 A1 `% ]) j. ^0 D8 k//+------------------------------------------------------------------+3 y3 g' {4 B9 \% [9 t- S. N& O9 e
//| virtual stub for PredictPrice (regression model)                 |
4 P) {! @# g9 P, _" B. y//+------------------------------------------------------------------+* x( }) H) p+ h% Z% B+ n
virtual double PredictPrice(void)
9 C  Q( J# L, G% i5 a{
1 F6 v* k" ~7 s  c; o9 vreturn(DBL_MAX);
+ X- x# g  t. Y8 c$ {. Q}! b+ n# \7 k$ }
//+------------------------------------------------------------------+
  j" j7 g7 z* y) }//| Predict class (regression -> classification)                     |
- Y7 g% h, h% _0 p3 h//+------------------------------------------------------------------+
  Y" Q6 R0 h. V% {% Bvirtual int PredictClass(void)
& h+ `- \+ r+ u3 u1 U  W{
7 z: G6 U( m/ P3 \$ Ydouble predicted_price=PredictPrice();! Z* l/ k, }' m  `, A8 H
if(predicted_price==DBL_MAX), y; [# m6 z7 ^" j
return(-1);4 [; m$ `8 i' M+ N+ {5 M
int    predicted_class=-1;, ^6 c" @& h. E$ ?
double last_close=iClose(m_symbol,m_period,1);. V1 V. V7 b3 h, E
//--- classify predicted price movement9 I. A, s7 x- ^6 g& E
double delta=last_close-predicted_price;
6 W0 `( h  a; Vif(fabs(delta)<=m_class_delta)
! f$ ]  s8 `  ?predicted_class=PRICE_SAME;3 r4 t, H0 S- E
else
) P# t8 r$ _9 e* b0 aprivate:
5 f; x9 g6 B0 D$ }int               m_sample_size;+ }4 X9 o, H* w! J- Q) V  o) P
//+------------------------------------------------------------------+
1 }$ v) g2 ~2 w& @virtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)
" h' N: v8 D$ }8 X& f" M% {{
1 e: ~- u1 ?  w4 T//--- check symbol, period, create model
* {& O0 w, |! X! ~if(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class))+ S& M3 {0 _) c+ X4 a6 T
{% R4 _: t8 g% _) |
Print("model_eurusd_D1_10_class : initialization error");
0 {/ }1 j* U- L8 t. Y' y7 Wreturn(false);
: S! \, ~. |; z! K, G4 m}
* `$ e2 n- P5 S6 P//--- since not all sizes defined in the input tensor we must set them explicitly# d! G; W) p7 A. m/ K
//--- first index - batch size, second index - series size, third index - number of series (OHLC)
) g: Z3 l! c0 u/ i. j% Econst long input_shape[] = {1,m_sample_size,4};
3 @% D4 j9 p* Nif(!OnnxSetInputShape(m_handle,0,input_shape))2 G9 o) o9 I( D
{* g3 x6 o* ]$ O8 x, r) h! B
Print("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());2 P, S. J; L. L  [
return(false);% s* u  r" Y! T9 B9 g+ K
}( z, E' d2 u+ G: E1 r: z2 n
//--- since not all sizes defined in the output tensor we must set them explicitly
3 \+ L3 r; o8 [4 e3 ]0 y//--- first index - batch size, must match the batch size of the input tensor
% o" {- j: C+ @: X//--- second index - number of classes (up, same or down)
4 o& J2 F+ y. ~const long output_shape[] = {1,3};
9 ^: K: W6 i1 B$ }2 y2 oif(!OnnxSetOutputShape(m_handle,0,output_shape))) Q% a# S+ H& k' ~9 Q. Q
{8 t! X, k* I1 N% R  v( {. L4 h  z
Print("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());* s- @3 v- l8 y6 q
return(false);
; X6 `6 y6 ~+ P5 w2 I1 w7 |& M}7 I. h) E) `- |! @, q8 C
//--- ok( ?( f$ ?% n6 a, P2 W' y
return(true);5 a/ X/ N4 L3 r- U6 N$ F8 K. S3 T
}
* C" p: Y- c# H* l. M0 x  {# x//+------------------------------------------------------------------+
+ P' E% n. @' o$ Q//| Predict class                                                    |
, e% R& b( J6 l2 @+ G//+------------------------------------------------------------------+
% ]' {! ]2 a% p5 E- H7 Y* X2 qvirtual int PredictClass(void)
* M9 b  p& a1 I. b: e{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-24 03:47 , Processed in 0.407464 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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