私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
) ?+ X6 G; F# O0 R在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
- g) J) o! j. g& I. U$ B为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
) r! Z! ]  |+ s/ j0 V以下是制定这些规则的代码。* E0 D  I* F1 q  l# [
//--- Indicator ATR(1) with EMA(8) used for the stop level...
3 U; H) l+ H( M' ]; E, fint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
  ?& j) }1 {3 K# o3 ~int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);% ^" K3 W. V" }% `! T
//--- Define a variable that indicates that we have a deal...1 w, s4 v5 m4 {
bool tem_tick = false;  b  g( y9 Y$ g' m8 O8 N! B
//--- An auxiliary variable for opening a position* v% B7 Y, n7 k! t
#include<Trade/Trade.mqh>
* Q6 P- R2 _0 @) Q7 _. Q#include<Trade/SymbolInfo.mqh>! K2 O- `: D0 d5 C) h5 j% p' a5 R/ m' E
CTrade negocios;
: [' `3 I' ~$ c8 Y9 z7 m6 {( ACSymbolInfo info;3 X7 z, W. D! x0 b; J6 f1 T; H
//--- Define in OnInit() the use of the timer every second. y4 ~( D8 o- |- j, W
//--- and start CTrade
! [6 P: R$ l: ?int OnInit(); u/ y' @& Y" D: U5 M& o. {" x
{- m' S* d4 n3 {
//--- Set the fill type to keep a pending order
4 p. S; A- s6 e1 G//--- until it is fully filled  F9 S5 a1 B9 j& ]: r6 G! X: ^4 }
negocios.SetTypeFilling(ORDER_FILLING_RETURN);) u. o; T! S# h. J+ j2 [* {  `
//--- Leave the fixed deviation at it is not used on B3 exchange% N; D: u; I  |" T; Q7 ]! N
negocios.SetDeviationInPoints(5);
5 A! d' _5 C) Q- y2 p1 t//--- Define the symbol in CSymbolInfo...
* @- s) F% E% qinfo.Name(_Symbol);3 s& \  ]( f7 F" [4 M; U
//--- Set the timer...8 o! c; N1 {; z9 m
EventSetTimer(1);0 A  h& v$ \9 o; Q  \
//--- Set the base of the random number to have equal tests..." h  B: s- k! H% c
MathSrand(0xDEAD);
" r" `$ \+ B+ l& _return(INIT_SUCCEEDED);$ ?& [# ?- w, t2 U3 [" h
}
) ?8 R, \3 H  z' \, R: v+ @+ b  `//--- Since we set a timer, we need to destroy it in OnDeInit()., {! T+ `- F* R( _( D6 H
void OnDeinit(const int reason)
( y! d( S7 z: X" Z{
4 z0 }, d1 i% C* l2 u) }: OEventKillTimer();3 |: X, \* W( n2 V+ j. X; q' j
}
; l3 t5 z+ E) t; c1 u$ F- j//--- The OnTick function only informs us that we have a new deal" W6 p8 x" O+ Y* w
void OnTick()1 g' q' s8 k! }2 c8 ~
{
2 a- e1 y3 H) |- q! K* E. r$ ftem_tick = true;
- V' W4 X3 D4 L, A- G+ R) q}
; R6 U/ x/ T1 b  E% T- N: J//+------------------------------------------------------------------+6 D9 ?, x1 d. W* t& @
//| Expert Advisor main function                                     |
7 f+ r! {, q7 Y6 O% o1 T2 K//+------------------------------------------------------------------+
4 Y3 R2 h9 d, g$ h: Q2 p% |' kvoid OnTimer()
, ~5 ]/ J# q$ L" A3 B6 ]1 k) i0 }{
" e, D& C* V) m+ d4 TMqlRates cotacao[];5 J7 h8 k+ c7 u) b5 V" O
return ;
- U# I" R; a) o  w' Eif (negocios_autorizados == false) // are we outside the trading window?
& f) O# Y# R# r5 kreturn ;
/ q: o2 J3 Q( s' y//--- We are in the trading window, try to open a new position!
  Z) }4 Y& Z% @* }int sorteio = MathRand();
