私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
, g0 u& o, `1 b$ F* }& `% T3 O0 [. i在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。5 g+ o7 O4 w! A& m
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。3 h2 P% _2 z2 T* c6 H, p
以下是制定这些规则的代码。! }: g" ?5 s0 f$ U$ C- G# h: x
//--- Indicator ATR(1) with EMA(8) used for the stop level...
3 @8 V/ b4 r) V+ i: W2 Nint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);* j( `9 r. h/ L6 u- D* S
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
0 l! H0 A1 n' t; I//--- Define a variable that indicates that we have a deal...5 ?' i9 q: f$ n
bool tem_tick = false;
2 p6 Q+ {, \: v: Q+ \. V' e//--- An auxiliary variable for opening a position6 ~* \# C) L  ?5 l0 z# B6 L1 q& w
#include<Trade/Trade.mqh>
! |  A$ g! D8 e) v5 O0 E#include<Trade/SymbolInfo.mqh>
4 l7 G+ l- E5 x3 N0 hCTrade negocios;& l' p  Y: C$ ^, l8 @/ R: x! [
CSymbolInfo info;) {3 }( g# {: [3 \
//--- Define in OnInit() the use of the timer every second
$ h6 T, H/ f" f& T& N# y& Q) q5 s//--- and start CTrade6 Y$ K  w6 k& J7 d' R! A
int OnInit()) I' g: x. j0 X4 s& a
{+ o6 ]' g" a! b; z! V6 B
//--- Set the fill type to keep a pending order
( A4 H) m3 V4 O6 s  c6 t//--- until it is fully filled
# H& ~  _. Z0 ~6 w+ g" }3 p, c: |4 znegocios.SetTypeFilling(ORDER_FILLING_RETURN);
( }' v' d2 w  R5 W6 Q7 M//--- Leave the fixed deviation at it is not used on B3 exchange
6 G6 a3 @2 _  [' G; Znegocios.SetDeviationInPoints(5);! X* R/ j6 _7 a
//--- Define the symbol in CSymbolInfo...
$ V) p0 b% G- \; @: u; w1 oinfo.Name(_Symbol);9 I$ L. R+ q* j& f* p" T# N
//--- Set the timer...
% w& N  W. X1 l1 LEventSetTimer(1);% Q2 o8 k. A+ Y
//--- Set the base of the random number to have equal tests...
. U7 V9 U4 m3 X) {MathSrand(0xDEAD);+ D" {6 E2 U# m- Q- d! \* ]9 a
return(INIT_SUCCEEDED);1 w" Q( g) g- c/ G: ]* Y
}# x/ }3 c0 W+ B$ _, i" [- z
//--- Since we set a timer, we need to destroy it in OnDeInit().
/ {7 M; [7 ]# e$ f4 s" _: x$ m7 hvoid OnDeinit(const int reason)6 Q" \) [0 }' M$ M) \  [/ m* h
{
5 O7 m+ c, r, j- o0 ]- `/ `EventKillTimer();
2 K/ w7 x' w  _}1 x; \7 W. {; _* l1 @, ?! C4 a) B
//--- The OnTick function only informs us that we have a new deal0 J( a3 y1 J: G
void OnTick()
/ ^/ R/ r+ T# w" O* @{; R; h+ w- e2 J  `0 r1 q- }- {
tem_tick = true;7 U2 O2 j( Z$ `4 s$ A
}* c5 m7 `: n5 j- x: Q' z
//+------------------------------------------------------------------+' J- B. E1 ~7 B$ X+ h8 g
//| Expert Advisor main function                                     |
7 R8 u  J+ `( N; I" k. {//+------------------------------------------------------------------+
; w" Q6 {( I$ Q) O+ y- dvoid OnTimer()
* n- d8 s* z2 ^+ B0 K{3 D* d  L: s- w3 V) S* s
MqlRates cotacao[];0 T9 _9 G+ R* i! d' t2 W+ A9 |( W
return ;- J3 l) ~1 R7 ^/ M2 w; ?
if (negocios_autorizados == false) // are we outside the trading window?
% P/ u6 ~' `0 M4 `return ;1 Q. f) h# |5 V. @
//--- We are in the trading window, try to open a new position!
$ L5 q1 A5 k, r& _3 l* ]' R6 [int sorteio = MathRand();
+ w5 c/ J" \7 N6 r* p, y//--- Entry rule 1.12 }; k, G1 G. n! T0 R3 E7 j
if(sorteio == 0 || sorteio == 32767)
% j/ i3 d$ b5 H3 K1 P2 O3 wreturn ;
9 k) f3 ~. Z* U0 k9 Pif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy* j/ ?1 [$ G% a: A
{6 ~1 I: ^* q3 u+ Y) S8 R
negocios.Buy(info.LotsMin(), _Symbol);
- [9 W1 u* p& x; `7 }9 o8 M) h}
( y" J+ `0 V" ]2 J, I' ~$ _else // Draw rule 1.3 -- odd number - Sell
, _4 {& ~5 S/ K6 I8 h{
! G& e$ _5 @0 O/ ^& Z  unegocios.Sell(info.LotsMin(), _Symbol);
6 }' X6 t/ L  v8 W}
  O. S- E* s* Y' Q}9 G/ z* E. ~% D5 i2 i
//--- Check if we have a new candlestick...! ?! J" D5 u( L" l, n& S
bool tem_vela_nova(const MqlRates &rate)
- l2 k2 k. ?0 K6 h# p5 A/ `8 ?{
; a& a- d1 l* _, [! K{$ j# K; f) t* X
ret = true;
) y9 j8 Q7 X7 Hclose_positions = false;; C6 V, O- o2 M0 J
}1 k9 @+ Y+ ^$ `6 h
else/ q* ]0 p" h1 y4 E0 j
{
9 E+ J5 h: b% V; F" [if(mdt.hour == 16)  z1 H" |, \; Z' U& K; U
close_positions = (mdt.min >= 30);
) o* O0 e. U$ x0 J5 g" J1 X3 T}: u7 S: L$ Z; L: G" m# ~! F
}3 Z$ P! t( o( W! I: F
return ret;
$ J0 ^' b5 p. R: ^" K}2 t% _- K* e- f; J; _7 c( |
//---; ~& |+ `4 Z! F2 B; K- ?% I
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
2 x1 D% c' a! d+ n* P{0 ]; o9 V) q4 a
if(PositionsTotal()) // Is there a position?  s1 X3 r% b; Y( e. g2 W* g! g$ Q
{! f% n+ D$ k% A# C# m; D8 S- a
double offset[1] = { 0 };
: E& n+ |/ d0 W; N2 B5 }; V9 p7 tif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
( e5 \) B* N; l' g9 U- \&& PositionSelect(_Symbol))  // Select the existing position!
! e: E; x  b  I{: W5 [+ l+ o, b- |; T' ^" q
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);, [9 D4 l$ X" v$ A3 Z0 M% o
double SL = PositionGetDouble(POSITION_SL);
; ^. l9 Y# }$ Wdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));+ z" V; a& M& }% Z
if(tipo == POSITION_TYPE_BUY)& b- D  ]% ~7 x3 S; l2 l/ o
{$ \+ ~/ J* h7 X  d4 a( j5 l
if (cotacoes[1].high > cotacoes[0].high)0 ?- p$ S5 `/ o
{
3 R( r' V, j* l7 E8 u  `4 Udouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];$ G# S; \; K3 Q0 J7 e" D
info.NormalizePrice(sl);
. B* b  z0 }5 E2 ?: y( T% Iif (sl > SL)) p; Z) j# R# T" y6 V# s
{8 D; k% s9 T+ B$ E
negocios.PositionModify(_Symbol, sl, TP);
$ p. c8 H' Z$ D% v8 J}
3 W5 j4 Y1 o$ {; H! F& |! F, q}& z: H1 f: g+ R
}
2 v9 T/ w: _1 s9 K# felse // tipo == POSITION_TYPE_SELL
4 c) d8 H6 E+ b, o{2 A& X) N4 E+ `% p9 K, e
if (cotacoes[1].low < cotacoes[0].low)1 W' |: ~/ {7 E  u" o5 }
{6 T# q9 J/ Y" p; U8 C) j, J
return true;3 F- {4 O% g. P& c2 B
}" d% \2 \9 S! b1 e! f' h
// there was no position
0 C) s6 h0 d9 X# qreturn false;; A8 _) i# w! X" o4 n
}8 R4 @0 i  j3 A' p7 I0 M- D- A$ X% G
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
0 E" i! K, N) w  V8 w到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-28 18:40 , Processed in 0.834396 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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