私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
$ i- D! J9 c' H: D. t在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。) r# r( K: {1 Q/ Y
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。. k+ {1 h) e* `) C
以下是制定这些规则的代码。
0 X! v5 [& J6 H+ Y//--- Indicator ATR(1) with EMA(8) used for the stop level...# u# I1 ^8 L: d8 Y
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
  ?; Y1 n1 b4 Q4 Gint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
! g8 |) j, [7 H1 u% x8 N; X//--- Define a variable that indicates that we have a deal...
. u% x6 {, S! fbool tem_tick = false;1 J& q2 T/ @4 `8 V6 |( c9 i
//--- An auxiliary variable for opening a position* o& P5 G' Z& y# @
#include<Trade/Trade.mqh>4 ?% ?* R+ ~0 d1 M2 G
#include<Trade/SymbolInfo.mqh>' ?  @2 P8 a" D2 |
CTrade negocios;6 x8 _6 I- @' e* G$ c1 y' [# T
CSymbolInfo info;
3 `; t: ^. d6 V6 D7 K0 u//--- Define in OnInit() the use of the timer every second- `3 m8 q; d+ Z9 }* k8 c
//--- and start CTrade2 D( P1 B, W7 c, ?( ?
int OnInit()
6 R0 E1 t% l3 Z: B! ^{
  D) X$ a) O5 ^, ^$ t- J//--- Set the fill type to keep a pending order) d- p' Z+ ~  T2 J. {8 ]
