私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
' \- f4 M2 ]' T7 a- n6 X在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
3 w" ^* X- E3 b- G) {为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
6 |9 J2 Z# Z% [' s以下是制定这些规则的代码。
! S, \: `/ i9 D" n# f//--- Indicator ATR(1) with EMA(8) used for the stop level...
/ y" V1 X: Y! I7 Mint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);8 ^" e1 d, E0 e9 b
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
( @/ F" {" S1 Q- b//--- Define a variable that indicates that we have a deal..., w6 q- t7 X& c; @9 K2 z. O$ ~
bool tem_tick = false;
6 i0 Z5 L  Z% O) W0 n2 E+ K9 S//--- An auxiliary variable for opening a position
) f; c  j- m7 L3 Q( l7 Q8 d#include<Trade/Trade.mqh>& g/ Q2 W) s8 x5 x. T
#include<Trade/SymbolInfo.mqh>
: |8 s9 j4 e: i, l7 N% `CTrade negocios;$ D7 A2 m2 z9 _8 A- y$ s7 N- H
CSymbolInfo info;
$ E* R6 u! B# \2 J//--- Define in OnInit() the use of the timer every second
, \/ [+ P8 q5 ~! q9 f& z2 i//--- and start CTrade
8 f: S8 ~; ?" S! j4 b' aint OnInit()
, u" b& p9 |3 s, K. t) L{
" q; J' g  t+ e: L  {/ E//--- Set the fill type to keep a pending order! ]* k% w1 V- ]4 O  e. A' P8 E
//--- until it is fully filled, g6 H2 r; _' f; W
negocios.SetTypeFilling(ORDER_FILLING_RETURN);& R' q& A2 D  w8 N$ l# _* p
//--- Leave the fixed deviation at it is not used on B3 exchange- A$ K1 G; M# _4 `4 Q  U
negocios.SetDeviationInPoints(5);0 c# \$ n! Q; E
//--- Define the symbol in CSymbolInfo...' Y  G' u5 W9 H
info.Name(_Symbol);& S8 W1 R! r' D! A! |
//--- Set the timer...
- K6 k* T) m8 c$ o) BEventSetTimer(1);
  ]2 j, X/ j9 Q/ e8 o9 G//--- Set the base of the random number to have equal tests...
