私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA# u" k; ]) a( n. y5 d7 e2 l1 K& h
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。. d) v, C, |# L2 o# O, H
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。! V/ I- [% w% |' k+ ]  q
以下是制定这些规则的代码。6 w1 |- F; u3 Y. @5 x7 K% R
//--- Indicator ATR(1) with EMA(8) used for the stop level...
3 p! K5 o3 G1 @int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
+ T7 _* ?; s8 \2 Mint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);4 Q3 @1 F6 r& f: Z: R6 K5 s
//--- Define a variable that indicates that we have a deal...
6 Z* M2 e" B# a' C+ C3 {bool tem_tick = false;* |2 A% T+ f6 V6 ~1 f# M
//--- An auxiliary variable for opening a position# T; F5 |; u4 V) w' B
#include<Trade/Trade.mqh>' [; G4 _" v! I( x4 a4 o
#include<Trade/SymbolInfo.mqh>) Y6 z$ A2 z6 `2 s8 ?
CTrade negocios;5 P6 ]+ ^2 n: B* R, A5 \
CSymbolInfo info;
( \3 y1 D. X. A( E. Z" R+ S1 y//--- Define in OnInit() the use of the timer every second
2 }/ P- S; A3 s' L9 b' \; q, e* a//--- and start CTrade
5 g2 T% ?0 l6 X! L2 d5 ?int OnInit()0 W; a2 w& C4 s! S  C+ {
{  c- C) w1 Q2 h) C; f& H
//--- Set the fill type to keep a pending order8 u# [9 x* p! {0 g# m+ r3 u! z
//--- until it is fully filled
3 L2 |. g. D4 C8 Bnegocios.SetTypeFilling(ORDER_FILLING_RETURN);0 G- r; D5 J& h' \4 M' {3 N
//--- Leave the fixed deviation at it is not used on B3 exchange3 i  k- y  r6 Y& u* n5 K0 o
negocios.SetDeviationInPoints(5);
* B2 U! j/ A. i3 }5 L! b//--- Define the symbol in CSymbolInfo...  {* O; }- L; O9 D" _
info.Name(_Symbol);
6 Z- K4 H* q8 V3 ?, \! z//--- Set the timer...
; o' I5 p& s9 @9 {& w, DEventSetTimer(1);
3 G7 r- b0 k- J! h4 F//--- Set the base of the random number to have equal tests...; G; j% I# m1 S9 C, @: A1 X8 W' j) [
MathSrand(0xDEAD);
% W1 h+ L$ N  V, F6 S* Sreturn(INIT_SUCCEEDED);
0 T9 G2 f* V, |8 z5 o}
; U6 v8 Q1 u1 K' o6 f; W//--- Since we set a timer, we need to destroy it in OnDeInit().3 o* J7 j! h' o) H
void OnDeinit(const int reason)
. z/ b! W5 b' H6 ]{
' ]. ^. _% C4 d1 y5 b* ~2 SEventKillTimer();
, \% ]: I9 q8 ?/ i4 O  @* b0 R}
& T+ n7 {3 L6 n. N* N1 t+ G+ c/ s//--- The OnTick function only informs us that we have a new deal& z& {! i0 _5 e% D1 q2 P, O6 Z
void OnTick()' D& N# W. o1 h4 u) ~; f3 }
{
/ Y+ e# Z: _0 `' ?: item_tick = true;8 u: d1 D/ o3 Z7 ^( d3 ~
}
% y, s' W% A% ?3 u//+------------------------------------------------------------------+
4 u! G* `9 |4 u2 I( p4 K+ Y' P+ _//| Expert Advisor main function                                     |1 F! E4 j- \; h0 u" T
//+------------------------------------------------------------------+# S1 o; F/ d9 }- b. u& I
void OnTimer()
  H# {) r" x' p& x& B{
' q  ^7 V- ~/ LMqlRates cotacao[];+ X. }& \$ K$ t% W9 P
return ;
9 {  b; F( |" W3 ?if (negocios_autorizados == false) // are we outside the trading window?
1 o7 u  u, d  Q; vreturn ;
9 |3 N+ s3 M' h0 j' \6 ]//--- We are in the trading window, try to open a new position!3 x- i# }: j1 G3 ~- I/ ]& }3 |
int sorteio = MathRand();
( k: e' E4 h; n; ?" t! m//--- Entry rule 1.13 l9 i1 D) q  ], e* B
if(sorteio == 0 || sorteio == 32767)
" d: }) @6 g8 }- |6 v# Freturn ;4 D% M0 R) r% j6 s9 k* m: d
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
' A7 O7 O3 V( T2 }! \( H. D{) e$ m* c# y8 q$ I9 Z. d
negocios.Buy(info.LotsMin(), _Symbol);
- l* i$ E. K! w6 o( u' t}9 r$ ~# O8 @" e3 g/ N
else // Draw rule 1.3 -- odd number - Sell9 }9 g$ g- @# p: H/ G7 l9 }" w
{& \8 Q( d7 v4 f( A1 Y
negocios.Sell(info.LotsMin(), _Symbol);
" }8 Y. O) M+ x( [$ ]' B" `8 q% r}
  c8 m8 v3 }0 g5 J) _& J}
