私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?
* Z* {& I9 a: I. i" ?2 q0 Q4 Y在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。
  `3 j- {& M3 x3 D我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进
$ n/ S4 p) j1 M//|                                             https://www.mql5.com |
2 J$ J, u" ]. E+ x//+------------------------------------------------------------------+
1 `% A5 X/ Z2 C8 v7 j* V//--- price movement prediction4 V" I. e  q. o( }: E% \# D
#define PRICE_UP   0
7 x( E/ p3 m2 n( O( M5 u#define PRICE_SAME 13 f9 c$ |: [! y9 F2 y3 T+ ?  \8 `
#define PRICE_DOWN 2
! \9 [4 K$ o; @7 q. c; G//+------------------------------------------------------------------+# `9 F  y+ K  E+ D0 M
//| Base class for models based on trained symbol and period         |' G& E4 ^; }: W
//+------------------------------------------------------------------+6 A! m* O$ K, V& G5 X) y( \" Q
class CModelSymbolPeriod
6 R" s3 O: c6 n6 ?* c{/ L+ P1 i/ n7 |: N. t. N: F7 n1 H$ v
protected:
3 ?/ H& ]+ |4 M+ ?8 Llong              m_handle;           // created model session handle' @( V0 i/ e& f7 U8 b5 Q" Q* Y" |& _
string            m_symbol;           // symbol of trained data8 C/ l' h6 j$ ]. A: V. K
ENUM_TIMEFRAMES   m_period;           // timeframe of trained data2 _+ L& E  b! O
datetime          m_next_bar;         // time of next bar (we work at bar begin only)* Y  Q* e; ^4 v" n. E. p
double            m_class_delta;      // delta to recognize "price the same" in regression models7 j  \, X5 f" B
public:' X* ]' m# q- r7 V
//+------------------------------------------------------------------+8 l/ B2 L5 \+ _+ ~  |9 b
//| Constructor                                                      |9 M6 l3 W- x6 m1 q- G
//+------------------------------------------------------------------+* Q: w+ D: C+ S0 m) j/ M/ i& s, a
CModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)
) Y; I( Q! v5 D. f{
* O$ x! U4 O3 j% p2 z6 cm_handle=INVALID_HANDLE;+ J0 Z! A5 w0 w  r- |( g' D
m_symbol=symbol;  R* K  T: v( p! m
m_period=period;  W2 q0 C( J2 l9 m; |2 J7 N
m_next_bar=0;
6 N) I$ l, P  T( J0 O- H2 rm_class_delta=class_delta;
$ X) a* n2 \; j7 u}
0 m. s7 x: R7 V, e  t//+------------------------------------------------------------------+& g* U9 v; N- ]8 D+ T
//| Destructor                                                       |
/ D5 N7 U  \2 W1 J4 ~//| Check for initialization, create model                           |# z  X: C* J8 K9 x* O; `0 I
//+------------------------------------------------------------------+
$ z5 t) H# S+ Nbool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])
" U0 h$ @! \- y+ s( |( x1 v{
( B8 {8 i* o" o3 D# x4 q( h) b//--- check symbol, period
9 Z5 h; c! l9 E' V9 j2 xif(symbol!=m_symbol || period!=m_period), k1 w& r6 D1 z5 }) M+ D) T
{
! `5 ~/ V3 \9 e) v" t; K% z+ a; {PrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));  a6 Z$ d  `/ l7 \  L! T9 k9 ]
return(false);) b* V, X, x' A& [
}
1 t; c  i6 E  P. n# m//--- create a model from static buffer
, c+ H2 d% C( pm_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);
" {" V  j! T. P0 w) P7 gif(m_handle==INVALID_HANDLE)
, Z- j/ N+ ]* F+ Q9 ~. n, K  h8 }! N) i; O{
" C. r/ ^+ m% y) HPrint("OnnxCreateFromBuffer error ",GetLastError());
8 Z  `0 u# m; X; G, }return(false);
* F$ D( X& p7 V5 w6 U( L7 a}
! w5 I' f; Z  Y/ ^//--- ok6 g. n5 t5 T& h) u3 Z
return(true);
) p6 g! O, U8 U, ^}
# f- v9 l7 `$ J# J9 y//+------------------------------------------------------------------+3 m2 r. j& T& H$ H8 _: ?% \
m_next_bar=TimeCurrent();
, @$ E6 S; E7 G7 r% l: @$ X' t6 bm_next_bar-=m_next_bar%PeriodSeconds(m_period);( X, k  b, O4 A8 D
m_next_bar+=PeriodSeconds(m_period);
" x+ C( f/ e+ u( N//--- work on new day bar8 t4 p7 A; x' m2 t
return(true);
  g$ O4 p$ F3 t7 U}6 V6 W* X- p6 D
