私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
5 {$ ~& l3 F: O0 m在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。& z1 E7 _, Y5 k  }8 ^( f
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
$ m- C: B( `' p. K; d# ?以下是制定这些规则的代码。) X0 v3 A: \' n. j& |+ B
//--- Indicator ATR(1) with EMA(8) used for the stop level...
9 i; g0 U1 q/ H7 k; R, j0 z' ]+ oint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
+ [4 C% a, X% C9 s1 y( L2 _6 B2 Xint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
$ k; o0 }9 B: I; u1 f0 N  |) \//--- Define a variable that indicates that we have a deal...6 ~* C8 O2 ~; U+ g" ?) q  |  M# D' u
bool tem_tick = false;
& ?' s8 S3 Y* ~4 ?4 w/ M//--- An auxiliary variable for opening a position
% U% H" d- R8 a* [5 Q5 k#include<Trade/Trade.mqh>8 l6 J% s2 k) Q! _; f! z# f* R, M
#include<Trade/SymbolInfo.mqh>4 @) ~3 F3 h2 L4 A2 t) o1 t" `- f2 S
CTrade negocios;! ]  U2 x, j7 s0 a0 r
CSymbolInfo info;7 d) G1 u$ n! k+ Q8 {- b
//--- Define in OnInit() the use of the timer every second
. j  c6 m  u4 o+ c2 I- J//--- and start CTrade% ^2 Z  r8 |' n8 Y4 i
int OnInit()% ^; B: `2 O% _9 X
{" N; @( _4 G7 A, |7 z
//--- Set the fill type to keep a pending order
9 b0 R' }' W; m2 r/ @& M//--- until it is fully filled
3 E6 i& E" l" d- d" lnegocios.SetTypeFilling(ORDER_FILLING_RETURN);8 N8 h6 e; N' f, p
//--- Leave the fixed deviation at it is not used on B3 exchange' X' h1 a/ \: K7 r/ ?0 W6 w3 x
negocios.SetDeviationInPoints(5);
' Z5 b& x* H7 ^! X4 L//--- Define the symbol in CSymbolInfo...2 \$ U& c" W! a. U5 U7 P
info.Name(_Symbol);
5 a/ B2 P# Z' R+ n: n//--- Set the timer...
$ ~1 X' w* R! x0 K0 X( E$ @9 m& yEventSetTimer(1);
3 N1 i. b. N6 a$ ~! q//--- Set the base of the random number to have equal tests...
4 O. s( l9 x* W+ J: X# ^/ wMathSrand(0xDEAD);8 O6 k$ T4 a6 x6 `; s0 F
return(INIT_SUCCEEDED);
) D- I  X: ^6 s: N/ \}, A) s* |" t, a0 {$ p$ i) y
//--- Since we set a timer, we need to destroy it in OnDeInit().2 W7 E$ G; y& t" Z! d( Y; o
void OnDeinit(const int reason)3 H- R( z+ y5 \* m! M0 P- L9 c1 ^, C
{
+ a& Q1 S: G$ f/ LEventKillTimer();
7 c/ v5 F- V% V- @}
. @. b& p. Q6 X+ F  z//--- The OnTick function only informs us that we have a new deal
, W2 @# b( B8 S+ Cvoid OnTick()
) F( X9 @- ]9 [; h1 v{
4 Z( w( F6 A5 W, g" }0 @tem_tick = true;
; p* w- m' p. q: o5 ?9 u8 b}
" Z$ N" o' T2 E//+------------------------------------------------------------------+
/ F5 X! D5 \7 K0 R1 i//| Expert Advisor main function                                     |2 r2 z2 c: B/ G/ D+ t# t' G3 o: b
//+------------------------------------------------------------------+
% I2 ?/ V6 e: D" B6 J) Tvoid OnTimer()
) Q6 I9 A  o4 h1 G- W. z{+ a5 y/ J4 [" n' ?
MqlRates cotacao[];
4 J; }! Q; H1 `0 }4 L, Ureturn ;
- d2 ~$ {$ z$ K; H/ w) d, Xif (negocios_autorizados == false) // are we outside the trading window?* `( _9 U" i" S5 A* \
return ;5 P6 |( E2 G$ t4 r& `6 D1 Y  C
//--- We are in the trading window, try to open a new position!
9 Z3 b5 t( g# _+ q6 {) L1 vint sorteio = MathRand();
8 M( Q- D6 o' t  T) x//--- Entry rule 1.1; U/ E1 r/ w7 _; u7 S
if(sorteio == 0 || sorteio == 32767)1 J/ w2 e2 M& R7 Y9 H# e
return ;3 U! |  ?! A( V, j) p
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
" q$ X6 y( M3 X; L- j{
0 o  }- e( a" E( c& x& K% c" Fnegocios.Buy(info.LotsMin(), _Symbol);
( D& a( u4 U" V}7 W" O9 \, P* F8 B# `* H: G
else // Draw rule 1.3 -- odd number - Sell1 \; H" K) q: B: H( R
{
: X  i( Y, t  ]1 p# G$ b- snegocios.Sell(info.LotsMin(), _Symbol);6 N6 ~/ i0 |7 {. T7 ~
}
, I* V: t; }1 R) y  R" {9 a0 \$ M}
  x- s) c" S4 T. m7 s0 B. V% C//--- Check if we have a new candlestick...) T- w1 S5 K2 L- b
bool tem_vela_nova(const MqlRates &rate)
' ]; ^. l3 A4 z! l6 r{
" i8 y! J' L5 e9 Q$ f7 [# S, ?{
  L" b0 G7 k: G. {9 ~ret = true;% ~3 t3 g) G. J1 r2 k
close_positions = false;1 ~4 z- L% ?/ U: f. W
}
# o, E( }$ Q4 T: Y6 g# k8 Telse
2 G9 @8 \6 y( U. g2 i* {% }2 R{7 q; }5 }, t- ^. p3 _
if(mdt.hour == 16)
+ {. v$ G, r0 _0 r8 I  W. dclose_positions = (mdt.min >= 30);
7 g& ^: G5 N6 `' C' r) Z; g}
# e: e" J9 E0 B) E}0 t9 o' j& y, I: T; i3 j2 K; n  n
return ret;
" o( s5 L) Z) |# r) g9 T}
! h: F( I* G, k. g//---
3 [, o' h: P2 c& Ubool arruma_stop_em_posicoes(const MqlRates &cotacoes[]), @8 f1 G' a# B7 c7 T1 i
{9 f" h2 o& ~8 ~8 m
if(PositionsTotal()) // Is there a position?
" J! F' l  H; {8 b: V{
6 m6 w3 F+ l1 R# ]# s( Z: ydouble offset[1] = { 0 };. o7 J5 }- H3 r* E; r6 W# B. D
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
0 g' k% i, t4 t6 x, Y) [&& PositionSelect(_Symbol))  // Select the existing position!
* U& F. i; G2 M" m; Q. m3 k{  T* E) j- S* H& ^6 h
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
5 q  E9 l3 c  zdouble SL = PositionGetDouble(POSITION_SL);
$ }# x+ }& u$ |% T) W3 U5 xdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
$ ~9 J* U2 b7 G! B8 m- \) t# C# \if(tipo == POSITION_TYPE_BUY)& q- a, k7 X5 o2 s! G
{+ l6 d- ^$ J5 `& O: a9 p4 r
if (cotacoes[1].high > cotacoes[0].high)
) s! ]+ H8 ]& V; R% s" `{
/ v8 J! N$ Q" y! k/ _1 wdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];( S* d# N! K( e* E) y
info.NormalizePrice(sl);
; X/ f; l7 N7 n8 rif (sl > SL)
; Q* U- N- i: n! y6 s{! G0 \& I. ~. W, ?. k
negocios.PositionModify(_Symbol, sl, TP);
) W- d3 ^% s- \0 ?0 ^$ ^$ l}' t" M3 ^0 b& G9 a* b- w& ?
}
0 V* |2 M/ f9 D. N5 t}( v* M6 p- J1 K! c' q
else // tipo == POSITION_TYPE_SELL
( o6 c5 ~7 B) B* l) [{6 |7 I# ^) K  M3 X7 l
if (cotacoes[1].low < cotacoes[0].low)5 T8 l4 ~1 b( o6 W6 a
{
6 b1 O* W0 ~" T. x. v, ~return true;/ i7 i: O7 b8 k7 t6 a/ M
}
) h1 e' C! ^+ I* F) \4 r. O7 n9 f// there was no position
: d; y3 _# s, O' mreturn false;' ^8 C+ {0 a3 C+ V6 V: P
}8 A4 O6 e+ t  ]; ?' m2 ^7 E& j/ m
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。% @  L( p2 Y& \2 T, A
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-12 09:31 , Processed in 3.506265 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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