私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?
" W( M' `+ a" I: p! s4 J在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。
8 y9 N' n* G5 v1 z我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进# E3 X! I/ E( X+ h- i. N" d3 H8 |
//|                                             https://www.mql5.com |2 i7 K; C# p" H
//+------------------------------------------------------------------+
9 L  O! A# D$ K% _# L1 ^//--- price movement prediction
) d& o) j0 V! V6 z/ m: J#define PRICE_UP   0
* R; x# Q$ u9 D: s- {$ ?, `- v#define PRICE_SAME 1
. B, N( Y" j! o; F* B5 _#define PRICE_DOWN 2
4 h7 O7 ]9 b4 n% d1 `0 N9 d% M//+------------------------------------------------------------------+
: T' D+ W! J# T" S//| Base class for models based on trained symbol and period         |
) @' U( \" A/ s- n//+------------------------------------------------------------------+
) s; e; c, i4 E+ M8 Y0 Z* Qclass CModelSymbolPeriod% r' H4 O5 D, n; h* T; _+ x9 P
{; u& z$ @: X& s/ R  O! g
protected:
$ V* {# G! y4 u8 d3 A' M( M5 C4 v- hlong              m_handle;           // created model session handle- j. j7 x  d% ~5 Y' g" c
string            m_symbol;           // symbol of trained data( I3 w' \. Q. b; a# J  J6 C
ENUM_TIMEFRAMES   m_period;           // timeframe of trained data
( V$ m8 ]! ~% V# `datetime          m_next_bar;         // time of next bar (we work at bar begin only)
2 i& k/ [! G% I. I9 |7 _double            m_class_delta;      // delta to recognize "price the same" in regression models
* j2 {7 t% D' ?" D% I8 I! Xpublic:$ }+ g* U$ q- Y/ W0 r8 I+ H  ~
//+------------------------------------------------------------------+
' V6 q$ o2 c& S' e! v; \//| Constructor                                                      |
* {3 z) [8 ^6 d  \% h//+------------------------------------------------------------------+3 t6 u1 N* J5 c
CModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)
. \( c' h8 j0 w7 B* v. Y$ H{% G7 P" r6 J& S6 K  A& [+ X8 p
m_handle=INVALID_HANDLE;
+ n; b  P6 R" S! [* p; wm_symbol=symbol;) c( J' a' ?8 Y" Y
m_period=period;
$ B0 I3 U2 a3 ]* Dm_next_bar=0;( _& e: G5 w; m% |4 T2 s
m_class_delta=class_delta;
. ^6 [, J. C, b5 A, m: I- x4 P}$ ?1 ?, C1 O/ g' L6 l, a% l
//+------------------------------------------------------------------+
' l6 G' H( J, y) ^, N- I  o//| Destructor                                                       |
2 C: O% Y" V+ o& ]4 f9 T//| Check for initialization, create model                           |
: Q7 y% ~5 k  B5 n; Y8 `' M% q6 B//+------------------------------------------------------------------+8 f$ q. G# ?  P" g1 ^
bool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])2 u! Z7 `, D8 W* c8 N
{
, a2 o. u& G% Q//--- check symbol, period; i4 V; s  L% ?7 o" u+ ?1 A
if(symbol!=m_symbol || period!=m_period)5 r3 I% J- N& S; b- r  P  J  m; t2 t
{
/ y8 Z! J  ]( T0 R: u7 M  iPrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));
' n: A4 {# h+ N5 }0 C* Greturn(false);
$ X. A9 b& i6 [' b}
7 o' ]9 o9 B9 \/ F" g! P//--- create a model from static buffer4 Q. i& D- d' j2 K+ `/ F/ v
m_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);
; h% w1 C. O2 r7 d' |if(m_handle==INVALID_HANDLE)
$ E( K. _) v7 O& o1 g{
' M$ d- f0 k: m7 ^- o  g  CPrint("OnnxCreateFromBuffer error ",GetLastError());2 ^  B+ N! x4 X/ k$ z$ t5 X
return(false);' N: g3 S! w8 `# v
}
- N6 N+ w( a4 S! T, Y//--- ok
0 i) v' Y1 o. Z2 Yreturn(true);: \! g0 }& ~, \
}4 K3 o8 d9 c* W6 k; U* ~
//+------------------------------------------------------------------+* ^6 E# H. |8 B5 n( X! W
m_next_bar=TimeCurrent();
0 T) R7 ^( J/ A& O0 v/ Mm_next_bar-=m_next_bar%PeriodSeconds(m_period);, e  r8 \9 u! R3 h: M
m_next_bar+=PeriodSeconds(m_period);
% _2 s) F  s: e4 {5 T$ W- r# h1 y//--- work on new day bar- I* h9 |' u. p% i; S2 T
return(true);
1 j8 f  h) c$ |" t}
+ k  H+ b! K9 F//+------------------------------------------------------------------+( B/ f7 R6 {+ |7 I8 W
//| virtual stub for PredictPrice (regression model)                 |
: e; \, I3 f% k5 {, ^//+------------------------------------------------------------------+
5 p- U. Z1 e8 f9 I6 V/ R! u$ dvirtual double PredictPrice(void)3 f! \2 M! Y) ^+ @( {  W
{5 O% n5 D/ b8 \" F
return(DBL_MAX);5 w0 r" D9 W- c
}6 E4 q$ y  P; N) K- U
//+------------------------------------------------------------------+
6 w+ q* B, J4 [# F//| Predict class (regression -> classification)                     |+ v, W$ g3 H) ^7 z/ c. f
//+------------------------------------------------------------------+7 B" Y4 P8 ~) H0 r7 s6 F
virtual int PredictClass(void)3 G, V3 H9 J* I+ S3 \; _
{
& @% C+ X- s! x( _/ @0 Vdouble predicted_price=PredictPrice();7 o" R8 U7 l, F/ k/ g  z2 U
if(predicted_price==DBL_MAX)+ q; _# C6 t0 a
return(-1);8 H: @" ?, B+ ~0 V9 P& s& z: G
int    predicted_class=-1;! H* f* K/ K$ p% \) ~% w& r
double last_close=iClose(m_symbol,m_period,1);: s9 B+ D# V; ^2 m" h! ]' }
//--- classify predicted price movement
4 W3 G; S- T' ^# j1 h" sdouble delta=last_close-predicted_price;0 A2 U& @. J8 J' O8 x  [
if(fabs(delta)<=m_class_delta)
0 @& y9 S$ O4 e8 [predicted_class=PRICE_SAME;, O2 G# x  j  D' O. }! k+ |4 {
else
0 A" p  }/ O3 B! Bprivate:
! ?. l6 ~5 O7 {; Z" K; Hint               m_sample_size;+ K7 H) ~; Y$ S/ u% L, `
//+------------------------------------------------------------------+4 W8 j$ @. z. `. {# i& [3 ~  o, k) a
virtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)* U+ w$ c- h3 K; S! M5 A  y
{
/ j1 i# X: L" `//--- check symbol, period, create model: T5 X4 y8 M! }+ O, N9 b
if(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class))  r0 m( v' N8 i8 V  {. \! [
{
" M& ~# E$ Z9 K2 W9 i4 k/ n6 }Print("model_eurusd_D1_10_class : initialization error");
; M& a/ N- Q# J3 T3 z; j/ ~2 f! ereturn(false);% q, L6 G+ x7 P3 n: W3 X( Z
}
6 I- x) w/ `: s7 Q% V! ?//--- since not all sizes defined in the input tensor we must set them explicitly6 V$ `0 S+ F" Z% a- T+ r( S0 [8 p/ z
//--- first index - batch size, second index - series size, third index - number of series (OHLC)2 j' j* G& Q2 _& F* P/ I
const long input_shape[] = {1,m_sample_size,4};" C6 j1 a) G3 t: x
if(!OnnxSetInputShape(m_handle,0,input_shape))( r$ |  Q% c! x" W/ @
{
* m$ C. g; j! ?: k; L* h1 F3 T- XPrint("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());
" O, Q- ]  F( r' _return(false);
- o6 g9 O7 @, F7 O+ c}
2 p: P9 K0 N8 q) M6 x6 {0 {//--- since not all sizes defined in the output tensor we must set them explicitly5 D! v0 M; Y( l6 [, N0 n
//--- first index - batch size, must match the batch size of the input tensor) O) C4 X% b; Y# g: j1 l
//--- second index - number of classes (up, same or down)
, z: \$ @9 X, r# i$ E5 Xconst long output_shape[] = {1,3};
3 {! ?4 \% s2 B0 N( Zif(!OnnxSetOutputShape(m_handle,0,output_shape))
! s% [5 O' R- {0 U( f' j( L{& h$ m9 {3 v7 `; ^" H/ x# n6 N6 }
Print("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());) }" G& |6 f+ y% ], {5 V
return(false);9 r; b& c4 g2 w
}" n- Q" s4 B8 ?, }
//--- ok
. L3 U7 n: U1 C6 Z$ l4 hreturn(true);4 ?. d, F: }2 O, v8 S, h9 |- m
}
6 k8 y3 U( Q/ X, ]3 v$ Q! n: ~$ g//+------------------------------------------------------------------+2 H0 X# a9 L$ i! d; F# p! M# c% X
//| Predict class                                                    |
6 P5 M0 t6 |+ M9 W  r//+------------------------------------------------------------------+
. v/ P1 E8 u' r: i$ ~5 j0 rvirtual int PredictClass(void)
3 S7 m$ o$ w$ Y0 g{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-20 13:35 , Processed in 2.743128 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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