//+------------------------------------------------------------------+
; i) Q( h* R1 ]5 h/ g0 G# t6 K//| virtual stub for PredictPrice (regression model)                 |
9 O6 O: v" d& s" f3 Y" Z! v7 ~//+------------------------------------------------------------------+
  {! I" p/ G6 t' T6 @0 r1 Qvirtual double PredictPrice(void)7 _9 [4 Q) Z+ w; f# P! ]
{
3 v6 ^/ ^" j) v2 Ureturn(DBL_MAX);1 q0 f/ v: P  K6 ~
}
' k2 _; Y1 a5 s$ z  ]//+------------------------------------------------------------------+
* G( j* G1 @% A7 r& I//| Predict class (regression -> classification)                     |) S5 E! f& t8 m" @& ^  j1 }7 g
//+------------------------------------------------------------------+
% Z) T0 p: m1 n3 O3 S( Q) mvirtual int PredictClass(void)7 o$ Y9 L, ^! |6 ]& V% u4 J; \2 j* c
{0 J" ]2 l9 L) [0 W
double predicted_price=PredictPrice();# w* _- E1 E4 L7 k' q
if(predicted_price==DBL_MAX). F- F" U, n! b" \7 m+ d
return(-1);
% Y' \# N  o3 ?2 L5 n! p1 jint    predicted_class=-1;! G$ _9 D. |) y( s: M/ y0 ~; k
double last_close=iClose(m_symbol,m_period,1);9 j4 |, J8 Y4 f. Y5 {% j
//--- classify predicted price movement
5 P3 h& X6 m. b0 z. C- ~double delta=last_close-predicted_price;
' T* V- l! j% b. `" wif(fabs(delta)<=m_class_delta)
+ K  i, D- C* U* h" w0 ^predicted_class=PRICE_SAME;" B% Y/ ?5 i! b- {* l  f1 F
else
1 O: {- z7 o! V7 x4 e3 v( V. Lprivate:) z5 T# W$ M/ Z4 }  N
int               m_sample_size;
! ~: k0 @& I$ V# ?# a/ H//+------------------------------------------------------------------+
& f0 L2 R1 B$ m9 _: \virtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)
( v1 O5 \# s6 T$ \6 z( g: [) c{
/ U& V% f3 [1 E: Q& [//--- check symbol, period, create model
" l! G: C: r& w( X& s7 `' A7 p. eif(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class)); {9 d. _4 V" M+ l  s& N
{* ]2 z. Y% e. {9 k0 Q# _
Print("model_eurusd_D1_10_class : initialization error");
; R/ q+ _9 `5 ereturn(false);. F$ V( o; u9 R% @) I
}  g# U0 U+ d4 t; m- \+ k' ?% D
//--- since not all sizes defined in the input tensor we must set them explicitly/ f$ x' s8 k4 M+ t3 x9 D
//--- first index - batch size, second index - series size, third index - number of series (OHLC); a: z1 J8 ~& P1 s! y! S3 {6 w
const long input_shape[] = {1,m_sample_size,4};& S7 _' k4 W. s7 {# Y# s
if(!OnnxSetInputShape(m_handle,0,input_shape))
. o" B4 W' N. B* x4 ~% u- n$ E{" m  j0 i2 E7 [& n6 A9 G/ z( p
Print("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());  x: _! G# q/ R( L
return(false);
9 E. M. `! J$ e1 c$ |}$ p# s& y2 M. ]" ?2 \
//--- since not all sizes defined in the output tensor we must set them explicitly4 ~  U" l0 `/ t- `. t' y) |
//--- first index - batch size, must match the batch size of the input tensor
7 R( X4 K9 W/ c+ u! K' e, H//--- second index - number of classes (up, same or down)
) w7 ?$ ?1 @( R( j* rconst long output_shape[] = {1,3};  z# N$ p0 A# y' l/ X' s$ N. E
if(!OnnxSetOutputShape(m_handle,0,output_shape))+ q( y7 L' A+ e+ p( |2 T3 d$ |2 `
{7 U9 ^4 T0 Y8 |% S$ c, |+ }, @3 u
Print("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());9 O( \, ]( v6 o/ k: m! H& {
return(false);; m. D/ y5 ~; x, b2 |
}
+ G6 G2 c3 ^0 L//--- ok
' B: @; R6 s7 \return(true);3 x5 A; \3 L# q: k' G0 a
}0 T% V! U* e1 v% [
//+------------------------------------------------------------------+
; Q3 I" F* R9 s8 M# R8 [6 ~6 V//| Predict class                                                    |
2 O8 L; v5 U. x( P8 m+ q( Z: f! T" v//+------------------------------------------------------------------+
2 M: M2 O1 o3 P; b: y1 L6 m/ Yvirtual int PredictClass(void)
9 i) P5 c! W' q7 u* t- k# J{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-22 17:16 , Processed in 0.775637 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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