私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?
% C+ n8 S/ n- V+ a在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。
$ u4 N. w+ Y' {6 n我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进
7 O& p. ^# v+ A4 R//|                                             https://www.mql5.com |
  n6 Q$ T7 z# H% d' z//+------------------------------------------------------------------+. `8 Q. y$ N) \1 y  I4 J
//--- price movement prediction& r7 I9 B) [+ W; v$ S$ {# G
#define PRICE_UP   02 E3 ?: n4 o; q" t
#define PRICE_SAME 1
! z( n) a; x, u; ?7 \! S- ~" I#define PRICE_DOWN 2; `5 U7 ?1 Z* ]& D
//+------------------------------------------------------------------+$ ?  ~# J& Y9 c" o. C) Q# y
//| Base class for models based on trained symbol and period         |0 C) ~) s: m, k; h: H
//+------------------------------------------------------------------+
+ ^; Q! h. [0 Qclass CModelSymbolPeriod9 X$ z, E0 H: v0 [
{
6 D2 U( z* U# p" s. Dprotected:) y! m: r. O) G- u
long              m_handle;           // created model session handle" O- X. W* z6 Q0 E9 Z" M  `; l
string            m_symbol;           // symbol of trained data) p: s6 w! t! U1 ?' q
ENUM_TIMEFRAMES   m_period;           // timeframe of trained data3 U! S! T, d" \$ k' I
datetime          m_next_bar;         // time of next bar (we work at bar begin only)$ c1 G2 L- K9 K/ p0 \4 x0 m3 n
double            m_class_delta;      // delta to recognize "price the same" in regression models7 O8 C- w* V  R( n+ a
public:
" B2 D  |- |+ i* ~6 [$ x9 p7 I//+------------------------------------------------------------------+4 F, `0 T4 U# T1 s& ?% u6 k
//| Constructor                                                      |" s( k# \$ q4 Q: w) |
//+------------------------------------------------------------------+
5 |' b3 ~, t* y& LCModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)
; B/ `( O" i$ N/ t( D{+ j/ {3 x' D. R& W0 V
m_handle=INVALID_HANDLE;; l( U3 N- a1 i
m_symbol=symbol;
& r5 T8 R6 n7 e; M/ Q: F. p8 pm_period=period;* }6 m) \  r. c; u' ?
m_next_bar=0;# g9 M' i1 U3 h+ t' ]
m_class_delta=class_delta;" K7 ]3 m7 p9 w5 L% N
}. g- w# Z! A" H* q+ f
//+------------------------------------------------------------------+) f/ d1 I, ]3 J! H+ ^4 E& T
//| Destructor                                                       |/ Z6 x0 Q# R( Y0 b+ [, n
//| Check for initialization, create model                           |5 q& ^0 [/ z0 w8 U& H1 L! b# t
//+------------------------------------------------------------------+* b9 ~' |: y# u* x
bool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])
! u2 U* K4 O# x6 H) n1 \4 p{: J, G; i/ d: n9 a5 V* `
//--- check symbol, period: O( ]. d' z3 _+ E6 `8 i, K% [
if(symbol!=m_symbol || period!=m_period)
( M" ^. u8 U- R3 ^9 v4 e- W3 T{0 _' g" f# Y6 y; I: S
PrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));. t% ]; C' o+ f& D( T
return(false);
; z1 K* I8 @8 T3 v5 G; x& i' b$ `}4 x& Y  v+ B8 X: s
//--- create a model from static buffer
) }0 m* J! c8 p0 @m_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);0 R) s3 @9 _: a- O5 v- ^
if(m_handle==INVALID_HANDLE)
! K/ i7 N9 @' B9 w% G) V. E* X{. i$ ~' y  i! R* W
Print("OnnxCreateFromBuffer error ",GetLastError());
" m8 w6 n' ^7 _/ N( u5 Z) Y! `' |; Q) Vreturn(false);
! J! y, l7 r/ E4 m/ I3 @}
' s; q* S, ~  W5 A1 U//--- ok) T4 r6 ?, |/ \5 Z4 Q% Q. k
return(true);
5 ^% r5 S* g$ x3 B}
( `1 c  O& |  `( J8 |: U1 c! l' G2 M$ a//+------------------------------------------------------------------+* u" k) W  y4 V4 F
m_next_bar=TimeCurrent();
1 S! j$ X+ V, y; G% f: Bm_next_bar-=m_next_bar%PeriodSeconds(m_period);
$ q8 C- `; J% T( J; D* Pm_next_bar+=PeriodSeconds(m_period);. N5 A: ^* m2 l& `. {
//--- work on new day bar5 Z( `' Z& X% x6 ^' ?0 A
return(true);
  U9 F! t" R. z2 r% v3 C}9 K4 S! C0 T4 Z8 V. h$ P
//+------------------------------------------------------------------+8 k  b7 J' `; c, Q  F
//| virtual stub for PredictPrice (regression model)                 |
) R3 p  n9 ]* x, M6 U1 b//+------------------------------------------------------------------+* O9 A7 J  y- u2 {' H
virtual double PredictPrice(void)9 q% H' |* c7 c2 w" [
{
+ U: k* b: Q  Kreturn(DBL_MAX);/ H5 R, X3 [7 T" \7 Z$ a) n
}& Z2 V; g% o5 ~9 W
//+------------------------------------------------------------------+1 A, e/ S- I3 O0 ?$ O0 e
//| Predict class (regression -> classification)                     |5 G9 _; P. ^. q: @4 i" F
//+------------------------------------------------------------------+
" C$ Z! a3 L7 L! U  Vvirtual int PredictClass(void)& C# \' ?& Q0 j$ @
{- s2 I% A1 }& L# a8 u, N2 ]% i6 y
double predicted_price=PredictPrice();
( y0 Q# s: [. g5 pif(predicted_price==DBL_MAX)
  r' h! B. V7 y. preturn(-1);' K9 r5 U2 Y$ W5 ]; `4 m, W
int    predicted_class=-1;
' D6 P: Z+ @0 Y: C# z2 a1 W- A* S0 idouble last_close=iClose(m_symbol,m_period,1);
- U/ n6 e. z0 r. O! g5 t/ C//--- classify predicted price movement# `, w1 }* V1 M4 M$ ]6 {3 H( t
double delta=last_close-predicted_price;- i) b/ z8 t' h( |! A  G$ O2 J+ R3 X
if(fabs(delta)<=m_class_delta)% C4 X" f, Y  h! c- |
predicted_class=PRICE_SAME;% g7 K% j) R8 A$ u) \! v3 z
else! O0 n% x# _) A) x- _/ N# n- S( ~
private:
# t( o) N0 x: [+ }8 Yint               m_sample_size;
5 I$ K6 v4 W5 @/ ^//+------------------------------------------------------------------++ t! P+ s3 X; k( V
virtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)0 e( E6 _1 A$ n, P- x& S) @3 h
{& j0 n) o% K' }: ]7 |7 |( h
//--- check symbol, period, create model
2 H! U4 t* V$ H! Mif(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class))
. |4 E) L( R, y{
% X% \+ }/ ?7 R" m! zPrint("model_eurusd_D1_10_class : initialization error");6 P. v$ y5 R+ i; C% |( W& Y' f5 p
return(false);$ Y' ]( o, E0 H. Q: Q) [1 c3 D
}
  @2 r/ ]  m' T+ N# E//--- since not all sizes defined in the input tensor we must set them explicitly
