私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
7 r( K% Q0 r' K, C在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。& R2 i, v) g/ H# }
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
6 @# T% k) [. I$ [; |( O( j) s# k以下是制定这些规则的代码。$ e1 Q) Y' c( G; |7 A
//--- Indicator ATR(1) with EMA(8) used for the stop level...
4 J* j2 \8 r: e" e3 c, a. B- rint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
' X9 u' P. E1 c& s* Z/ j; G1 rint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
& l1 Y7 e/ s% o; U; S//--- Define a variable that indicates that we have a deal...
  F( X! _0 l1 h3 Bbool tem_tick = false;
+ ^- {7 A* x4 B& `' A+ B//--- An auxiliary variable for opening a position; k' p' G4 l7 C
#include<Trade/Trade.mqh>- d5 ~9 I. Y1 ?- {  C
#include<Trade/SymbolInfo.mqh>0 h( \1 ?5 \+ f# b, o
CTrade negocios;; [/ A( I& ~' b" A
CSymbolInfo info;! N3 Y/ u2 J8 _9 i* C
//--- Define in OnInit() the use of the timer every second, S9 K0 H1 Z4 W% x+ {; w
//--- and start CTrade+ d5 q6 K; B1 g6 J# b8 g/ F
int OnInit(), @: r6 j. \/ G0 Q; S
{
. T2 g/ E3 K( w" t9 Z% g9 w, t9 e% w//--- Set the fill type to keep a pending order! X5 B# w/ {) {. T4 i
//--- until it is fully filled! _: n- e5 F/ |; p! M5 r) A
negocios.SetTypeFilling(ORDER_FILLING_RETURN);* X3 B/ _- K  [$ w& O3 }% [% C0 n
//--- Leave the fixed deviation at it is not used on B3 exchange
: b3 F, }( [# }% ]) Knegocios.SetDeviationInPoints(5);1 `& X7 x' e8 U. `" {/ d0 m% H$ \
//--- Define the symbol in CSymbolInfo...
4 S7 M4 K' N0 Z8 \! Rinfo.Name(_Symbol);4 x9 @. o6 F8 T0 D+ D
//--- Set the timer...
. c3 r8 P$ M4 o' ]4 ~1 GEventSetTimer(1);
$ c: C, v5 I! ^//--- Set the base of the random number to have equal tests...
: J# H) m/ J. DMathSrand(0xDEAD);
; ?' U$ ^2 B0 kreturn(INIT_SUCCEEDED);2 c5 G/ @* G+ Z" E
}# L7 c! o( ?  a3 [* S3 ^
//--- Since we set a timer, we need to destroy it in OnDeInit().! E1 Z. b# z+ I& g
void OnDeinit(const int reason)
! P/ U3 n/ x* N  V{
0 Z! y* F( o0 o2 T+ H5 tEventKillTimer();
. M! O/ z" D2 c! }) q5 h/ d}
+ W& z6 \- z* D3 a5 _//--- The OnTick function only informs us that we have a new deal* _3 _" a' b3 y+ w. W$ J7 n# g- p
void OnTick()
7 _4 r- P. s) O9 M; h0 L- a, J& b" G7 Q{3 N9 |( X- W' D4 O4 J
tem_tick = true;2 C) O, }0 M$ F1 m  a) H8 p- i% ~3 X
}3 h0 h  o1 @6 P& z7 `! H8 ?! C
//+------------------------------------------------------------------++ f9 F4 b7 s% A% ]( i3 D
//| Expert Advisor main function                                     |0 {+ ^* c. q; X0 L
//+------------------------------------------------------------------+( v, v' |' T& N: X; t
void OnTimer()! B# C$ ~- x$ U
{- P9 q0 |) o, ?3 f, I0 R
MqlRates cotacao[];% W: c. Q2 A  {# f
return ;4 d" V9 U6 r" }' }7 a( K
if (negocios_autorizados == false) // are we outside the trading window?
% ~% U5 P* b/ ~3 V0 [& p" Vreturn ;) N4 ^. z1 _: c# Q( \
//--- We are in the trading window, try to open a new position!
) `1 c  t8 [8 a8 Z  A% G3 xint sorteio = MathRand();
/ ]- f* t# c6 r//--- Entry rule 1.1
" r* W; Z( a9 T& Z$ _2 M4 m/ ^if(sorteio == 0 || sorteio == 32767)
7 V. o: A1 X- }# I5 d& K9 M# h* vreturn ;
* L7 k2 ]2 [  V/ N2 _if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy; G2 j( }" ?% [6 G; n: S
{
6 O) B/ Z4 x9 x* R9 ], ?negocios.Buy(info.LotsMin(), _Symbol);
$ o, W: W. t2 ?) R3 o8 X}
6 Z& @* L1 W6 z( b+ r% Y6 _9 c/ nelse // Draw rule 1.3 -- odd number - Sell/ j6 z* z- Z( U7 ?9 L
{% H+ F9 I7 I! N9 M6 |
negocios.Sell(info.LotsMin(), _Symbol);; A" {" V2 {1 `( H- @& v  D8 i
}& W  P; n5 j; R
}
; A3 o, U5 z1 G" M//--- Check if we have a new candlestick...
) ?, j; d, [2 Z& Bbool tem_vela_nova(const MqlRates &rate)6 i% n% {4 \' a
{
% c' T' `! w$ h& ~$ Z! \1 K5 ~& ~$ C  ^{+ B, K, ~. A; q) L4 |0 [
ret = true;
7 u# T: f& O/ H" g# S# B6 Nclose_positions = false;
( L' ^7 p! x, [! a8 r}
% O  s0 l4 x" m: aelse
5 S  b) C# e  s  R8 [5 p* [0 f{! C6 o* I# [9 _5 j2 r! j
if(mdt.hour == 16)- h+ z! G3 \+ v/ k# j
close_positions = (mdt.min >= 30);3 x3 p& g7 L$ p8 U) U$ i
}
8 ?# L) Z6 y! L3 ~}9 b" X/ Z  c4 Z; X
return ret;# p1 S+ }2 n; x$ w) v6 B# \. _
}5 i* q. u' _% k: q3 J2 y  I. |+ s
//---
2 F7 l4 C; j% P7 G% \! Wbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])# j: X  F: n, L" r) Y' }$ K# P! y
{
7 w1 y2 z( n; E6 m7 o% U( wif(PositionsTotal()) // Is there a position?
: t/ h/ {  y/ w6 x{1 @- p6 C% H; Y' P8 s5 @
double offset[1] = { 0 };1 K( ]' `+ t: S. `
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
( F: ?$ @' `% j0 }- J&& PositionSelect(_Symbol))  // Select the existing position!
0 ?2 O3 T4 v. d; Y{
4 J% |+ u8 ^" B) T6 oENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
& Q$ f  H3 D( q% |9 J# X. vdouble SL = PositionGetDouble(POSITION_SL);9 n- l. @# ]# _% n3 s7 V7 t
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));, I/ w" E1 R, |; R
if(tipo == POSITION_TYPE_BUY)- U" p. t; S- _' R
{/ t8 i+ `4 }9 O: a" ], U' S8 d/ w' ]
if (cotacoes[1].high > cotacoes[0].high)- c: k5 |9 S& Y- w/ ~4 {# x
{) z2 J) h. e6 W+ w
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];0 H8 t" O4 t. F( o
info.NormalizePrice(sl);
" V( W2 t+ Y6 L& ]) o* z* [if (sl > SL): F) m4 L' o2 b2 Q7 |* S- [' ?
{3 f  u& Y" @0 ~' H  b; H# r
negocios.PositionModify(_Symbol, sl, TP);$ ~/ r5 _3 M$ {1 z6 z# ^6 ]/ |
}
  _  S( l- i# e0 a0 j* x}0 e9 t4 @" b5 J
}
4 i/ g! X1 ~9 kelse // tipo == POSITION_TYPE_SELL( k  [6 F  g7 l, S
{
2 ?# @" }2 I. A8 U. u0 Uif (cotacoes[1].low < cotacoes[0].low)
: L7 o3 A3 G& |+ W* E0 K( S* i{
) P( n, r& \5 e! Vreturn true;2 p. J4 [: v& b. I5 P
}2 t3 q  P' u0 ~- G7 \8 O- W
// there was no position
8 `0 {" B( j* ]3 @/ H% m0 Oreturn false;
  X/ s4 o+ b6 h; I! N}
( L1 w9 B. ^7 _2 X, V我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
. v: Z- Y' _2 }1 D6 `: o* {到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-9 01:34 , Processed in 2.764692 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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