私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA6 B$ k2 Y0 c  L8 q. Q$ N" _
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
1 [  D/ b' Q  _6 m: |, {$ U为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
" W+ ?3 _" p4 T2 D5 @6 ~* T2 t( M! U以下是制定这些规则的代码。1 ^: o" R4 s+ e3 J; A* w" q
//--- Indicator ATR(1) with EMA(8) used for the stop level...9 u. c1 ~& }# x7 y' @* a# D
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
" v, j9 q! e( I/ V  |int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
5 M1 {8 Z& L0 Z0 J0 _  w; i//--- Define a variable that indicates that we have a deal...
5 N  K# t1 b2 V- o4 Z. W2 rbool tem_tick = false;
! H, E7 X$ z/ h//--- An auxiliary variable for opening a position* u7 i, a" d: H; b
#include<Trade/Trade.mqh>: N9 j  M% f$ ~  k, e+ k4 q
#include<Trade/SymbolInfo.mqh>3 i. b1 @3 V* f
CTrade negocios;' \+ W3 ^6 ~- X% Y# O8 ^
CSymbolInfo info;/ Q9 _# d7 O0 \* B: _( S! \1 R3 W! Y
//--- Define in OnInit() the use of the timer every second
* l1 d3 e2 M7 B/ b. }//--- and start CTrade# I( v7 D5 C1 D# C& I
int OnInit()
3 Y  K8 v. y4 F' ?1 M, }* j{
1 |- h1 b0 w4 ^* c) M9 Q//--- Set the fill type to keep a pending order
. C3 }) A% H( p" ^8 B//--- until it is fully filled$ x' l7 _# |/ N: m' n. u& H
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
3 h, m" Z; B+ h/ g//--- Leave the fixed deviation at it is not used on B3 exchange8 a1 r1 @0 q  _9 c
negocios.SetDeviationInPoints(5);
  N7 V/ E' W# D$ G& f& O9 `//--- Define the symbol in CSymbolInfo...- W$ Z7 ?# w( W& k5 T, j+ g
info.Name(_Symbol);
9 e& y& d3 v; E. ]: Z  o//--- Set the timer...5 J! J& K1 H- z3 n! B; D! U
EventSetTimer(1);9 x1 L0 z+ S6 o7 x
//--- Set the base of the random number to have equal tests...
7 v% l1 }/ ?1 }! B) l. {. tMathSrand(0xDEAD);/ [( J# ^/ l+ f" P+ m: u
return(INIT_SUCCEEDED);. ]. s& p+ {' Z' Q4 D3 z
}
' q7 N# W# G) T% _//--- Since we set a timer, we need to destroy it in OnDeInit().' E& k1 @$ K  N
void OnDeinit(const int reason)
7 |  M. k6 @  @* O% s{
: z6 ^0 w" N# q3 n! j9 O) fEventKillTimer();. `. |# `0 v% E4 s* T. n' Y: j
}
% B/ a; u/ Q2 R6 ?- R3 m1 x//--- The OnTick function only informs us that we have a new deal
/ ^. K6 ~3 w8 K: z1 |3 Bvoid OnTick()2 ]4 w6 X7 G0 E6 P0 q* V
{
. O" X# `  s* y; item_tick = true;
$ L, h# @$ k4 t9 d* p}
% x$ l* w2 ?% u+ \9 S//+------------------------------------------------------------------+- v& n, G7 A. X4 n( V  R6 x( \9 v- z
//| Expert Advisor main function                                     |$ p% z0 ?3 E+ M: V4 A& i9 n* V
//+------------------------------------------------------------------+3 k$ u! p! d" S' O& f
void OnTimer()
- J: z) |; H* @5 S{
  p& \4 w2 B2 |5 ~4 N5 L2 F; q& Y9 z& GMqlRates cotacao[];+ r( `2 d, v, Q* J4 s4 M  ~
return ;3 `2 o9 B' n# C- Y# c
if (negocios_autorizados == false) // are we outside the trading window?
- g4 V" Z5 }2 O# O$ b7 \return ;
' J0 i! W: G' b5 P$ ^" z//--- We are in the trading window, try to open a new position!2 X3 D' x. X% F6 ]
int sorteio = MathRand();' }, S7 ?9 Q0 e2 s; J
//--- Entry rule 1.1, ^% Y2 l8 n8 J2 {
if(sorteio == 0 || sorteio == 32767)
$ I# t! h1 X) Xreturn ;$ j8 v! H1 _* x  G, S5 J/ `% M
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy* w- e% Y3 Y9 H. c& w
{2 B1 F8 S! Z' ]& E  F, W
negocios.Buy(info.LotsMin(), _Symbol);+ w. k% o$ J- s+ T5 g( C
}( H9 O' B- Q! H. B7 y7 A
else // Draw rule 1.3 -- odd number - Sell$ P. G# ]' Z  {4 \1 Q2 X
{
4 ^4 N& x9 r# w' }7 [negocios.Sell(info.LotsMin(), _Symbol);
+ u8 w  Y6 T" `9 L! V}, ~) h1 |) p) W: W2 b) R- E4 k9 d
}
* c% ?* x0 {3 N//--- Check if we have a new candlestick...
2 M, N# I! q* e5 g; jbool tem_vela_nova(const MqlRates &rate)) y5 V0 H, c$ @- U- s
{: N( u  q7 [  Q$ Z) V
{! f% H, T: ]- {7 B; R, B- ?
ret = true;2 _4 i- n5 L: \" w* u5 I( J
close_positions = false;
: O! T& r, M2 G4 q2 O9 V! B+ f}$ l8 `8 n/ s3 Y5 J( x
else' U7 p3 X6 L8 B$ p4 _
{
0 T+ @* P# }% O* I2 Qif(mdt.hour == 16)
3 I. E- j+ W( r3 T* H/ j7 q! U. e  |close_positions = (mdt.min >= 30);
! O( t7 {1 u& m: U3 s! c) j}+ T- l4 v+ u+ M& l" b, C3 s$ c
}0 l4 e& k, G; c8 A, _9 P
return ret;
7 }/ j& [  Z$ n" V( F}/ s( P' q3 l! D0 m
//---
4 b: G7 X  T. T; c5 N/ L6 abool arruma_stop_em_posicoes(const MqlRates &cotacoes[])8 n( j% x4 x% D: O3 I: _! a1 Q  u
{# G. [3 e, F! P. S4 ^$ a
if(PositionsTotal()) // Is there a position?/ f/ U0 _7 z" @+ h5 a2 A8 X+ l
{
2 C+ ?5 E7 J1 @7 h3 N2 f' _double offset[1] = { 0 };
8 ~! O/ H" i3 d$ @if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
4 X4 r& [8 z- n! A8 `2 J/ _&& PositionSelect(_Symbol))  // Select the existing position!# H) c$ A7 r5 V' i* s- i
{
/ z$ P9 A1 F! z4 O4 ]6 |ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
; U  ?. @0 c4 ~4 L' h9 udouble SL = PositionGetDouble(POSITION_SL);
2 |# A0 M2 }% i& A3 bdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));% q  o- ?, S+ p) N  |1 q0 H6 Z
if(tipo == POSITION_TYPE_BUY)
# A: F; a! c3 X  b, Z* N5 s( N{& e0 I+ @5 _, y6 \' O6 E1 r
if (cotacoes[1].high > cotacoes[0].high)' h) d2 w) A0 p! y$ A2 m  K5 N0 S
{# q8 ~9 w/ {' f. l3 k
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];+ i  q# _  ]( Z7 v0 s
info.NormalizePrice(sl);
$ N; f; z* l0 i; ]% Cif (sl > SL)8 k& F" \8 l2 S+ I1 {$ x
{# s, G9 o' E) D# p' z
negocios.PositionModify(_Symbol, sl, TP);
4 I& \. e5 ]- m& D9 I}- o2 g* _$ J2 O% g9 K% l7 x
}( u) f$ B; `1 S- u+ X3 s! X  f
}9 L3 k" t7 L* `& i
else // tipo == POSITION_TYPE_SELL
/ t$ R2 q1 q8 K0 |) f{
4 l3 e: p: u/ i7 A" J9 ~if (cotacoes[1].low < cotacoes[0].low)! j) r7 b* N( u
{+ n% h1 F' w' I, M# m3 p
return true;9 L, \5 B# g$ N) ^9 c! B
}; a; g9 y9 a/ v, c' a4 L
// there was no position
# l' V3 Z  U* ]7 L" |6 k1 J" K: m" P$ j* ereturn false;/ B4 f0 H  d- z
}# k9 ]) v9 k4 P
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
5 ~2 t) R, Y# A到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-4-30 23:13 , Processed in 1.702164 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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