私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?
6 g1 V' e& @# n% m' h9 W  G在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。
; w0 t/ j0 W* E我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进
' M- o/ z" x* @. O: o//|                                             https://www.mql5.com |4 U! g; r( C$ |3 d4 ]
//+------------------------------------------------------------------+
5 t4 F, L" v9 {" L//--- price movement prediction
, Z# {4 \- b1 K) W/ G5 j#define PRICE_UP   0+ K" C/ O3 g: O2 k+ @
#define PRICE_SAME 1
. I  h& T5 l" Y+ X7 L#define PRICE_DOWN 2
' K2 f- }' C. y% u//+------------------------------------------------------------------+
/ k6 o# \) E4 b0 s, P; Z//| Base class for models based on trained symbol and period         |
& o* J1 b8 o. t7 d, z& {9 B//+------------------------------------------------------------------+  `3 n# \; `  m5 N
class CModelSymbolPeriod, e+ k# p) j6 s! T! y5 v
{8 D7 h( I0 V- u8 ~: I' q
protected:6 V$ o# E! d9 b8 j. j% V
long              m_handle;           // created model session handle
& J% N( _9 L, w$ Fstring            m_symbol;           // symbol of trained data5 l$ H* j* N' b* f3 T5 C
ENUM_TIMEFRAMES   m_period;           // timeframe of trained data* `( W- C  Q* X8 u$ G) l
datetime          m_next_bar;         // time of next bar (we work at bar begin only)
+ w! }8 Z5 b# _9 R6 w' `double            m_class_delta;      // delta to recognize "price the same" in regression models6 W, \1 I/ i  c/ H7 x
public:5 q5 p( {  g3 e# G
//+------------------------------------------------------------------+5 j! {2 {$ x$ e$ g! r* T
//| Constructor                                                      |, L  n8 T& V! e2 }
//+------------------------------------------------------------------+; x0 k6 F! N) E6 E
CModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)
3 o( \& V7 G: b. {; ~$ h{8 U4 W4 H% ?! s. ^
m_handle=INVALID_HANDLE;
7 l3 ^: m5 g) Q$ Q: n* r+ rm_symbol=symbol;
2 o  ]2 `) A( I% F- a& p8 P6 ym_period=period;
/ x! ^9 r$ ]6 k2 z. jm_next_bar=0;
4 t' b6 U* _& b. Km_class_delta=class_delta;2 l/ ^" l' V- P# y+ H
}
  H4 D& _. d+ C: }1 i  T) q$ i* A" R, ?//+------------------------------------------------------------------++ R" I5 `2 ^% T5 \+ D5 o/ c
//| Destructor                                                       |$ b$ [% Y" z0 M7 V
//| Check for initialization, create model                           |" c* t6 M9 g/ T3 J$ @# V
//+------------------------------------------------------------------+
2 e3 y6 ?3 I6 S5 u; ^) L3 Jbool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])+ m- C" v7 w% g6 b1 o
{2 o. e( }: y3 B. X, y4 ?. v: B
//--- check symbol, period6 c( q& A2 `. b6 Y" E7 d
if(symbol!=m_symbol || period!=m_period)
. H9 G5 d  |- E- f- O{
! N6 w* {8 u" c* bPrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));
9 ]) I; Y- X7 X& preturn(false);! p; Q! u( N* B- a! x1 p( |8 {
}3 l+ F2 e' T2 g1 b$ r5 [8 L" {
//--- create a model from static buffer0 Q( W$ M3 s- c* l
m_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);
) V3 R) H7 _4 M" f0 y# Zif(m_handle==INVALID_HANDLE)
/ j- Z; ?$ J8 H  ]2 @% v{
4 G0 y$ a) t! z! u4 r( RPrint("OnnxCreateFromBuffer error ",GetLastError());" E! J0 E8 @) x% Y3 \
return(false);/ a: a$ g0 ]4 R
}0 \8 N9 G: m* i; m- h) }
//--- ok6 }! U1 e, ~. g! r) Z
return(true);$ O, E2 {0 {. \5 D8 R& j
}
0 S' A0 R1 M$ @  c; V# A//+------------------------------------------------------------------+' G0 z/ R/ J# M: B3 q
m_next_bar=TimeCurrent();- j6 |/ \- V# c
m_next_bar-=m_next_bar%PeriodSeconds(m_period);7 ~0 f( A: f! T. T. i8 ]' R9 U# [" P
m_next_bar+=PeriodSeconds(m_period);
& `" k/ G1 u- B2 o//--- work on new day bar
& a+ @& q; ]. greturn(true);( ~: Z* n' P3 w% u. Q" y& [
}
& W- c# a, j; u0 \' G/ ?! t//+------------------------------------------------------------------+9 K! n4 I1 J! c% I8 j
//| virtual stub for PredictPrice (regression model)                 |4 c2 r. \9 H: m3 q5 I8 b$ u
//+------------------------------------------------------------------+6 h$ t7 g& x3 Q1 t) \# V
virtual double PredictPrice(void)+ D, V7 M' n; s' {
{
' a# q9 H2 U0 f2 I) V  ~return(DBL_MAX);
8 m  ?: k- t/ o. x, b9 D}
" D5 M: k' r# r! ]& d+ c//+------------------------------------------------------------------+1 h" @& ^* Y% I) Z% r- i
//| Predict class (regression -> classification)                     |4 m# o+ E  B/ s1 A; s6 P) m
//+------------------------------------------------------------------+
/ a9 ?. u/ k( r; J& P1 fvirtual int PredictClass(void)' E# w# T4 k- O. Q4 B( o! n
{
- B2 h/ J  H: T* {double predicted_price=PredictPrice();
1 ]) u- t6 g, q) G& _1 v; j& v- Hif(predicted_price==DBL_MAX)
0 J! O, Q" U& M6 T' ireturn(-1);
7 O# e6 _8 ]$ ~( B6 ]int    predicted_class=-1;
$ b3 @: q5 d: ~/ N. j9 {: Jdouble last_close=iClose(m_symbol,m_period,1);
! v- e6 S6 q" M" j7 R: O1 s( H//--- classify predicted price movement
0 g/ T( D, W. G' j: _7 ydouble delta=last_close-predicted_price;
; G1 T1 Q) O' ?! n  S0 h" ~/ Eif(fabs(delta)<=m_class_delta)
! M% R8 T/ `2 z" D# E) e/ a& }8 Ypredicted_class=PRICE_SAME;3 A5 k$ |7 i; r" a! A3 E
else; [/ N  R. f" j. E8 M, f& Q
private:
0 K4 u5 o' B" d8 k/ xint               m_sample_size;* @5 c  Q; l0 r7 ~, k. X
//+------------------------------------------------------------------+  a% R" j& P( [, K
virtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)1 w4 {) U7 q3 [8 {1 u% ^" w
{" l6 f9 A# S+ d3 ]" n  ^9 y. `
//--- check symbol, period, create model) @# b( r( ^. n2 H" f' v. D. \4 G
if(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class))
: D0 s9 _$ j  r3 F# U1 v{- C* C# L& ?% W. \% \; t' i
Print("model_eurusd_D1_10_class : initialization error");
/ g' a7 p$ U' ?8 p* S2 q/ ]return(false);* b+ C' Z0 T+ G4 |1 s" B
}) `! Y) W" d8 a9 {$ z( m" S
//--- since not all sizes defined in the input tensor we must set them explicitly8 G+ p- m5 Y9 Z) ?# I
//--- first index - batch size, second index - series size, third index - number of series (OHLC)
# l# G" }* ?  o' u. lconst long input_shape[] = {1,m_sample_size,4};
$ B0 O- K1 z4 C/ L5 Y& S0 K3 mif(!OnnxSetInputShape(m_handle,0,input_shape))" {& h2 f! _7 H* A
{; B) j& N0 d$ z; ?
Print("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());
; A5 d) ]: S+ |$ R8 treturn(false);
2 u# I6 a( b/ q9 T0 e% [4 K}
; y4 g" ^9 E: H  s//--- since not all sizes defined in the output tensor we must set them explicitly
& S) c) q" z# o# C" N0 p2 s% V//--- first index - batch size, must match the batch size of the input tensor
# s8 U5 L" U/ Y& r7 w  T" ]) y, d7 r1 h//--- second index - number of classes (up, same or down)) z  T2 D. [# V5 r% P8 m
const long output_shape[] = {1,3};
# |( O* y# t, f6 L0 Gif(!OnnxSetOutputShape(m_handle,0,output_shape))
" O7 _5 Z8 C7 n3 ~/ U$ Y6 x+ ]{; e* Y- k; n& }& U: J0 i: r
Print("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());
1 P; g1 F( ~% Jreturn(false);
9 X9 ^, L* m; o9 B; x9 _/ Q4 s}
, {' ], i1 E* H/ |& t9 z/ T//--- ok5 o, r0 Q0 U% ]; v! I# ~) M# t
return(true);
& K$ S  e3 Q9 S  G3 Q}
* {- v, j! Y# A//+------------------------------------------------------------------+( Q- T- G! y+ z+ p, t
//| Predict class                                                    |
6 Q# a: e7 S- ?+ u. z1 i5 z; [: A//+------------------------------------------------------------------+- w' [$ b# d# C6 c; J* x- a. s# [
virtual int PredictClass(void)
9 c! k! C( y6 |8 x8 ]" `: C* L' I) t{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-3-5 04:42 , Processed in 0.868613 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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