私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA- n3 J3 J4 n" J; D% O, H. f* g( O
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
/ `5 @7 W7 S5 q: ?/ |8 o为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
* o' u( J% f5 g3 X$ G! S3 n以下是制定这些规则的代码。. K: X; E' k+ v6 ~# ~$ Z
//--- Indicator ATR(1) with EMA(8) used for the stop level...0 L8 z4 [3 C6 h. ~! W& {! X
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);" ]9 h' P- s+ }2 ~' _1 o8 B
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
6 z. h6 O( Z1 j" n: m//--- Define a variable that indicates that we have a deal...! j* E6 x; ^. ?6 F) L0 a/ e5 `7 Y
bool tem_tick = false;! n7 u6 m/ r$ W4 N% I' Q
//--- An auxiliary variable for opening a position
  k* ]' H& f! S. c4 R  p#include<Trade/Trade.mqh>7 @; h, u5 \2 z" S1 o. u
#include<Trade/SymbolInfo.mqh>
& q3 B  Y7 C8 G9 M* v. S9 v" mCTrade negocios;8 G5 _% I: X: N4 [; ~7 x
CSymbolInfo info;& ~1 c( X, C* R' o( T( ]
//--- Define in OnInit() the use of the timer every second
, p' V8 f! I" \  R//--- and start CTrade& L. n# d7 F, ?# e
int OnInit()3 U8 @5 u4 Q1 M% _9 v
{3 y0 V% O. d5 Y, r
//--- Set the fill type to keep a pending order
  u  e6 Y' d" E$ B' o/ w% x( F//--- until it is fully filled
  h* b' h8 q4 `8 N5 mnegocios.SetTypeFilling(ORDER_FILLING_RETURN);1 y& Q: j; [% b' K; q; h1 w" B
//--- Leave the fixed deviation at it is not used on B3 exchange5 a* ^  z1 e8 S2 H: x2 W, v5 b
negocios.SetDeviationInPoints(5);
2 W" y1 D: |: ^5 C4 F$ k//--- Define the symbol in CSymbolInfo...
4 k2 w5 t* ~, t% d$ winfo.Name(_Symbol);
1 X0 b. T/ B& a0 {//--- Set the timer...5 S( I- l- N0 `; P& O* _$ P4 u
EventSetTimer(1);. e' e: |. r  s
//--- Set the base of the random number to have equal tests...
8 _  h% P  E. p% v: p/ B- {MathSrand(0xDEAD);/ \( g5 g7 \: ?$ {- j
return(INIT_SUCCEEDED);
7 D2 B! Z! h7 v  {- a}
4 U4 b6 t6 h; Q  w0 `5 D//--- Since we set a timer, we need to destroy it in OnDeInit().2 C4 j: c2 [$ _
void OnDeinit(const int reason)
7 T; e  B  P% \{
! N2 w/ r' G  m  W" h2 i9 REventKillTimer();
' Y/ n8 A0 ~1 c3 Q8 k% d}
, a" X% j7 l: J& C0 H" ~) C( o  L//--- The OnTick function only informs us that we have a new deal5 _: W& q' X# x- H6 ~( V8 [
void OnTick()8 _& O9 Z  P0 I7 R7 \, j6 R. c4 ?
{
, l( B8 G6 m3 U3 Item_tick = true;) g& I+ v  S4 x% @
}
9 L, a) m! D- X2 u- f& Z  |9 @//+------------------------------------------------------------------+
  y9 w8 d! g) z7 M6 M, u9 E/ y6 q' Y//| Expert Advisor main function                                     |
) K1 c: x* K) L& G//+------------------------------------------------------------------+
0 b# B# N7 {- s5 b7 I5 ~3 L2 jvoid OnTimer()5 I! z3 J- d  [! A0 Q* f( G
{
3 F3 W- g; [9 I6 c) r( d4 d: xMqlRates cotacao[];
8 q, c$ S7 V. |return ;
8 P3 P9 l: C' h! a0 Rif (negocios_autorizados == false) // are we outside the trading window?  i, N: H' ~; d, t* C& C
return ;* C  ?+ e  j/ B2 o, }- {9 L+ F
//--- We are in the trading window, try to open a new position!
+ o7 ^- c6 u  m* Pint sorteio = MathRand();* t0 [4 r3 A3 e) a- ~
//--- Entry rule 1.1( N' M" q7 q' i  s- z) U, J
if(sorteio == 0 || sorteio == 32767)# h1 R5 G8 Q4 c8 C) @2 T4 g# T
return ;/ e+ p) E& t7 D+ O- m* v
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy: n4 b+ a8 O5 N7 u( |
{
, G9 V" k* K8 p' l  hnegocios.Buy(info.LotsMin(), _Symbol);5 t2 ~: `5 Q7 Q  B+ T" h$ O
}
/ `* H% M3 w  r, _0 ^0 Melse // Draw rule 1.3 -- odd number - Sell
& c4 S6 o" U+ R  c{
+ X; K2 ~2 a% J  g. jnegocios.Sell(info.LotsMin(), _Symbol);. z8 G+ I0 x1 `* @
}
- Z) n1 ]4 s: y* [+ ]}
0 v, ?; T) \5 c" K6 n1 c+ x//--- Check if we have a new candlestick...
( w, h+ `4 e) F0 Dbool tem_vela_nova(const MqlRates &rate)7 c! P! ^! y4 ~3 E: P5 p: N' E
{& i' b; O) T3 r8 U. Y
{
  v. n$ w1 L7 p, S" U. {ret = true;
, @8 u4 Z1 K( S! J6 w- T, Y4 xclose_positions = false;6 Y0 t. o+ P: D5 G+ b6 q& c
}
/ q/ t  t! k# x4 J9 ]else
9 ^; ^- Y" w/ r2 j{6 }" A3 o8 ~6 O1 j8 p" R6 e* D
if(mdt.hour == 16)
5 U" P8 `) O8 J' C+ n  [' Zclose_positions = (mdt.min >= 30);( p% L9 R% P* o  x4 k% x& J& P* Z: i2 S
}5 y" K" h9 |. b. w: O1 K1 O
}
- K7 M* a: w- g2 Zreturn ret;
3 I2 p6 o% Y- n( x* r, ]  i}' N( ^2 x; [# h2 L; G
//---$ S$ r5 q( Q+ t( A* {3 E* |5 d+ v
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])1 k' J1 C7 \' j
{& Z( r. f/ _8 H
if(PositionsTotal()) // Is there a position?
- s- v5 g3 r: [, o{
' n( E8 o0 Y7 @" I! G% |+ ]double offset[1] = { 0 };, b7 b/ A: i' h! H5 C( m- p: j* r3 H
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?& Q* |; M5 a) _) ~: ?% _2 p
&& PositionSelect(_Symbol))  // Select the existing position!9 H- U1 g- J, Z) c# F+ k
{
1 n) N+ x3 R& R5 O. C3 P: t. pENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
$ q. f! ?8 e9 x! r0 A# [double SL = PositionGetDouble(POSITION_SL);
) M7 O: `) l+ V2 j; w. s4 ndouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));/ W+ ~  `! j( N$ h* Z, s( p
if(tipo == POSITION_TYPE_BUY)
, _4 Y! M' p; g2 s. Y& ~{
6 m+ U$ N0 M$ ^' [, kif (cotacoes[1].high > cotacoes[0].high)0 a( Y! x- M0 P! z+ v
{
# S7 W  V# f( x$ Idouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
: c3 f+ O- j6 r) ~info.NormalizePrice(sl);& m) p9 @0 r- ]/ @/ m- K( |9 o
if (sl > SL)  G# ~9 ~6 d. P5 ^( P( u
{8 r" s; d! g: u% y, f
negocios.PositionModify(_Symbol, sl, TP);
4 D. x1 J% T8 s: P3 V/ ]6 `}
) Q. e- P& ^3 f}
' a  E7 l1 `" ~) t' k  X3 r}: h  T9 v, a8 r9 F2 T
else // tipo == POSITION_TYPE_SELL* M6 _7 Y3 f' a
{
5 Z( B# h6 p, o6 s5 }9 Aif (cotacoes[1].low < cotacoes[0].low)
9 }* m* m/ h0 `  o* [$ \+ q{
2 }+ G: C. h$ k/ v! @+ xreturn true;; k" {) S5 A" H7 n7 i3 ?
}" x$ \2 V( j! i$ H/ U
// there was no position
$ ^- K) [9 q6 X; freturn false;
8 h* c2 |& Z. j  h7 d- h& T5 @}
& I/ S* h' V+ C$ Z( w" W" G6 J我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
/ [# |5 g" B2 B3 g+ n3 x4 d. y5 [0 r到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-27 19:29 , Processed in 1.348948 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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