私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?
/ G7 k3 _$ L" b! Q7 R+ l( q' u+ E在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。
5 @) y( S$ `& O2 O9 I4 v5 n) v$ S我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进0 u% W( X) v5 n6 F7 h3 C
//|                                             https://www.mql5.com |) ?5 H" B5 Z5 i9 p+ R" }0 W0 {
//+------------------------------------------------------------------+7 [, G' U0 s1 Q3 J( }% ~
//--- price movement prediction
/ @! s. b7 a/ H) _( Z- u2 E#define PRICE_UP   0
5 `+ T$ q% A% q1 m/ D# M" L#define PRICE_SAME 1
; e& h6 _2 _) h3 R#define PRICE_DOWN 2
# d% M/ L+ i) z7 t//+------------------------------------------------------------------+9 U( e; E( x; q1 I
//| Base class for models based on trained symbol and period         |3 g/ \/ ~/ o8 g/ V
//+------------------------------------------------------------------+
8 t- ]1 Q* V0 E8 A" G0 ~& T( Cclass CModelSymbolPeriod
5 {2 ]; }; I' ]: F, E{
, m7 a" x5 L8 zprotected:
# y# l( R, F& X. [1 ]) Blong              m_handle;           // created model session handle
- n! D: ~% I+ c& p- Y% n4 ystring            m_symbol;           // symbol of trained data3 F3 a& q: v- H7 X
ENUM_TIMEFRAMES   m_period;           // timeframe of trained data
9 Z% j' X; o$ \: h7 Sdatetime          m_next_bar;         // time of next bar (we work at bar begin only): r0 [: ^! [/ ]2 ?5 G/ G% H, J
double            m_class_delta;      // delta to recognize "price the same" in regression models
% c* c& K6 t$ ^" N1 l1 J( hpublic:
, P" L, F, J: w; t- O//+------------------------------------------------------------------+
% [/ S0 l3 M+ n' _  f, w2 k//| Constructor                                                      |7 s# a" N, G+ p  d* {8 B2 }
//+------------------------------------------------------------------+
. F- F- Q, M% Z. w0 bCModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)1 r+ Z) M/ S/ V! {6 e" b+ G9 X; L3 M
{8 h9 O* C( @$ J' v8 T
m_handle=INVALID_HANDLE;
6 W% u7 S- O; {! rm_symbol=symbol;% g4 ^9 S% u) x
m_period=period;: ^) q! F/ y/ S2 X
m_next_bar=0;9 D& h0 {7 L6 b
m_class_delta=class_delta;
  K2 ^6 ?1 x; d" \}, B# P2 p$ ]3 N( L
//+------------------------------------------------------------------+, b9 @* b% n. p! i" N# _
//| Destructor                                                       |
. ~$ i1 J' B3 s& F& H" F//| Check for initialization, create model                           |
4 E- A4 a: k9 r1 {//+------------------------------------------------------------------+
5 a$ F1 }& x3 O9 a1 f7 X7 @* D9 X  abool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])' S* _4 V+ w$ u! X1 _
{( z* n, H: Q  U' z: _" j" j
//--- check symbol, period
& t! d# ?/ R( H7 i3 p( X( [* Nif(symbol!=m_symbol || period!=m_period)
! [* s3 Z' n* {! q; X{  v4 B! q: i! @( T8 w4 i; ?
PrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));
& I. S  R$ }7 l- [. l  T/ r2 Dreturn(false);. ~* {  [, ?3 r4 U4 ?) ^
}
2 C" O' O2 D1 a' k//--- create a model from static buffer
& f/ ^" m( N) O5 g4 {4 Sm_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);
0 j- @' N6 ?. Yif(m_handle==INVALID_HANDLE): k' d' D# i: Y5 W* P- Y
{( [3 u5 D4 A2 w
Print("OnnxCreateFromBuffer error ",GetLastError());3 z9 y) f  u/ i3 g1 ^
return(false);
, l  C3 A6 q# D1 t5 c}% }& o( n# |7 ^* h# ^
//--- ok' n/ _  x! A% b' f5 j  J
return(true);* G5 E, B% `: A2 q$ }3 ?. J
}+ z+ {: h- a8 o
//+------------------------------------------------------------------+3 E1 K5 S' P' A  R* X& c" f
m_next_bar=TimeCurrent();. }$ ]' `% R; `! m% v. _* Q% }5 ?
m_next_bar-=m_next_bar%PeriodSeconds(m_period);8 T5 A& ]; a; @* `) Y  L# F1 |
m_next_bar+=PeriodSeconds(m_period);
( g5 K# y' g* d! n//--- work on new day bar& x) g# S, s2 ~: @3 J. L
return(true);
9 h5 I) O0 _- Z# U6 D8 z4 d2 D- t}8 W& O4 t' c- Y/ B) T& t( F
//+------------------------------------------------------------------+0 z! F7 |2 I9 F, j$ f
//| virtual stub for PredictPrice (regression model)                 |! I$ W% O% I1 @& `+ o1 S
//+------------------------------------------------------------------+1 v, ~& P9 a- ]  i. D
virtual double PredictPrice(void)% l2 c- j6 w' r
{
. R4 s. X; }" U) E& sreturn(DBL_MAX);
; P  E! A, {- D- Y5 U" l+ j}
) F7 l( g# U; R2 s; e//+------------------------------------------------------------------+$ Z7 [3 m+ T& H
//| Predict class (regression -> classification)                     |7 s# d: Y* j) m3 V
//+------------------------------------------------------------------+4 t* o/ L. M* }' u0 D2 q
virtual int PredictClass(void)
3 C* i2 k+ D. N! C) j5 L. X{
: O8 Y/ a; e" H/ Sdouble predicted_price=PredictPrice();" E) U- }5 F0 V5 e
if(predicted_price==DBL_MAX)
3 q' m7 j/ I3 N+ H7 Yreturn(-1);' c5 k$ F) I+ x$ F6 R
int    predicted_class=-1;
; }( T2 p6 i% J, S" Z7 b; udouble last_close=iClose(m_symbol,m_period,1);
9 H8 A$ m% R3 l( b. t8 P//--- classify predicted price movement
& T8 k9 n5 J5 K, P, X' U7 H$ Sdouble delta=last_close-predicted_price;6 V# k  `5 X7 }. z5 r) J
if(fabs(delta)<=m_class_delta)
  v( f* J  F$ Vpredicted_class=PRICE_SAME;
4 V$ u6 ^6 W' ]. O" D  ~else8 p) J/ Y; H* E* \: T" O1 E
private:* F9 X8 r5 O* C2 I
int               m_sample_size;1 l$ _$ k8 E4 o$ k; G
//+------------------------------------------------------------------+8 c+ e, L. a" }9 N6 T1 S
virtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)
; k  Q1 a5 r2 v' X$ F: Z! Z% n{
2 o1 ~5 o# g  ?, C8 M- d2 y; v0 X//--- check symbol, period, create model4 g+ J/ o9 [- y$ k8 e" k% I
if(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class))/ \0 y2 d! D7 [2 T+ p3 t( S8 ]( b
{, X4 G2 C- f' ?
Print("model_eurusd_D1_10_class : initialization error");" d. X# @1 t& C
return(false);1 ]% Y9 i3 [8 \
}
& s* T5 o9 ?" V! y3 w% l//--- since not all sizes defined in the input tensor we must set them explicitly
) w! p3 m+ l! L//--- first index - batch size, second index - series size, third index - number of series (OHLC)( ], V% a0 x6 f# E7 y0 W
const long input_shape[] = {1,m_sample_size,4};& ?, ]% }* Y1 R, M8 ]- W1 C
if(!OnnxSetInputShape(m_handle,0,input_shape))) H8 q& B* P+ {+ O$ }
{
! `+ v5 r% [/ f% r2 b0 Q2 NPrint("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());: C! Z5 i  C: Q. }8 ^. @
return(false);
. L% c7 L7 N4 N}
0 F, T/ Q' C: V% t9 ^1 D& }//--- since not all sizes defined in the output tensor we must set them explicitly! @' s0 B2 [+ ?/ J
//--- first index - batch size, must match the batch size of the input tensor
( `0 m# O3 y' z8 o$ \- H//--- second index - number of classes (up, same or down)
( @8 w3 U' R3 xconst long output_shape[] = {1,3};
5 ~; b1 ?$ W$ c& M/ R: ~if(!OnnxSetOutputShape(m_handle,0,output_shape))
5 }+ V. o% X- f+ b" n# s{
+ g2 b8 J2 t1 O+ ZPrint("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());2 h/ u1 Z* V) S# n2 B
return(false);8 J3 j# W$ D+ P, Q* Q/ `/ z" \
}1 y/ i. P! j+ W0 e& Z) m3 x0 X% C
//--- ok+ f$ Y' h8 B6 c2 J
return(true);" s# P" R1 n! b) X- U
}
# u0 _: X1 O2 e! C7 `//+------------------------------------------------------------------+
% v1 E+ }+ [) B0 z/ e  f//| Predict class                                                    |
) Z5 M* R$ Q" Q- N  i//+------------------------------------------------------------------+1 f2 v# {! _: _1 x
virtual int PredictClass(void)
  M# ]6 Q$ @) x: ]) a{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-19 18:46 , Processed in 0.423256 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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