私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?
' g0 T* \8 P8 T# x6 y- _在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。8 ?! A2 P: n1 x
我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进8 Q0 e9 b- P, u% ~2 }* @
//|                                             https://www.mql5.com |# r# J* R. }# @' y/ M6 b0 g
//+------------------------------------------------------------------+" q; ^) L7 Z. _
//--- price movement prediction, _' i( x5 M2 q1 q: ?3 }9 i
#define PRICE_UP   07 s" U: b1 g+ [
#define PRICE_SAME 1/ D+ M; h; B; G) F
#define PRICE_DOWN 2
& a& f' l- J4 A% z5 b+ N6 s6 h//+------------------------------------------------------------------+
" A" r; K  i2 V, ^, K//| Base class for models based on trained symbol and period         |( V* n& |7 O0 g
//+------------------------------------------------------------------+1 a3 _9 J6 s/ j7 e4 z* Q3 e
class CModelSymbolPeriod
: T2 B& L$ B0 e8 p0 ?* F{
/ H/ z; g9 \% e4 A: Nprotected:3 u$ y( N: q% \0 W/ d( l) N8 P) y
long              m_handle;           // created model session handle
. [0 r1 i) K9 O0 c/ C9 Wstring            m_symbol;           // symbol of trained data) F. d) C5 l* y: U
ENUM_TIMEFRAMES   m_period;           // timeframe of trained data
$ h- g) G9 W9 \2 m7 t/ wdatetime          m_next_bar;         // time of next bar (we work at bar begin only)5 M. v6 a9 h3 b" @$ |
double            m_class_delta;      // delta to recognize "price the same" in regression models
% C/ D" J/ p" Ipublic:
2 _$ G' ~3 z! {  G//+------------------------------------------------------------------+" O$ T# L5 U. s0 K1 [$ E* r
//| Constructor                                                      |
: Z! l9 _) V1 d5 j; F- _( o//+------------------------------------------------------------------+( [" \0 g* m1 h+ t6 {6 G* N
CModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)
0 O& m, F  b9 _% t) A! J! C" `& ]{0 ^8 O1 G3 B% L( U9 m0 S) V% N+ Q
m_handle=INVALID_HANDLE;
3 d, `/ H* D+ f' Sm_symbol=symbol;
8 i  _' O! i4 I# Em_period=period;; K% J; ^- N/ V3 S
m_next_bar=0;" I$ W. {% o2 c3 R& p1 \
m_class_delta=class_delta;1 R- M8 M7 M2 G# M# W6 y
}' @2 j; w7 S' U2 @+ d
//+------------------------------------------------------------------+5 f: K  Y* E5 ^$ @5 x" E  B
//| Destructor                                                       |
( w  C! L6 x/ H+ R$ [1 U4 I8 X//| Check for initialization, create model                           |
# ?: B0 X# T* c7 m8 l# `//+------------------------------------------------------------------+/ Z: o: P" x6 l
bool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])
" \6 B2 l( ^2 i! @! B& C: |, |3 \{' C$ f9 ]$ O1 `0 C2 I. P# y
//--- check symbol, period5 s& e  D0 {0 O" S7 _. F. s& @
if(symbol!=m_symbol || period!=m_period)
2 b3 `" v0 g1 c" N! a7 ]{4 W1 y/ N) }/ x# i' l- |
PrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));- g4 |  _/ i5 b' k
return(false);
! B6 {# ?7 S& v" s8 E" A8 g}
$ f' P+ w/ w2 o$ R//--- create a model from static buffer
* r2 ]6 }+ I7 [" w- bm_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);) A" z4 }7 z- f, A
if(m_handle==INVALID_HANDLE)! }7 E7 H2 F- K# I( Y$ h. e7 q* R6 j
{
% Y4 z( p& H+ @% N! e: P1 M1 `  y  nPrint("OnnxCreateFromBuffer error ",GetLastError());
8 w1 P& M1 x. X, P' v' Q; V. y, T  Preturn(false);  q- \2 ?5 l+ I+ i3 p& D/ R
}
* A" s6 P# x  l" P% `//--- ok
; I! L* w' B4 ]0 Hreturn(true);5 v* q% K/ n1 ]+ T0 ]* |; X  f
}- t8 B+ G* S$ B2 O) C9 |7 A
//+------------------------------------------------------------------+
  `0 ]4 A# V1 l2 `m_next_bar=TimeCurrent();
  \) @( R/ y  [+ Bm_next_bar-=m_next_bar%PeriodSeconds(m_period);- E+ g4 H4 D  ?" i* G$ ]2 l
m_next_bar+=PeriodSeconds(m_period);+ P  ~- @: U( c# N  \. M
//--- work on new day bar
5 L9 S) Z6 n2 a2 o# `2 K! K# q: _6 Greturn(true);
0 l/ a" I$ Z) O; S+ K}
; A1 e' ^9 ^6 j//+------------------------------------------------------------------+
8 g) ~# `/ [3 ~& A; h& Z# G//| virtual stub for PredictPrice (regression model)                 |# Y% C% b* I* `( N- o5 K6 y4 D
//+------------------------------------------------------------------+
6 k, Q/ h, i2 {  b4 Z& B2 Svirtual double PredictPrice(void)2 k+ u. z% N% z8 a
{# V" V3 @4 z  X( K- W
return(DBL_MAX);0 N4 F5 ^3 {2 S$ u
}5 x3 O3 L! k5 R
//+------------------------------------------------------------------+
5 }5 d& k6 |7 `. Y. B1 u% X( f//| Predict class (regression -> classification)                     |
8 F# @+ Z4 i, [+ A//+------------------------------------------------------------------+
  Y3 M) h; s6 L( {! Nvirtual int PredictClass(void)6 X+ _1 j" b- ^( Y) a  Y; K
{
. t- j  Q( Z9 m% Vdouble predicted_price=PredictPrice();" d# @+ U; T9 v4 m6 l8 q
if(predicted_price==DBL_MAX)
, C+ b" p& A3 e  p8 u5 E9 J, ~return(-1);" q$ s" H6 U: Q
int    predicted_class=-1;
* j5 i' z# I/ cdouble last_close=iClose(m_symbol,m_period,1);
, R& e6 c; D0 Y4 O. w//--- classify predicted price movement/ Q3 Z4 h$ z2 {3 {1 P
double delta=last_close-predicted_price;# _3 }% S3 R; O+ M  i
if(fabs(delta)<=m_class_delta)
! Y6 R# x( a- qpredicted_class=PRICE_SAME;
7 ]. s! A" ]1 Y  Eelse
" s" {- t" l1 A$ U( A& Eprivate:
9 N  l& q1 D9 T! |% ~# f: \$ Dint               m_sample_size;
  R5 I; t3 [; |' l' v7 p- ?3 }/ b//+------------------------------------------------------------------+
& l6 y- G+ f3 Ivirtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)
8 i: r6 r( a1 l5 ?9 A{. X: |$ h- V5 X! M; p; C, U
//--- check symbol, period, create model+ x; W( w7 b' {( j  G6 z' d
if(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class))
5 e) Z2 L" @  W4 L. E! u) N{
$ d' M; z7 W; k" tPrint("model_eurusd_D1_10_class : initialization error");
) l; B7 G; p1 J% K2 O2 H4 @9 wreturn(false);& W: m( }) \) ^, P
}
* L& A; v2 ~: H7 n4 a1 [# L//--- since not all sizes defined in the input tensor we must set them explicitly
1 Q/ P8 t' a: @//--- first index - batch size, second index - series size, third index - number of series (OHLC)
7 S7 v1 k; `& ^6 R9 X; {+ econst long input_shape[] = {1,m_sample_size,4};
" H: \/ q$ ]0 F0 t1 R2 l# a5 oif(!OnnxSetInputShape(m_handle,0,input_shape))
8 B0 b7 c% l- x: |. ]{
) c4 N4 ?2 v# P0 n4 H) E8 ]4 N' qPrint("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());
* p* n% h) Y+ X0 T# U, freturn(false);
" x& M3 z7 ~' z" k' @' r}
# t9 m& f+ J0 \, s# z6 T//--- since not all sizes defined in the output tensor we must set them explicitly
+ |( v  W( r5 U8 M* C# D$ m3 i//--- first index - batch size, must match the batch size of the input tensor
+ C3 G" I- `, E' T: J//--- second index - number of classes (up, same or down)
1 V& |! ], f1 A; u& Dconst long output_shape[] = {1,3};. t: G0 ~' T/ {: Z0 v: e5 i
if(!OnnxSetOutputShape(m_handle,0,output_shape))
* ?- B7 v) `4 O+ Q{
; H9 g+ j) u  `9 }; W' ~- }Print("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());0 X: E4 b9 R$ a+ O
return(false);- u' F( r1 |/ q. D$ g8 W0 E
}: @5 q& B; K- `% [' F
//--- ok( d# K% O" j7 B' F
return(true);$ i! ]$ E. w2 E
}
0 S1 U4 i! M/ w! t+ `8 }//+------------------------------------------------------------------+/ \4 @5 z$ \1 \2 B0 ?- j
//| Predict class                                                    |
/ V5 t4 E9 O- k9 C* k# F5 r//+------------------------------------------------------------------+! Y' R& o4 b# L# U$ o
virtual int PredictClass(void)6 F, C" H  G5 `/ y# U
{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-3 09:16 , Processed in 1.375704 second(s), 32 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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