私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?. X% T/ S6 n: ]
在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。
& s& c# y# e# P0 o2 \. k+ F我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进/ E/ y. i/ p7 x: m
//|                                             https://www.mql5.com |
- M1 L- m( S3 E# s  I/ ]//+------------------------------------------------------------------+
4 t! m9 x( O! w5 H. n2 q//--- price movement prediction5 k& Z2 S6 j7 x' S! Y
#define PRICE_UP   06 A8 k6 Z' l$ X% i( H
#define PRICE_SAME 1
" i& Y2 m3 g4 Y#define PRICE_DOWN 2
$ w1 e$ u7 s+ p. B. S6 o//+------------------------------------------------------------------++ R, w1 ~8 `0 m1 L3 e
//| Base class for models based on trained symbol and period         |, x6 W+ J  C3 [
//+------------------------------------------------------------------+' u; G! F0 B. }  o/ b( b! `0 q
class CModelSymbolPeriod. |/ Y" P5 d0 F" h
{* V. s  b$ G% J4 E2 x
protected:; f5 g- ^* p9 j3 H) v8 @5 ~" O
long              m_handle;           // created model session handle
9 j& F6 r. y% s" bstring            m_symbol;           // symbol of trained data; J" G# j) |: X
ENUM_TIMEFRAMES   m_period;           // timeframe of trained data
) Q: Q" `7 l2 R, ^- ?datetime          m_next_bar;         // time of next bar (we work at bar begin only)/ O( t% k0 ?3 t8 ]+ C
double            m_class_delta;      // delta to recognize "price the same" in regression models# f$ t7 B% k1 w6 e, {' i9 K" n4 {
public:7 ^. Q3 G6 E2 w4 F* h" \$ N
//+------------------------------------------------------------------+0 S+ A! h7 }/ }/ q
//| Constructor                                                      |0 h6 y, g. }8 f6 O
//+------------------------------------------------------------------+
/ t$ ?1 j5 b3 L; S5 w. \, S  DCModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)1 W* a- Q- Z1 T4 M- f
{2 p, Y! o% a* _3 v: B
m_handle=INVALID_HANDLE;
- U2 Z) s0 F4 C( N) q3 t+ n% V1 d$ ^m_symbol=symbol;
. `$ B8 u7 C5 T9 F9 mm_period=period;* |, {& E- h% D6 i4 H! I
m_next_bar=0;
& T0 ^( m9 Q- [2 @) Vm_class_delta=class_delta;& M0 p# N- g% L( m9 \% J1 H' |  q
}; P) E$ v! Z: W1 e
//+------------------------------------------------------------------+3 C  w& u$ v) C9 }9 o& d9 U
//| Destructor                                                       |
5 y7 c: o( b* G7 d. L* U  N$ k//| Check for initialization, create model                           |
+ v4 ?# o* {# _0 L5 r" m- E//+------------------------------------------------------------------+9 H& c+ n6 O# M2 \0 J7 ]6 v& i
bool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])# D( {* h/ p+ y: s7 o( B$ ~
{
6 ]0 N* X1 P: s3 I# u//--- check symbol, period( }+ k" @- j) y" @' n% x9 a0 x) b
if(symbol!=m_symbol || period!=m_period)4 ~5 e& R* l8 D& Q
{
; A: G3 V" N- v5 k6 H8 g  l# KPrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));4 K/ h; l$ @% }8 w  P! a! u
return(false);9 E7 ?  i: b" c( `7 }' z  R
}
( z5 ]& l5 V5 n//--- create a model from static buffer0 Y1 A% t$ G: h5 y/ g! q
m_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);
+ ?+ H& j4 ^1 s; J+ Y  V1 Jif(m_handle==INVALID_HANDLE)
* I# l8 Y+ i* W{, d" c9 Z/ I5 @8 _! a+ l$ n
Print("OnnxCreateFromBuffer error ",GetLastError());
, N" I! Q- S* g. W4 ~% O# F9 Areturn(false);
! w2 V. H2 m+ M& U! `+ x}8 z, _3 \3 H. o; f9 W$ M
//--- ok! H+ X% a: y" C
return(true);
' Z* N9 [5 F' u* G* j/ p( n}
* a- t& l2 I7 F. p//+------------------------------------------------------------------+
& y7 c9 y0 j7 mm_next_bar=TimeCurrent();
9 \9 U' u9 `( k/ R+ em_next_bar-=m_next_bar%PeriodSeconds(m_period);1 \8 F1 F" x5 |
m_next_bar+=PeriodSeconds(m_period);& j/ i8 _. T1 v$ y$ b
//--- work on new day bar
0 H0 C  L# `* b( g8 Wreturn(true);( v, n+ A' W6 |
}$ \4 i. r* |4 N  Z/ u
//+------------------------------------------------------------------+0 u6 h+ ~  H3 J
//| virtual stub for PredictPrice (regression model)                 |4 i% J2 V4 A0 _( A) O$ k
//+------------------------------------------------------------------+
9 x! Q1 P# o" P! S. C  Fvirtual double PredictPrice(void)+ _) a, x! U1 `  K
{
! q- o  Y# L: ]+ r% y1 x% @return(DBL_MAX);; R2 g$ [. X. I. C
}8 J& j  a5 ?. ?! v5 ?6 p
//+------------------------------------------------------------------+
" N. R8 q7 h) @7 ^5 d9 d//| Predict class (regression -> classification)                     |9 x3 X5 P: M" L/ d- R: i; F
//+------------------------------------------------------------------+
- E7 s) {/ i8 O" P/ w. M. c. Kvirtual int PredictClass(void)
6 j) T. w9 S6 d{
* G( N/ j2 O) }) e+ g9 i9 ^double predicted_price=PredictPrice();% T4 }. J0 a' k( ]" ~$ A* r
if(predicted_price==DBL_MAX)2 w  ~) @. k7 V# g3 D9 \% \6 l
return(-1);
8 V" M1 }2 N8 r: f0 ^# dint    predicted_class=-1;
0 b+ `! r* R$ Q8 L$ P& Gdouble last_close=iClose(m_symbol,m_period,1);5 ?( j4 x6 ?( u. D- `% g# y
//--- classify predicted price movement
, h0 A; k6 A7 ?) D% T4 adouble delta=last_close-predicted_price;, \; [3 R* [( V: y
if(fabs(delta)<=m_class_delta)8 ^3 A% b- X0 Y1 n) ?
predicted_class=PRICE_SAME;) Q/ {+ q7 {% ~3 I
else  f6 i7 f, \2 K) k' y0 l8 J
private:
$ `3 w! y* E' c6 i7 x2 yint               m_sample_size;, y% k+ |5 @- \
//+------------------------------------------------------------------+
. |/ g  R& \7 u9 jvirtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)% e5 Z9 z% a  S( |( F7 b  }
{0 C3 O' t* G7 `, j( t8 p  V- y# j
//--- check symbol, period, create model
$ A6 Y$ U( H8 {4 ?4 R: S6 q9 g" @if(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class))
4 t5 w: G% m& A. b{
& K7 I2 c! U8 V! \. N" @) k4 tPrint("model_eurusd_D1_10_class : initialization error");
' Z2 p9 T; Z% _4 w* ^. preturn(false);) n1 i) s8 }  `3 i! i6 |
}& r& n+ e5 c% f! a/ Y& f
//--- since not all sizes defined in the input tensor we must set them explicitly8 t1 L$ @6 {3 [3 l% g' Y4 B
//--- first index - batch size, second index - series size, third index - number of series (OHLC)
" v* j6 H  M& Mconst long input_shape[] = {1,m_sample_size,4};
& D  H: Y2 Z/ rif(!OnnxSetInputShape(m_handle,0,input_shape))" o5 h7 i# R( z8 R
{
1 y& d' z4 s% o3 B0 f! APrint("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());" u4 ]* O0 {- g  S  r
return(false);1 |' @9 ?& k. m) a* Y6 e9 h! L
}
  d4 e! o/ ^' W; T8 }& U) m& T//--- since not all sizes defined in the output tensor we must set them explicitly/ M/ ^# l. ?# ~8 O
//--- first index - batch size, must match the batch size of the input tensor; ]. z6 D/ P- Y7 p9 a1 D) e) y
//--- second index - number of classes (up, same or down)
& ]8 K3 T7 J' `1 s/ mconst long output_shape[] = {1,3};
1 t+ d, B4 C% A5 A! Hif(!OnnxSetOutputShape(m_handle,0,output_shape))& A0 h1 k0 I) Y6 }/ D( N0 t' o
{* o7 X* Q. \+ R' D( F
Print("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());2 ~' \% ^9 w5 k& ~
return(false);
# k, K( C. M& `6 u& I}
3 v. j1 ~7 ]# K//--- ok- u  j( t5 h. r4 j5 h( r7 [/ E; b
return(true);5 d; e- s& ^- }# s! q  Q" {: W
}0 w, _0 R0 p9 b3 t/ b5 W; m
//+------------------------------------------------------------------+( e5 r/ G3 ~, D/ o) Q! {
//| Predict class                                                    |1 M+ D) y" u* F2 ^6 A( ?: h; }3 L7 K
//+------------------------------------------------------------------+/ o- q, m* g/ ?* x( \
virtual int PredictClass(void)
1 `- K- f+ c5 o# N7 S8 R' L7 E/ z{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-19 13:58 , Processed in 2.693374 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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