私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
, a2 L! G4 O; u7 X, [% t; b1 h在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
) N" u- l, H: t5 n+ h3 R9 _9 p* O为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
% j9 B" ]( w$ C- }: a! v! n0 w以下是制定这些规则的代码。
( z9 ^9 `5 c, ~9 B: i8 {//--- Indicator ATR(1) with EMA(8) used for the stop level...+ e2 o. m7 d! ~
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);, v5 H6 ]9 m' X4 P9 p6 Y% A" y3 n$ W1 Z. I
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
2 V" W) n* e3 W9 }//--- Define a variable that indicates that we have a deal.... J; \  t! k  h6 H: ~4 Q
bool tem_tick = false;
( h- e0 R; O0 V3 c//--- An auxiliary variable for opening a position
2 L) n1 z* a0 Y# R! J- r$ ]#include<Trade/Trade.mqh>* J& N' j! K: }. a. V
#include<Trade/SymbolInfo.mqh>
8 e; ?! u0 o! @* [+ x; t9 j" n9 P/ o% aCTrade negocios;
! U4 P; i1 m2 V8 ]. J( z" DCSymbolInfo info;
$ V: [* l, h+ @. Q; |//--- Define in OnInit() the use of the timer every second2 v7 @, {2 C' q% X  Z4 W* m1 S
//--- and start CTrade) i) W2 h; W1 X+ D% Y
int OnInit()# R3 ~2 T6 W& L* d1 L) m. ^% ?4 O
{
* w) P; j+ J( C: k//--- Set the fill type to keep a pending order
# P  [0 d9 H; I  I( r9 Q//--- until it is fully filled
/ u5 \8 ^- Y+ y, M" b2 g% x2 Onegocios.SetTypeFilling(ORDER_FILLING_RETURN);
+ A9 U7 e4 `2 t4 v, ]: C2 e/ P//--- Leave the fixed deviation at it is not used on B3 exchange7 h) a; r. G) N3 ]
negocios.SetDeviationInPoints(5);
. k  F# l1 l; T5 p! c0 T, j//--- Define the symbol in CSymbolInfo...
0 f8 H; _  b6 P0 Sinfo.Name(_Symbol);
8 a3 q5 u/ J6 r9 o//--- Set the timer...9 E( @  z1 v3 l+ i
EventSetTimer(1);# _2 i: p- I& Z+ D& k& y
//--- Set the base of the random number to have equal tests...) C4 s" ^/ O8 F$ f/ G9 z/ k# z9 a
MathSrand(0xDEAD);# D/ S0 Y# \1 J
return(INIT_SUCCEEDED);7 N! s2 q% Z. O- W6 Y$ ?
}
7 ^4 L7 y' ?; D& q0 V! t; m//--- Since we set a timer, we need to destroy it in OnDeInit().2 b0 _1 j' H6 T2 t  K
void OnDeinit(const int reason): G+ `9 L; G2 A
{
% U, @; d6 A  I! w. SEventKillTimer();+ T$ n/ M1 ]# }9 R2 e: ]
}
) Z% [% \2 m% J  i//--- The OnTick function only informs us that we have a new deal
5 A1 T" P; V( y; G* Bvoid OnTick()
; q% O5 h8 a* H0 ^2 \{! B' d3 k, G; z3 W7 s
tem_tick = true;
  N, D* C1 C' C& T) V" I}
0 T; D/ V, |7 ]. Q5 i3 U2 H6 r//+------------------------------------------------------------------+! o+ q8 z  J& `( D" V/ a) e" t
//| Expert Advisor main function                                     |# T8 m8 B) c& C' x0 W2 j! |: ~
//+------------------------------------------------------------------+2 l% N8 Y+ s% x, c% L+ F! ?% b
void OnTimer()
) P0 _5 ~0 \- {/ R+ p: ~& ?2 R{( p+ w$ N7 O1 j8 _
MqlRates cotacao[];
) ?/ F" h- ~. @9 s3 jreturn ;
9 ^1 G. y8 O' U( hif (negocios_autorizados == false) // are we outside the trading window?
. j- E/ G0 W. R2 z! c; ~return ;0 B- j4 s# n2 V: R4 i1 x6 d3 b7 J
//--- We are in the trading window, try to open a new position!
; {6 Q$ K; C) N9 R- kint sorteio = MathRand();& e# Y7 b6 J, E4 n) W8 w8 |! T
//--- Entry rule 1.1
5 o3 k: c0 e- Q# X' hif(sorteio == 0 || sorteio == 32767)
5 T7 D5 k5 t% V! z  Jreturn ;- V1 G+ t" e2 u* e5 D
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy- B, Q6 X. D9 h, J$ \
{4 J5 Z# x+ W3 S- ~
negocios.Buy(info.LotsMin(), _Symbol);
* K% E. M* `- t' v  c}) Y2 z' l' w9 X. M6 ]5 g9 H
else // Draw rule 1.3 -- odd number - Sell. q- [9 q/ x/ h
{0 G% A8 J: ^8 s  @3 ^1 a
negocios.Sell(info.LotsMin(), _Symbol);% c9 f9 h# o$ A
}
3 T% s2 t/ b; d# m; Z) H}
9 T4 _$ y5 d  U3 X- n4 Z$ d//--- Check if we have a new candlestick...5 z, l% J" U& S2 L
bool tem_vela_nova(const MqlRates &rate). a- _# Q# ]! g# H
{6 B) r3 @. g9 M4 I0 U! ^
{; {1 W4 ?7 g6 @1 a4 R* V  h
ret = true;
$ N2 A7 ^/ r& P& Vclose_positions = false;
  D. Y& r9 S5 \# I1 K' D}
0 `9 X2 E9 O1 }5 e4 c, a4 _9 Welse- u' q. R0 d3 p5 i8 [# `
{
# h6 |1 C8 s& I5 W/ D$ ]% Rif(mdt.hour == 16)2 o: k" E6 Z  G8 G, }
close_positions = (mdt.min >= 30);% A8 n7 \* R: I3 n# ~
}
6 b3 D' r+ R3 d0 b- o. ?+ t, B}
  a$ Z# m9 u3 ?5 q; |# u$ hreturn ret;
9 |% I. Y! O: M}
8 S8 g( X/ w9 ~& P//---' l$ ~: j+ C3 u) S# z7 B* c' X/ a, {
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
5 i% e" a/ ?9 }  P! X; H$ R( P* y; f{5 @$ C2 ~; V$ ?4 ?1 S
if(PositionsTotal()) // Is there a position?! c" A( I' z- e& a" g
{
' M9 Y: y0 V' p: F" t7 Cdouble offset[1] = { 0 };
1 t  f! B; g2 P# `) q3 Fif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?+ b+ R% V+ g  P
&& PositionSelect(_Symbol))  // Select the existing position!3 F  A3 e, }8 J9 m
{1 K$ i# m) ]! T
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);5 u! [7 ?8 ?# w
double SL = PositionGetDouble(POSITION_SL);
8 C* r) d2 X9 ~  A, R: X- ~0 v% Kdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
5 a5 f) O0 m6 }/ a& c4 \, B5 Zif(tipo == POSITION_TYPE_BUY)
  v4 s+ Q0 l5 T3 S" B{
