私募网

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?
% f, P' n2 k0 l  r: o& {在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。
% a! L' v5 I% G2 W我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进
: n4 E6 q- v" {) Z7 n//|                                             https://www.mql5.com |
# \, e& t7 t/ [//+------------------------------------------------------------------+
; a: f4 m9 g: A  e//--- price movement prediction0 h- u- p, e, M- ]: T. \% }
#define PRICE_UP   0
: \& P1 D/ A5 k1 w, A#define PRICE_SAME 1) I6 i, V4 i/ C6 B) T% l8 Q
#define PRICE_DOWN 2
" R/ {: B" c& }//+------------------------------------------------------------------+
& }3 n  a% L# k6 c8 a//| Base class for models based on trained symbol and period         |
+ \5 P" l% n0 |8 T7 M0 Y//+------------------------------------------------------------------+( J9 x$ Q: j, z8 M
class CModelSymbolPeriod
' O, k- u) l/ g/ [" u: w' S; R7 @{
& \# J) H, W( g% k7 d4 m5 r0 d$ xprotected:
* v' A; }7 i4 P* p" h5 Z/ n1 wlong              m_handle;           // created model session handle1 ?: P0 R- f8 b- q1 {. ~& Y
string            m_symbol;           // symbol of trained data
$ E% J3 W8 L6 _1 h+ |ENUM_TIMEFRAMES   m_period;           // timeframe of trained data- ?% S) F( l4 E, |1 F' S# y3 X: F
datetime          m_next_bar;         // time of next bar (we work at bar begin only)
" v8 l$ |, U; c* _1 ]6 j4 gdouble            m_class_delta;      // delta to recognize "price the same" in regression models
  \' R; Y7 N6 H+ npublic:
: S; Z5 W% R1 I$ y& n//+------------------------------------------------------------------+
+ }6 K# ]0 r( M0 }: r, Q4 M- c//| Constructor                                                      |5 D2 P  D, S& U# b
//+------------------------------------------------------------------+9 }( P6 \% f5 b7 s
CModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)  F  k& a* N5 E9 C& O4 Y
{, P& k5 B! l4 N) W5 _$ J
m_handle=INVALID_HANDLE;
8 J; c) C+ g2 h: d7 n1 n6 Zm_symbol=symbol;& t( d" t+ ~6 o5 [9 |; V6 C. d
m_period=period;- s6 o+ A+ o/ B! m3 d5 W' z
m_next_bar=0;* b. A) Q0 V0 T+ l7 a* |
m_class_delta=class_delta;3 Q( p/ r7 q6 k$ @  a' j
}+ j% C' t& q% [9 J: a$ b, S0 z0 F
//+------------------------------------------------------------------+
$ e, g1 o& t9 J  W! d9 D: H//| Destructor                                                       |
0 g2 |9 j$ r. M0 Q  |5 V//| Check for initialization, create model                           |( p4 ~" f. }( ]1 L
//+------------------------------------------------------------------+9 R1 K$ S$ `6 ?; Q
bool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[]), ~; r9 m' {! M0 {( ~4 f
{- Q0 u* \  j( E" g* n$ s
//--- check symbol, period
0 i/ v  G0 R* V1 @if(symbol!=m_symbol || period!=m_period)
, ^- h# G) K: H, W9 u6 k{  |4 E/ f& v! I( d5 B2 E
PrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));
, ]7 K0 w9 ]6 Z* L- p, u# ^, a$ Nreturn(false);  n: M  y! W+ a3 x4 _7 n+ B- x
}1 J; }; Y' V: \9 E9 B: u/ I
//--- create a model from static buffer" K9 G2 H" W) ^! _8 m. F9 P7 F
m_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);3 }1 u: i0 O6 H0 d9 F) y( J
if(m_handle==INVALID_HANDLE)
+ T. T- @1 f6 R+ _* ]' a{8 B1 H6 V# j, a* F) i3 K
Print("OnnxCreateFromBuffer error ",GetLastError());
  |( a4 U$ v/ z3 ereturn(false);
* L% R/ U& z; P}' d3 Z" f' r% n  f8 k
//--- ok
) w1 f' D9 v2 O- }return(true);
2 N2 H8 V% m' @# y% @' E1 j4 k% ~}
3 F5 d% Y4 w) P" `$ K( |//+------------------------------------------------------------------+
4 E9 {3 i" R' n' f' {1 Vm_next_bar=TimeCurrent();& M% t& V2 H1 P- m# i3 E
m_next_bar-=m_next_bar%PeriodSeconds(m_period);! h1 a- m9 R& u- q0 b& m7 Q
m_next_bar+=PeriodSeconds(m_period);  X. ]# J: Q' A# r; C7 k
//--- work on new day bar
# m9 a1 }5 K+ ]2 `9 ~3 ~' [return(true);9 ?( H+ N/ ~; z& z: k" A
}
2 C! v# d; I- `+ Z8 X$ Q, n- }//+------------------------------------------------------------------+
% B/ y4 K6 a5 d- j: s//| virtual stub for PredictPrice (regression model)                 |
/ G6 ^0 P" ^3 V- Z//+------------------------------------------------------------------+
/ h6 @$ X" Z2 S( t' G# kvirtual double PredictPrice(void)$ A1 ]/ @9 W/ X, u8 `6 V& ?2 M
{, j8 H3 |- r6 I& U# m1 \
return(DBL_MAX);
4 j1 W" B2 F! I, q6 \: I  e$ j}7 f+ R8 V  B0 t/ l
//+------------------------------------------------------------------+
) ~8 a/ |0 F$ q& g. A, \5 T$ c* Y//| Predict class (regression -> classification)                     |: E/ L. k3 Z8 v( _% [' Z# D5 o% Y
//+------------------------------------------------------------------+
& ]% D8 F! ]# cvirtual int PredictClass(void)
: o+ v. y; j* n8 k8 @! P: N. n+ f{
9 X! Z4 l2 B2 I7 ~  ydouble predicted_price=PredictPrice();
. B+ Q3 {% \6 W/ \6 wif(predicted_price==DBL_MAX)
; x3 V- W9 @4 h  s3 G% @return(-1);4 R+ a) K8 R, K- x# _; D" o% W+ s
int    predicted_class=-1;8 a: J5 |9 {6 o& K+ G
double last_close=iClose(m_symbol,m_period,1);
7 V* M& f0 r5 v4 z. s1 n+ ~5 r  X//--- classify predicted price movement
6 a; Y4 f% q4 z9 t; Adouble delta=last_close-predicted_price;
  s  d6 c4 a& r" E& c0 e0 Bif(fabs(delta)<=m_class_delta)
# ]% D+ k, J* W- Z2 k, V9 l) apredicted_class=PRICE_SAME;
7 l3 Q& u$ |  k0 w- xelse% ?0 j/ D1 `1 u  u
private:; v3 P- x  z/ W5 R# O, }  k5 K
int               m_sample_size;1 P6 Q& c8 Q$ s7 |3 q% A# |
//+------------------------------------------------------------------+
4 X5 c! j4 ~* h7 {) ]  ^virtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)
* s: ~4 m- s9 ^% t' n{5 X7 n+ j( h( y* a
//--- check symbol, period, create model
( H2 V+ _8 Q" @  e& D0 l8 h" A8 fif(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class))) ?4 c, E" }: `/ G- k
{7 }0 c. Z, {' _6 }) K
Print("model_eurusd_D1_10_class : initialization error");
- ~/ V& a) O) [0 [4 Greturn(false);: O5 G" W! V+ c3 Y, Q
}- V# j! m2 j4 U% d4 w# f% c5 d, R
//--- since not all sizes defined in the input tensor we must set them explicitly
6 }% V& @+ q: {0 D) p, k$ i9 D//--- first index - batch size, second index - series size, third index - number of series (OHLC)" T, p7 a( q) z) Z3 m3 @
const long input_shape[] = {1,m_sample_size,4};% U- X- H8 _& x: [8 P! m9 j
if(!OnnxSetInputShape(m_handle,0,input_shape))
* _5 n, o! }0 T{
% h  T3 o! ^7 j+ ~Print("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());/ }2 U& B% a8 q, f6 W2 _! h
return(false);
% V$ n& T9 D) w+ q' w# r3 ~}
- N) m  Y7 P9 g: S% C) v* k- T7 F//--- since not all sizes defined in the output tensor we must set them explicitly
. h; l0 C, n4 x5 Y3 G6 Z3 j//--- first index - batch size, must match the batch size of the input tensor, f2 f' H: q6 `% w9 R
//--- second index - number of classes (up, same or down)5 ^4 e7 h9 y# a$ n
const long output_shape[] = {1,3};
. A) h  i' w' J; x  U) Uif(!OnnxSetOutputShape(m_handle,0,output_shape))1 T6 i0 B! R- O- Z- v5 o( i2 u8 t
{
# Z6 o3 Q! f7 z7 GPrint("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());
+ g! p# p, }' ]. w' U! U0 Ureturn(false);
( S8 e# C* @- {2 i: }( C+ j}
1 S" ?, s6 h8 ^( h/ @' g! k. e//--- ok
5 P( E4 `& ?. S  U& ~0 s! f/ Rreturn(true);
/ F- s% x& f1 q4 A- N. M}
1 }4 {0 m5 w5 ~9 \! c# c# z//+------------------------------------------------------------------+
1 g% Y# ^6 N' v; `3 B' E- N  Z//| Predict class                                                    |, M. e- o/ q8 A/ w+ [% l% O
//+------------------------------------------------------------------+, K, X8 A( }0 b5 z
virtual int PredictClass(void)7 v7 p4 s" c! x
{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-30 03:52 , Processed in 0.417592 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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