//--- until it is fully filled) |7 h0 J+ h; Y$ K( T- A3 |
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
4 B. u2 ~$ ^& W; {//--- Leave the fixed deviation at it is not used on B3 exchange7 g! ]4 H. H1 {7 E
negocios.SetDeviationInPoints(5);
# A& \* P9 a/ D# |$ `0 G% M2 y//--- Define the symbol in CSymbolInfo...
- N' r8 K" }# q( V7 h% G; C# {info.Name(_Symbol);
/ y  b' ^: ~1 a/ l//--- Set the timer...
7 Z& v1 b$ [# y) j+ x( }EventSetTimer(1);0 ^' H! u7 t" O+ W* z5 c
//--- Set the base of the random number to have equal tests...; F: d# r' ]& S# x" v& _
MathSrand(0xDEAD);4 c7 B- k2 P  c! C( s) [! G7 V4 R% }
return(INIT_SUCCEEDED);
* E3 B4 O! h6 ^}
3 V, T. v& _# Y# }- _//--- Since we set a timer, we need to destroy it in OnDeInit().
( m: p' q0 p# J7 E- S6 yvoid OnDeinit(const int reason)
( x, q& c/ ~; C{
! |1 K8 ?1 a8 G* N1 E( cEventKillTimer();8 p( w1 e3 [' ~- N  X9 Y
}
. u2 H4 d' c+ o" s4 N/ r4 C- ?//--- The OnTick function only informs us that we have a new deal/ N* \; r) w; W) ]! C
void OnTick()
. ^% D8 m! P. g& o4 C4 V7 |{6 S$ V# u* v1 \- }. B- p
tem_tick = true;7 o; A3 s& Q( C8 M+ K. J' h
}" ~2 k0 a0 Q/ A
//+------------------------------------------------------------------+% G% p$ u$ e) V7 g2 |) o! [
//| Expert Advisor main function                                     |! {8 u' x. D3 l
//+------------------------------------------------------------------+
- B7 F+ ~8 [$ h: gvoid OnTimer()6 x5 r! {) F& N9 b8 N
{' E4 c7 U, y1 D0 \. @8 d# a
MqlRates cotacao[];! f9 G8 D2 A- s! g& f
return ;$ o5 k% ?( l! e
if (negocios_autorizados == false) // are we outside the trading window?
* O9 D6 w" N; U. H" Ireturn ;/ F( S; S2 v6 k/ R) t. n& s
//--- We are in the trading window, try to open a new position!8 s$ |9 ]! P; \1 _2 n. B; r
int sorteio = MathRand();
- a, ^- h; ~' D) Z7 d% Y  e0 C//--- Entry rule 1.1
/ ?2 L( O/ m) h& Lif(sorteio == 0 || sorteio == 32767)3 \9 y# ^" y+ H4 b6 W, \
return ;" u2 n9 @' K5 G/ P
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
3 K  J5 m/ f# C/ l0 L0 G" ~+ Q{
) q; Y  I) L  h; ?0 f. pnegocios.Buy(info.LotsMin(), _Symbol);. i. n5 j, X; J# V: W2 @' r
}1 W& \/ [9 b: a. H8 h. `
else // Draw rule 1.3 -- odd number - Sell
$ u, w; y; X& L8 k{
7 n3 T$ n$ N$ Y: ~# w9 snegocios.Sell(info.LotsMin(), _Symbol);6 k) N, Z& h- K4 X" I1 o
}- [* K* D5 @5 j$ B/ \
}
1 ]: @( [7 L9 v//--- Check if we have a new candlestick...; e7 p5 B2 U5 H( K- u  e
bool tem_vela_nova(const MqlRates &rate)
1 Q4 e8 N" I- c% j{, {* d0 L! _  T
{
. @# h# W! G/ a3 l' j% Cret = true;) z4 t) s  k; A' [
close_positions = false;5 Q9 ]* c  H6 S7 n9 n: x' H) r
}# [! Q4 ~. A; p4 s5 N' g. E
else! j8 R0 D  G7 t. M' [+ B
{
1 T  ]2 M' e5 [if(mdt.hour == 16)
2 l  o; y# Q3 M+ {close_positions = (mdt.min >= 30);
) \& g! ~: R- B1 Y}
! g9 B; @* S$ y! h' t0 V}1 F, G' o. y% L% l
return ret;
- e. T0 R  I  p8 n0 _  U% [}- o( p! w1 K, i# u
//---
/ _5 R/ J0 R( Jbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])' z9 n6 j9 Z: T# E1 H
{' D% Y2 Z7 P) ^% `0 z
if(PositionsTotal()) // Is there a position?! }8 X7 \7 _0 V
{
4 \- j" V9 I5 Ydouble offset[1] = { 0 };
4 Y  [4 l+ m  k& Q6 A3 oif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?. z; v1 c2 E2 Q: ?5 y; F, Z  m
&& PositionSelect(_Symbol))  // Select the existing position!
% A# t, b: u9 v/ w1 H3 g{$ z& H+ E/ o( W. s; b
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);! v. X6 t; B2 Z' }  x4 A3 R% F5 i
double SL = PositionGetDouble(POSITION_SL);
, A$ b- g) C: _6 _4 r2 q0 s$ M2 z9 L; udouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
; u) d# c* |4 a  u. b6 w& c$ ^' xif(tipo == POSITION_TYPE_BUY)
8 P# @1 B6 k6 d2 Z0 I9 R! U5 Y3 O; l% N{' M; c5 Z% M2 Y$ V( V' R' H
if (cotacoes[1].high > cotacoes[0].high)
# Y1 P) E+ E* \( z{
+ `5 V9 s9 o7 p8 E# cdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
- M5 ^& B/ q* X' u' i6 Ninfo.NormalizePrice(sl);
# J  e5 D1 [+ ~( A; t9 i( D6 rif (sl > SL)  D2 G" g# _; X4 |* k2 ?. G  F
{
8 G% u( [" {( enegocios.PositionModify(_Symbol, sl, TP);) ~* t4 R9 E7 O+ q  `$ u
}7 d9 @3 U" t( {; Y, U* C2 [( p
}
& N7 Z! S2 M' @0 t, }0 l; m; c}1 Y, Y, p4 v8 y) a
else // tipo == POSITION_TYPE_SELL
7 \7 ?) D- B/ `{1 G1 `9 L4 Y) T' v; O
if (cotacoes[1].low < cotacoes[0].low)( H. O. ?6 p0 o
{
2 Z( y0 n! @1 L- ]: dreturn true;7 x: x% K' h% m3 H* L' e( @* m
}7 T: m% O; ]' F) i$ x
// there was no position
0 [  U8 n  l: e0 T/ Rreturn false;
' w5 o2 {' ]0 N" b& u}1 w  S& r, b9 m6 |5 h+ d3 L
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
' v' h3 Z' ^) h& i3 H6 r! P到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-12 20:19 , Processed in 0.485124 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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