私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?( J2 V) }1 |9 q$ n4 e
在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。
0 g2 e( f. d. n  i1 g) m5 J9 q我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进9 G& A2 d% F+ _. q+ {% B
//|                                             https://www.mql5.com |
' J3 j9 Q' \8 d3 F) C//+------------------------------------------------------------------+8 C- F$ r. D' a
//--- price movement prediction
* n+ P; @6 b' N/ X/ N#define PRICE_UP   0
6 u7 w. ~- U2 H; \#define PRICE_SAME 1, `$ h4 F" @1 P+ e8 q$ K8 i# e
#define PRICE_DOWN 2
: M( u4 l( Y+ H//+------------------------------------------------------------------+) U" F# m. {, }- Z5 e" h
//| Base class for models based on trained symbol and period         |+ p/ V9 k, n( Z7 g- Q0 G
//+------------------------------------------------------------------+
; n' W; k4 ]9 N# e/ Zclass CModelSymbolPeriod
+ m: ^1 w# F8 O. z9 H# C- x$ Q{
# t' i6 s. i: G# ?; A0 `/ mprotected:
4 y. ^9 j% q$ }: k5 Glong              m_handle;           // created model session handle
. x' e8 E+ T9 e- bstring            m_symbol;           // symbol of trained data
+ k' W" x( M% e0 I. F+ LENUM_TIMEFRAMES   m_period;           // timeframe of trained data
$ S/ [' U! ?8 i& M: j3 r7 Edatetime          m_next_bar;         // time of next bar (we work at bar begin only)
, i0 K  N4 l5 r/ x$ Gdouble            m_class_delta;      // delta to recognize "price the same" in regression models
, w7 P% A) ?! npublic:
; o4 c# ~7 Q4 V+ x( `9 c9 H* n//+------------------------------------------------------------------+/ ^, P5 q' Y4 ~
//| Constructor                                                      |
5 k# q6 t- O) {6 i% I//+------------------------------------------------------------------+
  T) Y$ C' j5 I0 D0 I2 d8 WCModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)
  Z: j: F8 ?  |  \{
' f7 @& h8 e/ G& f. Im_handle=INVALID_HANDLE;
& \9 |, ^1 H+ ?3 y7 Rm_symbol=symbol;7 X; I; t$ f8 n3 ]: e# A) M
m_period=period;
$ H! u! ~$ s" T) D$ Qm_next_bar=0;
, x9 A6 B% ?& f2 N9 u" @$ L/ p+ ~m_class_delta=class_delta;
- R2 a" `9 h0 n2 J0 h3 n+ }2 [}2 j8 q/ k1 R8 v( H/ ^3 }
//+------------------------------------------------------------------+" o7 k+ R0 E9 Q8 p. f7 z
//| Destructor                                                       |" S/ a# x: r) t8 _& L4 K3 w
//| Check for initialization, create model                           |
" A+ I. j6 I8 S5 W4 d//+------------------------------------------------------------------+, o% s3 i. f: P: S1 f
bool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])0 y0 b* r9 |/ T. G4 M7 L
{* I& E1 H# G3 y+ p
//--- check symbol, period: M- H, y# E  p9 p, E
if(symbol!=m_symbol || period!=m_period)
5 w2 Z& R2 m& D0 i6 p{
2 M" X- p/ A" n2 qPrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));# d$ d1 @9 c* @* ?: M7 T5 P
return(false);
$ e4 T6 ^. t2 m$ x; |. M8 d}
8 F; ~) k4 c/ {: V, Z//--- create a model from static buffer
  D8 V3 W  r4 `0 F2 k) vm_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);% z+ e: U; p4 W) q# O
if(m_handle==INVALID_HANDLE)3 B/ e  ^4 J) N( U
{
: v1 w/ y5 u7 D& G- Y. J* JPrint("OnnxCreateFromBuffer error ",GetLastError());8 F+ c3 r/ z3 F: y; A: Y. N
return(false);0 _, e% j8 l% s
}( B( c3 j3 ~/ [
//--- ok7 R* R# C- [. B! [+ J4 B3 L6 r
return(true);, [, a; v# o1 Q9 q! [# A7 W
}8 @$ D6 x6 J8 |" c: c  q: e
//+------------------------------------------------------------------+
, S6 c; B. d0 l0 R8 q+ ~m_next_bar=TimeCurrent();1 i8 ^" F3 N, n! ?) P5 Z$ f
m_next_bar-=m_next_bar%PeriodSeconds(m_period);
5 A; ~  p" q. n' o: dm_next_bar+=PeriodSeconds(m_period);9 j% _$ H1 W! t" f0 D8 {$ \
//--- work on new day bar
/ e6 T# ?6 I$ X5 V$ wreturn(true);- c9 Z: o; l$ T5 F8 C8 [
}
' V6 l# ^2 T) s; }//+------------------------------------------------------------------+& G4 ]3 b) x# }8 p2 b. C
//| virtual stub for PredictPrice (regression model)                 |6 s, U) \' m5 c3 ?, [. `
//+------------------------------------------------------------------+
% l! m2 V/ K5 K! D4 ^& p9 Tvirtual double PredictPrice(void)4 ]) x  ~- v' r3 o0 p$ Q( ~3 u7 ]
{+ B1 z1 p+ p* ?# h$ U
return(DBL_MAX);
5 M- b8 }* I- N& Z}" v0 x" n& F  o' l" @2 Q5 c
//+------------------------------------------------------------------+
6 {5 m$ p2 J# w! v% Q$ o: g) j//| Predict class (regression -> classification)                     |
& g* K" y& e) |( m" }9 R' `; n//+------------------------------------------------------------------+
8 I% c$ h  U1 R7 r: Xvirtual int PredictClass(void)5 U( y1 \: }4 Z/ W- b
{
8 c* P, t: I) i) k7 Sdouble predicted_price=PredictPrice();
: U* h( z8 o6 s5 G  rif(predicted_price==DBL_MAX)9 y- d' S& F- S( z% W* O  D# ~
return(-1);. L& U( e" L2 g4 `) u1 _8 B9 L6 `: }
int    predicted_class=-1;) o+ M' d9 [% t5 Z1 X1 h- w; C5 E
double last_close=iClose(m_symbol,m_period,1);
4 k$ J% d# k& d! R//--- classify predicted price movement8 k+ p; w/ ?$ K; `7 p% P1 v- Z
double delta=last_close-predicted_price;
, D9 f; f2 Z. X6 ]- I6 f+ J4 xif(fabs(delta)<=m_class_delta)
0 `4 l0 W2 v4 m: I" Ypredicted_class=PRICE_SAME;
/ R& J7 t% \3 D9 p0 ]( Y5 [else; T4 ^+ O9 D; |
private:# l7 U# X8 _* {
int               m_sample_size;6 u( \9 p1 j2 i. t$ O
//+------------------------------------------------------------------+
+ ~  E2 [" G6 z0 ~+ [, S) R* k4 Yvirtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)
  c- j6 ~% v7 L& K{
2 G2 `! O' A" P1 A3 T( Z: G+ ~//--- check symbol, period, create model' ]' X' r8 a: ?5 I; _6 p
if(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class))
, o: _) ~& f' p* ~{. N7 D* k' I4 q4 o
Print("model_eurusd_D1_10_class : initialization error");
' b% M2 h8 V6 M+ c  Breturn(false);
: p# ]3 n# N5 q; D  y}. F  v  @/ p8 Q9 m
//--- since not all sizes defined in the input tensor we must set them explicitly) k0 e3 G3 `% b3 @$ H& j
//--- first index - batch size, second index - series size, third index - number of series (OHLC)) r: h1 K" [* M7 P3 z  V+ D
const long input_shape[] = {1,m_sample_size,4};* ]. o9 V6 m. Y
if(!OnnxSetInputShape(m_handle,0,input_shape))
% G2 v$ R2 b$ O, s7 V) K{
9 b; ?1 ^( v" @! |: cPrint("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());
2 K6 X0 h& A' f5 R! Hreturn(false);
: I# J; E( H) J2 o}  Y& s6 L. L6 G
//--- since not all sizes defined in the output tensor we must set them explicitly3 Q! v! Z: c) E$ n
//--- first index - batch size, must match the batch size of the input tensor
9 _) v7 ^! X7 s//--- second index - number of classes (up, same or down)
3 O5 c7 P$ }7 r& Kconst long output_shape[] = {1,3};: m7 @& Q: Z0 i0 Y* ^
if(!OnnxSetOutputShape(m_handle,0,output_shape))4 k  r6 Z' l! `
{
- ]0 p) o9 o6 dPrint("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());% V7 k9 ^; f! d+ g$ b: N3 Z8 j
return(false);8 g. X5 M6 D7 p7 ]  @* Y" i
}
4 c" w) z' R% g+ `3 y0 g/ f//--- ok
0 |' ^2 j, q7 H: C7 [% f3 `return(true);* g6 I; r8 r) w$ h( }
}
& i* p% N; T* H& a! |1 S//+------------------------------------------------------------------+7 t# M8 {7 C3 k
//| Predict class                                                    |0 f- L7 S+ O  t+ s5 h9 Q
//+------------------------------------------------------------------+( L: R2 E9 k( X0 F( k  Q
virtual int PredictClass(void)  _. F  x/ U3 w/ p4 j
{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-27 14:46 , Processed in 0.705587 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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