私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?
; j2 T, d# z/ J在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。6 o1 I# w/ m9 d9 i. a
我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进
# t- B: I5 L( A" {4 w8 ^& ?$ d- k" Z//|                                             https://www.mql5.com |
" G+ n" l4 J. B' \; `& t6 e+ r" _//+------------------------------------------------------------------+5 m5 m1 {  C4 `; P
//--- price movement prediction# w& d6 D. e# T8 x0 A. p! T+ X
#define PRICE_UP   04 `+ N8 o# G1 r) |' ^( l1 M3 W& M
#define PRICE_SAME 1
" e' I- c' J$ I  A#define PRICE_DOWN 2, D# M  r% I0 G1 Q; ?: c
//+------------------------------------------------------------------++ W( {2 I% S' g  g( c+ x/ a/ [
//| Base class for models based on trained symbol and period         |
# O3 |# p. [  \" ^//+------------------------------------------------------------------+. o6 H9 v7 g$ ^" m! R8 k
class CModelSymbolPeriod
! R/ b# q. K$ X% S0 m# @{
; H$ b7 [  t; V( Bprotected:6 }" m1 J: ~# q; P% N
long              m_handle;           // created model session handle2 f9 t4 z! `: x" b1 C" ^# {9 p6 W
string            m_symbol;           // symbol of trained data/ h$ ?- {5 r( h
ENUM_TIMEFRAMES   m_period;           // timeframe of trained data
! _4 G' h( b. D  r; d. {3 X$ y. Ndatetime          m_next_bar;         // time of next bar (we work at bar begin only); s- y' M6 @; q) k) w* J8 j
double            m_class_delta;      // delta to recognize "price the same" in regression models" j$ f9 R! N! D' r) q
public:3 U! A+ y0 j4 H( D/ C
//+------------------------------------------------------------------+" B1 t! J2 |9 S0 G2 o" z
//| Constructor                                                      |% Q; ^7 Z4 ?  j2 G! f" |1 v1 G) s8 I- z
//+------------------------------------------------------------------+% ^2 d" O3 }; l' ^5 U
CModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)6 D( B! I5 u0 n0 S/ `; N
{
" ~' d3 y9 m) Y- n/ n" Om_handle=INVALID_HANDLE;
6 C4 V. G5 u: g% K2 _m_symbol=symbol;; a  J8 I1 k6 I2 w! x
m_period=period;
! s; W* v/ C: A+ ?m_next_bar=0;0 ?7 M# l; }, W7 R$ n0 S: U
m_class_delta=class_delta;
: a' E, ?. k5 F, d* e* }}! i% S9 P3 O+ H& V6 `6 ?
//+------------------------------------------------------------------+3 p/ O, h$ z4 S3 t& h
//| Destructor                                                       |
& B, \: e3 l6 |0 f; e//| Check for initialization, create model                           |
$ k8 M/ a) D; H. u+ N//+------------------------------------------------------------------+! q& W& z, U9 G& V4 ?, D
bool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])
# O  }4 h9 `6 p, r; |{, p' E9 d  J: K' F- Z( b) g' o
//--- check symbol, period* }2 O# `) V& h# {" q
if(symbol!=m_symbol || period!=m_period)8 c) a$ `5 i7 f% p- h
{
; ^/ c4 I4 a* m1 RPrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));3 x& o6 b0 L5 Z
return(false);9 N1 ~8 ~; n1 l* F$ t
}1 ]+ |9 ~3 Y; t+ M' b& v3 L
//--- create a model from static buffer
! R3 u+ [7 ~9 W: _m_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);
) d7 T# q& ]2 R: z4 V2 d& }if(m_handle==INVALID_HANDLE)5 H' o. @1 L3 S! Z
{; p* n" D! Q0 K" q
Print("OnnxCreateFromBuffer error ",GetLastError());
* Q. v" c) K% e; e6 V$ Zreturn(false);5 n9 l! T+ \* ^2 m% W
}
, Q& W! P0 Y. A& V4 u//--- ok) K5 J' H7 Q7 `8 b- `' z  H! w
return(true);) a/ `5 U" a6 a6 j" A# l
}& c! L7 p; {" U5 m) s1 H& Q4 Q
//+------------------------------------------------------------------+
7 S1 r" o5 i% M( F& qm_next_bar=TimeCurrent();& S) y. u1 H2 _. e! [+ _& Z& c
m_next_bar-=m_next_bar%PeriodSeconds(m_period);
3 w5 v" i- @' em_next_bar+=PeriodSeconds(m_period);
) p4 v, H7 k2 D/ d6 o//--- work on new day bar
4 W, e1 N  h1 O/ preturn(true);: o+ T- v  f0 D, [. u( l# {& g5 f
}5 q" q1 `$ Q0 @
//+------------------------------------------------------------------+
  Y# _0 p% U: o3 _, r1 b- r7 \7 E//| virtual stub for PredictPrice (regression model)                 |
  N6 s3 J% g. t* ?6 w* \//+------------------------------------------------------------------+! }' W$ ~3 u2 w4 A) u8 V
virtual double PredictPrice(void)
+ g2 s) k; ]$ `{
9 L( S' o6 v: g: z2 Z  breturn(DBL_MAX);
+ |/ E0 D" X# D" ^, X/ I5 z}7 p" [* S' e; ~/ {4 [0 P) c
//+------------------------------------------------------------------+
& {% b: U& {' J1 {- |# U, b//| Predict class (regression -> classification)                     |
" _2 y( J% D: |) B+ O) r//+------------------------------------------------------------------+) I9 q- i/ A) G# G% U6 {* O
virtual int PredictClass(void)% w# d7 W7 _5 A! h1 L" z" T
{
/ m* J0 R, B+ z% Bdouble predicted_price=PredictPrice();1 v3 R1 Q# S+ P1 @4 x
if(predicted_price==DBL_MAX), C4 K- L2 b$ Q& J# z* J) ~: D
return(-1);; c2 Q" _$ i$ j6 Z0 u
int    predicted_class=-1;
7 G  o1 p' u6 C* S% zdouble last_close=iClose(m_symbol,m_period,1);
& @% |5 G0 f5 Y. s% J& ?//--- classify predicted price movement9 h# |! {- K' k7 J2 ~4 I
double delta=last_close-predicted_price;
8 {& l% o6 p8 x+ xif(fabs(delta)<=m_class_delta)6 F  {$ a/ T0 k; W; w. ^# |. `2 t7 k
predicted_class=PRICE_SAME;
8 V# h# W# F! R* k3 Relse
; z0 s! g: V0 R9 y) qprivate:; j" a2 }/ U9 g5 w' @' A& y- l! g
int               m_sample_size;! X- i* t* Y  D$ a; t8 v+ j/ j
//+------------------------------------------------------------------+
3 X! N* L9 g/ Dvirtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)
4 y9 R* v+ L& R7 N5 S' s{
" J/ q  I% ?8 y( |5 w4 A//--- check symbol, period, create model4 L, C# B3 x& g7 @
if(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class))" G% k: g  h+ F6 ?" c
{
6 l1 A5 Q! B: z, NPrint("model_eurusd_D1_10_class : initialization error");
. j4 p) U# |7 F7 @1 a, Q9 Y- Rreturn(false);# w: p! s  {" b$ i1 P
}
) m0 B3 P& \7 z; O8 z1 g//--- since not all sizes defined in the input tensor we must set them explicitly
1 T, C  R& T7 k//--- first index - batch size, second index - series size, third index - number of series (OHLC)  c* K4 b, z# i9 Q
const long input_shape[] = {1,m_sample_size,4};0 e" L( K8 u, q0 L, a' o" f
if(!OnnxSetInputShape(m_handle,0,input_shape))
5 M; `% z) V8 a{
( z0 o% @! N$ b6 r: {+ wPrint("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());8 n7 `/ I( P" N' z+ ?
return(false);! V1 a: F" X- J; {
}
: z" s5 j) m, [7 J; D$ W//--- since not all sizes defined in the output tensor we must set them explicitly7 g' d1 u3 K2 f9 ~3 |
//--- first index - batch size, must match the batch size of the input tensor9 f3 F" s: n* r# C2 q1 |( N% s7 o
//--- second index - number of classes (up, same or down)& {0 e4 o$ K- O& w% ]
const long output_shape[] = {1,3};; L- F8 f$ r/ o7 p! F. `
if(!OnnxSetOutputShape(m_handle,0,output_shape))3 ~' C. x0 d3 b" d2 L+ `* Y0 T
{8 Y) ]- e+ E. H5 b
Print("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());! C- T' K) E5 T3 ~, `
return(false);" C8 K2 M7 E8 \+ S9 a
}
3 V8 G5 S/ S" Y- c& U$ Y# D4 K2 M//--- ok* f) Y) Z! i. M" h
return(true);: Y7 f  F) a7 d5 f: l
}! ~7 ?5 J" K0 U8 }" \
//+------------------------------------------------------------------+: r  u; W6 p9 Z! y+ e8 c, L& [: S
//| Predict class                                                    |  D( A- V, C8 H, Q# r
//+------------------------------------------------------------------+/ I# P. q! D! ?9 v) ^
virtual int PredictClass(void)1 f- g8 d  d8 g1 _3 k
{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-12 16:31 , Processed in 3.002088 second(s), 32 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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