私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?& D4 I' |$ H4 w$ X; S5 @4 p
在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。. u9 r" K/ Y0 Y* \& e  [
我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进( Y% a& y' d* V. @4 ?
//|                                             https://www.mql5.com |
2 \4 \# s- V7 X8 h//+------------------------------------------------------------------+
, u: O2 a! \8 b+ Q1 q* S//--- price movement prediction8 ^, M" S8 R6 i) P' u- I& @
#define PRICE_UP   0
6 {7 P" e8 p* ^7 d1 l3 Y1 r#define PRICE_SAME 1. b( f  z* E5 @0 S' \, {
#define PRICE_DOWN 2
6 C' a" j+ ]; e# `; w$ q5 R4 a//+------------------------------------------------------------------+* P6 K) n( k% R, t; o+ Y" f( h
//| Base class for models based on trained symbol and period         |
9 H9 V$ `% L: ^2 [//+------------------------------------------------------------------+
" B  v+ Q; P# j) L; p% l: hclass CModelSymbolPeriod
0 @5 q4 s$ Y/ D: w{0 [  v! z- S8 }6 q+ E
protected:
, Q" s- j/ m; s4 s/ T" xlong              m_handle;           // created model session handle
& c7 j5 o" A, u& ~/ T1 O. ?; ]string            m_symbol;           // symbol of trained data/ \& v3 s) b( F- E8 b; [
ENUM_TIMEFRAMES   m_period;           // timeframe of trained data
* |3 B0 b. j" t* Ddatetime          m_next_bar;         // time of next bar (we work at bar begin only)
: L' N: @+ M5 z; z# ^- N3 Wdouble            m_class_delta;      // delta to recognize "price the same" in regression models0 L3 G8 F5 g8 h
public:
" ]$ D4 o, }- G+ v) a//+------------------------------------------------------------------+0 X( B  B5 E! Z' M
//| Constructor                                                      |
, H7 u' B' u0 Y0 N0 f//+------------------------------------------------------------------+
4 e5 F( c8 x$ b6 a9 A6 `* @+ ?CModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)
1 |; g  i% n7 X# c4 L) S4 |{
$ }, X& j0 k: R# |m_handle=INVALID_HANDLE;" O7 k* K% s& P3 q' T8 s- f
m_symbol=symbol;& ]  ?7 i  C/ Q; N  G4 s% b; Q
m_period=period;7 |( ]) e$ H, k, [
m_next_bar=0;- j! o; S3 d+ ^& k4 F
m_class_delta=class_delta;, x' b) O2 o7 s
}
( O4 n3 R9 V+ ]) i//+------------------------------------------------------------------+1 D% k9 _6 u8 T. h% I7 L$ r
//| Destructor                                                       |4 X# k% z: w/ E
//| Check for initialization, create model                           |4 m$ W) G8 p% g, Z
//+------------------------------------------------------------------+0 ^; x9 n. z' R; y
bool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])! {* s. o4 ~3 F5 H- O) g( o: U  w
{, q" K* R1 {5 x  m
//--- check symbol, period
" ]: E8 I5 V, g% s; _( |if(symbol!=m_symbol || period!=m_period)
+ A7 V/ s& {* h, ]+ e, _) [. i{4 |4 K5 B3 E2 {5 Y- R  @5 Q3 k, s) ^
PrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));
! V. V& n& t, R; b( rreturn(false);8 G( f3 A5 r9 [9 T8 V
}- O4 k  k5 B2 j6 k$ w
//--- create a model from static buffer
* R+ S' k( m* b% a6 G6 j9 Gm_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);
+ o, ]8 J& l0 `6 r/ h# z9 Dif(m_handle==INVALID_HANDLE)! ]/ y* R, h' S3 c) C( W6 n" |
{
3 h4 b  h- W& |* m' k7 f6 V' ~% MPrint("OnnxCreateFromBuffer error ",GetLastError());
0 Q: W& G' |, ~+ j3 I# y8 X# oreturn(false);
+ J% v; E: m9 d6 H5 s0 W}! M+ V9 m& s2 k" I8 d
//--- ok, j+ q3 x* ]. `- J2 F7 U
return(true);- }3 O: j  H/ n* ?5 i
}
" q' [- @( P- ]! g7 j& o  U//+------------------------------------------------------------------+" N- s, |! q, a( n7 M! a8 i
m_next_bar=TimeCurrent();/ t! m) I+ K6 s0 u% y
m_next_bar-=m_next_bar%PeriodSeconds(m_period);8 n8 |% M% m5 H8 O6 z# `6 s
m_next_bar+=PeriodSeconds(m_period);
; x" m+ j; _8 C! O//--- work on new day bar
) m+ k2 {% [" ]: q6 jreturn(true);$ W" t: F: V9 A8 U% X6 `. {
}" A. N, C- D6 H; w+ y6 W
//+------------------------------------------------------------------+4 N* h0 p1 f5 |9 N8 t: [
//| virtual stub for PredictPrice (regression model)                 |
( y! i0 ~% [* s8 O& @, J//+------------------------------------------------------------------+
% i! @( q* {  `0 m" bvirtual double PredictPrice(void)
+ Q8 w! w) ~# ?$ Q{$ F3 J. P' Z3 p  y4 q) k
return(DBL_MAX);9 }0 q9 U- Z* F: @* v. v9 g0 ]7 K
}
3 p1 J4 \' V: H2 D& e: j$ ~//+------------------------------------------------------------------+
0 X# e# a  R* }//| Predict class (regression -> classification)                     |  p( l2 D+ ~8 M( K6 X
//+------------------------------------------------------------------+. ?/ T  }# X$ S6 U( N0 d
virtual int PredictClass(void)
' z/ ]6 I6 R7 C( n* N3 \. x{1 {4 x: ?; s& {6 r
double predicted_price=PredictPrice();
; E5 _( A  L% T2 D) v& {" |if(predicted_price==DBL_MAX)8 Q/ A- x- g+ d6 z0 x- Q% a
return(-1);
! P' `  D. e& G: nint    predicted_class=-1;+ j: ~* q/ V6 M- }
double last_close=iClose(m_symbol,m_period,1);$ V5 p9 |1 x: o0 I8 p2 r
//--- classify predicted price movement, x4 S6 O% q5 l8 U1 X
double delta=last_close-predicted_price;: K1 E5 \6 n' E* D/ v, K
if(fabs(delta)<=m_class_delta)
4 F* z9 v6 ]7 w6 U) U! O3 J+ ppredicted_class=PRICE_SAME;
# `/ f$ V) D$ O: A; K( @: c8 B( aelse: `/ u3 ?. E" _( w# w' k0 X6 I* Y
private:
- C0 c& r$ e5 D8 h* {int               m_sample_size;. w* T7 t5 A: _% E* i
//+------------------------------------------------------------------+
" J8 ^' _0 y' evirtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)$ r- J( r7 y- K* m( L( @- r7 C  s0 [
{
4 a8 N# h5 m% @* J//--- check symbol, period, create model. c( O+ s: C9 P1 d+ g
if(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class))  V/ U8 n# R2 v/ N  `1 [
{
4 M! M$ }1 [5 r5 w! KPrint("model_eurusd_D1_10_class : initialization error");
# H  j# Y" H" C3 X# |- j. d3 ~return(false);
# s/ u/ h% A- A& l}/ q. s7 H& a" I# C
//--- since not all sizes defined in the input tensor we must set them explicitly9 [8 T' n9 m$ `9 [0 _
//--- first index - batch size, second index - series size, third index - number of series (OHLC)( O& x, _2 Z9 F" m* H$ Q
const long input_shape[] = {1,m_sample_size,4};5 A% T& k' U" V3 k
if(!OnnxSetInputShape(m_handle,0,input_shape))  x5 L: g$ B! x* l$ ?* i; `
{
: x6 r9 u0 Z: Y% G5 i8 g, jPrint("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());
8 Q8 C/ x: S2 G9 Freturn(false);
* k0 C3 h2 l2 S& {7 Y* F}
- q0 W' b: x" J) K- ~: Y: m! w0 z//--- since not all sizes defined in the output tensor we must set them explicitly
5 E, n  O3 Y4 O//--- first index - batch size, must match the batch size of the input tensor
6 N% x  J6 A" y$ g4 H) O//--- second index - number of classes (up, same or down)) n0 v9 D: n  j5 C5 e! S
const long output_shape[] = {1,3};
! l+ x, D8 b: F- j% `if(!OnnxSetOutputShape(m_handle,0,output_shape))+ C6 q5 |! }, u6 _+ x; B8 _
{
4 {3 }9 X6 C1 b" |8 yPrint("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());3 O5 i$ w: U6 [. Y: U0 j; v* c% I* _
return(false);5 Y' E7 |/ ]4 k2 W) N
}- y" i2 I7 |" G  ?0 O' S- ?
//--- ok
7 X# E$ F0 w! u, s+ |2 }& Nreturn(true);
' R' x" D& M/ L6 h( L1 N1 y}( I/ y$ u' L1 L5 O3 N
//+------------------------------------------------------------------+4 F' h$ N% j7 ^$ I- |
//| Predict class                                                    |
1 @7 x. ]% M9 L//+------------------------------------------------------------------+
: O) A1 Z/ J( xvirtual int PredictClass(void)8 f  D* L! l# m) W
{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-4 10:29 , Processed in 0.541574 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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