私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA3 W# o0 O/ }/ H( S* H, A
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
1 Q  O" Q- v6 L. o* [为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
( e: }, x* C& |8 V4 G& d" u以下是制定这些规则的代码。* ^& ]: N: D6 @" j
//--- Indicator ATR(1) with EMA(8) used for the stop level...
0 T: V2 |; h& A$ p0 i. r% ^int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);0 M  U/ B+ \; @- O3 Y. z: q7 H
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
$ c: ~7 x5 G6 _/ k! h% E* @7 p//--- Define a variable that indicates that we have a deal...! @: [6 B! |( B
bool tem_tick = false;
" O" W8 R1 E! B8 z! W- E//--- An auxiliary variable for opening a position
! g4 x1 t. j: }$ ~#include<Trade/Trade.mqh>
* N' J& j# J6 x/ }#include<Trade/SymbolInfo.mqh>
  H+ k6 R8 P; y" o& k' zCTrade negocios;
+ [9 h) b8 ]: m- P6 q3 NCSymbolInfo info;% W7 q7 d: Q# m! B0 j
//--- Define in OnInit() the use of the timer every second
2 [* Y5 R4 h! p) u//--- and start CTrade, U* l  Z% f: E) J% r
int OnInit()
/ ~. T$ f4 Y# A% b{
* O: s: n5 ~4 a//--- Set the fill type to keep a pending order& Q6 C# ^- s7 ?' l& l2 u
//--- until it is fully filled& r2 G4 s1 K; b- Q, v
negocios.SetTypeFilling(ORDER_FILLING_RETURN);8 S7 J5 q0 l8 S% X9 u1 z. o
//--- Leave the fixed deviation at it is not used on B3 exchange# T9 Y3 T# N+ q5 l/ V5 ?' E9 D- t
negocios.SetDeviationInPoints(5);
! m: s0 n0 n. d//--- Define the symbol in CSymbolInfo.../ O/ j2 h2 {+ \( f' u$ ?/ Y) D
info.Name(_Symbol);
. R' D, o  d( e0 t5 m1 D. N//--- Set the timer...
# O) Q/ z* h) Q- R7 ?  r; H# n  zEventSetTimer(1);4 x8 |5 G- T* |& Z1 m5 k
//--- Set the base of the random number to have equal tests...! F# O  H& j2 r1 e) O$ F
MathSrand(0xDEAD);" `. _, K7 I7 e( \) X, C# {
return(INIT_SUCCEEDED);
! Z7 ^$ E  {1 m! t+ ]8 ?! B}
. T( h5 H5 x! t5 @1 I; s//--- Since we set a timer, we need to destroy it in OnDeInit().+ f& r# H$ q5 T: r$ Q% n
void OnDeinit(const int reason)8 g0 v0 z) E/ X$ H, d* o
{
7 ?$ p4 P* @# _0 gEventKillTimer();
: M) S8 e* s% L# @. K}* b/ m: F, O9 B# d
//--- The OnTick function only informs us that we have a new deal
- W$ _7 n5 c' A( y# z; ?, {3 Kvoid OnTick(); W: w0 I+ G1 R, [3 F
{
# [1 o& I: C. Q4 Utem_tick = true;; x  B! k& E( \  L5 G4 h- Z& h
}
: {& ^5 I$ e7 M1 C% J# R//+------------------------------------------------------------------+
1 }/ T/ t( W$ c: ]//| Expert Advisor main function                                     |
# ]/ [0 k) K, D9 _0 R4 m//+------------------------------------------------------------------+2 I; x! ]( g1 I/ i2 }; A  Y7 r$ c
void OnTimer()
3 X" v; k) r1 }  z: U{% h3 }7 V, d3 `! }4 [
MqlRates cotacao[];
; v3 C/ l: A$ kreturn ;
# w* {/ L8 k1 P; Uif (negocios_autorizados == false) // are we outside the trading window?
6 L+ y* p, ]. o( a& A2 v" |; n* treturn ;7 w4 M4 G/ g7 B
//--- We are in the trading window, try to open a new position!8 y: \0 S$ i9 f
int sorteio = MathRand();( Z" T$ O2 P2 Y2 Z4 R0 s3 a
//--- Entry rule 1.1% ]7 B2 D& T8 o' B9 v, m
if(sorteio == 0 || sorteio == 32767)$ t  v8 n3 B) V6 v8 Z" U* v0 T3 `( w
return ;
% r- s! o/ q+ y& N2 ~3 N0 Cif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
9 Q2 ^6 h' c$ o7 w, {6 s) c{6 I" e/ }" Z8 \& {3 ?
negocios.Buy(info.LotsMin(), _Symbol);6 S9 S8 ]6 O! W, y% X" ^9 ^
}& w1 ?+ C9 L* j4 Z# y
else // Draw rule 1.3 -- odd number - Sell- x* {% [8 k+ A) a4 W$ W3 P( W
{
# {7 I. d5 s' j5 v( _negocios.Sell(info.LotsMin(), _Symbol);* v8 g/ r' K0 m1 D
}
: R& O$ y5 n/ Y% y( [, A}
5 ]- s4 e/ B) z+ P% e* k: ^7 e* q//--- Check if we have a new candlestick...; U% ], F5 Y0 V" ]6 @7 }
bool tem_vela_nova(const MqlRates &rate)
" x# R1 b& h: ^  u3 J% s' a# r{
- ]& t, Z* ?* K( t7 [{
1 H. l9 g  O0 `* c: n- @ret = true;
" x6 B) _& K! M6 Zclose_positions = false;
: I, d9 r3 z  P5 I5 ?5 |0 P}
* R; Q; ]" o6 i. a0 |& Q0 D) @1 b( ?else7 }6 y- y: U4 y" m- `" H
{
" J* }3 o& m' H: u% e! Jif(mdt.hour == 16)
2 g0 e- t% i  c7 l* a! T6 T/ dclose_positions = (mdt.min >= 30);
! s; G1 U" w" P! U* X) f% a5 @}
$ w( j  {2 Y( b4 V' V7 `}! q0 Q. Z' i$ U3 i, C
return ret;
/ n! Q3 y& [8 ^3 m}
3 f* M! Z  j* u3 L8 C$ q7 u//---5 s' T' L8 g4 L+ {7 s4 x# B: d& Y
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
8 K$ y. n& G+ |! K1 _$ l{$ ~8 `9 t7 ^! |! U& N
if(PositionsTotal()) // Is there a position?
. B6 d; ^& ]1 E; o{
# ?" s7 V% |7 m3 g% }! F8 e4 a* J4 [double offset[1] = { 0 };( h  u& Y# X7 B3 F$ ]
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?/ }& ^# w0 e6 y6 i% Y
&& PositionSelect(_Symbol))  // Select the existing position!3 c6 F9 o  ^) X* @0 o4 w
{
0 h/ _2 L  l0 j% u3 Y4 Z& d- ]ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);% ~3 g# `, |& \+ w; z
double SL = PositionGetDouble(POSITION_SL);
2 t0 J, @. _7 v8 |# d$ ~+ Jdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
+ p* S* k5 b$ @if(tipo == POSITION_TYPE_BUY)
" L8 x/ R0 N; }  @* m) C+ _- v: h' W{- s' r7 m+ _8 K+ P  y1 e) r
if (cotacoes[1].high > cotacoes[0].high)5 w. R) z, V' J) W: z/ w
{
0 S8 D- z, c, Bdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
& ?+ t! E9 }' ^; a( o% uinfo.NormalizePrice(sl);8 @! U2 [, x: \  q2 x
if (sl > SL)3 p7 ]' g, u! L
{; S7 Q/ A  R* M: l
negocios.PositionModify(_Symbol, sl, TP);
0 g" L* p* x- X" {: S6 Y1 v( H6 i}2 c5 L; y0 j' J% ?
}! W" M) \# K7 b/ h
}$ J1 ?% `* l+ d+ T1 B/ d6 w- b& e
else // tipo == POSITION_TYPE_SELL
& v* S4 [& s( H# }1 u{
" h0 v1 S2 b! L: n/ g$ i: Rif (cotacoes[1].low < cotacoes[0].low)
; L+ K# n% f8 ~{
- k& k% Y& E. G! Preturn true;# z# X. v; ]# C, v' {: U) c& V
}9 B- n$ l1 K, _; \+ E6 P4 M
// there was no position
" [" y7 S5 w" H1 Y* A# k5 y5 Preturn false;
4 |3 u4 d, m% _  r0 N# H/ J}3 t( C; k. \+ s9 ?
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。( _+ x  K# s- b9 ]6 N- c, s
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-23 18:47 , Processed in 0.404218 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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