私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?
- W! F: p! D: q3 S) f, D4 y, p在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。8 h7 W  b5 ]+ P( P: Y& ]. v
我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进
; j6 K, ?/ S+ J' @$ b+ X//|                                             https://www.mql5.com |
' q3 N% y7 ?9 u8 b" T! ]//+------------------------------------------------------------------+- S2 J$ k& G+ F! K/ e0 }" o
//--- price movement prediction" r4 `  j: Q7 z  A6 F
#define PRICE_UP   0
2 G. S6 j0 @7 @; X& {, b#define PRICE_SAME 1
4 I1 U2 B! H& d$ v! R#define PRICE_DOWN 2
6 ~4 p/ ?) q6 X! x8 z9 B% u//+------------------------------------------------------------------+
9 f5 |. X* r$ ]& ]1 T$ N7 O//| Base class for models based on trained symbol and period         |. i' J7 A% l$ H
//+------------------------------------------------------------------+
, T, X- o# o, ?- ?2 hclass CModelSymbolPeriod8 D9 U/ n1 o) d
{  [2 G4 E1 M. f8 h# L
protected:
  l) o( O6 F: K0 {  Q6 K' E% i- {  glong              m_handle;           // created model session handle  n+ L& Y3 {! Z
string            m_symbol;           // symbol of trained data. `* P8 G& P0 x' `; H: u; D
ENUM_TIMEFRAMES   m_period;           // timeframe of trained data4 {) S. C+ i% \1 V! y5 F' K
datetime          m_next_bar;         // time of next bar (we work at bar begin only)  z. ~6 v' a& l  u& C" U
double            m_class_delta;      // delta to recognize "price the same" in regression models
4 z( C! T4 R6 H! [. ]) S  z, ypublic:
0 e, y% I& d  I, U) D  A! w//+------------------------------------------------------------------+
5 _3 J) c5 }( b2 d//| Constructor                                                      |# T3 H0 b$ @. h" ?) h5 a  _- Y# \
//+------------------------------------------------------------------+
4 ~1 g/ I! |6 f6 t4 tCModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)* \, R6 ~  x. D2 v
{
9 R6 }$ ?7 j" _1 [3 I# {# am_handle=INVALID_HANDLE;" f( T4 t. n% i4 e* z
m_symbol=symbol;" A0 P1 Q/ P/ ]' Y' k
m_period=period;
, I: V6 [- @  @) @m_next_bar=0;/ |, e( x/ [( Z6 _1 O, n- J; L5 e
m_class_delta=class_delta;
$ q6 i, R# G! [6 i}
8 }, M4 n8 u  q/ e  Y; i4 V//+------------------------------------------------------------------+
# S$ H+ {8 n: R8 y$ ]//| Destructor                                                       |
  b& ~& k8 B( P8 X! |9 F. H; T//| Check for initialization, create model                           |
8 g7 f' Q. h' r( y8 Z9 c//+------------------------------------------------------------------+; |* x: W3 i% o- E- c' ?+ _
bool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])& v5 c$ Z" |) O
{* K( y* Y/ j% c; R( E2 M+ f& k
//--- check symbol, period
+ b- m, e3 _" Q6 u# ~if(symbol!=m_symbol || period!=m_period)4 H4 v/ P5 r0 h. b6 K; `1 u. g
{$ R$ r! \  }2 D" G
PrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));
  O1 h3 N- F2 N+ H+ yreturn(false);. h3 n, d) T* O9 p+ ~
}
# V4 E! G+ r1 J//--- create a model from static buffer3 F( z! x0 v5 @0 G6 _. `9 u; o/ b+ G
m_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);$ l4 H3 m' w, M* D) V* A
if(m_handle==INVALID_HANDLE). S0 A2 s+ L" _7 V/ \
{
7 b* }7 @  @! I( DPrint("OnnxCreateFromBuffer error ",GetLastError());0 A4 T4 U% Q1 e
return(false);
, a6 a* S. E: w* n}4 G' T' M. i. g5 Z" l5 C
//--- ok
0 g+ ]0 O# @8 y; Dreturn(true);
# y, B+ x3 E' H1 v% E}3 {3 w+ x# x: a2 K; T; _
//+------------------------------------------------------------------+
2 f+ E6 k- F) ym_next_bar=TimeCurrent();+ s, f/ D; o1 K% F- n) E- ]: V
m_next_bar-=m_next_bar%PeriodSeconds(m_period);
1 }+ i; M+ B# R9 x3 m0 bm_next_bar+=PeriodSeconds(m_period);
  A4 y1 I  W) `6 e9 Y& y  }9 H//--- work on new day bar, J$ I% a. b; Z/ E8 w( Q# d1 K
return(true);
) Q$ t9 `. e* I: U4 Q3 a}1 O1 E. v! m+ y; t4 v$ c
//+------------------------------------------------------------------+
9 o/ t7 E' L# W- e$ Q& }1 F$ u# G8 T//| virtual stub for PredictPrice (regression model)                 |
$ o9 u2 E7 Q/ Y//+------------------------------------------------------------------+
/ \6 L- s, `! V1 Mvirtual double PredictPrice(void)0 x6 R. F1 }8 M# e& ]" z9 K, j
{
; S! T. u$ q' Areturn(DBL_MAX);
$ |7 e% l" z) d8 P- p0 I* g3 c}
' J- G- S  H( _; c$ r+ s6 {//+------------------------------------------------------------------+) c% \; l, K- i# F( Y$ D' c/ W
//| Predict class (regression -> classification)                     |
/ q2 m6 l3 g! @, x  }//+------------------------------------------------------------------+
, C4 }' g; g  p; ]virtual int PredictClass(void)) W; [8 _0 N: u" J5 |6 W* ?
{' X; q: h+ {! @& \
double predicted_price=PredictPrice();
, F* o8 R" b% L9 Z% S7 Hif(predicted_price==DBL_MAX)
, J  [* a6 T# \4 ?8 m& |) Y) Kreturn(-1);. j# ^9 W4 ~0 Q% s6 c, z
int    predicted_class=-1;  p' |: D2 n4 c
double last_close=iClose(m_symbol,m_period,1);7 h9 `' O, K( F, d! t8 T3 w5 `* t" N
//--- classify predicted price movement4 _; P; Y( K# D8 f; s6 o6 Y# ]8 {
double delta=last_close-predicted_price;
+ A" j: V0 H& I/ N! C$ R$ |" ?, W( Gif(fabs(delta)<=m_class_delta)
* t5 G5 u% Q5 A5 _" v% T  W9 \7 xpredicted_class=PRICE_SAME;* m" k; S4 s! h& [1 T7 G# a. U
else
% Q+ E4 D. V9 Z7 Gprivate:
7 y9 P# z4 X; s0 A# T7 Z: ]- ^int               m_sample_size;
) w4 i( m6 D' m% w* _3 j//+------------------------------------------------------------------+0 c' i! a4 @& g7 a! A9 e6 \7 u
virtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)  Z9 A% L: s2 i# F; r! k* o
{# e6 a# l* H6 q/ x
//--- check symbol, period, create model
. k; N! J8 Z9 `  `if(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class))
9 [1 f; v  ]! z8 G0 b. A{/ s1 C* H0 H; q+ l; c8 H5 n" w
Print("model_eurusd_D1_10_class : initialization error");
- Y( B8 n/ I+ U9 T) Wreturn(false);
, V# k* r( |# O+ k}% L+ u8 d. L) O1 R" _, h
//--- since not all sizes defined in the input tensor we must set them explicitly
. D4 x( {7 [" x: y- _) r//--- first index - batch size, second index - series size, third index - number of series (OHLC)( J0 {) s9 F! J) n5 I# D5 ?/ b+ d$ g4 X
const long input_shape[] = {1,m_sample_size,4};
, }6 r. c! `2 Nif(!OnnxSetInputShape(m_handle,0,input_shape))4 o, _# ?" h- ]; M5 K% n( Q1 h
{
  z# Q. v0 n3 i' @0 q2 X0 IPrint("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());% H. R6 ~! y1 s; A) ?1 D1 i
return(false);
: E* Q4 j0 E- Q. o}; q* h* I8 t0 ?  }- e: r  [7 h3 p
//--- since not all sizes defined in the output tensor we must set them explicitly7 Q0 m2 `7 l. _7 T
//--- first index - batch size, must match the batch size of the input tensor5 g( G$ K% u! }
//--- second index - number of classes (up, same or down)) `' r" ?6 \" W# m' d$ E) C
const long output_shape[] = {1,3};- [# f0 j" G' }' ]7 g
if(!OnnxSetOutputShape(m_handle,0,output_shape))
% e/ p' J" p; O0 t* ]{: p) ^3 P: r* h1 K# [
Print("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());
. \5 e. ?, e% H2 M) l' C: e0 ureturn(false);
; b: F, ]1 ]8 s1 |+ W}+ I4 n3 ~4 F8 l0 r
//--- ok
: B. q/ u* r' N2 ]2 }' L3 {return(true);9 b! i+ _2 J1 X; j. g
}
& i0 k, S0 q5 E//+------------------------------------------------------------------+
5 F. g% k$ k9 M% _8 N$ p//| Predict class                                                    |
* V: [4 U; r$ W, V: P//+------------------------------------------------------------------+" I. R' v3 U/ |' B8 P  L8 |, q. K
virtual int PredictClass(void)8 m. H2 v. f: H' B% Y( t
{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-18 14:54 , Processed in 0.404036 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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