私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?
( p( v3 \: \7 A3 L在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。
9 B& L2 k& E# _我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进& L9 a% G8 D8 m9 u
//|                                             https://www.mql5.com |$ U% B# V+ N/ H# b9 o% V5 M; k7 `4 N
//+------------------------------------------------------------------+
2 {0 _: m) ]3 r  i/ \) T- G: M$ m//--- price movement prediction
& T, B; X' `# d" B. S; |#define PRICE_UP   0
3 ~" G* W% t; n9 T#define PRICE_SAME 1
! U4 a+ a. R  r" Y( |  U. H#define PRICE_DOWN 26 j% y# F8 J5 ?1 i: p' t
//+------------------------------------------------------------------+
* v' z0 z! K& ~# [1 F5 @' Y& W//| Base class for models based on trained symbol and period         |3 O: u* i- P& d' G; Y3 Q2 \
//+------------------------------------------------------------------+
, L3 Q2 z" t( F( [7 `0 ?+ vclass CModelSymbolPeriod
, ~2 m6 u  X) A* H1 m{
4 W9 k; c1 h" o( Jprotected:
4 A* |7 s" l$ P1 Olong              m_handle;           // created model session handle5 M* [7 V- r4 o( p% ]' a3 Z  x: D
string            m_symbol;           // symbol of trained data4 P! e$ @  l; A' v8 G
ENUM_TIMEFRAMES   m_period;           // timeframe of trained data$ J+ n. I" W2 s: m" J" a2 \
datetime          m_next_bar;         // time of next bar (we work at bar begin only); ^' O' E2 K% R
double            m_class_delta;      // delta to recognize "price the same" in regression models0 W% b) |& S6 Z& Q
public:% h0 X9 Y. g, F0 f! Z, G8 b
//+------------------------------------------------------------------+
+ U' i; U2 H6 _! E5 e//| Constructor                                                      |
* t; ^' a& U/ \: r2 L" `1 N//+------------------------------------------------------------------+6 Q% V4 O+ d. V
CModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)5 v. l7 l* C) ]. |5 ]$ G
{; ?( Y. ]2 I5 v, E# m" x
m_handle=INVALID_HANDLE;
$ M* r5 I8 S1 B9 V+ i' ?- Um_symbol=symbol;
2 m8 r, j2 ]  a8 B4 \4 r9 V& x3 Em_period=period;. q: b( z8 S$ ?+ Y
m_next_bar=0;6 u+ F5 p6 s! e1 d* g& x
m_class_delta=class_delta;3 G, S( C' O3 K# u6 n* K
}, y# u. G5 L- F1 h! p
//+------------------------------------------------------------------+
% {7 u! ?" r* _7 j, _//| Destructor                                                       |3 g! k" n. y2 {. d; ?
//| Check for initialization, create model                           |: e$ Q  |( e$ i: b
//+------------------------------------------------------------------+
7 D+ ]; K1 Z# b$ obool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])
; X& \; g% Z( F! X- W; P( U{
7 b- K) L! Y5 N7 P2 k//--- check symbol, period
$ R: A" O& g, B: t9 i, jif(symbol!=m_symbol || period!=m_period). E# b( M  i0 o' D: m) a. ^# I0 C8 @
{; P; s' z* Z. k6 F3 s' i, q0 g4 j
PrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));
! L8 J. u8 D& V5 t5 Preturn(false);
: h7 c/ @( h2 P# _3 `( H}
2 C" r+ j4 L! x. G" f//--- create a model from static buffer8 R; z* N6 o5 V3 a6 b
m_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);: c  z1 M( E% s' K- J1 N0 V9 B
if(m_handle==INVALID_HANDLE)1 Y0 z! a+ @! A2 v* a7 [! N
{
: E* H2 S/ e( j& P* ]9 BPrint("OnnxCreateFromBuffer error ",GetLastError());- s+ a7 Z5 Q" ^* O8 @. s) Y
return(false);
- I( Q, Y+ S6 \: p}
5 J' _6 i% z7 `//--- ok& |& s9 f6 L6 r9 R( _
return(true);. {6 j7 M" X+ h) D
}
' c& y3 N9 t% c) U  m6 o//+------------------------------------------------------------------+
3 g! R0 p' K5 n/ N; N; f# Zm_next_bar=TimeCurrent();6 p# r/ E, V0 j5 ]8 f4 ~
m_next_bar-=m_next_bar%PeriodSeconds(m_period);5 {( B( ]' U2 K$ H3 d
m_next_bar+=PeriodSeconds(m_period);; a4 d% t& z  b" \* d
//--- work on new day bar6 t: S/ H! e( M% m
return(true);/ U: W, U/ N+ s9 {
}9 I3 s( n0 |! p' i. B! K
//+------------------------------------------------------------------+0 Z1 n7 r* o7 l; y  X& @
//| virtual stub for PredictPrice (regression model)                 |
0 Z( X: S# ~" A0 k$ n/ Z3 T//+------------------------------------------------------------------+
+ f  u" e7 J9 v6 C2 cvirtual double PredictPrice(void)
* j2 i6 `2 ~5 A$ u) V{- L: _: w* k+ l. @+ j4 S: Q4 R0 }
return(DBL_MAX);
2 U$ `; \4 S- o* V}
9 {: y1 D4 @0 j( \& Y+ B//+------------------------------------------------------------------+. i7 G$ V- v. f, H8 I. a
//| Predict class (regression -> classification)                     |
  D; G* w, A" ^3 [//+------------------------------------------------------------------+7 X2 s8 q) v" H
virtual int PredictClass(void)
, R- M5 h; B% x9 `$ Y# w/ s7 z) O{
4 |& f1 q! V( k; |9 W' ]& }: ydouble predicted_price=PredictPrice();1 ^8 Y  Q! @+ M0 E" m
if(predicted_price==DBL_MAX)
  K: p, ^6 z- n4 `: _# a; O9 ^- t  xreturn(-1);
5 J. I, A) i7 n! f$ p( e" sint    predicted_class=-1;4 H, D0 ~! z. f0 X% n7 V1 K
double last_close=iClose(m_symbol,m_period,1);7 n( q1 E7 W8 `5 \/ k% J2 H
//--- classify predicted price movement
  R9 A; K+ [6 ?+ edouble delta=last_close-predicted_price;  x# Z+ c! `! U* r2 A
if(fabs(delta)<=m_class_delta)( [& ~( c  X- \
predicted_class=PRICE_SAME;& ?- x; Q$ N) T! V
else
. y( P- j  r7 P2 iprivate:5 F# ^- P, M% i
int               m_sample_size;* I$ q. }) j% {' |
//+------------------------------------------------------------------+
" H8 v# q% f$ L, z9 e4 N- Svirtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)0 E; q- w& H& ^( Q
{& q9 ?8 s+ r$ \
//--- check symbol, period, create model+ S" Q& B. S. R. C" [
if(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class))8 ^, h: I2 H, ~- F+ x) r  e
{
+ P) n; l1 w5 p1 I$ {) QPrint("model_eurusd_D1_10_class : initialization error");- E& @( t3 G* ]6 t. W& a
return(false);
  o# s. ]! o9 N) z% A% a}
8 ^% Y& i  O' a( L. w& Z' L//--- since not all sizes defined in the input tensor we must set them explicitly
4 k2 [) C9 ^! q+ ^//--- first index - batch size, second index - series size, third index - number of series (OHLC)1 Q8 A+ r/ J& L- B& O
const long input_shape[] = {1,m_sample_size,4};1 ]( m; j) U% ]; }8 q3 e, H
if(!OnnxSetInputShape(m_handle,0,input_shape))! g; G1 t3 L2 {$ ]6 u7 t" V
{0 K/ r  k6 @; c0 p# w& S
Print("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());# i& d% C) E$ j- r$ a  @0 ?7 G- T
return(false);0 y$ \) B) r% d
}8 U. }9 n& r  E3 Z+ s1 c3 y$ T
//--- since not all sizes defined in the output tensor we must set them explicitly* o2 ]. Z6 I: c% O
//--- first index - batch size, must match the batch size of the input tensor: P7 b3 j; m5 D/ R, P3 H# Y+ v
//--- second index - number of classes (up, same or down)
) Z+ m/ v5 s! h# R9 l# Xconst long output_shape[] = {1,3};
* [' g" h, p  I. X+ |8 @' pif(!OnnxSetOutputShape(m_handle,0,output_shape))9 A) j8 s! `8 w, [1 t9 r" f6 Z
{
7 N7 V7 S1 d' uPrint("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());
  U1 b  H- l( r, h% B% q* creturn(false);
( i- J! M/ d' O2 k! @7 I& f  M& j( G}
" N" v+ S* X- {7 u//--- ok
% U! Z8 e- S3 ]) B1 Y- Preturn(true);
* |5 n3 Z; G2 j9 x}+ F' `. Y; M6 K- H2 F
//+------------------------------------------------------------------+
2 S( @2 g$ |( b% l8 y//| Predict class                                                    |2 J; T# M! s# I& Y) k0 t  O
//+------------------------------------------------------------------+( B, h; D- ?1 B, ]/ \
virtual int PredictClass(void)
2 p3 y) L( V8 m& T{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-11 12:45 , Processed in 0.409983 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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