私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
- ~$ q+ Z% `1 d( \, m在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。. Q5 e$ P( z+ {% S( l0 F: G5 c
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
" _9 T7 O5 H; U# a以下是制定这些规则的代码。
( [" T; C9 X  z7 X3 D//--- Indicator ATR(1) with EMA(8) used for the stop level...
; ]$ @3 F. X5 ~: ?int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
2 j0 d; [; I9 e. Nint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
, o7 @3 h- K, a8 ^" E# u7 U//--- Define a variable that indicates that we have a deal...
6 e$ {: U0 W0 R2 F) b5 z8 abool tem_tick = false;4 i3 }9 u& Q# {5 B3 i% k
//--- An auxiliary variable for opening a position# O, ~6 R* b1 e
#include<Trade/Trade.mqh>9 C$ Y: l# _+ u0 ?) U
#include<Trade/SymbolInfo.mqh>. q' \5 W, k& e8 J# p
CTrade negocios;' r3 y( _9 T: L, _
CSymbolInfo info;  F& c9 H( `4 N
//--- Define in OnInit() the use of the timer every second
, n+ k  e+ b4 I5 h! [6 K//--- and start CTrade* B/ {- A1 X% s: @( ~
int OnInit()
2 N5 I# N0 B9 E/ @{
- [& L" Z, o4 Z& F" M//--- Set the fill type to keep a pending order
4 E! k8 ]/ r0 C( I+ j% r" Q//--- until it is fully filled2 n6 A: b, |& F& b% H5 y: {
negocios.SetTypeFilling(ORDER_FILLING_RETURN);5 M% f. V6 O; y+ v& G0 y3 s
//--- Leave the fixed deviation at it is not used on B3 exchange
$ V! i; M5 D$ ]* M1 O& Vnegocios.SetDeviationInPoints(5);4 V, }+ d( k+ p! U. j4 O" `. S
//--- Define the symbol in CSymbolInfo...
* L! k6 |1 ?- W/ T9 d* E. E; Winfo.Name(_Symbol);
# v; {6 L, F) g- Q//--- Set the timer...
/ v1 ^4 n; u' L! DEventSetTimer(1);
& G  p+ [1 p- O//--- Set the base of the random number to have equal tests...
4 e1 m6 G6 L- EMathSrand(0xDEAD);
9 T( N! w# F; E8 C2 Freturn(INIT_SUCCEEDED);# \4 D1 l3 j  V
}& g( k4 d" R: b) y6 a7 }+ \
//--- Since we set a timer, we need to destroy it in OnDeInit().
, k" S0 \! ^& K; S# k- cvoid OnDeinit(const int reason)7 p, o- g# X# k; N+ a
{% H  D" y5 m3 s% W. V
EventKillTimer();: v7 @# L8 p- e2 G+ A
}! T6 ]: E/ V6 g9 _
//--- The OnTick function only informs us that we have a new deal
& S/ H8 o: N0 Y1 o3 y, ?& c- nvoid OnTick(), p) W6 Q9 s( c
{
3 B1 s: c9 g2 z0 `% @) x1 stem_tick = true;9 W) H) x/ _" s% w$ ?1 c! q
}% N2 f8 U$ x; U* w8 `' A; t
//+------------------------------------------------------------------+
# V; K0 b9 J/ [* h+ h5 a//| Expert Advisor main function                                     |6 o0 R1 u; o' v
//+------------------------------------------------------------------+5 W1 y3 M$ g' G. K. ?3 A
void OnTimer()
( A: _: v4 x; P* a8 R; o1 @0 j0 q. z{. c* C4 r' C) X1 I
MqlRates cotacao[];
& @- d- l0 |( Y/ f' q* t# Vreturn ;
( l, b; Z! r% C. @; c. L& Gif (negocios_autorizados == false) // are we outside the trading window?
* G* d1 V5 k$ U! p. r7 Q; `return ;
; |$ N! X$ x# ~3 F; l& s; b/ j//--- We are in the trading window, try to open a new position!
9 C( z, m8 x' R( x4 t6 tint sorteio = MathRand();" K4 o. ]- J$ r( O, ]( j
//--- Entry rule 1.1- W, j, D7 o+ p- M% y
if(sorteio == 0 || sorteio == 32767)
) {" t$ b! X+ ]+ J! @% s  i+ o  qreturn ;
1 A' ^7 u2 o. K+ ?# k$ Rif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
6 l& k9 P" i5 K+ F0 z/ z8 E{) e, J9 X$ O, @. Y2 y, H
negocios.Buy(info.LotsMin(), _Symbol);: D& d# v: E( S* U
}8 e* H- V* G7 T/ J* p: N
else // Draw rule 1.3 -- odd number - Sell( m7 j! Y1 P) _3 S8 W
{
0 e/ W# ~1 \6 f' }4 t$ snegocios.Sell(info.LotsMin(), _Symbol);/ ]6 p8 [, p. v* [& H8 ^9 i
}
3 t/ y6 S4 F& V4 A+ v* N5 `}( s! s3 M9 D( w( C6 G, U
//--- Check if we have a new candlestick...7 L# U5 k- R. }: g! w4 @6 r- s
bool tem_vela_nova(const MqlRates &rate)4 ]0 @  r" C, ?# O  a
{
/ K9 b$ @7 F3 u+ U9 q$ ^* X/ D{
* \1 `) e& \% l# pret = true;* B+ c( C% E7 ^0 J, j0 G) B
close_positions = false;
% {2 s: Z* O7 m$ H# D# u}
+ f% v) j  i4 h  U. H7 pelse
2 b9 v6 W2 c/ O- D' }+ G( B& D{
$ O+ |9 D4 J# S7 W/ [if(mdt.hour == 16)
' x7 p4 b" n* gclose_positions = (mdt.min >= 30);
) F1 n* d# v3 A}
- O. W# i2 p7 z: O8 n7 e0 X}
6 u$ E7 w! g1 _% L2 k- V; Kreturn ret;. D! o' e7 D2 ^1 U
}
% p$ d$ t- X% Z# `! M" Z0 u//---
) J& [1 I8 u& b; X# X0 |- nbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])( B  g7 y3 l" ^2 q( b
{3 h" G! l; O2 |0 G
if(PositionsTotal()) // Is there a position?
) \  l; y6 U0 g{7 ?& _# M/ m2 n+ n
double offset[1] = { 0 };
) i; ?+ a) B# b# W8 lif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?( K# b1 a2 U2 L# W5 M
&& PositionSelect(_Symbol))  // Select the existing position!
' z5 k1 G! J2 T! Z* ?% U, O5 n{
1 \4 x4 c* D8 W% u! nENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
. c# A9 F+ G: C3 d1 xdouble SL = PositionGetDouble(POSITION_SL);& R7 K) ~2 [$ e
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));+ }/ w$ F6 U+ ^
if(tipo == POSITION_TYPE_BUY)2 L% i* ?7 y3 M, v/ y8 ~
{' a2 i: C0 J( }2 ?& Y! g
if (cotacoes[1].high > cotacoes[0].high): ?# t" h7 y9 _$ w/ Y
{
3 m, p0 L; B& ^2 Tdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
# s, f$ O: A7 Q4 Z: _info.NormalizePrice(sl);
' c/ u  @$ \* |* Q4 B) P: z  ]4 kif (sl > SL)
5 g7 N6 w, j5 M; x2 M{; U0 ~7 t- t# a6 R9 U# X
negocios.PositionModify(_Symbol, sl, TP);& E2 c, Q7 p. d6 E% C
}
  m. \: X: \' |7 L. @}
1 E3 ?7 y, {  x' W. w8 ~+ O2 O. w}
" ]) N, r. H4 k0 O% y8 b! welse // tipo == POSITION_TYPE_SELL# w" b1 k" x0 _) l
{/ s! h  ^8 M. L! a: l5 {$ p
if (cotacoes[1].low < cotacoes[0].low), C- Q/ X$ @* `8 `- [1 T( S9 Q0 I' n
{
+ J2 ]( k3 w9 q4 w' ireturn true;) u& e- n: P1 g+ N+ }' B8 @
}6 Q1 X+ L2 o, W4 u% x2 w; D
// there was no position! X" O7 P# R% ^) d/ H9 B& J: p$ A
return false;- q: y6 E' |7 z7 w
}# B- R) W7 V( F; B5 q% l
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。: C! \5 ?! I, `( @: 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-4-1 16:12 , Processed in 1.992589 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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