私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?
( q6 V% m7 _9 M在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。; ^& V" F, Y" n2 g) m( {/ r
我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进
" W" n$ f6 Y+ u3 \% ~  e6 R" x1 j//|                                             https://www.mql5.com |- i: b# n9 r  M
//+------------------------------------------------------------------+/ b) w: g+ E6 n, i6 H3 J/ [3 f
//--- price movement prediction/ w! F1 f1 h( @0 {# }5 V* @
#define PRICE_UP   0
2 N! h" v. M. Y% G' C7 k- G#define PRICE_SAME 13 S, l2 i4 f- O1 u
#define PRICE_DOWN 2( I+ e, p/ r' ]" h
//+------------------------------------------------------------------+
- A+ B5 U' X- O//| Base class for models based on trained symbol and period         |
0 @2 d1 q; ]5 R7 ?1 |//+------------------------------------------------------------------++ R9 I4 }5 w: J3 ]4 o, n% p
class CModelSymbolPeriod0 S* |: H# w5 x& c; s
{1 q* c4 s' P( c, t# N
protected:; k; P! M4 N5 j0 I7 }
long              m_handle;           // created model session handle
8 N4 r" V2 S+ a0 f! M9 Dstring            m_symbol;           // symbol of trained data. y' L& q) O; ^+ }
ENUM_TIMEFRAMES   m_period;           // timeframe of trained data
* a# b; ~) i6 Q0 W) ~datetime          m_next_bar;         // time of next bar (we work at bar begin only)
! w2 J6 _' x+ ?& ^/ u0 Udouble            m_class_delta;      // delta to recognize "price the same" in regression models
6 m% X" o& V1 r: ], ?2 bpublic:
# R5 N1 N- a+ B1 V//+------------------------------------------------------------------+
4 a4 o7 h' Q/ z- i//| Constructor                                                      |
9 v* B8 R' I1 p//+------------------------------------------------------------------+
6 H/ e* |) j  p+ ~CModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)/ d: X) ^5 U" f9 v( r
{' i  [3 R. e3 h1 Y  X. ]
m_handle=INVALID_HANDLE;
; V  e/ D: H, T, N9 h9 I0 bm_symbol=symbol;' Z0 `: w% ?$ `. A. }- M$ O
m_period=period;( L  c( Q7 L+ b5 p' S
m_next_bar=0;
; H' A' l& d' x4 s$ Zm_class_delta=class_delta;2 [3 ?6 J9 w# U  R+ \4 w: P
}5 ], L' `# I+ \8 {% a! `" ^
//+------------------------------------------------------------------+
. `. ]# U" U6 j- `* A1 [' A//| Destructor                                                       |
- \+ {. V- Z) ^. U2 o# v7 X//| Check for initialization, create model                           |
  A! ?6 n+ }. \$ B//+------------------------------------------------------------------+& l& w+ o1 l3 R" d5 {) @
bool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])
; Z, h) J- Y3 c: {7 |/ u+ t7 J7 ^{
, ?; i7 G) M+ E4 \% E6 X//--- check symbol, period
1 ~; H. k' i  ~% rif(symbol!=m_symbol || period!=m_period)( z) j6 R4 H+ L" h% U2 q9 m
{' C) D6 ]+ x) O
PrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));% o$ a, j" a$ }5 v; T
return(false);! T5 Y$ b( s6 o, S2 V6 f2 U" @5 R
}1 W: ]: e2 @8 ^3 O$ Y& w' z
//--- create a model from static buffer
$ J' N+ c/ }6 ]+ _  am_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);
+ }+ N4 V# S/ i' g, P  Uif(m_handle==INVALID_HANDLE)( ^, i- W' V. {
{/ u1 L3 \" y+ P5 P
Print("OnnxCreateFromBuffer error ",GetLastError());3 n5 i$ R4 l( D2 c. h
return(false);
! B5 F9 R9 `+ B/ l7 n. s+ E3 ^}/ x* s. q- u7 G) n" d7 i: U7 C' b
//--- ok% p- a" I. d: a+ ?% q
return(true);/ Q8 v9 b  p4 G+ S8 n- Q% W
}
) c5 M* U8 e' \8 Q* Z" _//+------------------------------------------------------------------+% `8 {# F9 D! k. Y! c) [- @
m_next_bar=TimeCurrent();
# y% R; s2 E* P* im_next_bar-=m_next_bar%PeriodSeconds(m_period);- S- ~8 c% n2 @5 M( W( G+ i3 K, q( E
m_next_bar+=PeriodSeconds(m_period);2 I& t  B# S4 k3 S) @
//--- work on new day bar' m0 M/ q) O$ O9 [
return(true);. z+ n# {% s+ P) g4 A# h. h* y1 u% z
}& k$ }- B9 x! C
//+------------------------------------------------------------------+6 I# F; D  V- `' {  ?2 P* L
//| virtual stub for PredictPrice (regression model)                 |; P: b* i, f2 h- P9 G
//+------------------------------------------------------------------+
3 k0 X4 X0 U0 n+ d. M+ i6 Ovirtual double PredictPrice(void)
% b5 \' p% e3 B& @: T; d% z; V{
! d# F/ k6 u6 _5 V$ c: Yreturn(DBL_MAX);
% w& ~- O/ w6 j' L9 L}
$ _. M# {+ w# F% D$ u- D//+------------------------------------------------------------------+
, \  D! ?2 k. L$ A//| Predict class (regression -> classification)                     |( }9 a1 \5 e3 U* c3 C; e* @
//+------------------------------------------------------------------+
" q0 D0 x# ^: i5 g4 n$ nvirtual int PredictClass(void)
0 P; S3 A2 @* j) _& ^! Y& D{7 g8 C7 C' l0 E+ o1 {
double predicted_price=PredictPrice();) \0 x0 B* t7 J9 L; w
if(predicted_price==DBL_MAX)# ~( U) P4 o' Z9 k2 |
return(-1);
: J5 v1 @! ]/ W" ~" C8 |int    predicted_class=-1;. k  V: d% @2 I( h
double last_close=iClose(m_symbol,m_period,1);
3 X1 o; q! U# r2 `* j, C, e//--- classify predicted price movement! F' c% C5 O$ g
double delta=last_close-predicted_price;
) |: k: d! t. P& v) F" U: H" Sif(fabs(delta)<=m_class_delta)# T) o% E: y3 i8 R( T1 Y9 Y9 x; C4 I
predicted_class=PRICE_SAME;& T, Z; _; n8 ]" {
else
. S% N/ @& T; S- K/ |4 K5 rprivate:
9 o, Q' S+ u. Z- A; t; Hint               m_sample_size;! ~# K6 `* Z8 h, S9 `6 K
//+------------------------------------------------------------------+
% _( [8 Q! Q2 z: {6 l# hvirtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)
6 g; P8 j& {6 ?0 V# [{
' v4 ~* C% R4 j0 o, y//--- check symbol, period, create model/ k% `7 Q: Y# ^; b- q1 `7 d
if(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class))
! t* Y2 a  \; Z* y3 `( \+ T- U) I{
3 A  J! [& h" U& z3 cPrint("model_eurusd_D1_10_class : initialization error");
3 v9 R7 f) z4 e# m: y+ Nreturn(false);8 c4 F" N5 v6 W) J- R6 A6 r
}
2 y1 X8 S  V0 @& r3 v/ P9 ~//--- since not all sizes defined in the input tensor we must set them explicitly
. \# y3 m# Q2 @" v; e7 W' Q//--- first index - batch size, second index - series size, third index - number of series (OHLC)* O8 A8 ?+ U2 k( p
const long input_shape[] = {1,m_sample_size,4};: p- `* W" F6 U; N# ~8 }7 m
if(!OnnxSetInputShape(m_handle,0,input_shape))
2 K/ O4 d# ^/ d5 z8 e{4 n" _+ S$ {, U0 Y9 ^! n3 B
Print("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());
& s9 C2 ?/ R& ureturn(false);
1 X. `' c/ e! n  ?6 s9 ?0 C}! d0 F% G3 R( v# q3 {+ R3 v
//--- since not all sizes defined in the output tensor we must set them explicitly
% J4 t. L# e# W+ i( d4 _6 i6 i* Q! Y//--- first index - batch size, must match the batch size of the input tensor' G/ V% w$ z$ @- y* D
//--- second index - number of classes (up, same or down)
7 i0 G4 \% f8 S& Yconst long output_shape[] = {1,3};* W' ^5 V0 L& G+ I: r9 y% H
if(!OnnxSetOutputShape(m_handle,0,output_shape))! @( c  q5 ^* `
{
- m  z' z; z9 L, ^  E' q% l3 ]Print("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());+ ~4 K1 @1 O, u
return(false);( u: U; E! U7 y) Z+ q
}
  ?0 P: a6 A5 d: N+ c8 \# f//--- ok
8 v2 w5 V* I! x$ Yreturn(true);
5 O- U+ p* c) q' e/ b% a}
: P0 E9 _) i0 b) y6 g9 y, ]//+------------------------------------------------------------------+
8 s+ Z/ h' {8 i  d* w& s8 b( G//| Predict class                                                    |; e/ u% ~8 Z& m) n" g& a
//+------------------------------------------------------------------+
, N! {! l. U: O" u' S& V- ]virtual int PredictClass(void)5 o. C& L1 U& {1 A$ V
{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-23 20:23 , Processed in 0.435589 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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