: S+ u' A4 B+ ~) W" b//--- first index - batch size, second index - series size, third index - number of series (OHLC)' r/ T. L+ }' s) S8 e
const long input_shape[] = {1,m_sample_size,4};" s0 _9 P# `  m2 M, h
if(!OnnxSetInputShape(m_handle,0,input_shape))
, K$ v5 i" Q4 ?" E{
! A8 k# {" b+ @' LPrint("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());7 S  L  n* A& d% [4 l0 X
return(false);# r. u9 k3 e3 r, ?5 E
}
0 L0 `. X( X, R( ?2 j& i//--- since not all sizes defined in the output tensor we must set them explicitly
) W+ e5 \" K: J- R4 ^# N4 ^//--- first index - batch size, must match the batch size of the input tensor
+ s' F0 w- C/ P! U  J//--- second index - number of classes (up, same or down)$ W3 E" N7 {# @* V, R3 k/ S: C. O
const long output_shape[] = {1,3};5 R4 x# p0 |, P: L: B. K
if(!OnnxSetOutputShape(m_handle,0,output_shape))9 a% `; A/ ^$ x- f9 e2 N  O
{
3 q1 K+ a! C( }+ [' x6 LPrint("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());) [+ v+ n& I1 U4 a
return(false);
2 M' k' k" R) P$ g0 O& T}
1 ?! w6 m5 F4 U2 r7 R; _$ Y! ~/ |% f2 @//--- ok
0 R4 W1 u' |. s. nreturn(true);- M& \, S6 |# n! h- c  p
}
- M0 p1 k: A  U//+------------------------------------------------------------------+
0 {( B% b  `' y$ J# j7 {* @2 s//| Predict class                                                    |
- ~$ D/ r, S  S8 H' X2 p//+------------------------------------------------------------------+
, \  [0 U& i) O) Q7 Lvirtual int PredictClass(void)
. L5 x5 m3 G! j1 ^7 Q{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-24 10:51 , Processed in 1.029816 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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