私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA  S, e4 _, Y$ F$ q0 n9 G+ a
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。4 b, ?! }6 w# {5 D; _" L, {4 Z! w. U
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。+ u3 ]# x( C2 \" M* ^) N
以下是制定这些规则的代码。# S% z8 ]4 B/ M, K8 B. X. u$ n
//--- Indicator ATR(1) with EMA(8) used for the stop level...
4 b5 r, Z  S" K  B* z5 l0 Q- Tint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
' R. h; m) F8 J; sint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);0 J& P$ [/ T1 s1 u1 }9 O8 w$ o. i
//--- Define a variable that indicates that we have a deal...
! Y8 a3 I/ S" S2 e" }: o; O9 Bbool tem_tick = false;
  M6 g  E: w% d. C8 Q8 d) J//--- An auxiliary variable for opening a position3 f# g" r4 ^7 ], L
#include<Trade/Trade.mqh>
$ n' e* c0 S3 K) H: M# h#include<Trade/SymbolInfo.mqh>
/ S. a4 j3 t( R/ u& d' ?CTrade negocios;6 R9 ]' J/ N' g% X5 K$ _, V; P/ T2 S
CSymbolInfo info;
: o! B% U' E* A" J//--- Define in OnInit() the use of the timer every second
$ R/ p2 j$ O! B6 g6 u: n' s//--- and start CTrade( d- p9 p+ o) P) L$ e4 V
int OnInit()) `( [: Z; ^4 L, `3 T5 L
{4 n! k- i& h0 W) r; i
//--- Set the fill type to keep a pending order
  L* w, J% m; N' {  N//--- until it is fully filled
