私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA& D* N8 U3 p- i1 X2 z
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
. \7 [5 ^0 z( t0 f! z4 z为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
6 g5 J; v6 P# n以下是制定这些规则的代码。
* f  B2 i5 h* P, o5 T/ R& Q//--- Indicator ATR(1) with EMA(8) used for the stop level...& v" S1 i9 S# s
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
5 C7 w; M+ H$ M. J$ R( z- Fint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
1 s! [$ P( {4 d" m* {% @//--- Define a variable that indicates that we have a deal...
' u2 b: y& F2 ~5 Z2 Kbool tem_tick = false;6 W) Q6 F& `2 i7 w' q
//--- An auxiliary variable for opening a position
  a8 A3 J& K7 i, `" F5 s#include<Trade/Trade.mqh>
4 e/ C: N- P' F" k#include<Trade/SymbolInfo.mqh>: k% t5 s" L4 S3 Z' {- M
CTrade negocios;
% z6 }$ E' `6 S; o6 ]* jCSymbolInfo info;
- R  m* B, _' a0 l//--- Define in OnInit() the use of the timer every second; D' i/ Y& `. n9 {4 m
//--- and start CTrade$ k& S  {7 `* s: ?" b
int OnInit()
1 H# X' k, x+ s# {2 f! T$ @{
. S4 H3 E+ D$ v+ K& o! ]% x//--- Set the fill type to keep a pending order
) O! G2 E8 u7 O3 H1 Q//--- until it is fully filled* ]* I: {/ \1 E2 Q
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
  w6 m7 B- p* n! P  N$ f//--- Leave the fixed deviation at it is not used on B3 exchange
& |  W  i* _. U/ J& Z; }negocios.SetDeviationInPoints(5);
# Y2 ]' J3 d; f//--- Define the symbol in CSymbolInfo...
4 T5 ?/ o( q: I) e2 |. L' s. V( kinfo.Name(_Symbol);' X; x0 G+ b8 h; g$ f1 ?. J
//--- Set the timer...
3 S/ ?) C) j6 j$ i6 EEventSetTimer(1);- \# ?* z2 H0 R9 y$ a# j% ]! o
//--- Set the base of the random number to have equal tests...% ?$ Q8 U; i# w' I  n, S% v- q
MathSrand(0xDEAD);9 N* @) T) d2 g# l
return(INIT_SUCCEEDED);% q* ?/ r* g! Y& ~1 l, i
}  x) |# N+ ~$ B/ Y1 I
//--- Since we set a timer, we need to destroy it in OnDeInit().
" o7 L" K1 o6 O/ b- k4 `void OnDeinit(const int reason)2 O3 l1 E: Q* j7 U1 [
{6 K8 z9 v3 J4 u+ Y
EventKillTimer();
& s4 R4 A2 @0 _% H}
% r- }+ ~( M* J: z//--- The OnTick function only informs us that we have a new deal
0 I& U! }% C6 i2 ]void OnTick()
9 k* M4 [" B  D3 ~6 @{  y0 j+ g% |, v4 h# h7 k& ~) V1 T) E& f
tem_tick = true;8 l) }/ i" i9 l  C5 \- Y0 T
}
6 N5 g5 `% e( a- t7 R; z$ v//+------------------------------------------------------------------+# K/ ~* J: q# W+ m5 S
//| Expert Advisor main function                                     |
! o; ?- N, j2 V) v//+------------------------------------------------------------------+
1 J& c1 _' {8 H0 m3 lvoid OnTimer()% ~% e" c+ {$ C/ B9 p! c
{
/ P0 F$ y; q6 l1 [MqlRates cotacao[];. u' J$ l) d: I/ n; C$ B9 O
return ;: ~% ~; N  Y6 F2 p5 {
if (negocios_autorizados == false) // are we outside the trading window?
8 K  }1 f8 R6 b, }0 x, K4 ?return ;! F$ d8 W. [; q
//--- We are in the trading window, try to open a new position!
5 ]! S4 ~1 H" Z( Q8 u. z- ?int sorteio = MathRand();
% V- u8 X$ e( `" p/ W' ]//--- Entry rule 1.1
# V8 f7 `. R8 O2 U9 w1 z  b1 Lif(sorteio == 0 || sorteio == 32767)- h1 W5 e2 z" [0 M2 V. ?
return ;
5 s+ ]" e* X+ H* j( l0 Zif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
; V! X: k: v* ^# q{
9 E! [" T- l& K1 Enegocios.Buy(info.LotsMin(), _Symbol);
' h  j8 C: R9 M! r8 B}
! ^$ n# n# T+ T) o. e$ z, Ielse // Draw rule 1.3 -- odd number - Sell
: i2 L" x  {: z- S8 T" T2 v, H{
# U8 d* f3 ]. E6 L* t2 C7 |* Mnegocios.Sell(info.LotsMin(), _Symbol);! W9 V4 m% i% p/ W4 c1 u
}6 S3 H; x5 _8 G$ c* v
}
7 L3 ]+ d  X3 Z0 R7 q  s//--- Check if we have a new candlestick...
/ c1 G; O4 h5 |' x9 Kbool tem_vela_nova(const MqlRates &rate)
, X6 n& g, G. L0 `+ I" a$ p{" ^0 A' S# ~  T9 o0 z
{
$ h) a" l) U9 T6 Y& d2 y- zret = true;
+ H! Z+ ^' e0 i7 d: Q4 ], h. wclose_positions = false;, k  z" _4 ?* i2 ^! \
}
+ v! D7 t" B: welse
4 K' J  M. z( w9 F  V9 m  ]{
1 g- i" q% V- A1 p* x  E& `if(mdt.hour == 16)5 ?! G. V2 Y4 \. h
close_positions = (mdt.min >= 30);
1 v9 O9 [# M4 f6 _) p9 F}1 \) o6 E$ B! T; X6 J5 d, i* w2 v9 v
}
4 O+ j- h6 x4 T! C. P. _return ret;: r- i$ z) F+ T1 }3 s6 p! m, g5 Q
}) f( O; v' L# C
//---
. I: K6 W* ^4 J9 obool arruma_stop_em_posicoes(const MqlRates &cotacoes[])% f8 v% s# j8 ^% T4 Y2 x
{% [- B0 W* H6 w5 Z1 M6 c* N* I
if(PositionsTotal()) // Is there a position?
- z* L- O) U8 z4 N; T{
# Z7 q5 a1 ~/ ydouble offset[1] = { 0 };
* ]/ X& m9 r4 S' V& ]if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?! y( G& m8 T  s% g1 i+ N
&& PositionSelect(_Symbol))  // Select the existing position!! S8 o9 M/ p' W9 C- `
{% O. W0 b, o  k4 ~' m
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);3 K. p7 m7 \& c
double SL = PositionGetDouble(POSITION_SL);
/ z2 d5 a* G2 ^4 r* R6 ddouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
4 J) X" O0 @; Z" z2 Nif(tipo == POSITION_TYPE_BUY)
& C. H3 _0 @$ v2 F, E' S4 Y7 L{) D% g3 b+ C  O
if (cotacoes[1].high > cotacoes[0].high)
( U; v% ^0 \, s' R( Q6 a{
% w* G: _: B; q0 g% Rdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];7 M" s8 k( W3 f+ w" {
info.NormalizePrice(sl);( s, A2 r* B$ U% o0 w! B
if (sl > SL)( ^2 Z" l" m. [
{
% Q! u' p* J# S! v5 h% y3 Nnegocios.PositionModify(_Symbol, sl, TP);: D$ _  g) M# L5 L# K0 k9 z
}5 P% X, m3 ]" j3 y% p5 F7 y& f
}, f1 ^/ T+ V1 J
}
5 ^, X% y# A. ~7 n- qelse // tipo == POSITION_TYPE_SELL* b9 J$ d1 q3 |* S  S! A& r
{% t3 u3 o2 J: V$ Y! }
if (cotacoes[1].low < cotacoes[0].low)
4 a$ |8 U* Y% ]+ s{
& u5 x& P! I' O  z/ i0 m: mreturn true;
5 `. W8 C; u! k- B1 H: R}- S, y/ S3 [- ?( R' U% K. k# `) z
// there was no position
/ T1 F2 h8 F  @3 @return false;/ ^. w, p2 J# S- A5 c
}: G) I! U/ ~/ @
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
) j9 w4 Y% A! D( c5 \; c0 S到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-10 10:47 , Processed in 6.252732 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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