私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?
6 o6 |& n( v1 \  G在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。
$ c& i4 ^& f- z4 Q5 m: |我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进
" V2 J. _# I4 r1 [+ A! f1 Y//|                                             https://www.mql5.com |
$ v  q1 @  D  M" O& {: P//+------------------------------------------------------------------+
5 O0 E; m( G  X# c//--- price movement prediction
4 L2 h, C( l' w$ M' K#define PRICE_UP   07 }, m8 R5 g+ x6 z- ~/ B, V
#define PRICE_SAME 1
3 p" }& Z+ ^+ k" q! m8 k* f#define PRICE_DOWN 29 q0 K8 v0 V: l* q4 F
//+------------------------------------------------------------------+# Y$ h7 k% k7 i/ ~, T
//| Base class for models based on trained symbol and period         |
4 U; H6 C; D7 h( ^; z/ R//+------------------------------------------------------------------+* a& ^( P  _* K; e
class CModelSymbolPeriod
2 p- A5 J" y( }- n  _{7 F; |/ U2 h$ Z! U
protected:
0 V: N" S" h+ k2 x7 V4 g& Glong              m_handle;           // created model session handle
2 K0 X" B! \# i6 @0 U7 D5 Sstring            m_symbol;           // symbol of trained data. k# J4 q- ]- B* t' z
ENUM_TIMEFRAMES   m_period;           // timeframe of trained data
9 B" _- p; `+ qdatetime          m_next_bar;         // time of next bar (we work at bar begin only)2 x9 z% M. p6 [( q, E
double            m_class_delta;      // delta to recognize "price the same" in regression models
: Y1 V( ]' G: V% ?public:% i3 x9 }  K" g+ j* n
//+------------------------------------------------------------------+
4 F9 O- _/ d) i1 W3 T6 U+ G" E. e//| Constructor                                                      |' O5 |% b2 k; I9 D: d' C+ u
//+------------------------------------------------------------------+( X' A: c0 s  r- R: n# O
CModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)
" e& ?; H6 W0 f  @+ B7 G! X9 m{
/ q" a( W2 U* `$ ?; h& jm_handle=INVALID_HANDLE;
8 e! Y. @; v& W5 h6 ^: |7 z* ~' [m_symbol=symbol;
$ t6 V$ J3 e( P+ qm_period=period;
+ Y( w6 {, q( t, j8 w. M, J  Xm_next_bar=0;
5 z- x! A7 b. r3 [) ]m_class_delta=class_delta;
6 M2 n+ Y5 d; `. e8 D" D}
! @. A% N! @2 w1 Y6 m) }//+------------------------------------------------------------------+
4 p5 d# k1 c% I2 N//| Destructor                                                       |: I, Y9 g0 c/ \6 @0 v1 b
//| Check for initialization, create model                           |0 b. Y$ Q; [- n! E
//+------------------------------------------------------------------+
& _8 D0 g1 u! g8 @! w$ p5 kbool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])1 {, B# _8 o$ o3 j
{
% ], C% ~' ?1 P; ]8 {& g5 r8 S' f//--- check symbol, period
" h3 Y6 _5 q0 g- R+ Hif(symbol!=m_symbol || period!=m_period)  p& u9 ]8 w, W$ V3 p1 R
{
+ _1 N$ W. G' LPrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));
% P: Q" `- g6 X2 w3 q% Ereturn(false);
# x& d' _; X$ U9 R# M. ?}
/ m6 j& f0 m+ `8 d- w  H4 ]( H. e//--- create a model from static buffer) p! H" V; d2 ^' n: E- ?' V" u
m_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);
+ y, g+ i0 I2 d1 @. Jif(m_handle==INVALID_HANDLE)% c1 |: e; z, M  C! C5 U$ s2 W
{
% Q( n2 r# @7 o$ j: lPrint("OnnxCreateFromBuffer error ",GetLastError());
' R8 l5 Z+ F7 M1 Y1 z. vreturn(false);# M! L2 ?. v6 a2 N6 I
}" E8 }# A7 y9 o$ O
//--- ok
. O6 A; Y2 N7 R+ T3 a7 G% Ireturn(true);% u3 g2 J5 h, {7 q- J
}: X( T9 w- }% w
//+------------------------------------------------------------------+0 Z* D, b6 j  m! D
m_next_bar=TimeCurrent();
& o2 ~: H" N6 n& \6 sm_next_bar-=m_next_bar%PeriodSeconds(m_period);
, h) |% `7 t. om_next_bar+=PeriodSeconds(m_period);
9 k' U! Z6 t+ Y! U//--- work on new day bar4 O8 e( u8 S# n7 f# [
return(true);
$ Q# U0 Q* d6 }. T. V8 o2 z}
' Z: J: v8 x! x) n* B. M//+------------------------------------------------------------------+2 a' E; h* M# r
//| virtual stub for PredictPrice (regression model)                 |$ g+ V& k& {, i0 a
//+------------------------------------------------------------------+. s) N. |3 {+ ]% W+ p8 J$ T4 }
virtual double PredictPrice(void)
8 V  b+ n7 e4 q8 e; p, F3 `6 q{
, C$ i/ k) b: Z% Ireturn(DBL_MAX);
; u5 z5 d  n( E5 x8 B- I}8 T: h4 M) C7 p
//+------------------------------------------------------------------+- V: l. V/ g' j( `( H6 H- F
//| Predict class (regression -> classification)                     |* J: a7 P6 f0 J$ e% L  g& z
//+------------------------------------------------------------------+$ h# V/ j' M" n- I3 e  d: r
virtual int PredictClass(void)
: e& _- Z* z: p8 ^( u  |! ]{
; S* Y7 u/ |& w% v. ldouble predicted_price=PredictPrice();
1 U8 |2 N% s5 q5 E7 v* l* tif(predicted_price==DBL_MAX)
0 f$ x( K2 v- @  u. [return(-1);/ v: r  }  u4 ?8 D: V7 g& x. x3 }& f
int    predicted_class=-1;& H9 x+ Y9 X) E- j9 o, C
double last_close=iClose(m_symbol,m_period,1);, m9 O& Q- T6 ^/ o- w
//--- classify predicted price movement
  R* q  }, Y8 C3 Wdouble delta=last_close-predicted_price;  u2 M) c4 P4 g0 |( B
if(fabs(delta)<=m_class_delta)
. b6 s# k0 L. S3 Lpredicted_class=PRICE_SAME;! S, A# g; J+ n
else6 a1 @8 O* \: ?, j; @3 P
private:6 u5 l1 A3 N3 \2 X, a
int               m_sample_size;& I. m  Z$ ^, X5 z, k2 ?. {
//+------------------------------------------------------------------+: p: S7 S5 a* x6 q6 l
virtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)5 H) m/ F/ d; k0 V
{; B% N  J2 ]  v6 M" C1 P7 u
//--- check symbol, period, create model
2 y) B* t# M- S  [if(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class))* [/ `; ]! ^5 a% c# B! n
{7 p+ k2 w( G& u0 H% d! n
Print("model_eurusd_D1_10_class : initialization error");& W4 Y7 h0 m$ z; D' k
return(false);
/ O5 [- L) N% s' }" B}/ q6 k! L2 S6 O5 @3 w
//--- since not all sizes defined in the input tensor we must set them explicitly
* C7 m7 z1 {& O//--- first index - batch size, second index - series size, third index - number of series (OHLC)# v% P: T6 r6 _' l- w( `; r7 A3 a
const long input_shape[] = {1,m_sample_size,4};, h1 G; q5 F; m; n% `/ I/ @  z
if(!OnnxSetInputShape(m_handle,0,input_shape))
2 u# E9 n/ t9 V{$ r  A0 I$ Q* ?6 N+ }
Print("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());# [3 ]0 D! X* \! w+ @
return(false);
( h! n/ {9 b& L0 m. Q}: }' |% B* V  E3 V4 @7 \0 x
//--- since not all sizes defined in the output tensor we must set them explicitly  M0 J% |" V9 H- f( |% P! H* p
//--- first index - batch size, must match the batch size of the input tensor
, h# |3 L- ~" F/ k* P//--- second index - number of classes (up, same or down)
1 J# g2 t' M" }7 O" o3 X2 Hconst long output_shape[] = {1,3};
$ t1 u6 I% ^$ S6 o% A  vif(!OnnxSetOutputShape(m_handle,0,output_shape))6 E: N: ]( {" G0 ^; K1 a
{
/ m. ]6 o3 S" x4 a6 G* O; {Print("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());
1 N5 @* g4 ]1 H0 ^0 _7 n" O1 j/ d0 rreturn(false);
. `8 W/ X  d3 }! X- s3 u; H" e}
7 {# ~. S6 t: y7 b6 A- U; Z//--- ok3 y: A1 d8 `- ]3 {9 c$ c9 z9 P
return(true);
. F; d5 ^0 P2 n9 R0 q# J}
: I: q, c5 h. @4 ^" d+ A, ?//+------------------------------------------------------------------+' d6 ~, \9 K& J1 _' m
//| Predict class                                                    |; g* U* ~' T6 Q/ V9 J& e
//+------------------------------------------------------------------+
1 k4 ^3 \! r; P7 h& M! Z0 `+ Yvirtual int PredictClass(void)
& h" L$ r& i4 P{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-26 17:05 , Processed in 0.488526 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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