$ Z% b& R* Q4 U: ^//--- Check if we have a new candlestick...
/ m/ V0 _. I" [+ r3 v+ P$ Zbool tem_vela_nova(const MqlRates &rate)4 U0 A! M# C5 B/ L8 |
{* [8 \0 I# J9 Q6 ?
{
8 G  m" X: S0 d5 H/ W4 Yret = true;$ H' A; E6 S5 @
close_positions = false;
5 v- _' E/ x9 |  D/ O}/ B2 ]4 J% d; S
else
! q- @; n' |& i, x{
. b! c* o( M% A. }if(mdt.hour == 16)
/ k5 _: _- X: Y9 S% ?( p- f4 J' uclose_positions = (mdt.min >= 30);, r1 x, }0 W4 }3 S0 D; L
}' {" o) e$ l) v' b
}2 p. w( p" G7 H( D
return ret;
- l4 R4 N- u6 ?& F}  W: K" K, E. m$ y2 H1 J: L: C
//---
7 \. [% d4 P% P0 @bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])" t2 O0 ~2 r4 X  ?# ?
{
  L  ~# K# X( p  \+ nif(PositionsTotal()) // Is there a position?
; ]+ S$ J4 u& u8 C{
# i) d" H- q* F3 v. Ldouble offset[1] = { 0 };
) W4 q2 N' v5 X/ s- Bif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
) ^  d$ j3 @0 z. k$ N; I) h, C&& PositionSelect(_Symbol))  // Select the existing position!; X  {; l0 U5 P  f  A7 A3 }. P
{* N8 I: `* {: j; ]' z2 ]4 L
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
. b, [8 G0 e: \* H+ I  \7 zdouble SL = PositionGetDouble(POSITION_SL);
3 t" R# H' H+ m' C" A2 {8 W+ zdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
: l6 Z5 J" |& P! P+ d/ \$ Uif(tipo == POSITION_TYPE_BUY)
+ }9 j" q% W: x9 B. M; j) B{
5 i- E7 G, C8 f1 ]) u/ c% kif (cotacoes[1].high > cotacoes[0].high), }& ?; R( R2 k, a
{+ M: U  S* j7 C  Q
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
1 I& D3 N1 P2 R! [; v6 @info.NormalizePrice(sl);
. n4 U; j3 C, e( a' t. }& Z- ~if (sl > SL)+ m7 \. X- c) V2 @
{
; c' U5 |: i" s! U! E% g/ onegocios.PositionModify(_Symbol, sl, TP);
9 N# k& E! w5 R8 K9 C# Y}( ~, e: Q8 ?5 ~0 Q9 G7 z
}
4 t4 ?; d# f, C& y* c}9 c+ @8 O% v0 T: y/ U1 ^8 p
else // tipo == POSITION_TYPE_SELL
1 E0 [+ F( F' l0 L  l{! [- l1 i4 p: v* ^7 }
if (cotacoes[1].low < cotacoes[0].low)
- ]* \. o* d0 R- f& r2 ~{
) l; Q% j% y0 L7 Zreturn true;+ k" L2 _1 T3 N/ o5 z' y
}2 S6 N& W& G6 j8 q$ K! j
// there was no position
; w# {1 b& j$ freturn false;; ]. ?9 X( l. T$ [' W' ~( e
}+ b) b" [( W6 M
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。" d: B( b' _# ~. D: _$ m
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-9-16 06:52 , Processed in 0.499306 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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