私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?
' v: D/ s' E6 _% h在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。
* h3 C0 z9 W2 O: b$ b7 o1 {我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进
* ]8 [/ ~" z' D3 t/ m7 O6 ^//|                                             https://www.mql5.com |
% C. G" |/ u9 I9 G//+------------------------------------------------------------------+
- P  ~* ]6 L! Q% P1 D  D//--- price movement prediction" [. l; G- @% b( D' e
#define PRICE_UP   0
8 Y/ u5 z$ r1 w. u# C#define PRICE_SAME 1
) q& p9 q3 U* q' u#define PRICE_DOWN 2
& y: N- i/ G& D9 ]: G//+------------------------------------------------------------------+
6 T6 M! U1 s- z! I//| Base class for models based on trained symbol and period         |6 P$ n, s2 U- X$ y4 ?- I0 i
//+------------------------------------------------------------------++ c: y1 q9 V0 [" k$ U7 p( _
class CModelSymbolPeriod
& o, s$ L! q, i+ W{7 y# @5 a1 b' K0 G
protected:
* J- z, R0 S$ I5 blong              m_handle;           // created model session handle
6 w& F% f/ g5 e* q, o; {( ]& ~& Astring            m_symbol;           // symbol of trained data
3 w  a) b4 N9 O3 b$ eENUM_TIMEFRAMES   m_period;           // timeframe of trained data
1 F0 T/ e) D; z$ j$ D- `3 |datetime          m_next_bar;         // time of next bar (we work at bar begin only)( T) `9 z5 I9 ]8 ?; s6 Y) I  @# X0 Z
double            m_class_delta;      // delta to recognize "price the same" in regression models$ t" K: X' A5 g: [9 U/ X  X- p- K
public:( H6 j  X+ C$ W8 [) \  {
//+------------------------------------------------------------------+' |1 C+ i4 O8 w! y9 j+ o
//| Constructor                                                      |
) H: F# \% a$ c- P//+------------------------------------------------------------------+
5 Q3 ?1 D9 q, _# C; T  n. o" aCModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)
. V: K& J: G# K9 ^! I{
( V4 V9 {1 p7 ~m_handle=INVALID_HANDLE;
( @# j0 `1 q! B. Em_symbol=symbol;
: ?% C* _3 A9 vm_period=period;
$ s" _5 {5 k  V- m; Zm_next_bar=0;) V+ ]1 b$ l- X% n7 i1 h4 ~  [
m_class_delta=class_delta;
0 K' v7 {6 N, _/ {; B/ O& L  \) g% p}4 c( [  Y: V$ e: c+ {
//+------------------------------------------------------------------+
6 p$ @3 e6 J% N. b) ?6 p3 |3 l//| Destructor                                                       |
' o1 a/ o5 Q3 T' b8 m, {0 C) ?//| Check for initialization, create model                           |
3 ]; Z. D2 H3 o  y7 Q//+------------------------------------------------------------------+- y  [8 k* I8 f3 D' M+ p& ^4 o- f6 D. |
bool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])- f  y3 X" E6 S: Z' y
{
% d" M& F, ^( Q  h5 {//--- check symbol, period
9 l3 x  @, A: L( o1 K1 U, ^9 vif(symbol!=m_symbol || period!=m_period)
9 n3 i% ]' \( G' e) F{: C0 A5 z" q9 A! R$ _
PrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));. e; r% Z7 G  h& I2 U7 \, _
return(false);1 k& J% u/ n3 p9 f* Q
}
% G2 G2 P" q. f/ O1 N$ Y+ ?/ f//--- create a model from static buffer: `/ Q- x" V4 U3 B+ F
m_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);
9 d" x7 ^& \* r, o9 J1 ]if(m_handle==INVALID_HANDLE): O' W- p, |7 m0 ~& `% v# Y. g
{1 ]5 k3 w! Q  K6 n6 A
Print("OnnxCreateFromBuffer error ",GetLastError());
* z+ f! s2 u6 w' N3 y* ]. Zreturn(false);
# i& o) g- Q/ C8 _& q}4 i- X8 B" U. j; i
//--- ok. K8 ~$ b4 t1 I
return(true);
6 X6 E% i: T( g/ H7 u' O}" W5 |1 }7 f0 V% E1 b% i1 Z" K
//+------------------------------------------------------------------+4 e0 @* P* {! d
m_next_bar=TimeCurrent();
$ o* o: O- f  G; [m_next_bar-=m_next_bar%PeriodSeconds(m_period);
) A& y8 r% K% p3 w* Z$ F1 L: Lm_next_bar+=PeriodSeconds(m_period);- f% e# ?& q; |! V0 p5 e; |( e
//--- work on new day bar' D; N8 \0 d8 ]. F6 I
return(true);& ]0 C0 [' I8 M# s# H, D- v, m9 y
}
& E5 O# X' d& P( m6 p//+------------------------------------------------------------------+/ D1 U2 D' T" r4 h
//| virtual stub for PredictPrice (regression model)                 |  j' [  z9 m7 g
//+------------------------------------------------------------------+3 Y3 X/ U+ j  j
virtual double PredictPrice(void)
' E: {5 Z; }/ E9 Z8 O& A% d8 E( w{$ C1 e! A0 w* V# e* y
return(DBL_MAX);
, C5 L4 E) [5 J) \  p, g}
0 b& V3 {- F3 }$ a//+------------------------------------------------------------------+8 i/ {9 N9 `  a; x0 Z
//| Predict class (regression -> classification)                     |3 e. d" E( i* i& d) O
//+------------------------------------------------------------------+
3 h5 ?0 a8 }4 {% U2 ]' pvirtual int PredictClass(void)
, v# h7 [6 I8 g& `. f# W7 m8 m* X{
; R/ J* g- D2 S( P: q' Zdouble predicted_price=PredictPrice();/ z8 H1 A1 V8 f1 m
if(predicted_price==DBL_MAX)
( t! i/ F7 V1 ]( G" }return(-1);
% d' A) y& N- ^6 uint    predicted_class=-1;
: t9 b, [, g, U& X- gdouble last_close=iClose(m_symbol,m_period,1);
3 _$ E, f7 d' i5 B5 P//--- classify predicted price movement9 h1 q- D9 y1 e! n) M8 W
double delta=last_close-predicted_price;
! }8 r# ^% A. I' pif(fabs(delta)<=m_class_delta)
1 {( v1 Z; L; B7 a; a4 V% Zpredicted_class=PRICE_SAME;- Y+ ?6 M, f- o0 T) q2 Y
else: g. ]5 x' V) K
private:
9 E1 i, \  f9 Nint               m_sample_size;, |+ X0 D; X& T: h! k5 w0 x
//+------------------------------------------------------------------+# e+ l) @8 O8 N7 E
virtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)# w5 a9 k( C8 q, {: [) V
{) h! A! x) O2 @! X. d
//--- check symbol, period, create model1 O4 ?$ `- a# ]  L# d
if(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class))
. O* Z, T; T! W5 k{, @, u& Z/ x4 A4 {
Print("model_eurusd_D1_10_class : initialization error");
" J7 W% C, L" W; a: c' S/ K) Areturn(false);
/ Z6 \2 k7 e6 R) f# w8 _}
# e2 p7 s; r* j//--- since not all sizes defined in the input tensor we must set them explicitly
1 `) [( _- @6 C# g//--- first index - batch size, second index - series size, third index - number of series (OHLC)# p% t9 P3 V) n! g
const long input_shape[] = {1,m_sample_size,4};" V6 t# r* u/ w. ^$ Z
if(!OnnxSetInputShape(m_handle,0,input_shape))% p" }" K! P$ c$ \" U
{4 r! q' A( \  t! v6 @0 c
Print("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());5 y" F/ _3 r# X7 c1 M
return(false);- c" C9 ~9 N# E# k6 d# y& u
}
$ {9 B! E# X& O7 }//--- since not all sizes defined in the output tensor we must set them explicitly3 V: _7 K9 M4 c2 b& C8 g
//--- first index - batch size, must match the batch size of the input tensor" v( j! c2 }) _2 o- V
//--- second index - number of classes (up, same or down)
0 c1 f( O* }' d7 Y) ^const long output_shape[] = {1,3};6 A: f6 T6 n/ _6 b0 ^9 d( x
if(!OnnxSetOutputShape(m_handle,0,output_shape))  Z8 @4 N. h9 G$ R# _) h- D
{& }# z* A& d/ h
Print("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());8 B* N2 D* |6 B6 Y
return(false);
1 h& d2 c2 u" t+ }}
; }+ |" J* x6 {1 x//--- ok
& ~& [4 T6 E; c# ?return(true);3 w) s* B* u( X  _' p; |8 \) y
}
# i- L! I% f1 ]. v0 H) q2 u( ~//+------------------------------------------------------------------+
& e. `* ?7 i- B- v//| Predict class                                                    |3 X/ o( l: E. }5 G$ G0 J9 P' ^
//+------------------------------------------------------------------+
: K" C: h: W7 C7 u% ?) U& Wvirtual int PredictClass(void)
; G  A" x3 H& ]+ ^! ?7 q{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-2 10:42 , Processed in 3.722529 second(s), 32 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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