私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA$ d3 C: I' U9 m6 ~& p
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。5 A8 G) m7 u0 c0 Q3 @: H/ g6 X
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
; ~6 Z; j2 w4 N, h8 q以下是制定这些规则的代码。
& i- e( b  Y. s8 M1 h% E6 D* D0 [+ C) V//--- Indicator ATR(1) with EMA(8) used for the stop level...
+ f. v' R5 i% H3 k; m9 \- ]5 @int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
$ d! a1 T$ h0 m6 `' j8 h8 b* lint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
. Q8 R! c, h9 c  z//--- Define a variable that indicates that we have a deal...! d7 s+ |$ G- O! w2 ]4 v# h
bool tem_tick = false;
  _7 E3 k9 u' p! {//--- An auxiliary variable for opening a position
. m  f; F8 ^& |! f7 o$ k6 G( D#include<Trade/Trade.mqh>
8 y9 N. f8 J) }/ n+ M, H; j#include<Trade/SymbolInfo.mqh>
7 I2 W3 q5 ^% z4 g! f* ?) QCTrade negocios;
3 L2 b# ?; z" r, h7 _- Y  NCSymbolInfo info;
( ]1 q# C" P- k//--- Define in OnInit() the use of the timer every second
# O, ~8 G6 r- E% T/ z7 q+ T9 M//--- and start CTrade
8 [5 j1 S% v/ G9 {. l# g6 T1 tint OnInit()
0 ?# ~5 z$ Z2 M- P+ Y! ]9 Y& N- o{
2 {- y7 i! _" ?  I//--- Set the fill type to keep a pending order
# X% K" }1 p! k& X/ o& N//--- until it is fully filled  W5 X2 k/ r& T  C' h* P  E
negocios.SetTypeFilling(ORDER_FILLING_RETURN);; @8 {7 W7 p3 \9 c, y/ }* I
//--- Leave the fixed deviation at it is not used on B3 exchange6 E) ?3 x; u* @: ?8 c
negocios.SetDeviationInPoints(5);
0 z: s% d" \- Z& n0 k! P- A//--- Define the symbol in CSymbolInfo...- Y" l' a, a8 a* |
info.Name(_Symbol);
  i" b6 @$ A" n//--- Set the timer...$ M: M! l$ |6 B
EventSetTimer(1);
8 x7 ^$ [5 b% _7 N) B//--- Set the base of the random number to have equal tests...6 W# Y- j5 O4 |) O$ C# Q) @
MathSrand(0xDEAD);
1 W. H$ m3 v; S9 w5 ]  \$ ereturn(INIT_SUCCEEDED);
9 p: y7 a" D+ [" R1 N0 e* b4 ?}2 V) A1 A* \2 r; \. {" R
//--- Since we set a timer, we need to destroy it in OnDeInit().1 k; n% q6 c( j
void OnDeinit(const int reason)
: i. g) G' `6 J  t{
! D: Y: Z. F8 H0 p6 N" y* v6 gEventKillTimer();
, z6 V$ t, D& X: ~}) ~% }  c$ H0 r2 W) C
//--- The OnTick function only informs us that we have a new deal
. ^# E3 _; A" i6 tvoid OnTick()
1 ]  H5 k/ `2 q, ?3 ~3 [{4 C& @1 T9 T4 j, T3 s
tem_tick = true;
/ u5 w4 p; P7 b" s5 l7 h: e3 R}
; M# d* d; Y9 l: m' l//+------------------------------------------------------------------+
; O7 m. ]5 g1 z0 e1 C9 L//| Expert Advisor main function                                     |
+ d! d4 e$ P& w+ D//+------------------------------------------------------------------+
9 a5 a; Q9 C3 Rvoid OnTimer()
! B; [' {% @) |1 h( _& E, k{
5 p. D0 \0 V% U: ?0 {MqlRates cotacao[];
: T% ]5 r4 m2 f3 m6 S! b  Hreturn ;
# }$ w% ^/ Y8 n( p0 P5 Y+ pif (negocios_autorizados == false) // are we outside the trading window?/ o" T/ ?3 k7 \$ Z. w: L7 u, E
return ;
/ t6 s# ?: E5 V2 }. ^6 j//--- We are in the trading window, try to open a new position!
+ w% Y" m. H& Z5 w) b$ j" Fint sorteio = MathRand();/ A4 |; S; `9 j: D; n* P
//--- Entry rule 1.1
5 J# h' o( w! ?( F+ [% cif(sorteio == 0 || sorteio == 32767)9 u3 s5 J3 a8 \3 g
return ;
3 r) |) v. f9 \' o) K9 ]$ ?if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
! @4 B9 W1 K1 f( y9 u0 X{
, t1 M# }4 x; X+ B, Y. K- ?+ u$ ^negocios.Buy(info.LotsMin(), _Symbol);
' G9 ?% e  n) n& f}
; ^+ U6 g+ l0 \6 l5 selse // Draw rule 1.3 -- odd number - Sell
& c, h: A4 ?- k* u{0 r7 l4 n; ]8 `: P+ ^1 A4 _- \& [3 I
negocios.Sell(info.LotsMin(), _Symbol);
" e, p2 f4 W& |8 y/ ~}
0 d9 N% M5 H( A# z+ ]& z}
3 o- E! z5 Y  F. c3 {" _//--- Check if we have a new candlestick...- C/ R& ]3 c' r: O& t1 w" y
bool tem_vela_nova(const MqlRates &rate)
5 }/ V6 P1 Z9 D{
9 v9 d0 `0 K/ ^$ Y) `8 Q{
7 |% s, S' F! `. p' @/ Bret = true;$ Y! i( i; ~5 k1 g9 F8 e$ h
close_positions = false;9 y* ^/ z3 e2 l; F/ {) R
}- V% ^, @+ w/ z( g' a' w  [
else
( J# x/ _3 ^) ?0 J4 n( d7 @! i{
* a6 A6 ~$ X* dif(mdt.hour == 16)
; U! u8 K( z! @) L: n) ^3 o7 d8 Nclose_positions = (mdt.min >= 30);4 Q% u! X- }- k$ W
}+ i0 d0 v# J: ?0 R) ~
}
% U  T8 Q4 H( {5 M: Jreturn ret;! h3 a2 v) \. \& F: b: a2 P
}
, u2 l6 K' n8 F/ ?7 z6 S//---5 f5 R/ y) c& y' }6 t% c
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
5 T  s. P4 D) F+ u; V{
9 ]: h4 J: M: A; k% Q9 `* M7 Wif(PositionsTotal()) // Is there a position?9 W, b( W. B7 i) Q& }
{- Z, u- n5 F" Z0 v' S$ a% `4 K7 i5 f7 q
double offset[1] = { 0 };
9 A5 B) V7 u8 L- A! ^+ dif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
3 }# m5 U- ]% b- B6 N( b" B2 i6 @&& PositionSelect(_Symbol))  // Select the existing position!
, [- @4 [7 ?/ _{- @2 y& e1 H2 N. T3 S' F
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
! n4 `1 J3 P- b% Pdouble SL = PositionGetDouble(POSITION_SL);
% Y, w6 |! S* v+ {( sdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
4 i5 K$ m0 H+ G& p* V% b. wif(tipo == POSITION_TYPE_BUY)' u4 F7 T& |! p$ z
{3 @* u5 m4 K+ A* R" D
if (cotacoes[1].high > cotacoes[0].high)& p. ^+ r+ I2 `+ R" u
{
9 _4 H0 j, v2 [6 [* h0 @; D; Fdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
% p+ f; ^( {0 k7 M; I' q1 x$ einfo.NormalizePrice(sl);- e( l5 ]+ g* r. D9 s6 l
if (sl > SL)( k( [6 l6 Y8 G
{
5 c* @3 q9 M) v( k1 ?% Bnegocios.PositionModify(_Symbol, sl, TP);: J# q. Y) @1 ^! |' v" e3 D! s) z2 U3 h
}
( o# p/ w# ~( R3 A5 A6 [" b4 u}5 M$ m3 N7 i- g. Y! T
}3 v) u) Y! ~) _8 t7 O4 [& c% j& q
else // tipo == POSITION_TYPE_SELL' B1 N* A/ |7 @! k% A9 E3 N/ Z
{
0 {- I# v0 t/ A7 {0 u$ v% Tif (cotacoes[1].low < cotacoes[0].low)
1 o: W0 h* W5 N6 U' Z{, q  N6 S' w. N, i$ j
return true;
5 R  o9 J8 r: U' z  q4 |}
2 S' i# \9 Y9 p8 f// there was no position; M7 f+ B# V6 w: E( h
return false;! J& i4 y9 B. l6 u- r1 {& A
}+ r0 c) g7 O& p7 `) h, h) l; u# G
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。0 C# s' H. M1 {- k& C3 W! `$ z
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-26 03:22 , Processed in 0.437441 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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