- E2 j$ P3 z) p. J" M& dnegocios.SetTypeFilling(ORDER_FILLING_RETURN);
' h7 }( A3 D( Y/ {6 |//--- Leave the fixed deviation at it is not used on B3 exchange, L" G( V% s8 R, M
negocios.SetDeviationInPoints(5);  A0 u- V& P1 w6 y$ U& T
//--- Define the symbol in CSymbolInfo...
2 ~, H0 P0 C( Z$ H9 F) a6 R) qinfo.Name(_Symbol);" l* |1 _5 q, J1 I0 S9 `3 F3 v
//--- Set the timer...
" T9 }% ]: G+ u- W# @EventSetTimer(1);( i, s+ k' `. S1 W8 u5 d+ T% x
//--- Set the base of the random number to have equal tests...
9 s" J5 z6 P; l: n% t3 T$ IMathSrand(0xDEAD);
7 A! A- x5 u3 F! wreturn(INIT_SUCCEEDED);  P: R  m4 K0 N% q; T
}" `# g' e, I# e- A' F0 j8 H' D
//--- Since we set a timer, we need to destroy it in OnDeInit().
6 g9 K% d5 }' b# _; Z, cvoid OnDeinit(const int reason); X6 m* D  f" C+ Y& p8 {6 I
{- q* S4 b' _; \: B1 C
EventKillTimer();4 |" B" {3 r1 @6 N4 i; h9 N
}
, _* w+ O3 G$ A  G, K5 Y9 W; _//--- The OnTick function only informs us that we have a new deal
/ T# {4 a. V7 `' F7 hvoid OnTick()2 B* X# ^; Y- }( L1 |
{# `8 m9 z! Y/ p, ~
tem_tick = true;6 m7 u1 k* W& K9 e; G6 u* k. u
}
1 k8 L" f4 n6 v" y4 ?) \$ _+ u- P/ N//+------------------------------------------------------------------+
; s/ s" ~  E- C, K# B7 F//| Expert Advisor main function                                     |  [& Q+ p) n; h/ x; c
//+------------------------------------------------------------------+
* Q- @5 C2 a! A$ _3 gvoid OnTimer()3 B$ b3 B2 f/ F  ?, `
{
/ F* i) z; w' s$ \( S# o9 o' \MqlRates cotacao[];/ O6 r+ L: c# Q) @9 O- E
return ;
$ y% A0 S# B5 hif (negocios_autorizados == false) // are we outside the trading window?0 t# E0 c6 H( o( s7 \
return ;% X# l' p8 \; n% N" t+ w$ |
//--- We are in the trading window, try to open a new position!) S. j& c2 O$ p) k, M) o. e7 W$ P
int sorteio = MathRand();
: y/ [. i( t' G//--- Entry rule 1.1) z" j" g. B6 v& l1 v3 J
if(sorteio == 0 || sorteio == 32767)5 z( B8 f" d7 D: u  j
return ;1 Q; V/ u9 ^9 o+ J* b
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy/ y0 k3 k$ N, s" N* L) R4 g9 |& r, k# l
{
1 }/ K, |* h. p. b* r( V4 Q7 |negocios.Buy(info.LotsMin(), _Symbol);
, ~) k# c1 l5 P1 u8 e}
; s$ e1 O2 O$ b$ T. Kelse // Draw rule 1.3 -- odd number - Sell' R, }  ~2 L9 s7 ]5 D
{; r5 v7 i1 T, N  H. P0 g
negocios.Sell(info.LotsMin(), _Symbol);
4 T# ~* D3 \, c8 P}
3 P2 W( ~0 r* L% _6 Z8 Y: _}% ~( X) V2 _* \3 v% T: Y- \0 N
//--- Check if we have a new candlestick...
  @. T- |% K4 i8 Jbool tem_vela_nova(const MqlRates &rate)
! G6 f) v! [6 p  e  ?5 R{( Q9 ~, M; ^% J3 z  z$ h$ }
{5 i8 c$ I6 Q- w+ M6 ]; |6 I
ret = true;
( B; j2 S/ v9 J$ H9 ]7 O' oclose_positions = false;/ B% j& x% `7 i. }4 u7 w% i$ j
}
1 K! a; z, \! y1 X9 ~1 ]else
+ O$ y5 a0 M( k* i{( H8 Q' |1 ?8 X( q
if(mdt.hour == 16)$ W7 q9 M0 f0 M+ m8 i* N8 a& U; h
close_positions = (mdt.min >= 30);
) I) J4 i- X8 w' C" i& z& T}* p: m1 J" R& Y9 w$ m
}
4 O1 d: y( T/ k/ O5 ?/ {' q( v" b) Rreturn ret;
  V# C: B# u( E4 ]& `  m9 S}) U0 i& R! A3 `
//---
. p! e9 ^( ?4 {) K% D1 Dbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
6 r0 n8 S  a2 Y4 u$ ^2 I{3 y# C5 l* Y1 a. [
if(PositionsTotal()) // Is there a position?
3 ]% C/ C; Z( |0 l+ ?5 g{! @+ p( b0 C: O) x# m1 n* t( S: y
double offset[1] = { 0 };4 y2 Y: ^* F$ \1 L) h5 z
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
- ~4 \* X& }7 q4 v&& PositionSelect(_Symbol))  // Select the existing position!: [: j! [1 p4 X+ O2 G
{
* r3 A/ x1 n  H( L6 j; YENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);5 y6 o6 T. ]4 m# T
double SL = PositionGetDouble(POSITION_SL);7 x! X/ Y) F% X
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
" _/ K# O( B- V5 N, F& `if(tipo == POSITION_TYPE_BUY)# e4 t7 w7 o: L! b# j5 h/ ~. d
{6 I0 H. `+ }0 b
if (cotacoes[1].high > cotacoes[0].high)2 E1 \% d& `; U
{( N1 ^0 I' P" _' h
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];8 K/ B. H, \& [0 S1 F
info.NormalizePrice(sl);7 B4 r$ J* l- V) K( d
if (sl > SL)
0 I) o! r% x- Y' F$ ^! H  b: g! H{3 G1 L" T/ ^; f, o1 c5 Q7 D3 R
negocios.PositionModify(_Symbol, sl, TP);1 A& u$ P: C7 {3 B( J; q, F
}0 |. g+ U6 _5 `
}2 r3 v% n, L+ E
}
- t+ L$ _! S  i; ^1 ~else // tipo == POSITION_TYPE_SELL; V' u/ r" s0 q' C; m0 P5 z8 q
{9 b  U) D" u& S, N9 m; j% m& Q- A# I
if (cotacoes[1].low < cotacoes[0].low)0 z5 p* M! m$ R1 l8 s+ I' _
{7 j6 o% H2 @! V- `0 l4 Z$ `0 k
return true;1 }; ]- P- X( H% s5 e; k4 V) ]
}
! j3 k1 ~$ q; ~0 \$ R" y// there was no position$ R2 e  S2 Z, d
return false;
& m9 U! i$ a8 F! p2 A}( F- k2 l" ^1 U5 G# Q3 T' L6 E8 J7 ]
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
4 `" S- V; I( v% D  v) S: H( K到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-29 01:38 , Processed in 0.547508 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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