私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA8 |0 r3 f! V4 E' @
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
2 z# e7 z$ \* X# \5 ~为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。( M6 X2 U+ O6 N
以下是制定这些规则的代码。2 |0 S' J- s2 Y; d
//--- Indicator ATR(1) with EMA(8) used for the stop level...7 q9 _' p& u2 Y1 I# q1 Q" M
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
0 z+ }7 a8 Y- bint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);: P; {5 H: c7 ?! v4 Q
//--- Define a variable that indicates that we have a deal...
+ J9 {* u  J: Q* d; wbool tem_tick = false;
& \9 X. e9 V! Z//--- An auxiliary variable for opening a position6 I. |& X9 j$ |4 G5 V* o, U
#include<Trade/Trade.mqh>
( v: C5 m( C4 X. e$ B#include<Trade/SymbolInfo.mqh>3 F* ?+ m7 ^8 t* U- S: W1 G
CTrade negocios;: g1 o5 Q3 m* g
CSymbolInfo info;
+ ~4 q- C) k  Z0 w7 I, n" R//--- Define in OnInit() the use of the timer every second
6 f) z! W# K  a//--- and start CTrade
) [, e. H6 y1 E7 O% V) Hint OnInit()1 J0 |( P9 P3 k) |
{. ~  U3 t3 Y. N" V! y% M
//--- Set the fill type to keep a pending order
( X: m6 V; n* O* ^//--- until it is fully filled
+ q# D( w% L+ Y" Tnegocios.SetTypeFilling(ORDER_FILLING_RETURN);8 c3 ?; T' n: N4 `
//--- Leave the fixed deviation at it is not used on B3 exchange
- ?# v7 m9 f' L' b" _negocios.SetDeviationInPoints(5);+ H. ~& G5 Y! h: E
//--- Define the symbol in CSymbolInfo...' \1 ~5 O8 _3 u, S8 C% b
info.Name(_Symbol);
) l/ x+ I; ]/ N9 S/ w//--- Set the timer...
. a4 b, Y: x9 m0 ?/ YEventSetTimer(1);
6 Z; l- W( P8 P3 [( f//--- Set the base of the random number to have equal tests...
4 s6 ]+ F. X% F8 dMathSrand(0xDEAD);1 u) D+ c$ }) i
return(INIT_SUCCEEDED);
2 E8 K: \. p; k0 R: I2 x% s}
5 q+ _* c, N! N: ~: a8 m$ r7 R* |//--- Since we set a timer, we need to destroy it in OnDeInit().* E" P+ x/ P( G) Q& _' h8 S7 ^( V! c# M; c
void OnDeinit(const int reason)# Q) ]2 d, [5 }' f& M1 J( i7 i
{
7 ~4 Y# _5 U' jEventKillTimer();) f2 d+ F4 o8 G- b, M: a
}' b+ A" X5 w" R# j8 I
//--- The OnTick function only informs us that we have a new deal- g3 t2 k( r# n  o3 T
void OnTick()
9 }/ r4 A: l5 h. u, c0 R{( B/ l& {4 X. D) Y* ]' O, @
tem_tick = true;
, e+ a' j9 k: K: u6 @  b}# E5 N0 p# }  J9 s# p/ a
//+------------------------------------------------------------------+
5 |/ Q* }2 x  ~4 Y% c//| Expert Advisor main function                                     |* e+ Q" `( v8 I8 k4 k% i  l/ X
//+------------------------------------------------------------------+; Y6 t- V( e! V- C& ^+ t  h
void OnTimer()
" n$ E# m- N+ Q, K* t. {* L{- d1 G: `* Y7 ?: |% y; X/ m
MqlRates cotacao[];2 Y, o6 C1 T8 u) c5 w) g: k' C! r
return ;& @: ~6 L5 s% l8 u
if (negocios_autorizados == false) // are we outside the trading window?! O) B3 N, Q/ ]- ~- _$ @1 w
return ;
4 l2 {! j9 x" {9 b4 O) T9 {* F//--- We are in the trading window, try to open a new position!
+ @  ?+ w  K6 W; x3 M& Q& \int sorteio = MathRand();
( a" O* |! T- M//--- Entry rule 1.1' u4 ]8 \2 Q: K* R, \
if(sorteio == 0 || sorteio == 32767). w$ c% F% _$ S( c- H
return ;
6 [8 V4 _% m% e8 p( Fif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy! P  f- z/ H0 w" }' t
{
3 m% ?% T* }% a( ]* z+ `negocios.Buy(info.LotsMin(), _Symbol);$ S6 }8 l: Q& C6 R% B3 w
}
# c& X% Y& ]6 l9 @else // Draw rule 1.3 -- odd number - Sell! B4 ?$ a: c. q* S. f8 x
{
0 V# |; }  i4 u: N# Hnegocios.Sell(info.LotsMin(), _Symbol);/ ]7 T3 C8 Y; {* Q
}
& A+ l3 z6 u7 Q) t8 W}) x  L/ u8 ]' l$ m" x( s! L( P
//--- Check if we have a new candlestick...) n. }* s' |7 o2 ~6 S+ K6 r2 G
bool tem_vela_nova(const MqlRates &rate)
, Q. C+ I$ R% T: B{) Z8 s4 i" W$ ]( ]
{, V5 X* k: o8 Y9 i  }) r9 G# A9 a) h
ret = true;( M2 s# g5 p& S- s2 Y) N7 Q: v+ M* m
close_positions = false;
9 m. l/ F7 L: G0 s( p; e9 w7 W* o, [}8 n( L6 x& n3 n9 q7 o
else
7 B2 T& w8 L1 z' [: Z% i: A{
2 g9 ~9 W( _4 x( [# gif(mdt.hour == 16)* \0 D" L1 X* W9 @  w1 D% W- `
close_positions = (mdt.min >= 30);& V" y8 w0 w5 {7 _# I4 a, m' z
}
- [! D5 T9 A/ a! A% ~8 S}4 [+ }$ I: m0 t6 h
return ret;
+ c0 s4 [) `/ X; N; ~0 ~}. P! r- J% E' l' M* L6 W& s6 X
//---
9 r$ d- i0 ^) a* Obool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
$ E  \8 ]  v9 X, n& v' V{
2 }! W1 o% m1 Pif(PositionsTotal()) // Is there a position?
. l6 D) c! x" h) l$ a- \{  z) e0 _! [: B  r
double offset[1] = { 0 };& M* ~. l8 m. S3 \4 g; L) y
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
  e5 |* U9 |) ~&& PositionSelect(_Symbol))  // Select the existing position!9 {# ~" u: R! b* w: z# s& I
{
6 t. b" [& F1 vENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);, e3 d6 a+ E- H. x: h" c7 D
double SL = PositionGetDouble(POSITION_SL);
( q! [% l% [; |8 sdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
2 r# R$ y% C% y. W$ A5 o3 ~3 ~if(tipo == POSITION_TYPE_BUY)/ p. c2 t; P. `3 @  X+ K
{
# {/ O! `% `1 z) b+ v6 q! Kif (cotacoes[1].high > cotacoes[0].high)
* O" F6 y& I; o6 e4 J{
/ M4 `5 ?* X8 r2 A6 sdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
6 A* n& b5 I* G0 Z( s! ]; p7 hinfo.NormalizePrice(sl);
' s8 p+ g) F# B" Cif (sl > SL)' c  }! ]* W& c* p5 t+ ^; z* D; p
{# m% r# D8 H1 s
negocios.PositionModify(_Symbol, sl, TP);: y' M/ p$ c# `
}; V6 \* J! i/ W$ L% M
}* E* p$ F% Y0 _& z0 T
}7 u( ^7 j% K$ B/ m
else // tipo == POSITION_TYPE_SELL
) F* W: w* ^4 ]4 L4 _3 r+ H1 ]6 E) b{
( M. I3 o$ K8 n# V6 kif (cotacoes[1].low < cotacoes[0].low)9 f$ ?4 e" |4 n/ X3 T9 d
{  I0 o. p- ^! [; x
return true;& c( j2 T; Z' W7 q
}
3 u7 H  b0 F3 C) t+ e- `! Q" e; @// there was no position7 P1 P7 O8 I7 E
return false;# V) w* d9 X  S. u& ~7 c
}
% A" K( A) L' }/ d: U+ c9 O4 n' g# O8 P我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
0 x0 O' l+ ^9 v' Q6 O, d3 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-12-26 20:36 , Processed in 0.394969 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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