私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?3 \/ L( D* n7 D% G! B/ u& {, e
在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。
% x' i. \& T9 D0 n; j我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进% X8 I/ y* `) Y2 J3 v$ f8 l& s. F- \
//|                                             https://www.mql5.com |2 H# P; b( {+ {
//+------------------------------------------------------------------+
( r5 {) D# r1 m4 l: K. c, q- @//--- price movement prediction
" s* A8 a. g5 u7 L, \#define PRICE_UP   0
* \# y; @. ?$ ?" S#define PRICE_SAME 1
+ K7 H2 [9 q6 {$ ^; ^4 {' A#define PRICE_DOWN 2
. @6 Z+ Q/ ?1 n4 |- ~: b8 c1 X//+------------------------------------------------------------------+& G" a9 @  Q5 E  Z/ c: @1 j8 L5 l
//| Base class for models based on trained symbol and period         |# D* j1 `2 r1 K
//+------------------------------------------------------------------+* @. p0 ~  Y0 x7 f: A" H
class CModelSymbolPeriod
, a; e1 o5 S: k8 W7 l{+ \9 l# ]( K8 d6 a
protected:
# D- m8 D1 T: Z7 @8 _, [long              m_handle;           // created model session handle* B! P  d  f0 y+ l3 J1 j
string            m_symbol;           // symbol of trained data7 q. q4 f( L6 V! |+ x
ENUM_TIMEFRAMES   m_period;           // timeframe of trained data- s6 ]  S7 r" h% W7 l2 Z* U: m
datetime          m_next_bar;         // time of next bar (we work at bar begin only)  A# D& Z# m# U1 D
double            m_class_delta;      // delta to recognize "price the same" in regression models
. m/ S0 A- I# S0 O( g* T5 Z) G8 N& _, `public:
  d; O! d) [' x, o% z5 w1 F% ?//+------------------------------------------------------------------+
- N1 P7 u4 w4 q1 I//| Constructor                                                      |
7 m1 q7 {8 _3 {( B' W//+------------------------------------------------------------------+
: i) }  J# A/ ICModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)
* f# x6 k# |6 u% j0 S; |5 w! B3 _{. J3 o; [1 L8 b5 l4 u
m_handle=INVALID_HANDLE;
: C8 C8 y: `  E' c/ ^m_symbol=symbol;
0 W4 S; k8 W, pm_period=period;6 a5 h( ^7 q+ j4 m* x
m_next_bar=0;) ?0 z6 H: g  n9 k9 r
m_class_delta=class_delta;
1 B: Z3 r' e5 O) D3 I" e1 m}: C+ \$ w; _3 R% m
//+------------------------------------------------------------------+/ O# N. g& Y) |* S8 g' n$ }
//| Destructor                                                       |
3 Z! |$ M6 M8 n% W0 r//| Check for initialization, create model                           |8 B. g1 s9 o" U/ g/ K  i# z; g
//+------------------------------------------------------------------+4 i6 x( Y# M! [" L8 l. D: r* ?$ X/ {
bool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])
. S0 `5 z# i4 u, F& ^{
' c- \$ J: y' l" B//--- check symbol, period# Z+ w/ h3 Q, T; V- t
if(symbol!=m_symbol || period!=m_period)
# x3 T. V' e1 |8 e  a* X{
- e  {- M) D7 W7 @+ a  x' l5 g/ _7 DPrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));
3 f3 n  a. f- ^' n+ q% P* r. freturn(false);
. u4 T' w- k! J7 j}; I6 [; M% M4 p; b
//--- create a model from static buffer
9 y. U4 [! P$ Fm_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);
6 D* v5 C+ V5 h& lif(m_handle==INVALID_HANDLE)5 i, e( j5 h# \8 i
{
  L" F5 V$ h: m/ HPrint("OnnxCreateFromBuffer error ",GetLastError());5 c+ Q7 J, W9 F. d- e, Y0 _9 G- A
return(false);
! u2 j* H' R# r6 P9 }}! h1 ?% I7 V! ?4 b# }
//--- ok
, g  O6 m6 _9 q$ w7 \- Treturn(true);7 Q/ p& ?: I2 Q) ^0 O0 D0 P
}4 _7 D( z) s3 \( p! w
//+------------------------------------------------------------------+' _1 [+ L& X. I1 C
m_next_bar=TimeCurrent();6 N/ E; _+ [# q& B( K
m_next_bar-=m_next_bar%PeriodSeconds(m_period);* W: p2 g! |- R8 ~9 D3 i
m_next_bar+=PeriodSeconds(m_period);5 [& `3 z# H2 O1 d6 r5 u6 L
//--- work on new day bar
. S$ r/ b* T) f9 dreturn(true);
0 Z* [# K3 K" t6 M" ]8 y}/ _8 }6 R+ h0 E# E$ ]; A4 o* [
//+------------------------------------------------------------------+
) d# w7 T+ r$ n" {' Q3 V//| virtual stub for PredictPrice (regression model)                 |
0 N" z, y( ?" v7 L0 z! ]2 _; V//+------------------------------------------------------------------+
! J/ g7 g0 l: E- [/ x4 Y* ^! G# vvirtual double PredictPrice(void)
( G( c  h5 n/ g- c. o{
; G) t8 [# z4 U; k: {! b# w, Ureturn(DBL_MAX);
6 d* ]9 D  Q4 e, U}
8 L' v' w9 ?5 p5 l9 C& ]) v8 B//+------------------------------------------------------------------+
9 |& z% P$ q$ O" Q! E  u//| Predict class (regression -> classification)                     |
/ G" Y: X6 P2 [" ]1 l4 u7 {//+------------------------------------------------------------------+
8 J+ R! Z& c' Z8 c6 p2 d: S: Avirtual int PredictClass(void)
' R/ ~, \& }% j1 b{& X% x/ j$ A) j, f1 N
double predicted_price=PredictPrice();
3 ?: \. I. b! K! h, l4 y- y. Sif(predicted_price==DBL_MAX)3 ?' M& T, c. O
return(-1);
  s1 @+ g1 U9 z5 B5 uint    predicted_class=-1;" K) \# V% [! \8 c: v+ U% M
double last_close=iClose(m_symbol,m_period,1);
8 N$ R/ g$ N9 P* X" a& _( _* Z//--- classify predicted price movement
9 a6 E, u( b% X. I! e4 }6 jdouble delta=last_close-predicted_price;+ a6 {4 R5 z& e) q  e) W
if(fabs(delta)<=m_class_delta)
  C, ]: A9 S0 Z. |  A) ]  ?, o8 Lpredicted_class=PRICE_SAME;
- `  f& s( Q! V0 u0 ?else& O  f' p/ {% x1 d' \+ O; W9 _4 s
private:
1 I$ J: U5 ~. B$ X' F6 Rint               m_sample_size;
$ g" H: ~- R' G" T//+------------------------------------------------------------------+. Z) d* G) c2 f' w& ~3 A0 G; w' e
virtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)' |; F) G5 Z& M  z
{3 ?! a4 _* O; K
//--- check symbol, period, create model, Z8 R7 F3 F6 Q1 @1 G0 M! l4 \2 U
if(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class))3 p8 F# r' y: K$ x$ Q. ^) a7 ?+ I
{
& N- p1 j& e6 p0 F( ~$ \% EPrint("model_eurusd_D1_10_class : initialization error");! r$ ?# M) x9 h6 K
return(false);  }5 Z/ H+ A# e) p
}# h/ D+ _9 z! F/ @' }
//--- since not all sizes defined in the input tensor we must set them explicitly
2 n$ e. m+ |( X: Z$ y8 q! [//--- first index - batch size, second index - series size, third index - number of series (OHLC)6 q5 k& D4 R% m4 e
const long input_shape[] = {1,m_sample_size,4};
7 t! E0 K4 G+ R0 O  @1 V4 _if(!OnnxSetInputShape(m_handle,0,input_shape))7 P1 |6 U" M4 D- G- ^) ]# ?* B
{
% A* Q, \5 ^6 i! p/ a. f+ RPrint("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());
' c+ P5 O# I7 Areturn(false);% |' D: x' U# h+ Z
}
/ ^% ?4 U3 b& k4 z: p8 d1 ^//--- since not all sizes defined in the output tensor we must set them explicitly- T5 f9 J; D. o% A6 M
//--- first index - batch size, must match the batch size of the input tensor
9 s* Y, W: X5 }. w//--- second index - number of classes (up, same or down)1 |& R6 o1 L1 ^0 k1 D
const long output_shape[] = {1,3};
7 l) a/ _' _* j4 O/ d! rif(!OnnxSetOutputShape(m_handle,0,output_shape)): k6 ]1 g4 u. [
{! ]1 |3 [$ O9 A* I
Print("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());
3 n) `3 H+ X, D( t( ~/ Wreturn(false);
* |! [1 w8 ^# y2 s3 ^0 Y}: W% w/ \- f. _, o
//--- ok. X) Y- @7 y1 Q2 Z  H
return(true);4 ?7 `! }9 \" f" \* H
}/ B+ s6 P( [9 i# ]+ x
//+------------------------------------------------------------------+
) E7 Q4 ~0 Y6 J: a3 f. l6 Q//| Predict class                                                    |  _. M: O& H" [) R+ ?
//+------------------------------------------------------------------+
# n3 S( x6 y1 N' F! t( p6 r: C0 mvirtual int PredictClass(void)# P' |5 B# J3 R9 o1 `# T
{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-14 06:15 , Processed in 0.980405 second(s), 32 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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