私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?
9 h2 ]0 ~7 v- I; Q在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。
) X7 Y9 L0 L( ~- T我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进
5 K/ ?" a! e$ o* R. K# W8 t//|                                             https://www.mql5.com |
6 ^% m& O7 T& E, d; }  F//+------------------------------------------------------------------+
0 ]0 v2 D; k  z& M- |' Q//--- price movement prediction
# e$ f: E5 k6 r8 U! |#define PRICE_UP   08 K" L, Y! F/ Y2 L6 [2 m& r, W
#define PRICE_SAME 1
9 P2 C: A' K  H0 ~# X+ R#define PRICE_DOWN 27 S% A) z  _3 w. t
//+------------------------------------------------------------------+2 @: U, |9 w$ t$ E! z
//| Base class for models based on trained symbol and period         |
. `. h) O; w6 f! c( g//+------------------------------------------------------------------+
/ _  F1 S9 f" b8 \& ^/ Oclass CModelSymbolPeriod
. R  ^. L; y% Q" @- b% A{3 b' U( V* t- D
protected:
) m: ~  W% y' O9 X) Z3 Y' H" ilong              m_handle;           // created model session handle2 n( [$ `, {2 _; I
string            m_symbol;           // symbol of trained data! i/ e' p- [* c, `8 u" Y- T
ENUM_TIMEFRAMES   m_period;           // timeframe of trained data( n* \2 h) |, r/ s5 W: f/ n1 j
datetime          m_next_bar;         // time of next bar (we work at bar begin only)
+ o1 J3 Q& r2 n* udouble            m_class_delta;      // delta to recognize "price the same" in regression models! j7 D7 H4 {& ^- k, G& S
public:
# D- g  j2 m3 c' I" m4 X//+------------------------------------------------------------------+
) S: H+ j9 C% o//| Constructor                                                      |7 K4 K9 y0 ?8 a# T- I4 s
//+------------------------------------------------------------------+. F7 x9 {. |% A& Y
CModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)
$ E- B1 n$ x1 Q- R2 O- i{
5 I7 U- n& `! nm_handle=INVALID_HANDLE;
& J+ |! Y6 I, Z, a) hm_symbol=symbol;- W) k: z* b8 s6 I" {. e% c
m_period=period;  E% d7 g5 |8 [8 I& Q: e" o
m_next_bar=0;
3 G& M1 y3 u& m" |+ }, d+ \7 w7 nm_class_delta=class_delta;* B+ T% [4 P& q" j
}
+ L! ]7 f9 y* w" G( j, z. w1 l//+------------------------------------------------------------------+% d9 U0 }! s/ L1 {, A7 l
//| Destructor                                                       |, m. w" _" y# t6 u
//| Check for initialization, create model                           |
: _0 l2 h  O: P/ i# }//+------------------------------------------------------------------+
8 `( B- P" ^, l6 y" g6 x% a1 ?bool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])
% y3 O% k- v3 }4 @0 O0 [' X{8 N- A" Z: h; f, g
//--- check symbol, period
8 Z7 q2 r9 I; t7 S! d. W5 B- X+ Qif(symbol!=m_symbol || period!=m_period)
4 x' r) [; a- h5 S{1 b: W7 ~5 Q9 c  ~. T
PrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));
! ]) |5 O* z* C9 h4 q  Dreturn(false);
$ O9 Q, J, e3 c2 z}) [" o% n' q8 L
//--- create a model from static buffer0 H+ v# y  n) D& n
m_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);) r" s9 ?) F6 Y/ p8 ]0 V- `
if(m_handle==INVALID_HANDLE)
" \: k0 x) j) r0 a' ^8 p{
: L/ M6 b1 g( {$ X  I/ f3 _Print("OnnxCreateFromBuffer error ",GetLastError());
1 X- A$ I0 }" v0 Creturn(false);
/ D4 q# J! d% s) Z}: m7 N: Q1 E% F$ L
//--- ok
- c: j8 l  r* y& O' oreturn(true);" _; T4 X2 C' \& Y9 t- r, x
}3 b  F, H- [5 j- j0 d1 `
//+------------------------------------------------------------------+
# ^$ h$ N) G4 A" J8 y. cm_next_bar=TimeCurrent();
$ O7 r8 q0 k' J# s9 e1 pm_next_bar-=m_next_bar%PeriodSeconds(m_period);
7 r! I! Y; Z& o# u/ X* e2 s5 |3 T% Ym_next_bar+=PeriodSeconds(m_period);5 _0 ?, u& l4 U# E
//--- work on new day bar6 X4 `9 c  z1 N; Q5 u* n
return(true);( @% v& A+ M5 Q: z4 n+ r, L1 D
}2 s. K! R8 b2 `4 j% a! e) ~$ I+ x, ]: _
//+------------------------------------------------------------------+- r1 a3 \6 _% d" m* C3 n
//| virtual stub for PredictPrice (regression model)                 |
& |* v; [( P  P; ]" D//+------------------------------------------------------------------+
0 y- X, Y5 {. i: [( ]virtual double PredictPrice(void)5 @& j+ P( F( q# S. @7 E
{
# N/ ~" b1 x- I4 U7 L, kreturn(DBL_MAX);
& o: a) ]' a9 |2 _  [/ P}
) U4 r+ m1 V' R) @) s//+------------------------------------------------------------------+
, Y8 @/ P' h6 l8 G4 g6 k4 Q//| Predict class (regression -> classification)                     |6 ~7 |0 n, T# B( _7 V
//+------------------------------------------------------------------+
2 J  W6 {: G5 I& Z( @virtual int PredictClass(void)$ p* |" T, T. H: R1 d( A$ \2 u
{
, x* b. [$ E) m& i, Ldouble predicted_price=PredictPrice();/ z. }4 D4 P6 y+ ^+ p4 S$ s5 l
if(predicted_price==DBL_MAX)
8 I/ D( E4 J* h! C0 L  Oreturn(-1);6 F3 C+ v. y! h1 x3 G
int    predicted_class=-1;
" i/ e( {# b" I; ^double last_close=iClose(m_symbol,m_period,1);; P* y$ R6 Z  a) m
//--- classify predicted price movement
8 \7 f  {+ A& d, F, w4 ~double delta=last_close-predicted_price;8 F4 L. a- g$ [; @  B% J7 g. a0 T+ I
if(fabs(delta)<=m_class_delta)
3 t- k3 Z# B, q# x+ Ipredicted_class=PRICE_SAME;
2 M* q  Z# a" r5 W! Welse
9 Z5 d5 |  o0 }private:
" I$ }- [; M2 ~" {7 Z% M; P( ?int               m_sample_size;
# e' R( T' f; ^9 l//+------------------------------------------------------------------+
4 O9 a9 u; K, U: S8 y  Vvirtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)' z+ ?  A! a. S7 V% j; R
{
! l( ^9 P' I% N( s& W# @; X: S3 r* Y//--- check symbol, period, create model
" k, s: `  A4 W; u+ z" r& Pif(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class))! S, f) R( a% l
{2 \+ a9 S! e  ~, `
Print("model_eurusd_D1_10_class : initialization error");: m; M  N6 l% _, f
return(false);
3 u5 T% h: W6 d4 C8 [& p}
- Q( J. i/ Q% e1 v9 r//--- since not all sizes defined in the input tensor we must set them explicitly2 x, R( ?( j% g; ~: _
//--- first index - batch size, second index - series size, third index - number of series (OHLC)3 E, x2 h1 e6 H# a* X
const long input_shape[] = {1,m_sample_size,4};
2 B% g1 z3 j) t9 C1 A, Vif(!OnnxSetInputShape(m_handle,0,input_shape))
/ D: a  V5 o% |  z0 q+ q' `{
: |0 D" Z0 K; kPrint("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());
* Y: Y7 h0 d) f/ vreturn(false);7 u' E# r5 Q+ P/ H  C( T9 y
}4 }- I" E$ V  d) b/ |7 M
//--- since not all sizes defined in the output tensor we must set them explicitly
+ [' c, l. f: d2 p/ F" l& {8 k//--- first index - batch size, must match the batch size of the input tensor
' \! {$ A2 u! u3 ^//--- second index - number of classes (up, same or down)# a; R1 {# Y' y/ B) c' i
const long output_shape[] = {1,3};/ m$ J8 T8 s: H! L5 A7 r3 @  E( R
if(!OnnxSetOutputShape(m_handle,0,output_shape))
' G2 `" D. N3 V- ~{8 A9 w$ ]" m( O7 |
Print("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());  q) a$ C4 p! @8 t  [
return(false);
% q( e3 p% I9 w4 v2 x3 X4 D}' @: O+ i# |8 B% h: r+ C; C8 i
//--- ok  X) n3 f. W7 ?3 b  A
return(true);
2 B7 m( [6 L, W6 G1 `6 G# D}
, K; y. T& b5 s: J8 j& G//+------------------------------------------------------------------++ u0 ^$ y4 a% B- r0 D9 ]
//| Predict class                                                    |
8 ^* R4 j' A* ]//+------------------------------------------------------------------+
) k9 O$ N8 o1 |' u) x$ Vvirtual int PredictClass(void)9 P5 F: k3 Y5 X+ C8 X9 q9 ?. K
{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-10 09:10 , Processed in 0.399795 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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