私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
$ k0 n3 U: h  d- d; H7 ]4 y在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
( r9 K3 v( d8 c* r- H为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。+ y( A. ]( M$ z0 `, p1 o
以下是制定这些规则的代码。
7 e3 L. M  Q' z$ R# S//--- Indicator ATR(1) with EMA(8) used for the stop level.../ v9 o! K: |' e5 _+ Z+ n# f3 [
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
% U7 t0 R) G$ }, X) U6 `, M7 H7 Zint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
6 k' m' @) P0 X, T: O7 f: `//--- Define a variable that indicates that we have a deal...
" _& G- ?" j  I4 l8 Wbool tem_tick = false;
) c8 d3 [7 N3 K& r//--- An auxiliary variable for opening a position; p2 s! ^8 g# s' X. i4 w) [
#include<Trade/Trade.mqh>( v, Y# x1 q9 d4 k6 _
#include<Trade/SymbolInfo.mqh>* k) v7 I7 b; P
CTrade negocios;
' _9 ^! t1 M* D0 m% wCSymbolInfo info;
) ]9 X. l0 c" I1 C//--- Define in OnInit() the use of the timer every second
1 w) {: Z: i. J0 I! ]//--- and start CTrade
9 @% J6 ?0 c# I8 R2 y- Z& Wint OnInit()
0 l/ W4 o) p+ |6 m{2 i3 H2 ]2 [3 f+ c& F% a
//--- Set the fill type to keep a pending order& |. g; m  q  O' D! x# N5 s5 k" W
//--- until it is fully filled8 `0 Y. N# ~/ }8 q6 U
negocios.SetTypeFilling(ORDER_FILLING_RETURN);! f8 W! C9 L. }$ }4 h9 [! k
//--- Leave the fixed deviation at it is not used on B3 exchange5 b( l% D7 k2 c/ K
negocios.SetDeviationInPoints(5);2 `# B% t' ]: U1 R* M; d$ t7 k
//--- Define the symbol in CSymbolInfo...% t9 B  T  G, h* h
info.Name(_Symbol);3 \# n! M  o( T3 Q8 W
//--- Set the timer...' p+ i, Y, b" ?; W+ B- N
EventSetTimer(1);
- p3 C& A6 R0 w( Q- c6 I( A9 b) X. p//--- Set the base of the random number to have equal tests...9 d1 \5 o& [$ |% _1 l3 q
MathSrand(0xDEAD);/ I9 i; Z, g) B9 t
return(INIT_SUCCEEDED);
  @' L3 O3 |; u/ E$ U; f' a5 X}; j% d: x: c7 U5 s% H% e6 b
//--- Since we set a timer, we need to destroy it in OnDeInit().6 d' N1 f+ `  g6 E" V
void OnDeinit(const int reason)
5 g2 g* j" C) S' q9 [2 G/ Q, h{
/ J" Q: @6 q  ]3 i" EEventKillTimer();! \. }: q% C  o2 [2 J
}0 Q( r5 z3 B$ F) P
//--- The OnTick function only informs us that we have a new deal3 z  z2 _: `) V" h( T5 {) W! {& n
void OnTick()# f9 [& M* I1 e8 H/ m2 n( ]6 J$ _
{
  `. R7 P( w! \/ X& |* r0 S) }tem_tick = true;) K- ^8 [: T7 H  q# a2 @& A
}6 D  T2 ]  W* n
//+------------------------------------------------------------------+' {; o' ~" m! h7 I2 r: l
//| Expert Advisor main function                                     |
- J5 c7 b$ F$ ]- C/ Y) P3 c//+------------------------------------------------------------------+
9 `! [: P  e* Q  ^/ Avoid OnTimer(): Q; f$ o; T2 h# m  j3 E
{8 v' u/ t0 K" T+ ]0 y  W; F/ l8 b
MqlRates cotacao[];- E+ F- V5 L5 z& l& b
return ;
  _/ D- `( N1 U4 [if (negocios_autorizados == false) // are we outside the trading window?
+ N5 `: e" i. [/ |7 V! N6 \return ;( ?! f: d, c: O! F' G+ K4 Z
//--- We are in the trading window, try to open a new position!9 |5 c4 v% K" t9 j/ s) ]8 C1 _
int sorteio = MathRand();
0 f2 a9 {3 ~) V" L//--- Entry rule 1.1
1 ^7 q) o* x% \) |4 _if(sorteio == 0 || sorteio == 32767)
% `/ i) o  Z5 }9 E: creturn ;
* o1 Z! y" Y: j/ b! P! |if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
* R9 b  ~4 @6 a{
7 }& N% d5 J: U" nnegocios.Buy(info.LotsMin(), _Symbol);
" U) T7 J! T: \: t( f}
! W' S4 _( n$ `3 jelse // Draw rule 1.3 -- odd number - Sell; H! m3 |7 }2 ]: ]8 K
{
/ C  c2 z& q: nnegocios.Sell(info.LotsMin(), _Symbol);. h0 k/ B4 _0 i9 W0 B# R, v% V. N
}! X7 N  _. t& ?+ \' F% [  Q, d3 ?
}# E! {6 X% [! e8 Z! L
//--- Check if we have a new candlestick...; n+ i( g/ L+ H  W, ]* a$ V
bool tem_vela_nova(const MqlRates &rate)3 D: {: C' a% C0 c/ s
{8 D% e0 w1 B! J9 X6 t
{
& t( f! z3 r' Y. Mret = true;
  s1 R, f% u8 Z. J; Oclose_positions = false;
% Y: \9 d8 n! I1 e, u}
2 s  B. K* V9 l+ K/ O* q. `: felse
0 i, l3 F0 \5 X! Y{
! D( s# y/ w- D+ Sif(mdt.hour == 16)
  X$ t+ \$ q. s' H. ?; Rclose_positions = (mdt.min >= 30);
/ Q1 D7 P+ u6 H* j: Y6 c}0 v" K# L, B9 H3 K
}& K" O6 f' E$ a$ V2 Q, o" ^
return ret;, J- h! R. e: X& \  I: ~
}! L: r; ]- C# ?5 u
//---7 p" g; m; {# b  M0 D
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])1 Q  ^* Q& |: Y9 t, f
{$ c' n5 k" U. K9 J4 k4 e' Q
if(PositionsTotal()) // Is there a position?! G" @6 b/ H& P
{
+ H& P8 e" ]2 I8 V) l- ldouble offset[1] = { 0 };/ K7 v% B& \+ `$ g$ w/ g( k
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?# s" J. l. H) v& a5 @( [3 \
&& PositionSelect(_Symbol))  // Select the existing position!9 J7 I9 j3 U: i+ T" B& |
{
/ i. b" P( ?: A0 nENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
4 @0 P; \- }% o  ~, W# {4 }double SL = PositionGetDouble(POSITION_SL);
8 ?* b9 S' i+ G# udouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));$ C" U9 F3 Y! \1 z! y
if(tipo == POSITION_TYPE_BUY)
0 M! m0 _  f8 G# w# v1 u4 a{
( `4 x" t: Y6 W5 v6 ]if (cotacoes[1].high > cotacoes[0].high): k1 x- E. z' n% C  |) n
{
, i* J2 G7 b: B' u  H& bdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];7 A- {6 U: d/ R, `% N3 r
info.NormalizePrice(sl);
/ y( U, l8 Y% \5 W5 Hif (sl > SL)
: z0 t; b9 ~) q9 Q7 s$ G{  z; z+ q% p) E  Y, F
negocios.PositionModify(_Symbol, sl, TP);3 Y$ O, S/ z/ V6 @
}
' m% S* p2 g3 v/ x/ S4 M}
  @5 J3 O- F( a; }4 \: k$ Q  g}: F  i, `# V, ]6 @+ E& c- w4 b
else // tipo == POSITION_TYPE_SELL9 S9 M0 Y$ W4 Y" M* q
{
5 i4 \! ^0 H. A9 I' Tif (cotacoes[1].low < cotacoes[0].low)& ?! i1 Y: Y* L# P, M; J
{" L5 _% f8 |- o, e% g
return true;1 N0 o3 J0 d: B, ?2 a3 n
}0 t- @7 T! i* ?5 q; z' y
// there was no position
2 H  ~2 C9 ~7 l& l: ^8 G+ K4 Jreturn false;7 e& e% a# i2 O* r2 A  c* f
}2 t% I8 V1 G6 v1 B$ s* ?2 |
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。2 D7 I( \4 `4 l7 @4 H' y( h
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-14 14:22 , Processed in 0.409126 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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