私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?& p; R) z: B0 `* l1 y2 I# p
在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。% W/ L6 |* ]5 f% ]& _2 C
我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进1 \7 k, I7 O* j! W! Z9 E/ v0 e. @  M
//|                                             https://www.mql5.com |! F" |" c$ g- `5 @  a
//+------------------------------------------------------------------+
' ^6 W7 m: k9 Q6 R//--- price movement prediction
; k% @2 L' y, h* z" A. u4 C/ }#define PRICE_UP   0
2 z: j$ L+ T# g% s6 m, {#define PRICE_SAME 1
8 {. i+ v0 [0 f, Z1 u#define PRICE_DOWN 2
, r+ p4 p: y. q3 `; G3 W3 r//+------------------------------------------------------------------+7 t' B- n/ `$ s% X) C6 [& @
//| Base class for models based on trained symbol and period         |  e) ?. ^9 c" B# d# V2 j( y
//+------------------------------------------------------------------+
* T- l  q- m$ I: h3 K# B, Yclass CModelSymbolPeriod/ o# P+ C/ W5 s1 R7 J
{
0 K& |) n+ z* Q. G2 q/ aprotected:
, [4 @" P2 E1 slong              m_handle;           // created model session handle
2 Z! d7 y% h* o7 h8 Vstring            m_symbol;           // symbol of trained data
' U5 x+ `6 X2 X7 x, R3 ^ENUM_TIMEFRAMES   m_period;           // timeframe of trained data0 F6 I5 L4 W$ Y- \1 {/ P5 Z* n
datetime          m_next_bar;         // time of next bar (we work at bar begin only)
' b( `' r4 u+ i3 j8 T( ndouble            m_class_delta;      // delta to recognize "price the same" in regression models
) K! g" U  l) ]1 Fpublic:+ \/ @. z0 u! X& o5 S1 u; ~$ n
//+------------------------------------------------------------------+
2 _+ O. j% Y1 H6 {//| Constructor                                                      |
( B/ H: D* @* k2 h//+------------------------------------------------------------------+
1 ^% v, H( q* z* u5 D5 sCModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)
& d! S4 n$ j2 K) s/ P1 \, l% D{
9 C6 L& N  f" V: mm_handle=INVALID_HANDLE;
7 s* s9 s) r5 H- Q$ q1 dm_symbol=symbol;
. q3 \+ ~1 |% {, Hm_period=period;
7 j. ?  v: B6 M1 Vm_next_bar=0;+ y7 X8 L3 W0 ?2 `* u6 O. C8 ?# l
m_class_delta=class_delta;
1 W1 U: w0 \$ w4 u; `, ~}
" D! }, k8 V- s1 O: r//+------------------------------------------------------------------+
  x! I( p6 K+ b$ Y//| Destructor                                                       |
$ a, I  q" i* j" b5 C//| Check for initialization, create model                           |
) U6 s  g! {. q) F4 f+ O  s//+------------------------------------------------------------------+
, ?1 v% Y4 o1 U) Z/ i# F: |bool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])
4 c: i  r" k* {{7 p' ~, ?4 x9 b. X
//--- check symbol, period
7 P3 U) W3 J1 {5 ]# r% ?# `if(symbol!=m_symbol || period!=m_period)" B% l+ u$ O3 L  S/ ?5 {
{6 x. U7 E+ \) D
PrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));) T( ^+ N9 K* N
return(false);
8 ~6 x) i$ ]0 o9 |3 n}
4 B* Z2 ^; f& }# c! J' h3 t//--- create a model from static buffer
' N2 N" u9 Z  z: X8 ym_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);4 N* |( S+ f. i9 y( ]( o  N2 H
if(m_handle==INVALID_HANDLE)7 Q# k! `6 k( Y
{6 m. Z8 l! Z; q7 U& Q' W6 q0 S( w
Print("OnnxCreateFromBuffer error ",GetLastError());' I% Z. W) s6 {% r- A
return(false);% j5 G& V# K! v- l: d
}  \# x* {' d$ e
//--- ok: Z- t# i5 r$ c7 _% L# G
return(true);* m8 X$ K1 G6 w! i5 Z3 E" P
}6 Q* w* V) B# m9 g( s2 s
//+------------------------------------------------------------------+
3 Y; Q1 S1 D, u: A5 R$ om_next_bar=TimeCurrent();3 u8 T% z/ J9 i& o
m_next_bar-=m_next_bar%PeriodSeconds(m_period);
5 \3 j; a& t/ }" \9 em_next_bar+=PeriodSeconds(m_period);" I9 q" T- t3 s: X" o5 P4 b5 Z
//--- work on new day bar
" L7 c5 F5 t, G$ t% n3 ereturn(true);
! y! \7 l4 E/ T}
4 w/ A+ U; `& G8 y8 _& ?//+------------------------------------------------------------------+
2 ~4 d, E3 o4 o, _: g, j: x* i: V//| virtual stub for PredictPrice (regression model)                 |
: g  F" K/ k7 u) L//+------------------------------------------------------------------+
( R* N6 i7 y! Y$ z8 [virtual double PredictPrice(void)
1 h- m6 @- h4 j( O& Q{( }2 M2 @3 D. f) T, ?% Q" B
return(DBL_MAX);
$ V+ M- y: J5 A7 p}$ w) v8 U- A9 U$ _' f# ^) ?" ]
//+------------------------------------------------------------------+
; _+ e; X" Y5 B  u# y//| Predict class (regression -> classification)                     |
- u% _2 ]4 }' N  u; }$ J! Y//+------------------------------------------------------------------+
( C* c& M+ n1 d) D  fvirtual int PredictClass(void)6 q% H$ r% D; T" @! @8 e5 S
{; |! O6 }5 [( |3 z/ y
double predicted_price=PredictPrice();3 e* |- Z- D) x: Y
if(predicted_price==DBL_MAX)) t* i8 F! c5 \5 F
return(-1);; A5 J9 x0 g6 ~7 }0 X3 [
int    predicted_class=-1;
) a9 o  Z( G! u2 q* ?  \double last_close=iClose(m_symbol,m_period,1);
0 `: X# B: @# K. H4 |" u, F+ J; f, _! B, k//--- classify predicted price movement
& r0 _  A$ L. D5 j' zdouble delta=last_close-predicted_price;
/ K( T2 o. t# v: F% i+ M. Sif(fabs(delta)<=m_class_delta)
9 p) v* V* J  U2 Y( i) F6 s: Epredicted_class=PRICE_SAME;5 I) k: J3 |9 o1 Q3 D9 M* v
else
" n' M0 Z: b3 l" m) h: e4 l* Q( m! U& |private:* k& o6 f4 f1 e8 F
int               m_sample_size;
2 _0 V# R  f+ f  j; a  j; m//+------------------------------------------------------------------+& |' l. [; G  k! @
virtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)
5 [0 T4 u; r) }# P( q+ S{9 m, }- f7 U# m/ r
//--- check symbol, period, create model$ F# l" z3 M( [* b% o) P& Y
if(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class))
. `) q% f$ [5 u2 W6 i+ L! [1 C6 w{8 @: x8 L# |9 Z2 a5 `# j' k! y9 o, k- T
Print("model_eurusd_D1_10_class : initialization error");
& s6 i$ Y- _" b0 a8 Creturn(false);
& Q, e, F3 P( @4 L8 j5 I}
4 a& `6 J+ u  e1 d* L7 Z0 g//--- since not all sizes defined in the input tensor we must set them explicitly
0 ^8 t' H! O( Q$ {3 i' Y//--- first index - batch size, second index - series size, third index - number of series (OHLC)
6 T# `# {) A' h& ~. s6 @! ^8 Jconst long input_shape[] = {1,m_sample_size,4};4 V" h8 N) I/ q+ c. p1 N( m5 x4 s; N
if(!OnnxSetInputShape(m_handle,0,input_shape))
* f5 e+ o( D6 Y9 i+ d{
2 x3 T. F) Y+ i% [- [Print("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());! l# ^& G8 y" U: Z" F1 m1 H2 B
return(false);* n! w2 R- O2 Z: O) K) O
}) z& m' r7 V- t$ L# u- |; e7 K
//--- since not all sizes defined in the output tensor we must set them explicitly
/ Q0 V+ F$ F* h2 i! _//--- first index - batch size, must match the batch size of the input tensor% C/ H: h5 a% I' i$ H+ Z+ u- @
//--- second index - number of classes (up, same or down)
; Z3 H3 U  C. e1 b" Iconst long output_shape[] = {1,3};
# u1 ]: g( g: t0 I: Dif(!OnnxSetOutputShape(m_handle,0,output_shape))4 D8 U( R- o3 W4 G9 B
{
( @2 w9 s* w1 L! }) jPrint("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());
3 C- O% r0 ~/ i6 o* P4 q" greturn(false);
2 d# c% j! Q  L/ T4 e}
: U* Q- V! a- K+ f4 R; K! R//--- ok* P9 e" r5 r  U8 F% j0 Z& D
return(true);
6 i) i9 C, C# T}3 h8 Y& v; o) K
//+------------------------------------------------------------------+, {2 L; V/ P: O3 }% I9 b6 W# x0 q
//| Predict class                                                    |+ a+ N& B% B# Z8 w# r, J
//+------------------------------------------------------------------+
+ Y7 n: |  L; j4 l3 Kvirtual int PredictClass(void)" t9 h4 E* c6 ^$ w
{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-9-18 19:59 , Processed in 1.162247 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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