私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA; i% x. R/ x, M. U8 ~3 x8 q
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。: n* _2 ?9 ^8 W& I& ]5 o# ~
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
9 [, {8 H; d' z" l以下是制定这些规则的代码。8 d( ~  o7 `& n
//--- Indicator ATR(1) with EMA(8) used for the stop level...( K0 i& |! w0 x4 A4 l
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);8 |1 E& c2 {0 C5 b
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);* x, a( y2 r( B. e, \. K3 m
//--- Define a variable that indicates that we have a deal...! d, y. X! Y+ {3 K  z
bool tem_tick = false;9 {* T6 D" D! S  V7 f& S( a2 N
//--- An auxiliary variable for opening a position
! }' d! I* \" c. R* r# t7 g#include<Trade/Trade.mqh>8 S: f2 N0 M$ w4 e, B& w
#include<Trade/SymbolInfo.mqh>
# B0 r0 ^) c0 c+ |6 ICTrade negocios;
" B$ H4 G% E3 S* |CSymbolInfo info;7 q8 X) Z  t% [
//--- Define in OnInit() the use of the timer every second6 ?# B6 ~: l9 B1 @: Q' R
//--- and start CTrade
7 j2 J- K( K2 U! n' R5 c- Jint OnInit()
' b2 i4 ?' k( t. U/ k- U{, o: h8 L" `2 ^& R0 \6 K
//--- Set the fill type to keep a pending order/ O6 m. X9 F# J2 c. [0 D, ?
//--- until it is fully filled
4 B8 S$ Q" \. q  p. Tnegocios.SetTypeFilling(ORDER_FILLING_RETURN);
) B) L; D% k: y: ~8 _, D//--- Leave the fixed deviation at it is not used on B3 exchange
) }! N3 W5 V8 p4 @/ \negocios.SetDeviationInPoints(5);
* e( z7 M4 W8 @/ o; ^//--- Define the symbol in CSymbolInfo...
/ q  `, U% }+ Y8 _* qinfo.Name(_Symbol);
  o8 J- f3 C, f' x+ G( l4 @//--- Set the timer...( a( j) _/ l! ]$ k1 d
EventSetTimer(1);
7 i* _* U! L5 i3 I- G" U//--- Set the base of the random number to have equal tests...! h9 Z2 C- h; K. `
MathSrand(0xDEAD);
4 Q4 y" |/ p8 [# Qreturn(INIT_SUCCEEDED);, _! i3 a3 p+ b6 r: I
}0 ~0 f' {  X* ^2 }( R
//--- Since we set a timer, we need to destroy it in OnDeInit().
0 j, s9 t2 F, E6 [0 mvoid OnDeinit(const int reason)1 R0 [8 E$ @# m# o. p/ c6 {
{+ Q- \3 ~1 t0 P/ F* T, V: \
EventKillTimer();( u3 h+ \* z2 i% @& X7 {  {4 k, f
}
: a4 m( K& v; v! h//--- The OnTick function only informs us that we have a new deal, j7 l% K$ T2 a0 U' Q
void OnTick()' h: R0 G9 `; z7 M2 ]7 W
{# t6 n) u. ?# r; q
tem_tick = true;. [) Y/ y7 [4 @# o0 \: g  ?$ j
}
& M- C  }, N$ s( ^9 l/ S4 w//+------------------------------------------------------------------+
% f" }! Y8 s& g//| Expert Advisor main function                                     |
3 S7 q$ s" p% }! j* P9 |5 I- m//+------------------------------------------------------------------++ O4 e  b1 y- g1 e8 M  e' h
void OnTimer()( R/ |) N9 M, `7 _) I
{$ z# j) T) F7 p- ~4 x, A+ @# y, T
MqlRates cotacao[];
, u2 J( r9 E% ~8 p( p# v/ ~return ;
! P( J  F5 a; P' s' n7 Gif (negocios_autorizados == false) // are we outside the trading window?
8 s# G$ c- o8 b# R5 g0 V& p7 w6 ~return ;
. s" Z) L" h0 t( t5 U//--- We are in the trading window, try to open a new position!
& n- `7 [. b8 P; I6 n4 Mint sorteio = MathRand();9 r: ]& H0 o, j% z9 F
//--- Entry rule 1.1
2 K1 |# W" w& n# B; `3 yif(sorteio == 0 || sorteio == 32767)
) B7 ]8 [' j; A+ kreturn ;; u6 Z& O: p5 D: w
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy! M/ J% O. l/ a/ \
{$ F3 N/ h' j8 F9 t; T5 A) t
negocios.Buy(info.LotsMin(), _Symbol);
" f6 \" _3 n: D9 x& Z9 s}: F4 V& P5 f. U
else // Draw rule 1.3 -- odd number - Sell
6 b& X  ]: q+ g5 k& [, v{
% S! v+ n5 j5 ]/ tnegocios.Sell(info.LotsMin(), _Symbol);3 P; m! v1 x- I7 F
}
- p; A7 o' w* R# {6 ?- w3 r$ v* e}' |5 ]" l6 r) R7 t2 f: a
//--- Check if we have a new candlestick...
' B* `) R0 V4 g/ F# K1 qbool tem_vela_nova(const MqlRates &rate)
5 i" {( J. K* |' _9 W7 g{
, Q; v/ r! U7 O' c{
( r  N7 R& f7 Jret = true;8 R* I7 W; o& C# _9 T5 t' n
close_positions = false;
# H# g. t6 p8 [. o- f}2 N/ P; ]8 J& v+ A$ C- c7 ?; T3 Y
else' O$ w$ Y# r! [: |3 v. e7 V
{: c( q6 z0 X( F
if(mdt.hour == 16)' U) X  l2 T3 ]5 q# Z
close_positions = (mdt.min >= 30);
  f9 h, S2 [& ?. g2 N}
; m3 G) |+ ?& U/ B8 [}, m' }* ]4 {: h% B/ ]8 P4 J: y
return ret;$ |5 D: \" ]% N' M9 K& M( r
}
, }+ A& K8 n# _0 T6 M$ {" b//---
9 ]; [. T5 E. N& B# l  v! mbool arruma_stop_em_posicoes(const MqlRates &cotacoes[]); Q7 D% L$ v, w: U+ K0 L& d
{
# O( Y6 y) `7 w' m2 [7 Eif(PositionsTotal()) // Is there a position?6 T) e3 |9 G+ Q+ ^: k* _* w
{) I' e" \# y' ]( a
double offset[1] = { 0 };
) G2 l. D# `; |0 jif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?, c- \" ~6 n4 n1 ]  \  |
&& PositionSelect(_Symbol))  // Select the existing position!
& S6 }, g& x2 s: h{
, Y3 B0 c: H3 L8 T6 u; PENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);$ N, I, S- ~/ y0 a9 H
double SL = PositionGetDouble(POSITION_SL);+ Q- W9 V3 a! _2 J' b
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));! i, [% z9 w+ S( k+ y& p
if(tipo == POSITION_TYPE_BUY)
' A1 z/ f8 _/ Z& P{
) H# B- C* F2 I) o/ U+ S1 B& Aif (cotacoes[1].high > cotacoes[0].high)! j5 d* R3 r0 T& g: j$ p/ c
{# F0 d* `* q/ r. e' A  R3 M
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
1 J) c8 W( D7 R1 o  B% x9 Minfo.NormalizePrice(sl);
) g( h! y! F" B+ R2 S8 Hif (sl > SL)
  k" U9 ~! X3 d6 Y1 V0 O' c8 P{
' W3 I& j+ H. U# X+ x. y5 K7 L2 inegocios.PositionModify(_Symbol, sl, TP);$ O# t' H7 t+ c5 `4 V
}
- f. f) V2 J- F( `9 K' U}2 h! H( e4 G9 R; S
}
/ g# C! n; ^! d$ I. helse // tipo == POSITION_TYPE_SELL
) @9 b3 O7 D* \: W5 ~{
3 `9 ?+ ]: ?1 C3 W+ K2 Yif (cotacoes[1].low < cotacoes[0].low)
% X* v8 u% C7 b{
. Y2 P" O7 M1 @& creturn true;
+ H8 B: Q9 s& V, C}
: W5 a  S) j; U" ]7 }// there was no position8 n. {" C# o- i$ x! r0 m: i
return false;# o) X. _7 }! M  S( o- e! W& A
}2 o, b5 O; G: ^+ ~& D  ^, H; |# c( B( T: t
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。( K# Y& ~# n5 H& w0 D
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-13 04:34 , Processed in 0.393158 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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