私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA# r) U" s( `4 ?/ }
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。. |6 E3 V% t, t- a0 i
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。; Y3 v  O5 ]8 D# i
以下是制定这些规则的代码。
3 Z0 C: i4 ]! m  `1 b# V, c0 k//--- Indicator ATR(1) with EMA(8) used for the stop level...
; \) U% J+ N% ~; O& Oint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
; l6 v  H5 g/ f) w$ Mint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);7 e* l. y- p) t3 B+ d
//--- Define a variable that indicates that we have a deal...
% C* Y$ T6 m$ P: U& Q( Rbool tem_tick = false;& F8 C" I0 J: e4 ~( d
//--- An auxiliary variable for opening a position8 G+ T& y# u2 q) H
#include<Trade/Trade.mqh>, b) q3 M, a. N9 T) a8 p
#include<Trade/SymbolInfo.mqh>
' v3 m: c" o' P5 E# LCTrade negocios;9 @! F, w! j- r9 B
CSymbolInfo info;
0 _3 ?: P, K4 R8 D+ ~; Y//--- Define in OnInit() the use of the timer every second
' R  E) R. N- ]3 d, b( S3 U$ t//--- and start CTrade8 ]* o' ]6 B" B7 ]- i5 N: k
int OnInit()& N6 b( g# Z# u. f: L
{, w% F0 P6 k4 W+ k, {% I3 b
//--- Set the fill type to keep a pending order' s: F% R! [' B* q+ f" c- N, p
//--- until it is fully filled& \, ]& p& P) I5 K
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
) W& m1 C' L% V7 I7 {//--- Leave the fixed deviation at it is not used on B3 exchange% i5 y5 p8 U7 N; u1 `7 b( V
negocios.SetDeviationInPoints(5);' ]. p5 K5 V8 l& b
//--- Define the symbol in CSymbolInfo...
! ^6 E0 C( _/ R2 |info.Name(_Symbol);
. f% D' K# |: R- b. K) t//--- Set the timer...' A  K& p! E6 v! B. O, i
EventSetTimer(1);
3 \; Q7 I' F6 R* @2 K/ K//--- Set the base of the random number to have equal tests...1 Z; d; ?& h- y+ q3 `
MathSrand(0xDEAD);
/ i5 V8 R: I6 I' Z# Vreturn(INIT_SUCCEEDED);1 w0 G9 @& z8 ~2 Q0 C4 I2 M
}
" a4 U1 R- D0 }& B) L) t//--- Since we set a timer, we need to destroy it in OnDeInit().
3 I$ Q% [2 d$ \7 V( X; J! h0 Lvoid OnDeinit(const int reason)
6 b' d7 x; w; @3 K{
: ~) i, h4 _  g( |  Q: w8 `$ |EventKillTimer();
4 T5 L( U$ ^* Q$ S: Q8 e7 [) @9 E}, j# h, @8 v) H. k1 e+ m0 X
//--- The OnTick function only informs us that we have a new deal
8 {; q# `% I! Z2 avoid OnTick(). S3 K% |, |) |  F) N8 T9 Y; I
{
# W! j+ W- f* R+ Vtem_tick = true;: }( S# k: w; h9 ~
}
$ L8 W, b! L; Z% o1 p" f//+------------------------------------------------------------------+1 k. E0 O, [$ \8 V4 F7 Q" z( T
//| Expert Advisor main function                                     |$ r2 ]* V& B1 x' R) V. T( Z
//+------------------------------------------------------------------+
7 [- l& S0 G" }; b$ E7 `) i( Bvoid OnTimer()' ~3 d' G' W. U% {% `8 c7 l
{
- z, b* V8 H" i. A0 ]MqlRates cotacao[];
4 V1 P2 f' }8 Y9 h  e0 P: Ereturn ;
$ ~0 z$ y2 }+ x0 a3 Kif (negocios_autorizados == false) // are we outside the trading window?6 z* h7 {- F- C- R7 v& E
return ;" u6 m$ W& i/ S- d$ k) _9 g/ b
//--- We are in the trading window, try to open a new position!9 }5 d9 [7 j  Z1 g8 N1 d8 u8 K# N
int sorteio = MathRand();
7 O* y( B) e3 Z0 |3 B, y//--- Entry rule 1.1
% u0 `: P" X; {2 Qif(sorteio == 0 || sorteio == 32767)
' F( L& `# d$ q# A4 {5 l9 L! vreturn ;
1 m2 s+ {1 j3 |# S- O" iif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy/ n) ?. p1 g" b: {8 m1 b
{
9 y: l$ v" v$ n! t- {negocios.Buy(info.LotsMin(), _Symbol);
! y9 D$ F9 I; {. w; x1 A}- \% c2 d) N. o$ r6 c3 ?4 p0 {
else // Draw rule 1.3 -- odd number - Sell
2 |8 p' A; w' W# w5 S{
/ _* d) O, I) g2 o9 L& w) Mnegocios.Sell(info.LotsMin(), _Symbol);1 \6 i; L+ u0 a; [, R
}
/ k7 g, e5 g  R, o9 `6 ]' M}9 ~1 W# ]) m+ `! V
//--- Check if we have a new candlestick...
. g' z& h0 F6 u9 [0 Zbool tem_vela_nova(const MqlRates &rate)
6 o9 Y% n) C# V- V' D/ G{5 K0 ?9 O# k- g
{0 U! B& ~$ f/ c) c1 L1 \1 ?% W' Q
ret = true;
+ Z9 S8 Z5 d# K5 ]4 b. X0 nclose_positions = false;
+ m$ }. K. M2 N/ d. K& ^}
+ U3 v+ L; X2 {else6 A& B$ ], M2 y$ R1 I
{
% U- L* R* E4 C+ u9 L6 Z6 S) nif(mdt.hour == 16)0 E9 w7 K5 ?* \, f
close_positions = (mdt.min >= 30);
3 U5 P- _0 f  e3 E}. [8 o: W  n+ X6 f( ^, w
}
/ A. h# l2 u  l; O" D6 freturn ret;
+ `8 k4 W( x7 B}
  O& D0 I& ]% ?! K  q//---+ t2 X# C  N: M& V$ @8 l
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
& o( z( d8 F( J& g{
' q3 J( e$ x% a% L) e/ Q# J! aif(PositionsTotal()) // Is there a position?5 ]& o+ f) Z1 G& m2 F) G
{$ F+ t) i* `; p& }5 {/ ~2 U
double offset[1] = { 0 };- R1 {) ~3 L( |0 Z
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?. E1 H/ K+ Y# ?$ n1 L
&& PositionSelect(_Symbol))  // Select the existing position!
) l* \* J% o& {' Z$ ?) v* _1 O8 V{+ \% p9 P; Q9 ]6 s7 d& c5 x( B( f
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);" T: _" m4 g8 ^$ t( N; X
double SL = PositionGetDouble(POSITION_SL);. a- M+ ?; t$ r  q' {8 }
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));0 n- g! A2 J' R4 z% A
if(tipo == POSITION_TYPE_BUY)
2 H' \. B0 U& s! ~5 j5 W{( ^5 ^: `1 K  m) d! {) V" `6 T9 }
if (cotacoes[1].high > cotacoes[0].high)8 F  {+ p4 ~: v3 M! o/ h) C: M2 a! V
{
: E! v( s" Y0 e* Udouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
6 C  x4 T/ s, D7 x* I- F- zinfo.NormalizePrice(sl);
2 ^- j& R4 c, H: |+ b5 N, xif (sl > SL)3 H, J6 H* _8 F/ a+ j$ e& M
{
! Y, w) g( W7 Rnegocios.PositionModify(_Symbol, sl, TP);6 `; |3 Z9 z, M2 V
}
* u! B! @( @7 a}
& I" B7 B, _8 z7 b: D- H}
: i8 Q7 J- w+ w5 a# ^% u* h" c- delse // tipo == POSITION_TYPE_SELL
8 |( F5 w' |8 G" A{
, Y) y) r: _8 h" }. Gif (cotacoes[1].low < cotacoes[0].low)
" H7 @1 g/ T+ d3 M$ S5 a+ V{
% r' K" C: C* a* ?/ yreturn true;9 f; A# Q/ U5 d" m2 G) F
}
5 G4 q  X4 @7 _+ B* B+ g// there was no position
$ G0 p8 _  W& T' xreturn false;0 _, k. O# y# o. q) c. A! x. ]
}
, V  G2 r* }4 l, g) Y" b& B! N& ?我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。& ~& y, M7 G- Q7 Z) m
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-24 14:43 , Processed in 0.448699 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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