私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?
+ j. s: E- t  d5 m3 n" b& Z在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。8 F8 m1 f0 d0 M4 W3 P; A. [
我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进
% a* i8 Q3 T- k/ Z//|                                             https://www.mql5.com |- f! P, a7 C; R5 L5 w0 m' }
//+------------------------------------------------------------------+
+ I1 [7 {- S# [3 P* E7 G//--- price movement prediction" L0 L; R* l0 u0 R* _# G7 |
#define PRICE_UP   05 I8 D- e4 m6 S
#define PRICE_SAME 1
4 J; A; K0 b3 B#define PRICE_DOWN 2
; Y3 l2 S& R) z# g5 H3 \//+------------------------------------------------------------------+' ?0 J, e% ^& m" E
//| Base class for models based on trained symbol and period         |3 z6 o* _! y) r2 \
//+------------------------------------------------------------------+
3 W7 |5 }' I; X" B/ Uclass CModelSymbolPeriod3 n( O& v! Y& [# o6 W) t
{& ?! c; e; u' i! K. y$ q' w+ Y
protected:" M/ T, F5 J7 a; D
long              m_handle;           // created model session handle# Z8 G1 T# h9 ^7 H. ]
string            m_symbol;           // symbol of trained data
5 h. O& l6 L5 l3 R1 n! [ENUM_TIMEFRAMES   m_period;           // timeframe of trained data
8 Q! }2 q2 R' @: U0 A; G" Z) `datetime          m_next_bar;         // time of next bar (we work at bar begin only)
3 Q+ b% w  j# B; e  i. ^double            m_class_delta;      // delta to recognize "price the same" in regression models; p0 B1 w7 N: g/ c* X! m0 V7 q: f
public:7 v' \7 ]: D& v# J1 e) N
//+------------------------------------------------------------------+) L4 ^$ r% Q6 H  P( t% L6 y+ a  c
//| Constructor                                                      |
: l% T. b6 m. p# R( h4 h//+------------------------------------------------------------------+, p  X$ ^! y, ^  |. k% U  n
CModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)
4 a- w  g0 a5 E{% _3 g! U3 }- n* o
m_handle=INVALID_HANDLE;6 Q9 s! E1 ~. v: n; U+ P
m_symbol=symbol;
: x7 V1 e: j4 G. sm_period=period;
5 h& W+ m( E1 W( |7 q# Gm_next_bar=0;
  y" h& c: G( K; W/ W7 X% |! b, Xm_class_delta=class_delta;
" P. F! t4 p" j; y/ _- w}
9 N  {" w- I3 K$ @//+------------------------------------------------------------------+( U5 b% U5 C% C% l& l$ Q  q: s$ K
//| Destructor                                                       |6 |% y3 m7 V* S2 G3 W6 m9 K2 P
//| Check for initialization, create model                           |9 u8 t  |5 Y/ `0 ^, B
//+------------------------------------------------------------------+
  r5 y( L6 J' S, j! r# B7 \* l0 Y- O, xbool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])/ E: O! I! z# n1 m3 |: l
{: j) t& ?3 {& }/ m8 B( q( N
//--- check symbol, period& |  p- o( m# P0 H
if(symbol!=m_symbol || period!=m_period)
. ~/ v% Y0 H& r{
; T" |* k9 ]8 {3 p+ a1 A, HPrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));
! J) V! y/ F( I3 ~; ~/ z, oreturn(false);# o  z/ `" R9 A2 y- B
}- G) \& X3 i- c. ]+ a1 _/ y8 U3 q
//--- create a model from static buffer. r( y0 i( B1 ^9 ~- H- r1 Q( c
m_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);$ U# D/ o1 y/ G- ?& ^- T4 I" x2 a
if(m_handle==INVALID_HANDLE)
& u; B0 x" m- @. @0 I' t* h{
( @1 M+ J+ S$ qPrint("OnnxCreateFromBuffer error ",GetLastError());
+ J( |, d* A% }8 i2 \" t5 Breturn(false);
5 i3 v4 O( d: J8 e1 }; Y5 l0 x7 T}
5 t% O7 t8 \" y- z. j" k//--- ok
/ B8 ^( R* B! B0 R# i& Kreturn(true);9 o+ O' B) Z5 n0 z
}
3 t6 o& c6 j) f//+------------------------------------------------------------------+0 k0 k) S! T/ Z! v2 M
m_next_bar=TimeCurrent();  R' f/ a3 Y. I6 P* \, t1 D
m_next_bar-=m_next_bar%PeriodSeconds(m_period);; ~, F  Z* m" T  s
m_next_bar+=PeriodSeconds(m_period);2 K* f& `. ]5 h1 m) a/ J
//--- work on new day bar
" h$ [4 _3 I8 Y' g4 F$ j. Oreturn(true);
, N. Y; e) r. c# U+ {" a$ r' K}* F/ w/ s0 E. R4 D1 d! e
//+------------------------------------------------------------------+
2 g# N) [4 ]7 `. k- T' U( J//| virtual stub for PredictPrice (regression model)                 |  c+ Q$ x  v% r- W7 Z4 ~
//+------------------------------------------------------------------+6 G# s- O; l; ]! A1 h6 ^
virtual double PredictPrice(void)
9 _8 f! S$ z) Z7 I' V: E( y5 q{" n% T+ E/ U8 X
return(DBL_MAX);
* s' j/ ?, L  f! H7 ]) _}
% a& O5 b# I5 _5 A//+------------------------------------------------------------------+& d  i! S+ ~( y% W
//| Predict class (regression -> classification)                     |
% [" ~! J2 T- |; F  F; N//+------------------------------------------------------------------+- c1 k4 e8 R( Q% T2 T6 r+ M! i
virtual int PredictClass(void)# j6 @9 a6 Q2 m# h
{
7 S  @: N) o8 H" X8 Gdouble predicted_price=PredictPrice();( M$ e/ m, @# E: u- m
if(predicted_price==DBL_MAX)( T$ \9 A! J9 O; A3 Z
return(-1);
6 `: |+ f: Z& f- Nint    predicted_class=-1;
/ _0 {% a% Q" D9 h8 Cdouble last_close=iClose(m_symbol,m_period,1);
! C( g* V  i* y6 v# E//--- classify predicted price movement# G; t" ?& `& |% L' z
double delta=last_close-predicted_price;
) F( E/ T' a, O7 {if(fabs(delta)<=m_class_delta)1 S1 M$ B! F" I! E6 \$ R8 R
predicted_class=PRICE_SAME;& f! ?$ _: J4 b1 u9 W: ]! ~, k
else
2 Y6 S( I7 [0 k6 s, }, J8 hprivate:
5 _; o+ w3 F2 ~9 Y; [; Jint               m_sample_size;- S$ {/ d" W. X3 l2 I, |6 t) |' ~
//+------------------------------------------------------------------+
' J3 F1 A$ a  x) I) }3 B# ?7 bvirtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)
; \& {  G! O7 E7 Z/ H{6 @! S) p; L' X
//--- check symbol, period, create model
2 m8 D- ]3 x' s/ L5 k+ mif(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class))" Q* x, Q0 s' G& \' h8 C4 @7 c
{2 y* w/ ?" l% J
Print("model_eurusd_D1_10_class : initialization error");
- M* a, C! I0 Xreturn(false);
4 v8 k0 f' F2 `}
) h/ |6 n$ N3 p/ G8 ]  H//--- since not all sizes defined in the input tensor we must set them explicitly& l; A8 ~' i" O) V, r
//--- first index - batch size, second index - series size, third index - number of series (OHLC)% h$ J: f4 c" L' m& F/ W
const long input_shape[] = {1,m_sample_size,4};. p$ S- T' h4 e" C+ Q3 u' D. ?
if(!OnnxSetInputShape(m_handle,0,input_shape))+ k' W6 Z; k5 a3 l8 B  @2 O$ s$ N
{/ e2 ~: N- e& X- u) Q$ D6 K1 t
Print("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());( f% z' l5 P. A9 |9 O
return(false);
6 B  U) l/ s& P( O$ D& I; u- m& K}
# Q: A1 L8 V+ A3 l* Q) X. j2 _2 P//--- since not all sizes defined in the output tensor we must set them explicitly
' h" Q; Y' y& p# d( s' l//--- first index - batch size, must match the batch size of the input tensor
4 R! K6 Q+ ^" ^1 o* I3 W//--- second index - number of classes (up, same or down)8 F5 N7 K: A, ]! L0 t* c; P: ^! \
const long output_shape[] = {1,3};
' \( k+ j) M0 i8 Fif(!OnnxSetOutputShape(m_handle,0,output_shape)); e' l. g1 S, `, r& `
{
9 p" C6 l6 V  {% k; M- _4 |' \* iPrint("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());
8 T3 ^+ J4 t7 t+ F+ _6 [* b- i9 @5 mreturn(false);# C1 K+ Y/ E) v
}7 H9 z- K9 m) Q1 v& @- ?
//--- ok
* t4 U- N2 Q, I/ @return(true);
! _, ?) D" [) M0 Q}
' ~8 K% ^& U3 `* u6 T% K//+------------------------------------------------------------------+0 v/ H$ D- F4 z: P
//| Predict class                                                    |
1 X- D  C; l; z2 y//+------------------------------------------------------------------+& L6 d" L/ p5 t
virtual int PredictClass(void)
* N# @( k( A3 g( J& ~2 m{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-3 13:56 , Processed in 1.452282 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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