私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
0 U$ G/ D! Y* f+ z$ w在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。  E3 N2 g/ V. r  H. D1 E3 o* F
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。* K# t4 x% D" W( j+ W5 X
以下是制定这些规则的代码。/ C' X" i% C, F9 z
//--- Indicator ATR(1) with EMA(8) used for the stop level...
# @5 j" I5 T1 W# p" c* D( M! z3 j0 vint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);6 y3 @7 g$ U' _* g1 C7 s/ c, p
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
: \, [/ S2 L! j" F. |; F2 c//--- Define a variable that indicates that we have a deal...
$ r! Q5 s) Y/ N" d& i4 O- Vbool tem_tick = false;) w$ D) }+ r7 w( _; J; @: `3 l$ _
//--- An auxiliary variable for opening a position
  ^9 L3 p/ M) q3 T% ^#include<Trade/Trade.mqh>( @" c+ a) S- K, ]0 {& C8 }
#include<Trade/SymbolInfo.mqh>! |- n/ k0 A, Z" y1 P
CTrade negocios;+ @# i; g& \9 s$ g
CSymbolInfo info;9 I: [, g" E4 Y3 ~3 R
//--- Define in OnInit() the use of the timer every second
2 P6 L( s7 q$ R' j: o# v: y//--- and start CTrade
; U- b6 c/ W! k- r% hint OnInit()
: q  C5 c( V7 U3 B  A7 f- C" B/ }{
9 _5 J. G3 E2 ]8 F& N4 C3 v//--- Set the fill type to keep a pending order
6 a. R, U: j. `6 R. k- ~//--- until it is fully filled0 y: L, j/ ]7 X8 g( Z# p/ L+ J) M
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
+ y' }+ [5 x1 q//--- Leave the fixed deviation at it is not used on B3 exchange
! s$ A& e$ Q' U- C- ~7 H& fnegocios.SetDeviationInPoints(5);  ?: ^: d' a& m* Q) H. c& z- o" ?
//--- Define the symbol in CSymbolInfo...8 o0 Y0 `" R( f; ~; [
info.Name(_Symbol);) A% r; f8 G, o$ Y; n( V
//--- Set the timer...% u, a5 _  k- n( Y1 {7 {! a
EventSetTimer(1);$ H, ~, Y, U& l% F/ E, Y
//--- Set the base of the random number to have equal tests...( p" e; W3 V! V
MathSrand(0xDEAD);1 L( c+ g/ ?5 g% O' t/ c/ ]' b
return(INIT_SUCCEEDED);+ t/ A4 Z, U5 U1 w' R
}9 {& X' o( j  a4 [
//--- Since we set a timer, we need to destroy it in OnDeInit().
) U* c; p! S5 o3 m) Dvoid OnDeinit(const int reason), S( _& m+ f, i* q" |
{
; _1 ^6 R7 I5 IEventKillTimer();+ B' t1 J' f, |4 N4 J
}
, l9 o* r2 x! U8 n0 Q//--- The OnTick function only informs us that we have a new deal
6 T$ i( ~( q9 x  Dvoid OnTick()# ?6 C, {* U  H+ g' a3 z7 _" d4 S
{9 x2 L, c% O5 S* @9 H
tem_tick = true;
" ?: X6 V. o: Z8 K1 ~( |( j" y$ ]; c}4 a# k& X4 D7 y5 _
//+------------------------------------------------------------------+" y2 q1 E* c0 ^) M  s1 E2 P
//| Expert Advisor main function                                     |! n* E" T! u& b8 I8 g- z
//+------------------------------------------------------------------+4 R% w4 t/ g- u4 H
void OnTimer()0 ^9 m4 V# d7 I( X
{; y" `* L$ T7 n' r) n
MqlRates cotacao[];9 q% N9 s* [; ~5 j
return ;
8 x- S9 y+ O: [! ]. Y4 Zif (negocios_autorizados == false) // are we outside the trading window?$ |/ h. T7 k- `6 v
return ;% T4 p6 [$ M4 U  ?! ~
//--- We are in the trading window, try to open a new position!
# P( p2 x* \5 i( W; h0 mint sorteio = MathRand();) w$ k. M+ S$ k2 v% w( M$ G: i6 y
//--- Entry rule 1.1+ o6 f$ O7 }( B: L/ }. ?) g; v
if(sorteio == 0 || sorteio == 32767)
( l6 o7 @3 B! U# D" _return ;/ d+ U' ~9 K2 n7 _5 q* M* T: j
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy- j2 B3 t0 o/ \" O1 m0 e
{
% _3 y, R( h7 b- v' U1 x  Jnegocios.Buy(info.LotsMin(), _Symbol);
7 C6 Q% B. x# Q' R- V; u}: H! d+ |' c, f' d
else // Draw rule 1.3 -- odd number - Sell
, g1 v  V" N$ T* r' I  r9 V{
0 x( v. I, P. R6 }' e& d7 Ynegocios.Sell(info.LotsMin(), _Symbol);
- H+ y, x/ K# s! g}: h/ d; X! a9 b. b. s9 N
}/ `5 V0 }+ k! A  g/ Q( h! {6 ~
//--- Check if we have a new candlestick...
: B# R3 y% |- Z8 F9 ^& U# {: H; abool tem_vela_nova(const MqlRates &rate); N0 `$ f4 L; ^. Z+ D7 F9 `
{, @1 B. S5 d+ T% E8 s
{
( \) e" v, k! e; ~ret = true;
  z1 t$ x! n8 X0 k, s/ ]$ Rclose_positions = false;# d- L% B( Q8 |. L
}$ |/ O3 b7 D3 H" t$ V: Y7 _
else; G3 }4 ^$ E! d1 N* Y
{7 h! B% M9 o  t$ z2 x# N
if(mdt.hour == 16)2 R  V0 D* v  Q3 ]
close_positions = (mdt.min >= 30);- v5 b+ O% d# O* t  u( ?! _5 }
}- y( I  ]6 Q0 p0 n( W
}/ n' i, i) ~9 K, i
return ret;0 p/ B! ], g" N# J- e! I
}
8 Q9 G4 P3 X/ h) K& W//---% ~* e' [5 J) L+ }$ B
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
' u+ \: U" J6 b7 d; P% F' r' M% P: X{& Z) `0 B) `" I/ W1 i
if(PositionsTotal()) // Is there a position?7 H5 V. D4 k4 R, m+ f0 m& H
{; S5 \  G1 j: P4 ?$ F$ E1 v. Z! r
double offset[1] = { 0 };/ R. [# P# l, G  Z& G) h8 a6 P+ r
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?! n. d2 h# _& s0 {8 w
&& PositionSelect(_Symbol))  // Select the existing position!
1 z$ q' p  I0 |{0 y: N& z9 I: w+ W* O
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);' O3 _, L- P" A3 M3 B1 B  Y
double SL = PositionGetDouble(POSITION_SL);2 y: t4 B9 E1 g# c
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));* X$ B! `4 Z& g" Q; u+ h6 |9 g
if(tipo == POSITION_TYPE_BUY)
+ G0 i: R/ G8 ^6 F4 [% U3 H{
# N, a( T" Y! w4 \7 iif (cotacoes[1].high > cotacoes[0].high)- p- |  D! F0 W" b0 ~& o' b
{
7 W4 R5 n2 X! C" ~# udouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];9 c1 Q4 h: _( }- H0 ^
info.NormalizePrice(sl);
4 f7 Z: \$ H% m: e( }2 _( h# zif (sl > SL)2 i: n, d; C# K  f
{
# L8 f4 L( z" E$ B  {+ qnegocios.PositionModify(_Symbol, sl, TP);
& h# o. y  B- W* V7 p$ u}+ S; b" I; G  T3 K( z0 l
}
% {9 y7 @' J/ T! V  b}
# _% i$ |7 I  telse // tipo == POSITION_TYPE_SELL- c+ z% l% P  R: D  w
{; z9 {7 s) I5 U0 F
if (cotacoes[1].low < cotacoes[0].low)  ]+ {4 i4 Y$ I' p& `
{0 N, X+ G7 ~6 D0 P2 d" }! n
return true;) C/ b/ w9 P, O  K+ x
}
) X1 |. R8 z% N- y% R) f5 I- F9 [* T// there was no position0 F) w% s( y' b3 z0 i
return false;
* T. h4 u+ ^; ?  X}
! B- N/ {" F; O* G+ r+ \+ d: k7 H我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
# I" K5 I: E1 z4 a8 @/ B到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-30 01:32 , Processed in 2.957313 second(s), 32 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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