私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?
* B- M3 S: W; l0 }" m# G* ]2 p在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。
' p, F: D  T+ v5 R我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进
- U( i  d4 Z7 f' }//|                                             https://www.mql5.com |- H! h- @' q  `2 j4 {1 B
//+------------------------------------------------------------------+
& V+ Y, {; P/ }. G, F//--- price movement prediction
+ l5 D% U+ p% O1 x#define PRICE_UP   0
6 q1 Z6 W% k$ r#define PRICE_SAME 1
5 K# A( ^% i4 \0 j* ^6 Z#define PRICE_DOWN 2
9 `$ s4 w" j  w//+------------------------------------------------------------------+
8 t1 G9 i6 T& G" L2 J//| Base class for models based on trained symbol and period         |
( [8 V+ E: [3 W/ c3 t//+------------------------------------------------------------------+
7 O! G: w  k( q. e8 O. I; {class CModelSymbolPeriod  w' u1 J8 g0 u) y6 r* W" |
{
, u, `8 o! V% ?# d1 w1 G2 _protected:
" M5 L" m; s/ c0 m  e7 k/ Plong              m_handle;           // created model session handle
# r& i7 w' w9 L0 g0 b4 l* {; Wstring            m_symbol;           // symbol of trained data
+ f, s- j! t* u9 f# d: y/ s  xENUM_TIMEFRAMES   m_period;           // timeframe of trained data
# I4 C5 T$ d3 O( ?datetime          m_next_bar;         // time of next bar (we work at bar begin only)/ O' F' N8 O- _8 C
double            m_class_delta;      // delta to recognize "price the same" in regression models
; a5 ?6 E6 h0 l4 X( S9 d9 S; Rpublic:
% K2 |3 J6 v( y0 j; V: b//+------------------------------------------------------------------+
: }1 s$ g+ R5 J: b! W$ `//| Constructor                                                      |
: ]. j! f- u( }9 H9 m( N" h% s//+------------------------------------------------------------------+/ j1 q7 F" c/ c+ r% k* [
CModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001); V4 c0 }* x! d$ L
{
) m1 k* `- y! Z& s& ]; O2 {m_handle=INVALID_HANDLE;
: G! D+ a+ o+ G- Pm_symbol=symbol;
2 ^( j) y* l- _( M! c* }7 }+ hm_period=period;
& u5 x8 g( |" Y& W/ ^4 Em_next_bar=0;
0 O# s* E: f- n3 t- J4 q! Q" ym_class_delta=class_delta;
3 e" D$ A; Y( \$ v}7 q3 ]7 q& F8 s- ]3 ?2 n$ W
//+------------------------------------------------------------------+  F/ `& g- F0 u# z8 W9 e- I& D0 d; J
//| Destructor                                                       |
" m8 `& b0 V& Y) J' U0 j% h9 G//| Check for initialization, create model                           |
' h* \( A9 M3 M1 y//+------------------------------------------------------------------+/ ?  q7 D7 O+ Z7 {' E8 g( c
bool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])
$ Q- f) n3 y2 G{
. z9 \7 t( R. a% k+ w//--- check symbol, period
9 K3 D' Y4 x8 oif(symbol!=m_symbol || period!=m_period)2 l1 `& N  G& x. i* d) _
{& a0 _" Y9 z% K7 b) ?+ u# I4 k0 y
PrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));( B% H& H* I8 w
return(false);
+ r. `- ?3 G1 \& K" C}
, g1 w0 U4 D% W6 \0 k//--- create a model from static buffer4 z# P) ?0 e' k  J) _4 E
m_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);5 _' A) y8 H/ B; T; S' o. A
if(m_handle==INVALID_HANDLE)
; L1 z2 l* I# h& d$ ^3 c{; y/ L. T6 U" W3 P% m4 Y
Print("OnnxCreateFromBuffer error ",GetLastError());- Q1 e5 n4 L) F& u9 X
return(false);) u7 `/ ^$ j8 H! m
}/ F/ o2 p- G# Z7 N7 g
//--- ok9 V! Y5 _- f+ L) ?
return(true);
3 H9 ^; K$ M: K+ x8 B& j  h% R}! G$ u5 ^4 ?! Y
//+------------------------------------------------------------------+
- @3 v, ]# f/ u3 Ym_next_bar=TimeCurrent();2 x( F* ]4 r3 c+ B) r1 Y6 R2 e' f
m_next_bar-=m_next_bar%PeriodSeconds(m_period);
$ [, c1 o/ U; H: t  C( Jm_next_bar+=PeriodSeconds(m_period);' F  J1 O8 `  V6 ^
//--- work on new day bar1 v8 l2 g) d2 f- @- g( Y
return(true);
" p2 ?, B' o. T# _# P0 i% K}
# K( o7 M  o* u3 Z2 L//+------------------------------------------------------------------+! m( G$ r( Y, [: L' J3 K  P
//| virtual stub for PredictPrice (regression model)                 |
, }* _5 u3 d: ?+ t6 e* w//+------------------------------------------------------------------+
! \+ ^1 G8 k3 z; g- [7 evirtual double PredictPrice(void)6 h# c7 `7 u: J# Y
{5 q6 N' J% l  g! M: U4 g1 O7 R
return(DBL_MAX);
: \& j. ~( C) d5 m# P( Q8 i  t8 m}
# S$ o; l3 p( d( a//+------------------------------------------------------------------+
: X  h2 u: B$ U( n1 b5 Y+ x//| Predict class (regression -> classification)                     |
  R; b* X8 _0 R6 u9 R/ N7 p//+------------------------------------------------------------------+
2 p  q6 f5 {# R7 hvirtual int PredictClass(void)2 ]! ^- t8 r+ X$ {5 J8 y, a
{! ]$ y  h9 `2 T" D
double predicted_price=PredictPrice();% S" H3 Q. A' D% h. J0 m
if(predicted_price==DBL_MAX)
, h- d7 b% {' O/ T' b7 G' Breturn(-1);
. u0 I! {! p! V6 G/ c. X" n, z" P5 Eint    predicted_class=-1;. ]7 P( x1 [1 I4 p9 s, }# `7 J" t
double last_close=iClose(m_symbol,m_period,1);
; w  p' J3 U. b' V/ i( s% R//--- classify predicted price movement7 W+ O% y& b9 g( v
double delta=last_close-predicted_price;
' K3 E# \8 o% Z* y! p  Yif(fabs(delta)<=m_class_delta)
0 p6 C8 S5 w, \& ^2 N* zpredicted_class=PRICE_SAME;$ R4 t6 F. A' Y
else% L8 I, M# g( D' U
private:, B  @! v0 P; q7 v1 o8 i2 L
int               m_sample_size;
) |( q" v& n" V: @1 D: Z//+------------------------------------------------------------------+, P3 w* @. w' A; h% S
virtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)
% |& j8 H8 ~' G2 \{
6 y" r$ P5 ^. h* p  L//--- check symbol, period, create model
  W% O/ W) g3 K) E" Bif(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class)); v' x- e" s" {+ n+ T
{9 I& m# h& B: k. A1 h  P4 V5 w
Print("model_eurusd_D1_10_class : initialization error");. I/ M3 [3 d8 k& N7 E
return(false);% x/ S5 f# b" [
}
# X3 P4 s/ i- e2 _//--- since not all sizes defined in the input tensor we must set them explicitly# p6 F- U* t* i) Q+ e3 F( L9 ?
//--- first index - batch size, second index - series size, third index - number of series (OHLC)* Q/ J* c8 \4 k
const long input_shape[] = {1,m_sample_size,4};
7 j! i% d  {; Q* rif(!OnnxSetInputShape(m_handle,0,input_shape))3 w6 _- f8 y# C
{7 q& @" T7 ~+ ?3 e& y
Print("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());' s/ ?" \: K3 a/ w5 q- B6 ?- M
return(false);
/ V0 @' a* w* {; ^' Q, \) ^  k}
/ h$ L/ X8 }% u& l//--- since not all sizes defined in the output tensor we must set them explicitly
' p# a2 r; l: I) M//--- first index - batch size, must match the batch size of the input tensor
" C3 \: [4 Y: @- f1 P$ T//--- second index - number of classes (up, same or down)
1 f  o( R1 o) C' x/ K3 W* ~( lconst long output_shape[] = {1,3};
1 M( }. }6 ]/ S" ^- E$ eif(!OnnxSetOutputShape(m_handle,0,output_shape))
! N; H0 y; y+ H* o8 J' H/ b6 r{; D% ~# H3 \; x3 g7 A4 v
Print("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());
# J4 g  {  d, k- o; R2 ^return(false);
% Q% V: [- w8 s  x}
, S3 O5 m- }2 c- f5 R/ [//--- ok7 O- a, O& ~4 c7 C8 V7 J( z
return(true);1 {4 v) n  X  ?, f4 t! Y4 J% Q6 {
}# y0 ~" U' `7 [8 [
//+------------------------------------------------------------------+
. E/ u+ J' J9 @( B+ g- Q* Y$ f//| Predict class                                                    |
* ~5 B+ g( }) A& a* t' \8 e2 l( w//+------------------------------------------------------------------+! S$ K# M- u/ s& ^1 A2 V
virtual int PredictClass(void)3 ]5 }; D/ m: M0 L  D+ B( f( j
{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-4 12:02 , Processed in 0.463059 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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