私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA! |, e) I) ?. l0 c3 }6 g9 J
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。/ V6 @: p1 ^1 a( Z7 x/ v3 V
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
: M+ i$ D, a  q以下是制定这些规则的代码。! i- T: i# a) W
//--- Indicator ATR(1) with EMA(8) used for the stop level...: Q! |" C" e8 U- L% I# k
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
. ~* v; {- \7 ~  ?0 tint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
# ?1 @- d8 ^6 x4 T: ~7 n1 v6 N- |, U* n//--- Define a variable that indicates that we have a deal...7 Y  k' _4 G. w/ q
bool tem_tick = false;
: p. c( @# K, e1 A//--- An auxiliary variable for opening a position
8 S  |8 I) R& y" u#include<Trade/Trade.mqh>
$ J; M( a' E2 G) C5 d#include<Trade/SymbolInfo.mqh>
- T( b! i! p7 K8 _! F' uCTrade negocios;5 Z  a" @0 ^  g0 j# L- I. Q. |
CSymbolInfo info;( h* O4 U* Y# Z1 Y$ M+ I9 l
//--- Define in OnInit() the use of the timer every second
# Y0 D8 I5 g6 W2 t//--- and start CTrade
# M7 V; {( Z- K4 M% eint OnInit()& q0 E* W8 J4 L; w) r& J
{
! ^& s+ X' T: j2 A) Y4 M//--- Set the fill type to keep a pending order' T8 Y' l0 Y% ]- f6 n( K. g' M- E
//--- until it is fully filled
9 s- E, O( k+ d) t7 W; mnegocios.SetTypeFilling(ORDER_FILLING_RETURN);/ C7 k  ~6 J8 e! T6 g( b8 o  `
//--- Leave the fixed deviation at it is not used on B3 exchange
, C( ?3 Y1 p; @' i8 znegocios.SetDeviationInPoints(5);
) w  _2 j  u" ]: j9 |//--- Define the symbol in CSymbolInfo.../ s2 t5 K* v( Z. W
info.Name(_Symbol);
8 }5 e, R; w- j; Z4 ~//--- Set the timer...
( m$ T% a6 e3 q+ M" Z. y9 F* h/ [% KEventSetTimer(1);
, {9 R! F/ l& n# ]//--- Set the base of the random number to have equal tests...
8 S% J8 e/ I# f. p. R3 BMathSrand(0xDEAD);) c$ u+ O! \2 ]0 z% m2 [
return(INIT_SUCCEEDED);) y7 \# l+ }1 u6 p
}
2 S7 e1 Y8 K  e+ x* {7 t3 P//--- Since we set a timer, we need to destroy it in OnDeInit().
7 q+ s; T* h& `% X) e& Cvoid OnDeinit(const int reason)- s, |7 P% v% m* j
{
9 S; w4 @, ?9 `! E. Q8 J. }EventKillTimer();/ ^( n6 I- A0 T1 _$ Q
}" x# m' k" t4 }$ `4 G& Z
//--- The OnTick function only informs us that we have a new deal
. x: j" Q$ L1 ]6 b7 b% \void OnTick()7 j  X" D* L% A$ C, O. C
{9 I/ W( r% P# @4 A
tem_tick = true;
/ B7 G3 _+ v5 D  X0 Z}) A% ]1 v' e+ f+ s) U
//+------------------------------------------------------------------+: A2 C% a+ b' ~7 q
//| Expert Advisor main function                                     |4 h3 `# M. a6 m% r  e- x  X
//+------------------------------------------------------------------+
8 N2 L3 b. @" i1 o- x- b% [: Hvoid OnTimer(), P2 j: k$ G1 e$ l! k7 _7 T
{; T% F6 |# U1 o0 X9 N
MqlRates cotacao[];
8 c+ y  N6 l3 M" o2 p9 hreturn ;1 a) Q0 {5 o7 k0 r! f' P* a8 k
if (negocios_autorizados == false) // are we outside the trading window?
$ X, w+ s* C0 s3 A" areturn ;
2 ^8 c+ O- q* g3 j//--- We are in the trading window, try to open a new position!0 s+ E) ^; b/ z4 U, [/ @& j$ i$ {
int sorteio = MathRand();
  H* O+ {* ^1 P+ q& x' e//--- Entry rule 1.18 [/ X/ `( W5 y! i) ?& {" D
if(sorteio == 0 || sorteio == 32767)
- e8 h: a& d. W$ {9 q' F' mreturn ;
; b: e' V( `( \! xif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy; E1 v0 d8 c9 q* L8 L; P
{
+ _8 Q5 T5 j* d7 |negocios.Buy(info.LotsMin(), _Symbol);
, y/ q  u$ ~4 y  |" V}
. r# f" [  F7 t8 L& Aelse // Draw rule 1.3 -- odd number - Sell
. o% k& H7 \5 Q) d/ j( w8 b{. t  A) y% C% V, M, v
negocios.Sell(info.LotsMin(), _Symbol);7 R, m; j" v) G# P$ G; p8 Q
}
4 r6 r4 w( W' Q; I}
8 _/ }6 \" E; y3 S! t//--- Check if we have a new candlestick...
( m: [" a# q' @1 W, ~bool tem_vela_nova(const MqlRates &rate)# `) y) L# m6 T& t! _5 K0 W
{
7 ?( c  ?2 m4 q- k" u% a, {8 Y{( A- ~2 |* B- f3 z7 }0 O
ret = true;$ k% }0 }# \& h
close_positions = false;
8 }9 O- t$ t" F6 J8 h" a}8 @  T3 l( @4 L& \
else) p* R( q8 @9 @) a1 B
{, Q+ c7 G5 I% S  z% o
if(mdt.hour == 16)
4 V9 S. m& R3 U% T5 G4 Uclose_positions = (mdt.min >= 30);; ?& y# e) C8 W( T2 ]6 H
}
; a3 V! h$ R' X/ _}
4 @5 ~6 W! v8 Wreturn ret;. i& z8 d' f- _$ o% }  y. _
}8 j! M/ z& x# s0 X( }3 j
//---
2 h% D) K: V7 c% Gbool arruma_stop_em_posicoes(const MqlRates &cotacoes[]): o, j/ ~; e& o
{
7 F9 ~; D/ _+ B* Y2 g5 m. |if(PositionsTotal()) // Is there a position?
6 D9 z  f# j! z{
1 U$ d; u9 V2 ~+ H7 T7 J' i" idouble offset[1] = { 0 };
( k0 v. V- M7 d' n0 L- Tif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?! Z" a2 V, ~( v9 L+ ?
&& PositionSelect(_Symbol))  // Select the existing position!! R3 F: N3 L2 W8 M
{& u5 Q5 g" R, O2 i4 ^! X
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
9 G8 r! y- B( U: j: ~3 Zdouble SL = PositionGetDouble(POSITION_SL);  o# z" B/ E1 ~7 w+ p
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));# f& H* |$ P7 n3 j0 n! @( X
if(tipo == POSITION_TYPE_BUY)
5 l& b9 Q) q- ?: \. [% f{5 x0 \# r) \* S* O
if (cotacoes[1].high > cotacoes[0].high)2 g# U! L  J: @# y# d
{  K" U3 x  |. f( W/ R' w8 F5 m
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
! t: M! x2 x; q; f. c& {" Qinfo.NormalizePrice(sl);3 C8 D$ c* {  K6 }* `# K
if (sl > SL)
3 O, ^& ^* g( k: x  b, V{
8 @! q$ L: [( Hnegocios.PositionModify(_Symbol, sl, TP);
4 f; ]- _* }$ J}2 j3 b( z" ^& e. a2 L$ E- G
}# w3 c9 o3 P1 t) U
}
* O: _- }( `0 L+ |  Belse // tipo == POSITION_TYPE_SELL
8 q2 {! s. g( j, [( a{
, }. ?6 W+ U1 h6 k$ f9 {) C  rif (cotacoes[1].low < cotacoes[0].low)' q8 y* B' U6 p. l0 W$ x
{4 h9 {4 ~8 F" w/ p" J
return true;- w, Y  a$ Q: y  r3 e
}5 `( p7 c* Y/ B/ d
// there was no position
" C7 G2 l& d- k/ Creturn false;% w" |& g) i2 N
}! c1 d; G7 z5 x! ~+ M* K: X
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。( B3 M1 k, z! f: d
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-26 07:09 , Processed in 0.393870 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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