私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
) \+ E8 }0 _+ M) b" X" E在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
# x. i0 h' z5 q3 w) E- ~  x; Q: h为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
# ?& V# W2 ?& `. Q' D以下是制定这些规则的代码。( h' I$ T9 ~8 o- u. p: A6 \% Y4 s
//--- Indicator ATR(1) with EMA(8) used for the stop level...: l; L5 Y/ z/ \( x! a  ~
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);. P& @, d5 t& F, R9 f4 B2 p6 }
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);2 g4 a% Z* @( y9 [0 W+ C6 D
//--- Define a variable that indicates that we have a deal..., n% `% d  O3 ~& R2 v# j  U1 V& x
bool tem_tick = false;9 f& ]7 c' g- |+ l) |7 v
//--- An auxiliary variable for opening a position5 B" |3 `& E, z" r/ z
#include<Trade/Trade.mqh>9 Z' u% @  N1 o9 o6 [- w6 I
#include<Trade/SymbolInfo.mqh>
9 F+ p$ s. f# F1 E7 iCTrade negocios;
! m- l0 a4 K/ \: @( NCSymbolInfo info;
+ _+ u( L% K: A1 t8 ~# [% y! T//--- Define in OnInit() the use of the timer every second* z0 A- Z' K4 [4 `6 O
//--- and start CTrade
/ }1 Q7 h5 `* {- O2 P9 kint OnInit()
) N' b' v0 E$ ~- q# G4 e4 h% i9 z+ i{7 n) j" g4 \+ |1 d% J
//--- Set the fill type to keep a pending order
$ A& O1 w# X8 _5 }//--- until it is fully filled! b. t1 d7 M8 D2 S  ?/ S
negocios.SetTypeFilling(ORDER_FILLING_RETURN);5 O* _9 A  ~/ T8 ~+ i8 B$ [
//--- Leave the fixed deviation at it is not used on B3 exchange! E: _' f  C& }
negocios.SetDeviationInPoints(5);
- k8 U- j. G' L1 A& }//--- Define the symbol in CSymbolInfo...6 ^1 d4 Q( p- @( Y/ o7 l
info.Name(_Symbol);
) O$ b  q$ {# [//--- Set the timer...) d5 o' J* z2 j( i
EventSetTimer(1);
! ]% Z1 l7 ]* x. \1 M' z//--- Set the base of the random number to have equal tests...
4 c/ N6 l* u, Q1 [* S  |  X( G/ AMathSrand(0xDEAD);1 [2 W  f) b+ h6 X" M. G2 C" E
return(INIT_SUCCEEDED);* q! J4 O; G& W# X# u
}- K( Q% z3 b, t/ _2 X- K
//--- Since we set a timer, we need to destroy it in OnDeInit().3 d6 p- }+ j3 }3 a4 o
void OnDeinit(const int reason)# q$ E; W! I! g) R3 g
{8 U1 ^; {) t: o: L
EventKillTimer();# O+ F* t/ w: z! ~. U, x
}+ r' \! ]0 m9 q+ D2 U1 g
//--- The OnTick function only informs us that we have a new deal
3 q- B! O9 }4 u' [4 Ovoid OnTick()
4 b; k+ P4 W  n{
, D, x6 x1 {7 h' A9 r, A* btem_tick = true;
& D) L  B1 V5 U}
0 N0 Z* z; b; n0 g: |- q//+------------------------------------------------------------------+
( G( C, k1 [3 S  g6 n* O8 B% E//| Expert Advisor main function                                     |
1 s" K9 P) l! x0 {( L) h( |0 b9 T" H//+------------------------------------------------------------------+
4 z5 [  l4 {4 ?2 I- X& d" N3 H/ Fvoid OnTimer()
2 F8 x6 l) [  O1 t' A& G{1 k1 r$ U8 k- k$ b
MqlRates cotacao[];
- T$ k) _5 N4 H0 Wreturn ;8 d' y' X* B& a2 m5 g$ F0 E6 U
if (negocios_autorizados == false) // are we outside the trading window?
$ V5 H  p. l/ preturn ;
9 d) P& f. E( p" e5 L# |$ U//--- We are in the trading window, try to open a new position!; p# D% Z2 T9 a6 |
int sorteio = MathRand();; e/ z4 q# T9 l2 T, V
//--- Entry rule 1.1* [# w3 J' J2 w- p5 e  R7 z
if(sorteio == 0 || sorteio == 32767)
* x7 N4 @; a9 J6 V  |! hreturn ;
- X' Q! u( H4 E4 @, k; _if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
/ O' X( o; X' o* `# H{
# X9 Q0 O8 {, Y% dnegocios.Buy(info.LotsMin(), _Symbol);
8 U) r- z" C' ?0 S7 d" Q- U6 G' M}8 k4 G) _0 k2 X2 M
else // Draw rule 1.3 -- odd number - Sell, _. O  \, H1 S$ k( {7 Y
{
- w2 T* L2 D5 a; znegocios.Sell(info.LotsMin(), _Symbol);
0 I" d1 O& x) U& T& e0 w}0 F( [3 ]# A- f( y( J( \
}4 s- m$ P! U9 m% y$ e
//--- Check if we have a new candlestick...
. }/ G) m; ]8 ?; ~1 T  \. Ibool tem_vela_nova(const MqlRates &rate)$ c$ i+ m7 Y8 [( t' V( O
{( b' o2 y8 K8 L% L0 e& E6 W8 m$ A
{& s" [& X; Z# ~- F+ \8 z6 a
ret = true;
4 W; z$ J  R) |7 _close_positions = false;, S# Z! d# t, y+ I! @/ a! j% ~' @
}
8 d' e# y6 r, k) T; x7 Z* relse
: M0 ?5 d. w4 S! A- @  r{
- \( h' o, t: R6 K8 F) dif(mdt.hour == 16)1 Z9 j! r& A7 ?% z' u! r
close_positions = (mdt.min >= 30);' W# @) g4 s) w' o
}6 ?+ w& W- C4 U6 r. M* d) F
}' x, y; H0 `# w# N) V0 y  @
return ret;
+ g: e; V1 J9 z. J" r}" U( I4 [9 B2 R
//---
+ Q( Y+ B6 L9 ^) \' G' o9 lbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])9 ?; r: N1 w7 e, N3 v0 ?( h
{
4 f7 s: k) H5 ?  mif(PositionsTotal()) // Is there a position?
. f: R3 d# Q, d9 f% ~# V' A{
3 H: P) N# t: Q; D2 D# Odouble offset[1] = { 0 };
) s1 H# p& F5 T+ Rif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?! {2 D: r' c) I9 z% d  {8 ]
&& PositionSelect(_Symbol))  // Select the existing position!$ X6 C: g3 Z2 ]) y- \( v
{% q- f+ ?2 P* E1 \5 ?: d$ ]
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
' j& l, z! y& a3 E' Edouble SL = PositionGetDouble(POSITION_SL);( H2 i4 y; p% R  i/ L
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
" J2 b8 h, }6 q/ J# P0 Nif(tipo == POSITION_TYPE_BUY)7 J2 l7 x2 i$ _( L
{
6 V6 m3 s# h$ V0 v6 B3 Mif (cotacoes[1].high > cotacoes[0].high)/ E+ c3 s" W; N9 q+ H1 `
{
3 y8 G% ]+ g9 Z4 k( odouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
" @& E& {/ H8 v9 ?% Hinfo.NormalizePrice(sl);
2 E1 E- j6 Y; Z  x- Pif (sl > SL)
" s/ V2 m* c$ [8 H% R4 z{
; f' W2 v+ @0 c2 |, l) q% B/ s) ?negocios.PositionModify(_Symbol, sl, TP);4 Y5 ?* i' @5 m* p# `9 V+ L; L
}, Q3 R' r$ Y& w6 Q9 U/ m
}
- |; R/ L1 j% W) }2 U7 C}7 v; I7 G6 }* B, ?/ j; E7 n; T
else // tipo == POSITION_TYPE_SELL
4 X8 R7 E' J0 }/ ?& v/ O{
7 J+ Q$ m& d3 ~( L2 k  Y: Gif (cotacoes[1].low < cotacoes[0].low)# G) L/ X3 t0 R# `
{
% r9 ^3 M( C0 @( h7 }; ereturn true;
! X1 R2 R% j% e% B! w}
2 W1 U4 G3 Z% `// there was no position
# C- X2 C2 Y6 }$ qreturn false;
4 ], h* x% d, J6 K; W  J! i}
  d7 F% A3 V$ U2 f3 ]' D我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
5 E( Z! Z7 C  t8 j: S5 s到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-16 08:49 , Processed in 0.481236 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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