私募

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

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

[复制链接]
发表于 2024-4-4 08:03:20 | 显示全部楼层 |阅读模式
1. 我们会用到什么模型呢?7 l1 }' Q5 o9 c+ F& W7 t( P" U
在之前的投票分类器中,我们用到了一个分类模型和一个回归模型。 在回归模型中,我们在用于计算分类时,用预测价格替代预测价格走势(下跌、上涨、不变)。 然而,在这种情况下,我们不能依据分类得到概率分布,而对于所谓的“软投票”这样是不允许的。
4 t" x+ F. X5 C6 M2 D我们已准备了 3 个分类模型。 在“如何在 MQL5 中集成 ONNX 模型的示例”一文中已用到两个模型。 第一个模型(回归)被转换为分类模型。 基于 10 个 OHLC 价格序列进行了培训。 第二个模型是分类模型。 基于 63 个收盘价序列进
# X9 ^* Z  d/ x. O" S/ K//|                                             https://www.mql5.com |
1 y3 o: ]0 |' v1 z$ e//+------------------------------------------------------------------+! _: M$ @' \$ v/ X$ b# R
//--- price movement prediction
  B7 t( {! Y2 y4 n9 i: f#define PRICE_UP   0% n! z/ M3 L6 T
#define PRICE_SAME 1
1 f; V- x1 B) i' n, X#define PRICE_DOWN 2
9 Q4 B8 T' {4 l1 P) U! j//+------------------------------------------------------------------+% W* J- a; N: V
//| Base class for models based on trained symbol and period         |0 ~3 q  G! J, v$ i+ @6 n
//+------------------------------------------------------------------+8 w) f$ }1 W, K8 x% k
class CModelSymbolPeriod
  U' J' {+ }/ X2 b" D$ K. W{! l! u0 ~! b) I9 E2 w" l
protected:
! v( z0 \$ I$ }# Clong              m_handle;           // created model session handle! z/ L* @4 U# o7 L  z
string            m_symbol;           // symbol of trained data, k4 j# E2 D( A
ENUM_TIMEFRAMES   m_period;           // timeframe of trained data# K% N4 P+ ~+ [, ?* {
datetime          m_next_bar;         // time of next bar (we work at bar begin only)
5 p" M9 b) `4 g) T  n: d( D+ ydouble            m_class_delta;      // delta to recognize "price the same" in regression models8 q3 p3 r3 Y; a
public:* e7 ]5 y' l  U- v7 U2 n; B/ O
//+------------------------------------------------------------------+7 S6 t, t* X4 I& E% M# ]. `
//| Constructor                                                      |
+ N7 ^4 J$ f2 _" D; J0 N7 R7 U% B% s//+------------------------------------------------------------------+
6 J; w7 O/ r9 B+ vCModelSymbolPeriod(const string symbol,const ENUM_TIMEFRAMES period,const double class_delta=0.0001)
# j; s# ~% J+ V. m) c! P) a5 n{% \7 R$ Q; s% f
m_handle=INVALID_HANDLE;: F! H: v$ s6 v! z
m_symbol=symbol;1 ], p, `) u. I
m_period=period;4 Y, h/ b& \2 l" F
m_next_bar=0;
3 j6 P% k- p0 C& Q, B. V% wm_class_delta=class_delta;1 x& T9 O9 r$ o! P
}6 z2 K: R; Y/ Q" m* ^: w
//+------------------------------------------------------------------+. [  j9 T" X! j2 L9 K3 ~% M
//| Destructor                                                       |
* `, C4 ]6 f5 Y//| Check for initialization, create model                           |
6 I9 Y) ^# m7 A$ a7 o//+------------------------------------------------------------------+) w! f) c& H. S6 Z4 k, b9 W
bool CheckInit(const string symbol,const ENUM_TIMEFRAMES period,const uchar& model[])
* c  S2 i/ h6 b6 ]- L4 I{# _  i7 E; m- t7 O
//--- check symbol, period
, v7 R! J% P* x3 hif(symbol!=m_symbol || period!=m_period)7 q; I& `2 p  T2 z* }8 ^+ U
{
* ^( |9 M/ k( A* _' o9 \3 APrintFormat("Model must work with %s,%s",m_symbol,EnumToString(m_period));
5 z5 G: o+ S4 Mreturn(false);6 Y3 ^0 q) B) H
}
0 V( O* c1 Z  E9 j( d' d* Y1 g//--- create a model from static buffer
: m- `3 Z" F# O, Mm_handle=OnnxCreateFromBuffer(model,ONNX_DEFAULT);9 C7 g8 X6 y# H: d3 D0 G- @0 Y
if(m_handle==INVALID_HANDLE)0 A! f9 [4 R& `  y7 l' t7 }
{
  h- t5 b. b! j) a, l, CPrint("OnnxCreateFromBuffer error ",GetLastError());
4 ^8 @3 @- Z3 O( Jreturn(false);
( n9 C2 ^+ g# p" S. \! K& A( m}
" x' Y  a9 s" K$ n//--- ok& l! D3 s, w+ s$ D% h+ M8 K0 w* r
return(true);
" ^4 I9 b% S5 e1 s}
* M2 f7 N7 R2 A" d7 d9 L7 L6 U//+------------------------------------------------------------------+, X  n2 a. @6 v7 R6 K  _0 i
m_next_bar=TimeCurrent();
# Z, f( h3 j% q* {0 s( F/ J% v5 ?8 Lm_next_bar-=m_next_bar%PeriodSeconds(m_period);
1 c4 J6 P+ \! D  r: |( _m_next_bar+=PeriodSeconds(m_period);
7 I, [! {. k. a4 [+ l7 z+ n//--- work on new day bar
0 C+ P: T% B/ Nreturn(true);
* r! e3 [+ U" D: H, P5 F1 s}
( z6 e8 E9 L3 B//+------------------------------------------------------------------+' `/ e* {5 x! c" T
//| virtual stub for PredictPrice (regression model)                 |
; S0 C# Z4 y! m+ o4 |//+------------------------------------------------------------------+  s) j" \, D4 K$ `/ |
virtual double PredictPrice(void)
- J: z+ A5 H& ]" ^{
2 m0 x' G! f# O7 H% vreturn(DBL_MAX);
( f) K# X6 p9 \) l3 o}  U1 L" L$ v+ o/ d0 R% n
//+------------------------------------------------------------------+  _- f. Z; V3 ]
//| Predict class (regression -> classification)                     |
* J& ^" v5 P  }//+------------------------------------------------------------------+( r9 a% ?! I7 T  d/ v9 {: m
virtual int PredictClass(void)2 U/ p8 Z7 C% S  E
{
$ N& t0 V& E# }- Y# ?. Rdouble predicted_price=PredictPrice();
/ }, ]7 [8 J# g$ K  o' r1 s1 ~if(predicted_price==DBL_MAX)
/ n! G* O9 o. ?2 Xreturn(-1);
! m( o. J* j+ P! [7 sint    predicted_class=-1;2 a1 R2 @# u. v) ~, j
double last_close=iClose(m_symbol,m_period,1);
, ~$ L, d& S% l* l5 X//--- classify predicted price movement! D. h7 f$ y! A$ H
double delta=last_close-predicted_price;
" i" q! Q' o8 ]( n& Wif(fabs(delta)<=m_class_delta)
8 R" G/ N9 c: j' S" Y6 m; G( dpredicted_class=PRICE_SAME;) v+ q+ e- z' c+ b( {- q: \
else
9 e$ p8 T  h' z2 a5 [5 tprivate:+ E' b& J+ V" K: a/ m6 k1 I
int               m_sample_size;& T) `# _! F$ O) n: b; o
//+------------------------------------------------------------------+0 T$ c5 q$ c. @. h6 B1 g
virtual bool Init(const string symbol, const ENUM_TIMEFRAMES period)$ q, H0 @2 ?0 Z$ ?* ^' {. K( _! C; {( H
{
; K+ B4 V6 {( f1 }8 U//--- check symbol, period, create model
& `9 R( O2 q- n+ p6 I3 kif(!CModelSymbolPeriod::CheckInit(symbol,period,model_eurusd_D1_10_class))3 D- X" Y1 `7 @: P" u" i
{
% x: ?% \- R7 w- d3 E" T' Z4 ^2 mPrint("model_eurusd_D1_10_class : initialization error");: z' r6 B+ `6 I" G3 j
return(false);7 Z% N6 p, U/ l3 Z/ [$ b8 P
}
- ?0 ]: g  M+ W& E( M( `+ K//--- since not all sizes defined in the input tensor we must set them explicitly  E* @( P9 i3 I1 s
//--- first index - batch size, second index - series size, third index - number of series (OHLC)8 p) {9 X* |% p5 o8 Y
const long input_shape[] = {1,m_sample_size,4};. q6 }( ~8 y* p3 _4 n
if(!OnnxSetInputShape(m_handle,0,input_shape)); \- G' M" t  R' _) K& r. S( }. H
{* U4 e- I, n  h. R2 O+ V
Print("model_eurusd_D1_10_class : OnnxSetInputShape error ",GetLastError());
7 c1 J4 j* Z% ?* k+ C: Ireturn(false);
1 s/ w3 A, G9 T$ {}5 C8 R% `( Y7 a' y
//--- since not all sizes defined in the output tensor we must set them explicitly2 X5 e3 Y4 V7 w3 }
//--- first index - batch size, must match the batch size of the input tensor
1 \: y$ ]* _5 p//--- second index - number of classes (up, same or down)
: z/ a# P: C2 \3 r. L( g* vconst long output_shape[] = {1,3};2 `, V# e, j) f; O
if(!OnnxSetOutputShape(m_handle,0,output_shape)); E( b' v# z8 [, v: O. W
{. p9 o; n1 y* ~+ n
Print("model_eurusd_D1_10_class : OnnxSetOutputShape error ",GetLastError());  d" {  c9 T; L  O9 g. a& B- R
return(false);$ o0 M! B& O" G
}
8 H& w7 ?! m8 j! ~6 ?! ]9 y//--- ok+ N. |0 G2 V* r1 {* L( \
return(true);& N, E; g) F9 l0 H  Q* M- @
}
9 R5 K1 v8 L: H1 c4 H) [) y( ?//+------------------------------------------------------------------+
3 V& a5 ~9 {, @9 l3 A2 ?4 {9 A. I//| Predict class                                                    |
5 D4 `% E$ U7 B. f//+------------------------------------------------------------------+
* Q  h( ]  N0 N- b" ivirtual int PredictClass(void)
, V8 q; R+ D! s% \0 d. l+ P% V7 x{
http://www.simu001.cn/x287997x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-7 16:46 , Processed in 1.357493 second(s), 32 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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