8 Y+ r! b3 R1 N, M- l& S: S: yif (cotacoes[1].high > cotacoes[0].high)
  }9 ~- c8 k) H- E" R2 Y" R{, T7 T4 B0 |6 C! N8 y2 t: c2 @8 |
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];8 H  B* y$ u5 T$ n( f2 }  Y
info.NormalizePrice(sl);
0 E" a0 i0 i4 I8 c9 l; u6 @if (sl > SL)
: s% S; F5 T( _{  m, s7 s1 i8 u. s0 h5 Z+ p. Y
negocios.PositionModify(_Symbol, sl, TP);
3 U5 {: |5 Q" C. ^, E, ^% G}# u/ s5 H9 ?5 f0 u" \5 ^% `! j0 U: A
}( q3 z2 E2 s9 s& X" c* e
}
; w0 y( B, D* y, J$ }. V! belse // tipo == POSITION_TYPE_SELL5 C: Q% r6 J# Z; X) ]
{  ~4 {# r8 x& ^4 @' t0 W6 V
if (cotacoes[1].low < cotacoes[0].low)% a& k5 H' x" P, z- R
{9 w6 K7 t/ e, c4 e: ]( l* G
return true;
: L% D9 r$ G' }8 b}8 G5 v5 _- \5 L1 u) @  v/ w
// there was no position
+ u3 n; K# s( m! A& g( yreturn false;7 P; l) o: s" _( o4 [' e& {% S
}
% d$ M# d  u/ u# i( ?: D我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
* p+ C# i4 [9 x9 ~4 z! T  x! {到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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 07:59 , Processed in 0.397961 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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