私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?
+ S" j, t- Q% }& O3 Z$ D' X在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。
% @. G0 b* I* T我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进! `9 O, z1 P$ w( Q2 B4 W
//|                                             https://www.mql5.com |
: W4 z2 m, c' n; }7 S+ Z- q//+------------------------------------------------------------------+" F$ T  Z7 U( d. ]6 c& s) P$ \$ k
//--- price movement prediction& h2 B; K/ ~) k3 Y0 O  E3 a' b
#define PRICE_UP   02 W" x+ t; C8 C3 E+ L
#define PRICE_SAME 1! ^; t2 o+ \9 X: C
#define PRICE_DOWN 28 B! W! A2 E3 S6 a! e* N$ ]' {
//+------------------------------------------------------------------+
3 E3 x$ b' ~$ [1 U; U//| Base class for models based on trained symbol and period         |: D& [* D3 F. i: E$ }
//+------------------------------------------------------------------+$ [5 c7 Z4 M2 V8 c; I8 o
class CModelSymbolPeriod% F$ D# _; v2 ]0 a; a
{
$ b$ e$ k" r! b' s( G, Lprotected:
( r8 R/ J4 K2 i* ~7 elong              m_handle;           // created model session handle
( p" r' m% b* zstring            m_symbol;           // symbol of trained data% K0 f- y+ {! L6 o/ H: x
ENUM_TIMEFRAMES   m_period;           // timeframe of trained data. Z6 l4 T7 j5 ^! H7 [
datetime          m_next_bar;         // time of next bar (we work at bar begin only)
2 N* i5 ^" F1 @+ Z) T# s( y6 qdouble            m_class_delta;      // delta to recognize "price the same" in regression models
/ Y$ I6 w5 ]( C; B& x  Hpublic:$ h8 ], P; E! Y6 m# \: n4 c6 Q8 k5 F
//+------------------------------------------------------------------+
. L, E  n7 c6 n5 c9 }$ o//| Constructor                                                      |. W3 q% {6 h+ i1 q
//+------------------------------------------------------------------+/ h5 P/ |! |) `' I& v) H
CModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)5 y2 C8 o+ z1 n: e
{
6 e, Y$ ]4 G) _/ C$ _( `m_handle=INVALID_HANDLE;1 e0 k1 @6 `! D
m_symbol=symbol;! P  F' |) u7 ~1 L, ~1 E
m_period=period;1 V' ?4 j- r1 h9 J+ Q% E5 t6 D8 r
m_next_bar=0;
7 i* D: \2 l3 z/ m! f" Xm_class_delta=class_delta;
7 M5 i# Q% X' C& M" ?}. o: ?6 V4 u+ P" ]2 q  [8 J7 F4 V
//+------------------------------------------------------------------+8 E1 m6 q) v( t  v
//| Destructor                                                       |
7 b$ \* O9 }  M0 z% g//| Check for initialization, create model                           |  y  \* U1 _4 i. g# k
//+------------------------------------------------------------------++ O( k& w3 F$ G, b! D
bool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])
9 K; n( {& G9 b{8 E3 x, O# D! ?+ S
//--- check symbol, period1 w/ ^( t, [" J
if(symbol!=m_symbol || period!=m_period)
3 l' S" c' I* @( a3 L; `" j{
) Q) P4 G7 A2 i  a+ J4 HPrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));/ j, C- U' ]: V
return(false);! Y- b1 r. q; w4 ^/ {5 f. V" S
}
7 ?# d$ v0 P7 V) f1 N//--- create a model from static buffer
6 ?* z) Y+ ^0 D+ V$ Rm_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);
/ N# e* g3 P* o8 Z0 Sif(m_handle==INVALID_HANDLE)6 F3 V$ V% O8 ~7 q* S
{
4 v* Z" W1 k8 A6 `; U+ O' D2 S& LPrint("OnnxCreateFromBuffer error ",GetLastError());9 Q' r) S, O; C9 K  ^* A6 B/ n+ _
return(false);/ Z2 C7 F8 E# l3 s! a
}/ `. k( v0 M- Y! x& K" q6 c9 e
//--- ok
  A  r" R' j- v/ Xreturn(true);. Q$ c* b8 h4 x* \
}- @2 e0 v( r' Z, l
//+------------------------------------------------------------------+( V2 p0 j. p( q- h3 B. N
m_next_bar=TimeCurrent();4 `7 F! f- @+ Y) _& `7 a# _/ @
m_next_bar-=m_next_bar%PeriodSeconds(m_period);8 q# Y7 w" \/ g* i% x5 z
m_next_bar+=PeriodSeconds(m_period);
7 n, t5 T) s, S+ N: j- h2 ^//--- work on new day bar
1 b" W6 x: r$ r6 ~+ A" Preturn(true);2 }; d6 K) p5 A7 L6 a
}% b# m2 {3 Q4 _
//+------------------------------------------------------------------+' o+ Y7 f4 L& g( J! T5 }. B, Y
//| virtual stub for PredictPrice (regression model)                 |
1 N0 o. `# P; r5 `: d& x//+------------------------------------------------------------------+
- A/ {& H! g8 U7 ?* x7 L- Dvirtual double PredictPrice(void)/ x7 ~& g# ], D5 w8 b/ W! k
{
" \) m9 I5 ]! T- ]3 n0 i0 H% H7 Mreturn(DBL_MAX);
9 a( g+ m- M" f, B0 k6 L$ o}) S4 \& F7 l7 n; q7 c' o- B
//+------------------------------------------------------------------+
" \! z& j( i; e; S3 m//| Predict class (regression -> classification)                     |
, D+ `9 q/ T0 }+ N; }/ g! W- ]//+------------------------------------------------------------------+
+ _  l+ a1 I: o% g1 l5 ]/ xvirtual int PredictClass(void)
: m8 T& {; U: I& J0 o3 v3 H{: N9 g2 m7 s3 z5 j8 f, F
double predicted_price=PredictPrice();$ b( |/ _- F2 k" N( G: V2 \' O
if(predicted_price==DBL_MAX)
4 B# B# U3 E- A  t& k# |# y, yreturn(-1);
- N/ e7 i) m3 a2 w( cint    predicted_class=-1;0 c  U6 R0 e6 y: B! I3 m
double last_close=iClose(m_symbol,m_period,1);
, e. k6 ^0 b) `( r+ K//--- classify predicted price movement3 W( W" B  ^" o5 q. K9 F1 `
double delta=last_close-predicted_price;
% @) b; _# W' U- H3 Qif(fabs(delta)<=m_class_delta)
, V/ C5 ]# P# tpredicted_class=PRICE_SAME;
- p" h" o) {* Q, n1 p3 P4 F; Relse
2 N; P) f! M7 q, k' Oprivate:
/ {  J6 z6 I5 f2 T6 Gint               m_sample_size;8 X2 q5 D7 {9 m8 U$ _& p8 p6 t
//+------------------------------------------------------------------+# A: G# K. G% E0 V5 L8 y
virtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)
) S; l; H5 J; U% U; K* K{
3 t/ Z( h2 A1 }$ d//--- check symbol, period, create model' q" K1 u' j& B: l( y) j0 ~
if(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class)); n" h9 j0 E* l
{
  ?8 e/ P1 ~5 t) z' E. e- yPrint("model_eurusd_D1_10_class : initialization error");
  {$ E9 ]3 A% M" Jreturn(false);
( d! D2 S+ [) v( b2 k& i! D}
% W8 @9 l- y* T* V8 F% k5 A//--- since not all sizes defined in the input tensor we must set them explicitly6 l2 {+ F) A, _' E& `& e6 X
//--- first index - batch size, second index - series size, third index - number of series (OHLC); L2 z( u; ~7 L, {" D
const long input_shape[] = {1,m_sample_size,4};$ I: J; O! p/ h2 l2 S1 T
if(!OnnxSetInputShape(m_handle,0,input_shape))
+ m7 q# c0 s5 \{
+ J: x6 l- ~% |/ N! SPrint("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());( r9 {2 Y" L# ]+ C9 {
return(false);
; E6 X3 }: ]' U) p. w}7 B  G$ s0 _$ e' _3 n! u
//--- since not all sizes defined in the output tensor we must set them explicitly9 D/ y5 W( Y8 e
//--- first index - batch size, must match the batch size of the input tensor! Q8 E0 f, r# Z
//--- second index - number of classes (up, same or down)
+ G; j4 ?5 _7 `# U/ @const long output_shape[] = {1,3};$ X6 H8 h+ P1 F
if(!OnnxSetOutputShape(m_handle,0,output_shape))& P7 }( o/ N8 e. v, J, J
{/ S" H5 `6 O: g6 o0 a7 r
Print("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());5 T/ K4 j; K/ z5 r
return(false);; ?4 D# P  O6 K; y0 e8 q0 d4 G. t
}
- [/ h% [& m; r! H2 Z/ Z//--- ok- e9 N, r; C8 i/ a+ \
return(true);1 o7 i. w$ ^* D" v; C  o
}
$ p* m3 k: z/ F//+------------------------------------------------------------------+! L6 i1 g* @- V7 g$ U8 @5 a9 v
//| Predict class                                                    |# V  P+ n. j& r
//+------------------------------------------------------------------+
) Z4 l: p9 r) b7 O! }5 q+ l, pvirtual int PredictClass(void)6 S" G0 L2 h4 j6 f9 |! B8 [( S( V) Q
{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-10-21 19:23 , Processed in 0.552423 second(s), 32 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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