私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?% Q. a& K3 s! x) Z; Y
在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。8 `2 U% Y: ~8 X
我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进2 |8 O# e, V/ |) M( X0 G& V/ x" Q
//|                                             https://www.mql5.com |
# s  F: u; c/ |' u3 J: H//+------------------------------------------------------------------+3 y- e1 u; @/ q3 p
//--- price movement prediction3 }7 g+ t& ~' S( ]% B  J4 y4 U
#define PRICE_UP   0$ y. c/ @4 x9 V0 h* W" p
#define PRICE_SAME 1" y/ ]; B9 I1 Y0 h5 v
#define PRICE_DOWN 24 c7 H, {3 S0 V
//+------------------------------------------------------------------+- G$ k5 ^/ m; i8 x' ?
//| Base class for models based on trained symbol and period         |$ H+ d' y; O: B1 j
//+------------------------------------------------------------------+2 z, p* M+ \- _/ @* n3 X/ e
class CModelSymbolPeriod. Y, |( m. b& w+ k3 c
{- ]+ ^! F8 L7 N0 N
protected:
) t5 B3 A3 |$ I0 m: v+ nlong              m_handle;           // created model session handle. A: j7 a- K8 j1 `, D. Y
string            m_symbol;           // symbol of trained data2 j. `% x8 j* n$ m4 \- {* ]' E
ENUM_TIMEFRAMES   m_period;           // timeframe of trained data
, R- W3 @' d6 d1 adatetime          m_next_bar;         // time of next bar (we work at bar begin only)
- f* G% k. {' @0 u) h7 Vdouble            m_class_delta;      // delta to recognize "price the same" in regression models: |+ P( t2 x* y+ J
public:5 U6 R* G% e2 @" E
//+------------------------------------------------------------------+
- S( G) t" Z/ d4 [! l+ s//| Constructor                                                      |' o# ]+ o1 w5 f9 s
//+------------------------------------------------------------------+( o, L9 S8 U' i
CModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)
; z  {) H8 p$ \6 h( D{
8 c+ ?/ y7 t% B* qm_handle=INVALID_HANDLE;
% ^0 x( \8 k. m+ c* A: ?5 }m_symbol=symbol;% Y+ X: }: c" q1 |8 F; K
m_period=period;
8 {8 S- N0 e* z8 S% Pm_next_bar=0;
0 V, f0 n* O# p' g5 [, _m_class_delta=class_delta;4 N9 z5 Y0 c- g, f! y+ g( r, t7 M3 ]
}  e  x% n# `2 P% Z% ~1 y$ `& P$ I
//+------------------------------------------------------------------+' A( i0 Q* }6 x
//| Destructor                                                       |& M  P4 @2 y+ f
//| Check for initialization, create model                           |. o9 o7 v; i& ~, I. Z
//+------------------------------------------------------------------+& h( f9 T1 ^$ R5 U
bool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])+ o' T% l' O1 r  p
{
7 @3 t1 A  k% n7 K' J5 [- s//--- check symbol, period. p- Z. U& h( H0 f$ C
if(symbol!=m_symbol || period!=m_period)3 j7 D. J3 r+ B1 H
{
: z9 d( L+ ?/ \4 H8 {, T- v! |9 }PrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));
- i+ V: ~. x1 H/ \3 T6 zreturn(false);
; H. \2 |: {# s* D" c# e1 O! q}
4 P% a) f3 l) @) F/ C) i- k0 L//--- create a model from static buffer
6 v2 x0 K0 B: ~$ c* {+ Y( cm_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);
5 f" ?# v* k+ k6 Bif(m_handle==INVALID_HANDLE)
7 I) G9 {# v& Z/ p! m2 f{
) Q6 [0 r- d  W4 A0 |Print("OnnxCreateFromBuffer error ",GetLastError());
" M/ m# v) O9 K, P- B8 w  A5 }return(false);
! C. Z' t) c9 ?$ L9 Q9 s/ @8 Y8 o1 q}. @7 w: m: n0 p" m+ c0 e+ e
//--- ok9 [3 o8 g! ~" O5 K" D5 {& n
return(true);
, P) c# Y# ~8 R( O' u8 r) l}! g0 J( \0 U! w$ @
//+------------------------------------------------------------------+
0 o2 ^: x- V0 l# }1 O8 O! vm_next_bar=TimeCurrent();9 M  Q$ z, m( ?' q+ y* O6 q
m_next_bar-=m_next_bar%PeriodSeconds(m_period);. ]+ B# s8 l( G% [
m_next_bar+=PeriodSeconds(m_period);
2 m( \: R6 m, D2 e//--- work on new day bar
  J+ E' L7 s. M* C" t$ J, T, dreturn(true);
& [) i( |/ b, e, c3 `3 s6 F}
7 i3 `  Q* f( c//+------------------------------------------------------------------+
# p3 \2 G! t# G5 j" `1 D//| virtual stub for PredictPrice (regression model)                 |+ d- J' [+ }" V. _" I- H9 r
//+------------------------------------------------------------------+
$ \  M1 f$ P; M' u* j( fvirtual double PredictPrice(void)/ V/ n/ p* ]5 i
{
# i* ], {- ?* h, {# ?return(DBL_MAX);2 H6 B9 M8 @1 I2 ^6 m, g: u4 N0 r  _
}% b/ h1 q8 r0 ~1 H, J
//+------------------------------------------------------------------+& ~, H" l- Z7 Z* O" @2 x9 [
//| Predict class (regression -> classification)                     |
/ W1 s: ^8 y' E0 f$ [//+------------------------------------------------------------------+7 ^. H. b! v, `( q2 @. o2 {
virtual int PredictClass(void)
6 `/ w* c8 M! x{8 l( d# a  C$ S+ Z+ f
double predicted_price=PredictPrice();
% b3 H0 a0 F+ r' Q9 \% Zif(predicted_price==DBL_MAX)/ `! K% X' U4 |! L, Z+ L: ^
return(-1);
* h0 l) S; Z0 c- ]. vint    predicted_class=-1;- I( _8 H* ~) B# ]8 Z# \% l& c
double last_close=iClose(m_symbol,m_period,1);7 m/ R9 d. s! G" h
//--- classify predicted price movement, k1 i8 M- E/ j( o" r+ u
double delta=last_close-predicted_price;
. a2 N: b2 p# ^if(fabs(delta)<=m_class_delta)
# a0 u( C( L& j! zpredicted_class=PRICE_SAME;3 W6 C5 M; V9 d. p3 t/ @: r
else
# l$ c- N+ H, e9 j0 Zprivate:/ B( ~& m- o" d3 ~4 h
int               m_sample_size;
$ V* m( t" [# }4 I  h4 l0 b: h7 c//+------------------------------------------------------------------+
& z! x8 f& s' m) a1 \virtual bool Init(const string symbol, const ENUM_TIMEFRAMES period); M( e$ x1 w6 }
{+ V( ~' t! M* b4 C4 m$ u% ~
//--- check symbol, period, create model
: ^8 p9 R9 X7 P( L4 {" f8 H1 jif(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class))- q4 V6 Y6 T- G$ E+ N3 o- L$ ^
{" }5 |5 D% Y+ L* \+ D% o
Print("model_eurusd_D1_10_class : initialization error");# d3 A4 R, l2 P0 E
return(false);
0 N: I+ o7 e3 ^! T$ |}2 y+ l& P! M7 ^. S9 B
//--- since not all sizes defined in the input tensor we must set them explicitly
/ J. ^; Y8 O& n  _  W: |! k3 `6 W6 ]//--- first index - batch size, second index - series size, third index - number of series (OHLC)4 e; r! V: A8 J3 S# m$ j* f
const long input_shape[] = {1,m_sample_size,4};8 J9 x) {, L% K3 ~, q$ T
if(!OnnxSetInputShape(m_handle,0,input_shape)); W7 J4 l- `$ ~. C
{
3 I+ ]- D: F; }, T7 GPrint("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());
- t" d1 E7 x9 U; u. M/ ]8 ?return(false);& f  }7 W! }6 U
}- k8 I1 b9 W; F- r
//--- since not all sizes defined in the output tensor we must set them explicitly$ O2 S1 j$ N. v, j  G! N6 ]" V
//--- first index - batch size, must match the batch size of the input tensor
5 d& S) a9 [, h# b! A//--- second index - number of classes (up, same or down)
/ G+ @6 e, @9 f, U; L: l1 S1 O3 W2 Qconst long output_shape[] = {1,3};
  u6 F  M- a! {1 c- ^0 I* j5 Hif(!OnnxSetOutputShape(m_handle,0,output_shape))  x  X9 U+ s  B! W# m# L9 V
{
7 _5 A/ e# T" b5 a; LPrint("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());
! s1 l5 e) O. @. d- oreturn(false);* u" ^% J2 D/ [& S8 ^) g
}
5 u& M# ~, V5 H' M3 ~0 y//--- ok0 L) H! h; z4 @9 Y6 @0 A& K! U
return(true);
* F' P8 _% L( Y( ?: A}
" D2 ]/ w$ q5 W//+------------------------------------------------------------------+
" ?( G1 K* ~  L2 D//| Predict class                                                    |( ?; v& I9 _. j% y$ C- `$ s4 u
//+------------------------------------------------------------------+8 k2 j- t6 c' Q: x7 e6 A1 m8 ]
virtual int PredictClass(void)( r4 A) i8 ~# T7 [0 C/ {4 G  D' I
{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-18 21:45 , Processed in 0.491573 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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