私募

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

期货量化软件:赫兹量化中为智能系统制定品质因数

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
8 i$ E5 J4 Z5 Z9 |在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。, W, W+ v) j+ ~; C: K
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
$ [4 A' H2 d8 B6 i3 |8 W以下是制定这些规则的代码。
, G) w3 w5 N9 R# Z" P1 K/ y//--- Indicator ATR(1) with EMA(8) used for the stop level...
4 N, ^, C3 G$ s: g& aint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);$ o- h4 j* v9 D' w) ^
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
- f) _2 G  S0 M" P" l. _//--- Define a variable that indicates that we have a deal...
: ?. \6 X4 x2 w. ^, m; l) g4 j: C! pbool tem_tick = false;
7 H: f  _% R8 j% v. j; V//--- An auxiliary variable for opening a position
" v& h$ ]) o& c5 ^& }#include<Trade/Trade.mqh>9 [! T8 ?" R( r. N; z( V
#include<Trade/SymbolInfo.mqh>" h- ]/ g9 O+ e9 {8 L
CTrade negocios;
3 `8 u4 j3 l- qCSymbolInfo info;6 X+ G+ w- A+ W: F! X$ g- W9 u
//--- Define in OnInit() the use of the timer every second
9 p: w6 t; r- ]//--- and start CTrade* Q% J6 [, o8 x4 }: R) c$ B
int OnInit()3 }4 r/ g1 z; L1 o& ^% F+ _) i
{$ L6 L0 [( W7 o! v+ h* E" Z
//--- Set the fill type to keep a pending order$ V- {* t0 l% _+ e* ^
//--- until it is fully filled$ Y/ R$ U9 @8 |
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
3 C. h1 t! E" E+ s3 ~" l1 h//--- Leave the fixed deviation at it is not used on B3 exchange
5 d) t/ K! C7 C0 Pnegocios.SetDeviationInPoints(5);
! S: ~6 O, @. ~+ K//--- Define the symbol in CSymbolInfo...
5 [1 e! A) {; s( f9 m3 ninfo.Name(_Symbol);
3 v* `4 ^# h' g) T0 f//--- Set the timer...' Q* Z& P* y; T% a; x$ z
EventSetTimer(1);: Q) y2 M! {- S8 I* P% T
//--- Set the base of the random number to have equal tests...
' `. p9 I8 ?- [% v4 P( {9 mMathSrand(0xDEAD);
) Z9 e1 f/ T/ s8 c! V. b3 sreturn(INIT_SUCCEEDED);5 \+ k$ j) \6 t6 Q5 ?9 W4 G
}; E) `$ S& _) m( r, x" |- K
//--- Since we set a timer, we need to destroy it in OnDeInit().
& r: y# j' X& |0 R0 ?void OnDeinit(const int reason)) Y6 A. ?% h$ Y0 y
{' C* m% S) S* K  r2 Q1 c
EventKillTimer();9 e/ u  P! }" S6 g/ v3 W$ J
}
  \2 `/ B# ]+ k# I, r& j0 l: u//--- The OnTick function only informs us that we have a new deal
  B* _& u7 v; G5 f2 @  O% yvoid OnTick()& T: A- S( ?  [
{% m% T6 G1 ^$ {; C
tem_tick = true;
1 G  {) D+ f- W4 }1 p- `+ Z}( C5 ?* p- G/ Q$ ]8 g
//+------------------------------------------------------------------+7 N1 X  I" K+ _) ^7 d& D
//| Expert Advisor main function                                     |
- C- w3 x- l: Z1 e//+------------------------------------------------------------------+
& A! F8 g$ t# T. f5 D! ^void OnTimer()
* w% y8 |9 a) [* G" M{# v/ J- F6 I# a3 R1 W+ ^" N
MqlRates cotacao[];) J) `4 U; Y1 X6 h+ }7 m
return ;1 j* ^2 @0 p& N4 [, Z' e8 B
if (negocios_autorizados == false) // are we outside the trading window?
# s; \  W) G  f& ereturn ;
0 H8 O+ |7 k, T8 W//--- We are in the trading window, try to open a new position!
# q( ^) H& T% y: \# Wint sorteio = MathRand();
3 E0 P) U. u+ p8 `//--- Entry rule 1.1% M7 b( b2 M+ \4 _7 l
if(sorteio == 0 || sorteio == 32767)* [: @; ~, d- N$ e
return ;
3 a3 n; o. S5 T* a! T+ Sif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy  f* @) s  {7 y  m4 @* H
{' d7 @% f( l  r# G
negocios.Buy(info.LotsMin(), _Symbol);  x7 ?) y  s) N& H
}
& _7 h1 o4 R" D( m! Z: I6 qelse // Draw rule 1.3 -- odd number - Sell
$ V; s$ @$ Y6 H' U& t  W{
. R4 B8 \6 {* a: X" s- gnegocios.Sell(info.LotsMin(), _Symbol);2 E$ D) K3 x4 N3 `
}
" _( j4 f/ d4 ~* f}" ^, Y  i: _) D, j
//--- Check if we have a new candlestick...3 a, h" a" K5 W: [. Q- b7 C4 P# T6 s
bool tem_vela_nova(const MqlRates &rate)& G: q' Z5 ]" n) E0 Z$ Z1 x
{+ |" `$ b% W# n6 [+ A( n
{
  n0 c8 M% [" Q/ aret = true;; q* k9 a# W4 V, W
close_positions = false;5 T( v6 U4 P; A; _6 _* L
}
2 j3 \8 N  ]9 G( Q2 C7 jelse
; @$ E5 @5 o0 ?; D# E{8 i* }. o3 u1 J, v2 h- b: C7 ~) H
if(mdt.hour == 16)
* B. [6 f0 {" D1 E9 zclose_positions = (mdt.min >= 30);
! I" N' G2 S5 G& \& n( j8 `0 A3 C( S}3 y/ j& N# G# M- h
}  T; G1 J- N; ^
return ret;3 y% P" h. y& s2 [, d/ C" x
}, t2 Z" `, _# }) L- U$ B+ i3 w
//---
. |8 W4 J8 K6 M8 F: Mbool arruma_stop_em_posicoes(const MqlRates &cotacoes[]), T& J! T/ a/ e8 O
{1 d$ v8 O; g) B( e1 l: G- P9 r( t  K" J
if(PositionsTotal()) // Is there a position?
1 Y, f2 F+ R& B* j{' I9 @" M! j0 M: j  I% E$ _
double offset[1] = { 0 };
5 x$ V; F1 e7 e. x4 P, g& uif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
" d3 x, h2 Q/ e8 o; Z* y) k$ s&& PositionSelect(_Symbol))  // Select the existing position!
+ w+ O8 u+ s3 a) ]" V{
" o! T9 Z" u2 ^$ NENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);' D- w& E  E  q5 ?: D$ v9 U
double SL = PositionGetDouble(POSITION_SL);7 A- ]) x  i, @! ^1 x
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));0 q# B0 |, K; r4 f, O
if(tipo == POSITION_TYPE_BUY)
. ~! G+ G% t' A+ S! l' Z{  C& d' `6 o1 W" u6 Q+ z
if (cotacoes[1].high > cotacoes[0].high)
" _( X: {$ T# q" x  s2 y{/ R- W; ~: W, ]9 v7 y6 N
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];3 b6 `- ?, S9 u
info.NormalizePrice(sl);# O6 `  M4 ?0 \" I
if (sl > SL)$ T, g3 S$ W* B2 b1 s' Q
{  h" Q% U% g& I- T$ }5 h) Z
negocios.PositionModify(_Symbol, sl, TP);# m5 K6 E- ^# }( Q8 `! y7 j0 \
}
' u4 o  @# P2 s7 x3 g! R}
# z. O$ d. r: l  E& C4 l}
) X" H2 ?; B. `$ W# F* k% xelse // tipo == POSITION_TYPE_SELL
- `3 `# T( W0 E/ |9 Y{4 i9 \. Q, P# ]% a4 U
if (cotacoes[1].low < cotacoes[0].low)
6 L# I( e5 E- A{
/ V% l7 v, Z3 Lreturn true;3 B! k* L& o- r1 @) K
}
; N3 _$ p& T# W2 |0 i// there was no position- P" q% D! d. A% H
return false;
% b3 A% j" U% s- w}
9 c- U8 s* Y1 w% l) P( S我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
/ A+ d9 u6 H) a/ A! Z到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-28 21:31 , Processed in 1.341969 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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