' |8 V& g) d9 z. sMathSrand(0xDEAD);
* d# h+ `+ B# I4 K: nreturn(INIT_SUCCEEDED);
- M% C/ a9 k( p  n0 h1 X/ y5 C}
) _2 u) ]( E7 [1 B2 l//--- Since we set a timer, we need to destroy it in OnDeInit().! L+ n7 f0 `: ^
void OnDeinit(const int reason)
  d  H& e6 A, f: Z( B3 b8 ?; S  r{
3 T* G! `! D- ?EventKillTimer();
3 q; k+ E: L! s9 @}! G& q( k0 B  P0 e5 @
//--- The OnTick function only informs us that we have a new deal$ v0 z* o8 i, B4 k! e. k2 w7 c
void OnTick()
* c& Q+ a$ T1 l) g, f{
* ^% {2 P; j2 J' atem_tick = true;
" n2 c8 W; Y3 t" u; ]; F. s& Z}) h" }2 `! i' [8 k
//+------------------------------------------------------------------+
9 P1 a6 G* M$ R( V//| Expert Advisor main function                                     |
% S5 q8 \* s% o1 i. Q//+------------------------------------------------------------------+6 x: e) m* D: p+ n
void OnTimer()% w, Z- P) `4 Z) Y& \- a$ x
{. S, z9 P0 n: R0 B
MqlRates cotacao[];, k/ @4 A! _/ B; j5 s7 J: ]( O
return ;" {- ?9 F3 a6 x6 f- R
if (negocios_autorizados == false) // are we outside the trading window?3 `! y- W( J0 V9 h3 O
return ;
8 X4 u+ [1 f6 L! T* v4 O//--- We are in the trading window, try to open a new position!3 i1 k% D# \4 o/ [$ _" s
int sorteio = MathRand();# y1 W( n( i: A  e% t
//--- Entry rule 1.1
0 B0 K1 F# `( L+ R% @if(sorteio == 0 || sorteio == 32767)
8 q! {7 J8 O6 x5 k0 N) @( mreturn ;
2 _$ |+ P4 g7 U% @( oif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy$ f: M3 G' }2 K. }; U3 D
{5 c: @* d: c- f5 ^% l
negocios.Buy(info.LotsMin(), _Symbol);
7 k4 b/ i4 G  {% w}
  ?9 P3 ~: k- o7 T6 t$ belse // Draw rule 1.3 -- odd number - Sell
1 v6 L' D" ~2 E{0 `# ]" ?2 D( e( ^9 \5 a
negocios.Sell(info.LotsMin(), _Symbol);
& o# A9 X" @6 t5 [}0 m" ~# p" X6 r& L
}2 x3 o8 U) b! h2 t* ]3 Z
//--- Check if we have a new candlestick...
8 E; t' F& y* r# C  D- u9 C% ubool tem_vela_nova(const MqlRates &rate)
9 D8 J( `0 f# W/ K. u$ }9 ~3 X{  w" k  h; W) C; U9 B; e
{
' S6 A: H0 A& X- n- Tret = true;, |: r/ \$ `7 Y1 i4 Z2 |
close_positions = false;, J, P- S' p8 ^8 @& u& M
}- w+ V  w; Y* D& e. X2 P- x& j8 s8 F' _
else
8 i$ L) v% ~. I{
1 K* i% o" ?/ h0 Uif(mdt.hour == 16)4 Z5 u) E1 C$ l3 d
close_positions = (mdt.min >= 30);
( D& _+ y5 g& n}$ G& g) `9 q! N4 @
}
) Z1 [4 j& ?- |5 Q& i( J- r3 @return ret;0 W- R( P, v1 K/ D( H+ I
}2 j7 u' I7 l% ^% L4 e* T
//---* M/ b3 a' A" O  C5 x! ~
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])+ W, \% ~9 q7 z* S6 g
{$ |* v; P3 ]" R# d0 ^5 Q; E
if(PositionsTotal()) // Is there a position?
* Q0 t3 O: a; U' O7 J{9 D7 y, _0 L1 P; w
double offset[1] = { 0 };
7 ]+ d! W6 y3 B: P1 \5 {& B- ?# Yif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
" C# g  T* c7 }. d! H  t" N&& PositionSelect(_Symbol))  // Select the existing position!7 L. q: c  s) u* \% X
{/ q( e7 z8 _. s( w% ^
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);3 Q( l' v5 n, ^, ]: R
double SL = PositionGetDouble(POSITION_SL);
6 M" R: b" U1 e* [double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));- Z/ z/ r5 }7 ~) c; ~$ ~
if(tipo == POSITION_TYPE_BUY)
# t% v3 g& w6 H7 B1 c3 a" l- a{
9 P  s$ K/ k  r2 U3 _' `if (cotacoes[1].high > cotacoes[0].high)
7 L5 c' ^3 i# l! `: I/ A. n{) ^& x: k8 U- A' S
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];6 Y+ i1 F! V2 L' R( O
info.NormalizePrice(sl);4 p  U" h, Y" M& l
if (sl > SL)
7 J1 l) [  T$ x9 F/ c. ]% {{
' q2 _2 ?0 y( ]. Lnegocios.PositionModify(_Symbol, sl, TP);
8 [, f( ]" I) g}
. b% r/ t; S6 d5 I7 q/ w}+ v! ~/ G4 ^. ?
}5 W2 U# ]' q3 h: h( ]
else // tipo == POSITION_TYPE_SELL6 B0 f* k0 O/ l% `# z3 |# `
{
# g1 c! k7 z2 p' Q! mif (cotacoes[1].low < cotacoes[0].low)
) v6 j0 p( Z! D# G{
9 O$ J" s% j7 p4 R5 o) Xreturn true;
0 n5 u7 r2 ~$ a" @- B}$ p( q) a, I/ N
// there was no position* _! S% E0 p. S7 s) U  Y5 F3 m3 ^
return false;- j9 Z. v* {# x% e
}
  V! Y$ ]) t. Z: P4 N( J/ n我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。* K6 O/ A) M, @% x5 e: }& }
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-12 15:22 , Processed in 0.396879 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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