私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA3 h5 h$ y  g# |& ]8 T3 F
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
. a3 a% ]" p/ \/ M. v  e9 ^为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
' [* b+ |! d7 Z1 e  L以下是制定这些规则的代码。
8 T2 i, y1 i( Y6 y//--- Indicator ATR(1) with EMA(8) used for the stop level...* d9 C1 B0 v; v# }/ u
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
; V( D0 ~8 e" r' Yint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
6 p( ]( a6 c6 e0 w9 p1 n: p% h//--- Define a variable that indicates that we have a deal...
3 w8 p0 p9 t- F( u8 V! Wbool tem_tick = false;, h" D! a7 r/ ^
//--- An auxiliary variable for opening a position( f4 p( R- ]( m# G# x9 d
#include<Trade/Trade.mqh>
! l) A/ {/ X% d& G% j4 N#include<Trade/SymbolInfo.mqh>2 r( g/ X, o. ?. `# [# ^
CTrade negocios;
) v  F6 H% f. T8 a. l! C+ m! QCSymbolInfo info;
2 w. ~% P  @. ~( P+ d" d7 ]$ L8 z//--- Define in OnInit() the use of the timer every second# T! W4 ]/ M% @& C
//--- and start CTrade
) X3 {6 `3 h& Y& Kint OnInit()
5 F% t8 V3 [" ?) B{
1 n% p2 E5 r! X' e2 d//--- Set the fill type to keep a pending order
4 X& u* O5 b: R//--- until it is fully filled6 B, p- ?0 q3 q& q) Y( |; S' N
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
% o, k' C* \2 V8 w3 B//--- Leave the fixed deviation at it is not used on B3 exchange
. m0 S- Z, \9 y+ j( o; Mnegocios.SetDeviationInPoints(5);
* ?/ I0 m$ o. _/ `//--- Define the symbol in CSymbolInfo...+ u1 I1 ^1 K, o+ G1 |2 U( ^
info.Name(_Symbol);" k) o* L# g: P' E2 g1 J! L( g
//--- Set the timer...5 f0 }/ |% H/ S; n1 E& [. Y
EventSetTimer(1);
- D' c+ z: N% J6 l" U/ n) B$ Z//--- Set the base of the random number to have equal tests...& q! b. m* O+ E1 k. H+ S' k! b( ^) `
MathSrand(0xDEAD);5 T2 \9 j- z/ W& D( M8 N* U
return(INIT_SUCCEEDED);
% R  y4 L) i1 A) D! Y' Y3 y; [2 L}) W( z! Q3 m  w# n, Q/ V2 d# ^
//--- Since we set a timer, we need to destroy it in OnDeInit().$ f1 v  h! N  j
void OnDeinit(const int reason)  q/ J* l/ A" w( Q3 |! k# r
{
, ?2 H# C2 k% k: F8 D( ^% @EventKillTimer();+ C0 V3 ?: V! o, D# f0 j; r0 |* u
}
9 R* _* Y9 R% `% ~//--- The OnTick function only informs us that we have a new deal
* q* v! G9 ~9 ^, I1 O- n- zvoid OnTick()
( h# I" w3 H* }3 x. s- C{
* B8 _4 q4 H6 ~, ntem_tick = true;# d! w  k3 J9 A8 g5 A- z- }
}
9 D/ f  t' Z1 a, [9 j7 a//+------------------------------------------------------------------+
- f+ T' p8 L/ u+ Y//| Expert Advisor main function                                     |. b" M! s! k9 A. e% r$ y
//+------------------------------------------------------------------+
8 C; |' [5 w8 k2 y  U, q4 N& Vvoid OnTimer()
1 ^: M' S7 m( G- V/ |{
9 s  ~* ?4 h2 c# R1 V( ]  u1 TMqlRates cotacao[];* e+ a4 L4 l0 K; x, ]
return ;. }% t4 K1 I& P) A
if (negocios_autorizados == false) // are we outside the trading window?
0 }0 Z. z' S9 c9 o' Y7 f4 A* xreturn ;( f& S" g+ M: b
//--- We are in the trading window, try to open a new position!+ `" L1 _) A" d6 W& c0 P1 p' [
int sorteio = MathRand();
- o1 _3 S+ h8 J5 C: I4 T9 V//--- Entry rule 1.1! f& E: N) n1 `6 L
if(sorteio == 0 || sorteio == 32767)
( t" O. W# j% U6 j  r* Wreturn ;& q, Z; b- }7 O& X/ b& |' K
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy) @4 Q7 E" y. W+ ^5 N# [2 U
{+ e/ k$ u* ~" O, o
negocios.Buy(info.LotsMin(), _Symbol);
* W/ d+ u+ k+ k/ G& V% j}
/ J; T: G) \, M& w  L" Nelse // Draw rule 1.3 -- odd number - Sell
6 j+ p* U- ~% C+ }. _9 L{
* v$ ]* G5 a  M: Q" ~% cnegocios.Sell(info.LotsMin(), _Symbol);
! e) J  J3 t7 ~}
" |3 w& r& }& p, Y6 \6 R: F}
) |, P$ _) \1 [1 s8 A+ ]  W+ F3 r//--- Check if we have a new candlestick...
4 z/ r! {) x: M  F; a2 tbool tem_vela_nova(const MqlRates &rate)2 v% V, D( l# b0 I! y2 q8 `
{+ J' ^9 n# V+ F6 g& G' o
{
* w" H5 i$ X' |: r! y( \ret = true;0 i5 j5 |4 ~* F  u& A0 {
close_positions = false;
' T& x7 R7 G+ e( J7 w6 M}& |* a, P; \9 {( k- V
else, J, r, W, g( o% V. }; t: l
{
1 Q, d" x' `2 f. R) j5 [2 dif(mdt.hour == 16)
/ A5 {4 T0 W% z4 z( h" D, ~& q. o* Mclose_positions = (mdt.min >= 30);# q3 f3 s; `5 j" Q: q
}
- U" G3 n1 ]: J( Y}
3 v1 D/ y9 [# F1 treturn ret;; R: \4 w5 h, V1 M
}
# B0 ^0 O& ^; w0 s/ r0 ^3 B7 s& H//---
6 c1 Y6 N* i' P% P, Kbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])* z9 q& E1 q2 O6 Q0 v7 J. ~/ h
{* W( K, f8 o) U- ]( [. {
if(PositionsTotal()) // Is there a position?
7 Y2 Y5 k* P. |/ H* ~+ j{
6 Y1 z+ h5 o. G! L. N5 P: ndouble offset[1] = { 0 };' }* B$ B7 Q2 w
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
& ?# O2 a% y$ E4 y/ b&& PositionSelect(_Symbol))  // Select the existing position!/ r& c( x0 q- I2 N! k, e
{, T: y9 n* @( t+ f: e
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
" E* M( `( j5 @double SL = PositionGetDouble(POSITION_SL);
6 w! Z8 n1 y6 v2 f, d7 M3 V" Bdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
9 z* d; O$ @  c  Zif(tipo == POSITION_TYPE_BUY)
+ ?+ N1 I: A0 i' [- _0 b( l7 T5 g{% y4 B# I/ }" g
if (cotacoes[1].high > cotacoes[0].high)8 U$ S; W& M% J4 a
{  @7 F. ?& p7 K5 d
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
! c) J- E1 e4 s5 a/ U1 R3 M  b7 x' Hinfo.NormalizePrice(sl);
- Y; c3 Y+ ?( b, D: X: w" G: ^if (sl > SL)
9 q1 C% i- U2 H9 s{
2 L& V! w9 K7 A6 O% a0 m/ G1 v/ Tnegocios.PositionModify(_Symbol, sl, TP);
/ m- ~3 m, w! x( N) n" e) h}
; b9 T9 U1 u6 W}: u8 v/ x8 h4 x
}
# v/ u) D7 E5 [4 |1 H# H9 Xelse // tipo == POSITION_TYPE_SELL* w$ `7 ~- T2 Q4 K
{
1 V1 x' {/ Z3 d; p, _2 g. y1 R1 r; ]if (cotacoes[1].low < cotacoes[0].low)
$ C) r! z* \2 }! `- _; R{
7 w) ~% a' h; @/ }8 R) ^  H8 `# A1 Lreturn true;
4 Q- g' ]0 ]1 i. k+ p}
- Q2 R3 n1 e6 m3 u- ^// there was no position
- J5 K# s) x( m6 r2 \return false;4 j+ n+ _0 p% y
}; j4 D9 ?  `0 I) H6 V7 P2 i
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
9 K2 o% a- O$ A到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-29 05:25 , Processed in 0.391837 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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