私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?
2 D* [; M% R% h1 C+ y在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。- B" x( O; L) h: _" i7 n) _
我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进; J& @6 g/ `. G6 o: G! x( W
//|                                             https://www.mql5.com |# [; v* B/ H' A" t0 |9 y% }
//+------------------------------------------------------------------+
% U; ?1 W6 K0 W, {  L& P//--- price movement prediction
* d2 {+ T8 }  d" o9 G: V#define PRICE_UP   01 Q$ e+ i7 G, A  @9 \
#define PRICE_SAME 1
; f  I9 F- v0 o8 Q$ c, e#define PRICE_DOWN 2
0 S- N. F" X/ d- ], g* `//+------------------------------------------------------------------+9 m' O. ]; b: B8 T+ Y
//| Base class for models based on trained symbol and period         |
' X6 f$ y8 ~) V8 E( {! [# C/ T/ Q//+------------------------------------------------------------------+$ a( f5 l/ ^3 k* B/ F: q; z% C
class CModelSymbolPeriod
' a: C( @, t' P{2 I. X$ u/ S0 Z5 r7 _
protected:
3 X% Z3 c$ ~: D! v- F" jlong              m_handle;           // created model session handle4 _) i& U7 O3 k8 h! W5 l
string            m_symbol;           // symbol of trained data- h) N- I2 ]4 W4 x" x% d- U
ENUM_TIMEFRAMES   m_period;           // timeframe of trained data- X3 w8 ?! a1 V! s3 j- r6 p* M
datetime          m_next_bar;         // time of next bar (we work at bar begin only)
4 u9 S. z/ G5 h1 l: I  Sdouble            m_class_delta;      // delta to recognize "price the same" in regression models
& @/ e; {0 r1 B& Q, ipublic:  M# `: b* w- E9 O% b* h4 p
//+------------------------------------------------------------------+/ e, D) v" E) _/ T; [1 d
//| Constructor                                                      |
8 ]3 W8 d0 T" ]//+------------------------------------------------------------------+( n5 ?6 s+ T' U% Z) l( ~
CModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)
9 ~- A' T# a5 H: w{' v7 Y+ N$ \7 b) e
m_handle=INVALID_HANDLE;
: ]9 s& ?- c. s9 C# O7 o/ Jm_symbol=symbol;
5 @" {# `1 L; ^5 {- K: Gm_period=period;
/ @$ J* g0 @2 L. Y, |6 \2 p" sm_next_bar=0;9 z3 \# X+ n# |; o) N3 v% X; u
m_class_delta=class_delta;; V, k- u! E( D/ M! x  X0 i+ v2 \2 V) ^
}
+ R0 [/ b. |8 {//+------------------------------------------------------------------+
* M7 i( g2 X2 l# [; R' [//| Destructor                                                       |
: j* [' Z- ?6 _% D//| Check for initialization, create model                           |
) B' m, Q$ z6 ~) B2 ~; g//+------------------------------------------------------------------+9 z4 O8 D4 L) m( L% ~! c1 e
bool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])
) G" |" `# J; _1 `. r{
, E$ Q- p  y) i1 f: o//--- check symbol, period# v  y, ^# o( J+ G1 W* n+ b/ E
if(symbol!=m_symbol || period!=m_period)9 z; Z; e3 X/ T3 s# f, k, r
{
& `& `. u* F! g# x5 ^PrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));
5 _8 b3 _9 s0 o7 O* x7 ^return(false);
$ A: m6 z( R5 u8 J" r/ x}
$ Q% V6 u- k& ~: V- t4 t//--- create a model from static buffer
, K0 [  A- p# lm_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);: q) s; |9 W4 e& C5 h9 h
if(m_handle==INVALID_HANDLE)  v/ z7 P5 D  Q5 m' U
{
/ q0 }2 \' h5 W. l) L4 n* C3 FPrint("OnnxCreateFromBuffer error ",GetLastError());) `( K1 [5 p% o! c  a% O( A
return(false);
' ]( [  {& G7 _2 O3 |}
! S+ i) b6 s, x- j/ w+ n$ K4 y//--- ok
% B9 m$ `, z7 O/ R4 ^5 ~return(true);) p6 L8 K  E6 K* R
}+ @: A% @' k, G2 j; ^) q  A
//+------------------------------------------------------------------+
' ^" b9 r6 e1 t' k" Km_next_bar=TimeCurrent();6 e0 q" y; V6 I& j; S1 o) ^
m_next_bar-=m_next_bar%PeriodSeconds(m_period);2 s+ o  Q, \0 C0 F6 H% x
m_next_bar+=PeriodSeconds(m_period);
1 V, y1 ]+ Z& N! Z/ X6 a//--- work on new day bar2 L) J" `: p: \9 F1 r* t. x8 |" }5 r
return(true);0 m  P4 P% i; q  r/ @: J
}* [0 `+ ?5 {  o1 L% j/ [# S% P! V
//+------------------------------------------------------------------+
& ^- j( a# d* S3 N" J6 m//| virtual stub for PredictPrice (regression model)                 |
! T, E: g1 U3 d$ |8 L* L//+------------------------------------------------------------------+) [9 j3 V' g( _7 I" W/ v2 n: G( T" ^
virtual double PredictPrice(void)
( X7 W) j  ?5 J; \5 Y! V- F+ D  {{1 Q' [0 H* J- H$ |
return(DBL_MAX);
6 g. T) u6 U, X, S$ q. ?) [- j* r}  J6 z3 @0 ]8 e& o  X1 i( k
//+------------------------------------------------------------------+
: Q2 P( x- @* E, [//| Predict class (regression -> classification)                     |- i5 {8 e$ E1 I% r7 A% |
//+------------------------------------------------------------------+4 S& Q, m+ P8 M% l+ q& R5 ]  `0 @
virtual int PredictClass(void)1 p7 y. S" i) }# X* S0 r) ~% g
{4 N. \% d4 \" s$ ]6 ]
double predicted_price=PredictPrice();
" Z3 h0 U: u+ _1 Pif(predicted_price==DBL_MAX)
) n( Z* L" J; {/ ereturn(-1);
  T0 }7 r9 O. x( M. t  K( pint    predicted_class=-1;
) [  o& m" D% b1 Hdouble last_close=iClose(m_symbol,m_period,1);
7 ?8 e) @9 u% n: L4 T- f- \' j7 V/ a//--- classify predicted price movement' _: B' v9 k  @0 x6 P; c
double delta=last_close-predicted_price;, V% l8 K8 X  N; ^
if(fabs(delta)<=m_class_delta). L6 A6 r5 Q/ I" V: j- g; p
predicted_class=PRICE_SAME;+ h4 ]% @! A+ y
else# ^( W0 B9 E( u( p" n
private:
! b! e2 U4 e! \$ Q: G1 R% Jint               m_sample_size;8 U# B) g! l! L/ c; l1 T
//+------------------------------------------------------------------+4 N1 U* j% Z: v
virtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)
; I4 R) I  Y  G4 b{$ T6 i* t# |  I# f
//--- check symbol, period, create model
2 [, C0 R! I3 ], I" a. `0 V7 iif(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class))9 I) N; R1 P& E! ~) E
{
5 ^( X6 n9 q. Q9 r" T+ ?% I3 N8 R" G" yPrint("model_eurusd_D1_10_class : initialization error");. o( q% L& P8 ?/ ~# S* V
return(false);/ W4 q* C" j" X" W0 j
}' g4 r* R$ V+ v- Q
//--- since not all sizes defined in the input tensor we must set them explicitly
& D) [/ G! ?# N# n' k: X//--- first index - batch size, second index - series size, third index - number of series (OHLC)+ N, y, y/ Y- Z6 {
const long input_shape[] = {1,m_sample_size,4};
0 d1 f5 w% x2 Sif(!OnnxSetInputShape(m_handle,0,input_shape))2 [" O- z1 N) Z2 B
{
8 F" _  V, y+ [6 L; TPrint("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());6 S, Z+ J& G* C0 }/ a1 J
return(false);+ f9 k& ~: o" L/ G( N
}
6 V: c3 b/ t" R- S0 D" e7 [4 J//--- since not all sizes defined in the output tensor we must set them explicitly! I% H( d* m* D% C2 J* R0 y
//--- first index - batch size, must match the batch size of the input tensor- B* f: L& Z8 t" U
//--- second index - number of classes (up, same or down)& T2 x- L$ o4 u% n- C6 @4 P' ?2 ]
const long output_shape[] = {1,3};4 b0 m9 ]# ?0 u! l: @5 x6 N, i$ j
if(!OnnxSetOutputShape(m_handle,0,output_shape))
0 O3 {5 l! Q! |- ^4 A+ o{0 }" x% P% h7 a1 E. n9 @) p
Print("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());
. `3 f5 b/ ^1 O; o0 U( ]. Greturn(false);8 z% S) `' H  o4 m5 r4 M  [& R8 s$ \
}5 c4 t# r9 B4 {$ [2 H& F3 p
//--- ok7 ~$ ]# j  n( e" m' J
return(true);+ U! W! g9 \( S* g3 Q
}
* y1 I- D+ U% x' c/ H* k$ X7 v//+------------------------------------------------------------------+
# V- {! y' U* Z" r' Q//| Predict class                                                    |8 f& x: {1 |5 @
//+------------------------------------------------------------------+
4 n) R1 [9 h" Jvirtual int PredictClass(void)
* Z8 H+ L. _! O/ z9 r{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-4 14:39 , Processed in 0.426193 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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