私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA3 e2 \7 W3 _8 ~; P
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。8 h' @3 v9 L" }
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。. w: j0 `# y( X7 z% q
以下是制定这些规则的代码。. D4 }0 N7 [  O2 {* Y( Z1 r) v
//--- Indicator ATR(1) with EMA(8) used for the stop level...
  b5 h. x& a' a: v6 a2 {) I5 zint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);+ z  S; b6 A/ [: m/ {
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);. G: ]' Y- A/ f* U4 Q* v
//--- Define a variable that indicates that we have a deal...3 Q9 l8 N4 M8 ^' p0 u
bool tem_tick = false;
- x6 _  B2 Y8 A//--- An auxiliary variable for opening a position  C- a/ ]  A" b0 W7 @
#include<Trade/Trade.mqh>% P, ~0 w# w) T$ Z
#include<Trade/SymbolInfo.mqh>, O% X: g: _  c' Z* J& b4 @: L8 ~/ e
CTrade negocios;  g* n# P2 m( E% d/ w% K( }: n
CSymbolInfo info;
1 s  z1 S* y1 m# K8 e//--- Define in OnInit() the use of the timer every second
' E9 R2 V/ c- q//--- and start CTrade
. B( r- t5 r1 n. x2 J2 p  xint OnInit()* Y( ~( b0 e+ Z! I0 [5 j+ ?
{
5 _4 z+ a( @* @% P0 w! m//--- Set the fill type to keep a pending order
) G4 a" q: ~0 y& B9 L* B//--- until it is fully filled& t* s& d) c  @+ ~( M& k
negocios.SetTypeFilling(ORDER_FILLING_RETURN);9 r' D  X5 ~5 `6 d& Y. ~, J) A
//--- Leave the fixed deviation at it is not used on B3 exchange
' j/ N+ k$ A3 q; a* X: p7 knegocios.SetDeviationInPoints(5);
9 J! Y; n" B" g: B* U. D8 O//--- Define the symbol in CSymbolInfo...
3 \8 I  K% J9 T( m. t4 pinfo.Name(_Symbol);
4 J+ F7 U7 M, m% s: i( a1 v! I//--- Set the timer...4 R  E& K9 A: K, X5 U  r9 A6 h: L
EventSetTimer(1);. g8 g- p/ g7 o0 P+ J: ^1 [' w
//--- Set the base of the random number to have equal tests...
, M& S; j$ q" k- G- U: xMathSrand(0xDEAD);
: s* g5 P" B* F' A& d( Preturn(INIT_SUCCEEDED);
8 U. G9 o5 g# u}+ n1 j4 z2 E% I% @
//--- Since we set a timer, we need to destroy it in OnDeInit().) w/ h. m) b* E$ V4 D, V; j4 m
void OnDeinit(const int reason)/ p3 Y4 v  M2 S) q, H6 P
{
  k2 U; F% a" m% D" KEventKillTimer();
6 I$ d  N* L! \+ k% a}
2 K: F  z0 m7 \: E//--- The OnTick function only informs us that we have a new deal* ^+ H: B6 O: `# G! s5 ]
void OnTick()
. ?. O! _0 B' X' T$ ?+ v  ^{. K& r' I2 {2 l( K$ L. l0 u/ |$ Z
tem_tick = true;/ M2 \. o5 _* u4 P- {; r/ i: v
}; @; s2 C  [) ]+ J
//+------------------------------------------------------------------++ _( A" w0 E# b1 A- ?
//| Expert Advisor main function                                     |) ?* b# j8 I: n1 A2 a
//+------------------------------------------------------------------++ N# `3 a3 G$ D* R( E9 d$ ^
void OnTimer()+ b! S/ D- g+ }! G& Z
{2 j0 X* j& U0 j* I( m
MqlRates cotacao[];
8 ~" H' c5 x* U3 ~# D; [- areturn ;$ U+ l* J* c/ \2 h$ w% V9 A
if (negocios_autorizados == false) // are we outside the trading window?
" \$ }# e$ Y& H1 I: kreturn ;
! K. F* m8 J5 A( L//--- We are in the trading window, try to open a new position!- \4 A1 K; z1 u6 d2 y4 {" s! c& s
int sorteio = MathRand();
) r/ d4 N1 B3 w( k1 z5 `' r//--- Entry rule 1.1
% s1 K  H: y% H- E$ R$ @- @if(sorteio == 0 || sorteio == 32767)" C% B, y' {0 {
return ;
5 b) r, C  F" \1 }# f) ^if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
! e* }5 A0 t  c/ |9 n$ H{- E0 X/ f2 Q! {& T0 ], W& S
negocios.Buy(info.LotsMin(), _Symbol);8 V& q. |$ O# F; n; Q( j  z9 {# h4 z4 z
}
- M1 X5 w0 H% L0 M5 [' lelse // Draw rule 1.3 -- odd number - Sell
$ l% ~1 g. B0 b( d( B{. [* f2 p' r# m: w  \3 a) }
negocios.Sell(info.LotsMin(), _Symbol);% \" c8 Q$ _7 X
}
" {( }5 b$ @3 S8 R}
/ {) Y: x  k9 r% r' \, J//--- Check if we have a new candlestick...
9 |5 w) g3 ], Z  ~" m2 C+ I8 m9 W$ vbool tem_vela_nova(const MqlRates &rate)
0 d8 r% c; W  T5 F1 I6 @8 w{0 S- q# g6 `* D6 X9 z+ E3 N
{
& ^6 H2 L5 z* t. k1 x% H6 \( Hret = true;3 G% n+ O9 P1 n
close_positions = false;
7 q+ E, Z8 q1 m}
3 h& A* O3 R) w1 ^! C1 qelse( T, ~3 @9 S; C: P! R# H
{
& g0 a. s3 [  fif(mdt.hour == 16), J. m/ T) h( Y/ N$ r
close_positions = (mdt.min >= 30);
) Z  y! Z* N+ s5 j}
/ z/ y: W' z% m. G+ k0 C  W}! ^# P. c  S) ]
return ret;# ], g7 e: |' C7 }" l5 z( y0 O
}* b/ n4 p6 a; ?4 h5 P
//---$ v! g9 J' s% y. v; D. }
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
) y7 r: w2 W) h, \7 {, y" D8 [. y{! }/ k8 A. e$ j; \# N* f( W
if(PositionsTotal()) // Is there a position?
! a( N6 C# i, p' w{
+ Y2 _; V% K- B3 Sdouble offset[1] = { 0 };4 H3 q6 c$ n0 h6 ~
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
1 `+ {' P' G7 I  L- q6 O&& PositionSelect(_Symbol))  // Select the existing position!
6 Z8 ]7 N' U$ }# z- F1 C) w3 ~3 E{+ h' h- T: i) s2 z. d1 n8 f
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);" u4 ~1 f: i5 J' P4 j7 n/ ]4 Q2 k
double SL = PositionGetDouble(POSITION_SL);8 {8 U) ^7 w" O! K; |
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));; z- Y+ e* }. \2 F
if(tipo == POSITION_TYPE_BUY), V, L# S3 N" F5 J; X8 c2 i
{
- x5 _7 a% {* v* [3 Z4 c4 g3 X. a9 Gif (cotacoes[1].high > cotacoes[0].high): P2 H, t' C2 T" E8 Z
{' o8 e, T, a# T4 s- t" ]
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
, B9 O/ z) v( ninfo.NormalizePrice(sl);! c; I8 v+ C, P7 l  `
if (sl > SL)
8 o* c# a, {, ~6 s{
8 q/ z; B: h4 A4 E( wnegocios.PositionModify(_Symbol, sl, TP);3 U  f  G+ y+ J5 k* ]3 z/ d
}
. B- ~. }) |+ z& P}; [$ u: B0 V7 ]; z" q# g
}
' f+ f( l( ~) x8 [9 @else // tipo == POSITION_TYPE_SELL
+ E3 b- O' y2 v! r) [5 D- {: X# Q; K{9 n0 M# G7 e% t% K( s* O
if (cotacoes[1].low < cotacoes[0].low)
; j2 c# k5 `; w. |{
/ L$ K% t6 f9 f' b# Ireturn true;
+ ]4 p: R$ i3 u/ d0 I}
. y% c0 o7 B9 V7 {7 \& c  D// there was no position
9 [3 b" ^( ^6 ^( N! m5 X# oreturn false;
) z8 ?# X. s) H}
: E# T6 R& w+ m; h! _6 O7 ?. p我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
" T$ {8 [4 n. X1 o' Q, d5 e- R到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-15 12:35 , Processed in 3.399035 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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