私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA8 @* J" P% {  c+ o( _
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。# ?! l1 ?/ J6 p4 B3 F
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。. e% Q( H3 p' h! V( T
以下是制定这些规则的代码。
% k5 w# q  `+ |/ `8 C' N//--- Indicator ATR(1) with EMA(8) used for the stop level...: E6 |% C  n& v& j+ p* l7 Q
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
+ Q$ z8 ]3 y  {7 \# E0 Zint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);3 x, [6 n0 `3 K0 o2 i& H
//--- Define a variable that indicates that we have a deal.... y& b5 Q6 k' C$ w. {- g7 `
bool tem_tick = false;1 Z" k0 f5 O5 c! B4 @+ J+ @
//--- An auxiliary variable for opening a position$ k! T) E5 P; e( s2 t5 y4 |  ]
#include<Trade/Trade.mqh>
* E0 Z  u' K( R! b. W  z#include<Trade/SymbolInfo.mqh>
" w* H( S9 `! j% W- @CTrade negocios;
) O" y6 k( D* k" U+ ZCSymbolInfo info;# i0 ]& f6 [' p5 M# _- d6 l
//--- Define in OnInit() the use of the timer every second4 r2 e* e# J! B2 Z2 n
//--- and start CTrade
6 a" K* O3 X& l, Q& Jint OnInit()
0 W1 ?& e+ K$ k: C{
0 _. _$ z* s* ?7 }//--- Set the fill type to keep a pending order
  z5 J& ^: y4 [5 d, w//--- until it is fully filled8 D4 E7 O. F. [/ b& G% W
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
) _. i- T7 x3 a% C9 \7 C//--- Leave the fixed deviation at it is not used on B3 exchange
, q% u$ S  T& k! enegocios.SetDeviationInPoints(5);+ a" W& M, P, n2 l2 @
//--- Define the symbol in CSymbolInfo...
7 |0 a) a$ U: g, J) q7 _  W1 l! p8 Winfo.Name(_Symbol);4 O5 o6 ?- e( R) @) K" ]. Y- Y
//--- Set the timer...
1 V+ q. C1 p& Q2 @' xEventSetTimer(1);
. t* C% w& z- b6 v& _$ y" _//--- Set the base of the random number to have equal tests...
: O+ j3 O3 v5 ^) S. U" I) }  iMathSrand(0xDEAD);2 U. V' }) E, P: B9 X; \/ ^
return(INIT_SUCCEEDED);; h2 J' ]: ]; t7 a, }* c  X5 A
}
+ m4 t, Z. g4 U: T$ L; W, z//--- Since we set a timer, we need to destroy it in OnDeInit().0 h! ~5 {5 I. U# r8 l
void OnDeinit(const int reason)
6 ^# p! v8 v6 Q# D# j{5 X& G1 f5 h/ ^0 G' W  I5 Q
EventKillTimer();
3 X% |, `3 N; N" g8 Z) k# w}+ \8 l$ `* I. g
//--- The OnTick function only informs us that we have a new deal0 \- b# o$ w% \$ U/ z+ ]
void OnTick()) a1 A3 S0 t5 i5 u
{
7 `3 y% u+ R( c, stem_tick = true;
' v2 c! U7 T0 n/ z6 S}
  V6 N) F6 q% [; h//+------------------------------------------------------------------+
) N: Z( i: |' A% S3 ]3 t//| Expert Advisor main function                                     |
+ Z) G& {# h4 m+ `  m6 u: g# y( e//+------------------------------------------------------------------+
7 t. I! T; }! W$ t' X' E/ j/ R/ ^void OnTimer(), s1 I6 v# A5 S: b% m
{
3 R; v# Q9 j0 I9 B! qMqlRates cotacao[];  u  r) T4 l$ s! \2 q
return ;
0 x! \# l  [3 i6 Q5 g0 f0 w4 uif (negocios_autorizados == false) // are we outside the trading window?' X, Q& x5 O3 l4 s, T  J, N
return ;% a$ [' H& X) L$ Q0 Z& U
//--- We are in the trading window, try to open a new position!
$ Y% T8 w2 Z) jint sorteio = MathRand();8 z  m; O  y' i' Z: R
//--- Entry rule 1.16 a, b  I! d+ A4 t6 f
if(sorteio == 0 || sorteio == 32767)+ J; z* }/ B, S' U. M
return ;* N. X8 I7 b% g5 V# j& h, ]
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
3 N# d5 x4 N; ^* `5 ?{6 \; d% U1 e& H/ d% e$ h5 Y
negocios.Buy(info.LotsMin(), _Symbol);
% u" o. Q7 o, m! t& N" z}$ p# V: f+ i: I  Z# o
else // Draw rule 1.3 -- odd number - Sell3 j6 }# ?3 R: a$ g
{) j# p9 O' F7 B, I& P
negocios.Sell(info.LotsMin(), _Symbol);4 j7 H) G8 }0 t+ V# N5 h5 q! L; V
}" Z+ y5 x& g' S' r
}
1 z  [! W& L$ @8 j" ^* n1 o) W//--- Check if we have a new candlestick...
+ n$ Y' j5 C/ b3 n6 \bool tem_vela_nova(const MqlRates &rate)& b6 f1 T! B& h  ^
{  H) {; h9 a/ G, P
{
  }  H2 B9 |7 E" d# T: B' X0 Eret = true;
% [0 g) }( ]* B6 }2 D, E) E" j# y: v, v" Xclose_positions = false;
& l( D) ~3 Q# U9 O}& R" |, `& {  [7 c& w0 P- E
else
2 i4 ]& o& K( z7 W  b{
3 u' ~/ w. k. @: `' I2 C3 fif(mdt.hour == 16)
0 y8 {* A: n! {+ f3 E( c# F/ c0 qclose_positions = (mdt.min >= 30);
! @& G/ s' b2 i3 o7 c1 C}+ t6 }- i0 f( t. r+ c5 `. H$ Q
}1 `+ c& o2 v2 b- d* n; K3 s
return ret;
) m- \, e, v; W; X- e' C1 I}- v4 `6 f; C6 U) U$ I
//---. q4 t& j; w  r
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
, M" p1 T% E3 E" w. R{
6 z7 ~8 w+ }7 ?4 M. g, @" H7 Nif(PositionsTotal()) // Is there a position?
/ K. L8 f" [; P  V* A" _{
# [4 N; ?9 d% w2 kdouble offset[1] = { 0 };) p$ Z" a, Z! k6 _! i( l$ w) L
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
% @) P* t' ~3 z&& PositionSelect(_Symbol))  // Select the existing position!, F9 i. p' n6 r3 {4 g. \, `
{
. O# M3 T- I( X% u/ N9 DENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);' B! A0 G- C: M
double SL = PositionGetDouble(POSITION_SL);
* r3 ^* c/ h" {7 kdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));' K* H: Y2 X. {9 g5 `
if(tipo == POSITION_TYPE_BUY)6 G6 B% w8 ?( j7 c* U6 N1 c
{
2 M( O( x* e5 j8 H( ^# Iif (cotacoes[1].high > cotacoes[0].high)
+ G, T# q* Z* v# Q; Y" i{9 h& |' W5 h5 c  x) b
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];9 O" I9 W0 A) }6 l& U0 l
info.NormalizePrice(sl);0 D/ J/ d# b) o. N2 k* I. W
if (sl > SL)5 Z9 |! ]/ u/ P- L) y9 W8 h: v
{7 t. q& [9 S) V- {
negocios.PositionModify(_Symbol, sl, TP);
8 k- }3 P& c; o}
3 K8 Q: n. W, S}' N7 F, c% t% c$ |' U9 G
}
. V  N# s; c5 jelse // tipo == POSITION_TYPE_SELL
  z# A1 F4 O+ X) t{; a1 X& Y' v2 c6 _  S/ t/ U
if (cotacoes[1].low < cotacoes[0].low)& ]# \$ {( k& o* L$ S' t& Q
{
/ X( w9 C7 v5 I. y. u2 N2 s- Kreturn true;  F" s* v& Z5 w1 R
}$ k9 h2 z- e  a7 \+ L2 [2 B2 Q
// there was no position
/ k8 T/ B7 n6 areturn false;
  s( o* r/ i- U, i. Y" E}) y. `$ a5 O; D& z2 ?2 w* c" j
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
& O+ O/ k9 D; W6 j' {' Q到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|Archiver| ( 桂ICP备12001440号-3 )|网站地图

GMT+8, 2026-4-23 19:33 , Processed in 0.486777 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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