0 u: C- \2 m$ @0 g//--- Entry rule 1.12 {' j  @, F2 }) d9 N
if(sorteio == 0 || sorteio == 32767)' S0 Z6 d1 p& Z# |6 O$ b! ^! i
return ;3 T8 q8 r2 O3 w) y$ u! k- @& u. Z
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy1 h# x% \4 L/ l% Z' f$ k- g
{
; b- ^" ^" c6 Y2 a+ nnegocios.Buy(info.LotsMin(), _Symbol);' Q0 k. L! {2 v- o' M
}
1 z2 b- P8 e  oelse // Draw rule 1.3 -- odd number - Sell+ ?. L. e( U* y5 e: _- _3 O2 l
{" ~# Z# m. k$ L8 T. H2 x
negocios.Sell(info.LotsMin(), _Symbol);( g+ E8 T3 b7 [: Y
}
/ T9 E. t2 O$ r9 O2 N( [}
1 R6 \1 B3 }7 H  A2 i//--- Check if we have a new candlestick...
% S, `- ~2 w5 h& v  pbool tem_vela_nova(const MqlRates &rate)
( ^* e- ^# z: a& ~$ U{
6 C9 R4 U+ r0 J  U4 {" i+ l; @2 V5 N$ U{
( d; }# A" P0 j  P* S0 d- C8 eret = true;( p) h- w5 V# b4 F
close_positions = false;
9 `7 u: N! u6 ?) r: {1 i' D& a. M- b}
# t- P. P: u7 y6 e8 @else: |% N9 l4 o6 _+ B) F9 b7 M
{
7 F! b5 s/ `4 ~4 `  Wif(mdt.hour == 16)2 o4 h5 B  }% k& G, g: E, x2 e
close_positions = (mdt.min >= 30);
5 Z: _) o' Z! R' y0 f}
* L) ?# R* ~2 V8 x& Z}
/ }$ j. O' i9 ^  }return ret;
6 t7 X$ ~, I% V4 A, U7 t+ g+ S( A7 x}2 O2 R/ F: ?: Y& J
//---
9 B+ C( K& F# H( r$ ~4 Ybool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
3 L8 H& c- b! I, b2 d: Z* t{( Q+ X8 t2 s3 z- d. G3 @
if(PositionsTotal()) // Is there a position?
5 o8 b& w* K/ @* X4 p{+ N6 l& V2 ]7 v$ s3 J
double offset[1] = { 0 };1 ]( [4 p7 F& p. _
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
" K' r! q; G5 z- i8 f! E3 P( z6 c/ x&& PositionSelect(_Symbol))  // Select the existing position!  ^: |" i1 M6 O
{
% S5 Y6 q( ^  g: TENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
5 r- K0 {+ q2 b( Z) i- c+ Vdouble SL = PositionGetDouble(POSITION_SL);
- ?) A6 T2 P; R, y4 vdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));% a0 y9 q# e5 V4 [4 y7 r2 ?1 l: `
if(tipo == POSITION_TYPE_BUY)
. q4 @4 e7 Y: C% p0 T{
% _* ^7 ^1 G% Z8 e; s( K# Aif (cotacoes[1].high > cotacoes[0].high): v6 n2 H( j4 h6 P/ i* @
{0 b% h3 M: c! U3 X6 m+ k3 K
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];; e' v1 G! O3 ^' }
info.NormalizePrice(sl);
. L) R) Z5 b, i$ N8 x- Gif (sl > SL): d( w, f0 L2 ?/ O+ @. {
{/ a6 o( \! }% Q5 y! f0 y9 f
negocios.PositionModify(_Symbol, sl, TP);
9 I) _$ U! u1 X4 A( [+ l) ^/ ^}% T! x6 \, i9 X- X" D
}
! ]) u0 g, Q' {: j4 D}& Y% Z8 D$ w# A8 ~: I! w
else // tipo == POSITION_TYPE_SELL  F  {4 p0 _8 `) d; {  e5 r0 _
{
% x/ h% q% j" b4 D: oif (cotacoes[1].low < cotacoes[0].low)
! R. _5 O1 W  y$ k2 E0 I{1 N( d6 U1 L; N; N2 t
return true;2 C  }+ k; _! d- I0 H! T
}6 u3 V( z. J. k2 m
// there was no position
7 p2 Z; g. E7 nreturn false;
# o+ u* T6 X# p}6 F1 _; S8 q( W2 H
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
; _9 M* D! L( Z% \6 E2 ~2 U3 I到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-12 06:17 , Processed in 2.000518 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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