私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
* M% U6 m! \5 E% X: B: p# E在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
5 A* A. _( L" u为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。  }! |5 m2 R* T+ ^8 h% f
以下是制定这些规则的代码。  i& U& `* _. V/ l" n' p; |" y9 M
//--- Indicator ATR(1) with EMA(8) used for the stop level...
0 {& \1 K- Y: f6 M3 wint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);& i  F, M) T0 r2 _
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);9 \1 ]8 g) M* c- G- t8 V3 M
//--- Define a variable that indicates that we have a deal...
5 {: ~: a. Y1 U5 Fbool tem_tick = false;
/ d/ T1 b  I  f1 _( x/ M6 Z//--- An auxiliary variable for opening a position2 y3 j& j+ G8 y0 m' y" A, p5 h
#include<Trade/Trade.mqh>
- T4 O3 R" k+ T) A- q& h/ ^#include<Trade/SymbolInfo.mqh>/ g3 Z* ~1 S! a% s& e
CTrade negocios;
8 b9 J7 y2 L9 ]  C, vCSymbolInfo info;' j4 m5 E: J! F$ w7 }& h
//--- Define in OnInit() the use of the timer every second
! A$ v" k* [' x" }, ^+ Z% `3 v//--- and start CTrade
/ o- D* T% k+ l/ o( y# x# Vint OnInit()
: ^5 Z8 ~8 v* ?( h. P  {" Z3 t{9 z5 }% c1 m* N; [
//--- Set the fill type to keep a pending order6 g- y- j/ p0 a% O8 V5 y
//--- until it is fully filled
9 }9 N4 H3 R+ b+ m; Ynegocios.SetTypeFilling(ORDER_FILLING_RETURN);
, z/ l" ]4 c. `//--- Leave the fixed deviation at it is not used on B3 exchange# J$ y' x; C" J1 ~; [
negocios.SetDeviationInPoints(5);  v: O3 ]% \$ H6 k3 x
//--- Define the symbol in CSymbolInfo...& a0 i" O; C) u9 T
info.Name(_Symbol);
. [+ b% C6 k( ?: \6 r$ B//--- Set the timer...( ?, j9 j  l  R( x# G8 f
EventSetTimer(1);
: V; N; v0 A+ K6 z2 i+ S/ {//--- Set the base of the random number to have equal tests...
! D5 _% H4 o* Y. IMathSrand(0xDEAD);% D# z( G  x/ S- S
return(INIT_SUCCEEDED);
+ ~, T1 X! @7 e}4 U3 w; _% Q( `# s+ K+ b5 ^
//--- Since we set a timer, we need to destroy it in OnDeInit().+ s( y/ i+ ]9 k: S1 H0 ^7 ]) j  z* M
void OnDeinit(const int reason)
# o/ I( T) T6 q1 ]/ i  o8 B{8 o3 T8 w! H; m8 B3 u
EventKillTimer();
& K0 D6 E$ o5 e' [  K! r}- S) Z0 B" l2 |0 {0 x( y4 J
//--- The OnTick function only informs us that we have a new deal
3 ^/ Y5 V! x8 w- R6 _void OnTick()6 g1 F- }7 J9 a. X  p  g. X7 h9 w
{
4 Q# U% c; @5 b) |" d5 B( r6 S/ Ytem_tick = true;1 K3 S" y, D2 ^% ~
}, H4 O4 J5 c  X2 f0 o
//+------------------------------------------------------------------+, L+ W  {! g1 e. o
//| Expert Advisor main function                                     |
4 H* b: Y& q( a  O1 ~# |; `* v//+------------------------------------------------------------------+
4 p$ ^- }0 l$ G  p: g6 K0 d+ mvoid OnTimer()
8 K. V' y5 L- ]4 ^3 _8 U* C( X{( D0 Y; d+ A" G' _
MqlRates cotacao[];
# v6 T) o3 \2 G4 }, W6 wreturn ;
0 R2 w4 @# z5 G( oif (negocios_autorizados == false) // are we outside the trading window?* x& ~# K2 P/ I# t
return ;; S+ t* q. D2 o4 b) a
//--- We are in the trading window, try to open a new position!
  ]- O7 B4 z) [8 L$ Iint sorteio = MathRand();
3 {. O) w4 j  B8 V" h) V  `//--- Entry rule 1.1. u# T6 ~5 W" }1 n
if(sorteio == 0 || sorteio == 32767)( z! ~/ }5 \. a# q  Z# I, o) l
return ;' E' |$ X2 f3 W0 n/ S
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy% H3 i. Q, S! F, f6 @3 M2 p. L1 B
{
; D7 n4 E0 g* u0 u. ~negocios.Buy(info.LotsMin(), _Symbol);7 C* b+ ~, o) D9 O
}
8 d' S0 O3 p4 w$ }  Belse // Draw rule 1.3 -- odd number - Sell
  |8 k( h* S: e6 S4 T{) F; L1 n9 L; J2 v( P* g
negocios.Sell(info.LotsMin(), _Symbol);
/ s' N& x9 w" i$ Q}9 g1 @  r* f9 A, U7 i! T
}
0 [9 l6 y. @! \9 B  ?3 f//--- Check if we have a new candlestick...
5 z/ [, O7 s! D* y7 f8 Abool tem_vela_nova(const MqlRates &rate)! L5 _9 K' h3 X& a5 T. F& n
{
( X, c4 }8 @3 n' [  C! K{
/ n/ @# ?/ u1 j# H8 w$ Oret = true;
7 ]# d; _$ j2 ~: h) Yclose_positions = false;
0 \3 Q5 o2 v/ }% ]6 v- A}
8 X' B% I) E+ c8 h! r$ ^else
7 K1 A: I6 l, G) ]. r2 W5 c$ m{2 }  @  `5 w$ x
if(mdt.hour == 16)
. ?6 D8 ]! X% C. dclose_positions = (mdt.min >= 30);& {0 F* f0 @0 P4 W3 m3 T
}  U9 C4 F0 c. \+ D0 E1 U, n* W9 R
}. i) {- u% W0 b) g6 R& ^
return ret;
3 T0 @  @" w3 ?}8 I5 K$ S+ `9 @0 C; I- w
//---
, |& r2 L, i7 f5 |' \bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
5 H5 _, Z$ o" V4 M' q! d{
8 f8 \! y9 {1 u6 r, O3 Q/ B' fif(PositionsTotal()) // Is there a position?/ T5 D" ^/ q9 ~% ~) _1 S% W
{
% q0 [  l& z% E' {- z& }, q8 Fdouble offset[1] = { 0 };
8 B/ Y  f6 b0 ^9 Q" Nif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
- d! t0 l1 U) r. Q& N6 |&& PositionSelect(_Symbol))  // Select the existing position!
- r! H; V7 j& H; Q& W{
" p' x! e" U$ K" h3 ?5 aENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);. w1 L* Y- R. z& b: E: G
double SL = PositionGetDouble(POSITION_SL);( b7 ^+ f; r/ }- E. T
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
7 [+ J5 L7 |9 L9 V' P5 ~# Hif(tipo == POSITION_TYPE_BUY)
# i! M5 ?4 _8 y8 |{
6 a% J* e7 ?! x) M2 e8 P% zif (cotacoes[1].high > cotacoes[0].high)) B# j7 k. g) E# G
{3 u1 H# y. N1 ?
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
/ d1 Q# _5 |, y/ Sinfo.NormalizePrice(sl);
4 U) K. d1 X7 v3 u  C, F* gif (sl > SL)0 `4 h: a1 t4 p) M" r
{3 y8 X5 \! c6 c5 R$ W! \
negocios.PositionModify(_Symbol, sl, TP);
% t/ f8 M( [% R  Q) I2 u}
" K6 I9 }4 g' ]+ I}
6 y/ k' d# m4 E}& v% k& t& [$ b6 b
else // tipo == POSITION_TYPE_SELL
6 n8 y: u1 w6 b6 {( Y2 ^/ x. c5 w{
& E) s1 d6 h6 y% Y& W& i7 ^if (cotacoes[1].low < cotacoes[0].low)
3 n4 ~& D6 |/ u4 g( t{* z# I, q1 v4 B8 ]0 j
return true;
  q8 |. U0 I! K) `9 y/ f9 R}
1 r' H/ q+ e7 g- C5 p8 u3 w// there was no position4 j5 M; I! f8 p6 ^* f/ e3 u- H
return false;7 l2 Y; @: Z- t9 _" |+ K( a; C3 R% o
}  w6 k  ]2 X6 K& [
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。: L5 G+ Q$ ^: [; W% N
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-8 04:48 , Processed in 0.762151 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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