私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
: _# N) g6 N* W, g( ?在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
. t9 K* ^( ?. s2 [为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
0 m3 \# a+ B, n  z8 L  d# U以下是制定这些规则的代码。8 m; o* j# L0 J) b. ~& M7 O
//--- Indicator ATR(1) with EMA(8) used for the stop level...
0 J2 y3 h6 X1 {7 Q1 E! \int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);- d8 L% r/ D: ~, ~
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
/ I: F# `+ d. E//--- Define a variable that indicates that we have a deal...
5 _0 S" T( Y( h- m& v7 vbool tem_tick = false;
# a1 p3 V, ]2 R//--- An auxiliary variable for opening a position  A9 w3 ?- i1 b# X0 ?2 D- p
#include<Trade/Trade.mqh>- b$ A3 \' @% @7 O' N: Q
#include<Trade/SymbolInfo.mqh>' j, O6 s' r& b* L2 l
CTrade negocios;
" ]+ D' T) n- Z, y, t9 H6 mCSymbolInfo info;
2 {9 d7 o/ k6 b! ~$ `' c) j//--- Define in OnInit() the use of the timer every second
+ j( w1 y/ U' [//--- and start CTrade% g9 `9 y9 _, w/ a
int OnInit()
# D) Y1 u  \, F/ _) h  M{1 u/ W; [5 z, g1 Y
//--- Set the fill type to keep a pending order" N. |0 `6 W2 i. I- p# J2 \
//--- until it is fully filled
8 m1 X1 Z; s/ Q6 v) o8 E1 k4 }negocios.SetTypeFilling(ORDER_FILLING_RETURN);
& J' z) J; J& J% L//--- Leave the fixed deviation at it is not used on B3 exchange
& `7 k3 I$ b. Z0 Z% _negocios.SetDeviationInPoints(5);
8 F; t/ Q; k  X$ W# Z//--- Define the symbol in CSymbolInfo...
; y; R; h5 z% @; i' s( w3 Winfo.Name(_Symbol);
7 T& v" E" d7 D3 Z//--- Set the timer...' B& y! ^( E2 h9 _; V% Y
EventSetTimer(1);
% C% `! l( `9 v$ t1 x- Y//--- Set the base of the random number to have equal tests...2 E: n8 z8 x5 v2 ^" B# _
MathSrand(0xDEAD);2 w, i4 V' q& Z9 Q5 N9 |
return(INIT_SUCCEEDED);' q4 U4 U! @) }
}/ [1 G. \) G/ @! ~  W' ?+ f. S
//--- Since we set a timer, we need to destroy it in OnDeInit().  y: Q. ^) U  y% v1 N
void OnDeinit(const int reason)
5 @6 J, O# p' K/ |2 @( u6 [{
  W7 v) m  k3 q6 EEventKillTimer();. x% x  P2 e) R6 L
}
* x9 H. m/ L3 S6 n//--- The OnTick function only informs us that we have a new deal+ |5 A! e* V5 P6 i, \
void OnTick()
# v& c# q" h4 B{
* V2 `1 Z4 O3 m; [5 E( p  {! W* Mtem_tick = true;
/ d5 M3 n$ L. ?0 r}, \4 ]# V9 v$ I6 h7 ^8 n
//+------------------------------------------------------------------+
& A6 y- I' U, O, c, `- o& D" b//| Expert Advisor main function                                     |1 S+ t& V2 K* i# w& ^
//+------------------------------------------------------------------+6 n/ J# u7 H9 W2 x4 Q* J4 P2 |# a
void OnTimer()6 p$ `- f8 R. ~1 i% z# X
{3 T3 D: U% Y* z; b! Y& E* J1 X
MqlRates cotacao[];, [6 y; [: U" D( W1 {
return ;1 b3 \" ?: H% f* `$ @
if (negocios_autorizados == false) // are we outside the trading window?
+ X8 P4 M( `6 z1 B/ G  A7 Freturn ;
- {7 n0 m4 Q. J; e+ Q//--- We are in the trading window, try to open a new position!7 `/ |$ L& d4 G; ]' ]2 m" v
int sorteio = MathRand();
- R6 Q1 X! s2 x# N, P$ L- r//--- Entry rule 1.1/ x7 ~7 t. j# C  m
if(sorteio == 0 || sorteio == 32767)4 x; k  B: h* F1 X9 O5 F& j3 Y
return ;
3 M, e; f0 |% A; L/ q) |! j, Pif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy+ v9 Z9 B+ n5 v" Z' N
{5 v9 F, g! o9 m: y* L' i# M1 L
negocios.Buy(info.LotsMin(), _Symbol);
2 [6 ]0 A3 g5 I8 f0 {7 L- ~- @}, A. d) k' M" g; u; s
else // Draw rule 1.3 -- odd number - Sell
' J6 F6 F: ?3 [7 A" |8 C{
4 ~) o" {# m9 H9 |negocios.Sell(info.LotsMin(), _Symbol);
4 p" A, |9 n7 A% `: Z. a}$ w! O" f# `- a' x4 I
}
, S9 |: D8 G2 }8 ^' K0 B5 W//--- Check if we have a new candlestick...
7 V4 W& ?2 E4 f" r  Ibool tem_vela_nova(const MqlRates &rate)8 I. e' o, g) N
{6 o' Z; M2 q' T9 Z0 [
{
5 N( x: C9 N4 S/ l4 o, B9 Xret = true;
5 s% c+ t1 e  \+ E  q1 B" Wclose_positions = false;  @. U7 V* f$ ^5 G/ J1 E8 |: P
}
- D# n0 i/ v7 o2 O& N6 oelse
* e% b. X! {$ J  b{% ^8 K: O5 [* R- V  P
if(mdt.hour == 16)' l' P0 L" l, Z% o# ], u
close_positions = (mdt.min >= 30);
; F2 A- l# k/ f  n: X}
) N; j* E" X1 y* g3 T% e, ^}
- N8 l+ ?' G& mreturn ret;
+ h$ X  x  }/ [0 ?: Y2 ?4 q+ g}
. D! y2 M) j) e# [//---
% i% Z) L* h6 V& u5 u+ a  tbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
4 Q3 S& ]$ Z2 m* H. R3 Z{9 l. L0 H- i* b9 L) ~; x3 H! O
if(PositionsTotal()) // Is there a position?+ k3 L" {* F) x
{0 t$ M; k" u% ~
double offset[1] = { 0 };7 {9 Z; b0 q& C& Y/ M) }
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?: f3 C5 ?/ q5 A
&& PositionSelect(_Symbol))  // Select the existing position!1 {7 i4 _; K. n, q; Q5 K6 e3 y
{
- B4 ^) `% _) D& I* kENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);. i5 D; z; f4 Z/ ~
double SL = PositionGetDouble(POSITION_SL);% D  J' w/ H( U
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));% E. B7 N) M! N8 I, W3 p( q9 o
if(tipo == POSITION_TYPE_BUY)
# i3 U  s9 T) w4 A! f{
! c, v9 F- X( Oif (cotacoes[1].high > cotacoes[0].high): v9 ~9 R0 j' _! S- L: u
{1 U* B( i+ R0 F0 f
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];( n" i# S3 m9 }% l( A( [3 B
info.NormalizePrice(sl);
$ j/ _% c1 E' H; L6 s! S: d( f8 nif (sl > SL)
0 x2 f* [' {" s4 B# ~# O{# p6 J3 S( ?% c
negocios.PositionModify(_Symbol, sl, TP);8 ~: M- H5 F: }" L( P6 K: J2 V
}
3 y* D6 g7 z( P/ V7 ]! V' j: y}
; h) F! g4 H/ n' e7 @2 H}1 N4 S! Q% ^4 U* N( J
else // tipo == POSITION_TYPE_SELL9 @3 D7 z8 g/ }1 i
{: g! `. B& E( o0 O" K$ l0 |
if (cotacoes[1].low < cotacoes[0].low)! x0 }0 n( Q* F8 e
{1 e: ^0 u+ l. p& v
return true;
# ~0 L. \+ }: d/ E' L}9 ~2 b& K2 {3 p& {3 m
// there was no position7 k7 a( T3 d  {$ S$ H  P2 r
return false;! I2 X% A7 i" h* \+ B& o
}
6 W: E, `# ?  j2 A% m' r我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
- D/ `7 t4 q/ u$ i4 t4 i到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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 13:26 , Processed in 0.674846 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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