私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA4 _  Q' v% J% o) d# o2 E$ D  d, ?
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
6 Y$ l( Z% f' R为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。* J# B2 Q! O. o
以下是制定这些规则的代码。
: ~1 j3 L3 a2 M9 q( K3 c//--- Indicator ATR(1) with EMA(8) used for the stop level.... b( N+ F$ i# r, r) T. U' h
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
4 v1 [: r8 Z, X! Lint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);" @- x& y7 S7 P( e& I6 j
//--- Define a variable that indicates that we have a deal...
$ y3 |" y! F4 ^0 X# {' \bool tem_tick = false;1 |' p0 m/ T; `9 K: E5 s/ j
//--- An auxiliary variable for opening a position
+ a4 @; u1 ^* l" h6 Q#include<Trade/Trade.mqh>. y' J8 n6 j$ J* u% t, C4 f
#include<Trade/SymbolInfo.mqh>
% O, G$ e# o1 `; D) t) vCTrade negocios;' H5 ~% G( o) u+ u& Z/ t
CSymbolInfo info;/ D4 }4 d/ R' c8 C
//--- Define in OnInit() the use of the timer every second
! Y- k) i9 a, n6 u4 D- k' R//--- and start CTrade$ Y2 R- t& g* {7 W8 z+ g/ b1 h
int OnInit(). c9 q: W* `; a, C. C
{/ F8 E- }8 q+ O9 v' d! U
//--- Set the fill type to keep a pending order" z  s5 F' Z8 s6 l6 T
//--- until it is fully filled  m" U+ C$ S! {8 {
negocios.SetTypeFilling(ORDER_FILLING_RETURN);1 T" E! @2 P2 O  V2 S; A
//--- Leave the fixed deviation at it is not used on B3 exchange/ j! k6 c1 A& m2 b: L
negocios.SetDeviationInPoints(5);+ p" ]* k* Y! G% g
//--- Define the symbol in CSymbolInfo...0 O9 J" L% {4 X7 _+ |
info.Name(_Symbol);, Q9 @, E( a# \" |
//--- Set the timer...
* i4 g' R! m' [EventSetTimer(1);
5 I+ @1 y0 P1 \1 a" b, W//--- Set the base of the random number to have equal tests...
+ Z: |+ w3 J/ `. _2 ~0 J* Y; p" I* I9 M$ gMathSrand(0xDEAD);
. Z! Z& m. ^8 G4 Breturn(INIT_SUCCEEDED);
+ [% B( l: R7 w* d- R3 v}
% w8 J' u. W! B; Y7 @1 e" C  U//--- Since we set a timer, we need to destroy it in OnDeInit().3 K% D! g1 F$ K
void OnDeinit(const int reason)3 ?- m8 {# W$ w5 D" N
{5 I" X# K- m, a7 q$ |# f+ j, p5 V
EventKillTimer();
/ l+ T3 q7 L% H& q; f- k}
3 \2 ~1 S6 f- w3 @! R/ ]# W: \//--- The OnTick function only informs us that we have a new deal3 T* k: A+ x4 V/ W, |
void OnTick()3 K- u! s2 b4 C0 J, @2 f" k
{
3 H8 F0 d1 N, C7 |% w* [8 `tem_tick = true;
, M& m! s$ c3 O0 \. u9 {}
/ t% K* @. G7 ]* o//+------------------------------------------------------------------+2 R" {8 p% z# {% G# O) _. v$ k1 ^
//| Expert Advisor main function                                     |
6 i9 @/ [" G, r, F% L: U! z, p//+------------------------------------------------------------------+
" d4 F7 _3 L  Fvoid OnTimer()8 p" L& N& N. S8 N$ Q" i) ?
{
" S# X9 @4 `. \4 j! ?$ bMqlRates cotacao[];/ `( N; o/ D9 Y
return ;
" V0 j; f. q4 gif (negocios_autorizados == false) // are we outside the trading window?0 I7 u; V7 I" I" J* C
return ;8 u- k6 E6 k3 Q
//--- We are in the trading window, try to open a new position!
/ D! W( O& [' M# k" Z/ t9 zint sorteio = MathRand();7 w- D! D  W7 K9 V. E9 u
//--- Entry rule 1.1/ a9 }- Q0 P5 _" K7 |  ?
if(sorteio == 0 || sorteio == 32767)7 q4 J2 P$ c& I1 }) R- V) b
return ;* V8 g: V9 p$ v# j4 T" X
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
. Z5 K: ]" h3 R: C{
( V; |9 W: L5 ~. l2 Inegocios.Buy(info.LotsMin(), _Symbol);" e+ y/ l9 w' f. h( i$ L: c  f, H
}$ r" D9 _/ ~# K- d; j: p0 }) Z# t( X
else // Draw rule 1.3 -- odd number - Sell
# b7 O; `: g& v) R{# J9 t2 H1 [  B1 b# J$ K: n# B
negocios.Sell(info.LotsMin(), _Symbol);( O# U; ?  [# u" z9 \" U4 f3 q
}
7 ~+ p, K9 {* N; m- W}
; b1 D" K9 |$ F5 m7 I9 W//--- Check if we have a new candlestick...
0 j- O; b/ V4 |' P$ K' N# qbool tem_vela_nova(const MqlRates &rate)
; f/ a/ J, p$ b  ~, P2 E+ ]2 d{' I  N  e3 S5 G0 x. t
{9 @  P/ w7 q% ]" x$ P
ret = true;
) o1 e7 I- S& W, E0 _8 P% yclose_positions = false;- \  _; u, R& x/ l$ I9 y; b
}  I6 B" K) t% \6 W
else
* p- n# o  M, |1 T# e1 ]3 R{) ?: o+ i* h7 e/ F# G
if(mdt.hour == 16)! [+ E* h: E: C5 _5 W2 U
close_positions = (mdt.min >= 30);
& d. n, |* V0 G4 q/ h$ \}/ J  z, u3 x3 y1 P+ k
}
0 _; }  l! G! I# [return ret;7 F4 z8 C0 ?2 G, T5 ?+ x2 [
}
4 V" K5 k$ S7 u, N. L0 o//---
$ @  V6 ^! g+ f" ]bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
# ~. I. g/ N" y" r3 ?8 h{0 V8 K* n9 j2 }
if(PositionsTotal()) // Is there a position?
9 L' `% w# @4 `- ~' s% t' s{8 C: x, f! i9 O8 ^6 f8 B% x
double offset[1] = { 0 };; h& }9 [6 [! S8 B) [' J# |2 A
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
: v* ?% ?3 b! v8 T, F&& PositionSelect(_Symbol))  // Select the existing position!
, e  D8 L  b& t  k* ^; o{7 [* X9 q. j% S8 `
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);9 g8 i, ]- ?  \( n- c
double SL = PositionGetDouble(POSITION_SL);
. g3 v* e  Q1 v1 Mdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));/ Y# B8 o: }) B/ y7 k' [
if(tipo == POSITION_TYPE_BUY)# t( P' t( \/ U7 `" F
{
9 s* R4 B0 l& {0 k9 O1 F2 A3 bif (cotacoes[1].high > cotacoes[0].high)
7 p( Z' A2 k/ u- W1 |{- P1 T- k% ?- a% n1 j/ _3 k
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];5 N2 G7 v+ v- e3 K# V+ l1 J% E$ A
info.NormalizePrice(sl);* D! j( [. i$ A- Y- o
if (sl > SL)
+ o' x; h) P# T6 S6 t2 l8 P{
, }# l$ ]) r9 R! x- w! p$ snegocios.PositionModify(_Symbol, sl, TP);
* f0 J% x/ N/ J. X$ t+ B" {: H6 ^8 z}% Y: _' b3 e4 |$ \: `. D5 j
}
. m. z' n, S" c2 E9 F# q4 k}4 ^: [) h  d* N& r
else // tipo == POSITION_TYPE_SELL! c& [$ ]" f8 D7 n6 A" l$ H( [1 L; }
{- x/ r& s9 C7 r8 @* X' h" ^- ]
if (cotacoes[1].low < cotacoes[0].low)9 X+ k: T: F8 ~
{
9 z. n7 S( ~; h# w! a: areturn true;
. I' R4 B. ~$ y" ]}+ h+ d- d) p7 k; i/ g& W
// there was no position
2 }2 _5 X( D# a2 _2 ^: H' `6 f3 Q* ?4 mreturn false;
! z& p% H- |% ?) K! Z}
- O$ i# ^6 Q4 A  b/ a我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
1 L" a% S5 R4 f: t* K# \' H9 f/ {到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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 21:52 , Processed in 4.134879 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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