四目观天下

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
6 M5 G3 q# k0 [, \/ n) E  l# n在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。* o' U9 R; N& U& y0 ]8 o2 n
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
  c: v9 z. z1 D以下是制定这些规则的代码。
3 [! H- s/ n: w+ N//--- Indicator ATR(1) with EMA(8) used for the stop level...3 {0 M& x/ S9 K' N5 N4 \
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);' X! ?% Q, `: ~5 {) H0 n
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);. o0 ?3 E* d3 L- G+ ?" O/ k7 G
//--- Define a variable that indicates that we have a deal...
0 l- t- B" |4 a: Y8 F* bbool tem_tick = false;
& S  i1 Z; {$ y; ]6 A//--- An auxiliary variable for opening a position
2 V7 t( N. Y/ }#include<Trade/Trade.mqh>
# A  T5 m0 [3 i8 K, ~: e: ?2 Q#include<Trade/SymbolInfo.mqh>  k3 W: R* |; U  Z
CTrade negocios;4 T7 K4 \, X/ i: m7 I
CSymbolInfo info;; R9 F- {9 Q& Z% [
//--- Define in OnInit() the use of the timer every second
% A3 ^4 [, J. O$ X& @6 X( V$ p# q8 M* x//--- and start CTrade$ P, V& k8 O! |, V  L5 k
int OnInit()
0 |" r2 d9 d2 g{6 o/ \: z" _7 y
//--- Set the fill type to keep a pending order
% @* t2 X2 o- j) P: m//--- until it is fully filled
! a* ]) s: p- @9 \9 Rnegocios.SetTypeFilling(ORDER_FILLING_RETURN);& H3 P6 b% S$ K6 x
//--- Leave the fixed deviation at it is not used on B3 exchange
- p% z  X/ O, s( u- g+ t8 k3 t' @negocios.SetDeviationInPoints(5);- T/ {+ F. H6 E& p* Z
//--- Define the symbol in CSymbolInfo...6 Q* V4 N% h8 f, _
info.Name(_Symbol);! X4 U5 h8 n, F+ A
//--- Set the timer...2 Y0 J; X7 C1 t
EventSetTimer(1);# V& I: q1 ?) h- O: ^! w4 V! ^
//--- Set the base of the random number to have equal tests...
2 O4 t! n6 X# X& r' E, SMathSrand(0xDEAD);
  N* e$ m7 t9 y* |  N' j' j0 E0 i7 sreturn(INIT_SUCCEEDED);
0 A5 X' _" W8 Z3 {; C}
9 A" D0 _1 g8 x1 I# B3 A% u5 K% Q  E//--- Since we set a timer, we need to destroy it in OnDeInit().: |1 x) o7 A. ~1 M( ]* g# k
void OnDeinit(const int reason)1 w, j  u/ N! j0 Z' ^+ K
{6 B, b0 j+ S( C! W8 u
EventKillTimer();
& A3 U4 [( e5 Z$ U; x9 f}8 u8 i3 m) g# ?- M
//--- The OnTick function only informs us that we have a new deal- \7 U; t. u: `% F) k
void OnTick()
/ m' I* a  D5 J! S+ o{
  W" V" q& M+ ctem_tick = true;
0 y+ }( J! \) S7 A7 a) `, b}- G; x+ c! E* R
//+------------------------------------------------------------------+9 i& {+ F& \: I5 B: s
//| Expert Advisor main function                                     |
: }* v$ B9 `+ ?* n  }//+------------------------------------------------------------------+
6 s8 m& M) l6 [* ivoid OnTimer()
7 n. f- `4 @( H  A: Q+ [{; |; c# p. Q8 O* {$ g# G
MqlRates cotacao[];
! h* B4 u4 N1 O2 M+ u6 F# }* m# ereturn ;
: q! K8 H. j" s7 Kif (negocios_autorizados == false) // are we outside the trading window?
; p6 w$ c: P& D+ P; O( k' g1 ereturn ;* w. Q+ h7 U. [/ {! c. R) D3 q4 e& C
//--- We are in the trading window, try to open a new position!
" ]' @: f+ j' fint sorteio = MathRand();4 L- T$ l* X. Y  f1 d( B. u0 `
//--- Entry rule 1.1
* z% _+ @& P0 u1 kif(sorteio == 0 || sorteio == 32767)
8 @7 B$ h* K! [5 e7 Mreturn ;
* y6 q. K" {( P" g( Jif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy+ }" w" ~+ d/ M/ }7 X9 |
{. G# s+ k' {0 ]8 e# W) S! O6 i
negocios.Buy(info.LotsMin(), _Symbol);
. J2 ~% k- B) T. O}# }: y% U- p( g& W' u# y% Q
else // Draw rule 1.3 -- odd number - Sell) d# @3 X! r3 I# Q
{& A* K( w/ b/ K8 t* C% P
negocios.Sell(info.LotsMin(), _Symbol);; ]7 B. L+ U% m# X+ X! J5 W9 i
}
$ A$ T! z: O& G}
4 b! i  k7 X' P( o. ^//--- Check if we have a new candlestick...
$ g' j  H/ U' v6 K2 Jbool tem_vela_nova(const MqlRates &rate)4 E8 q9 R: z$ f( a. k2 P! Z: D
{
+ Q' q+ }" q/ C3 ?% _{6 e1 S2 h5 w1 r. M6 X
ret = true;
/ s) r# b4 U, [) b, ?! nclose_positions = false;
: C& E7 K, G0 m+ @# o( L}) @  R: H: c. l* \4 ~
else( b8 q( K2 K4 n% H1 }; u
{
9 A( E) s/ E* `! N" ?$ X0 |! aif(mdt.hour == 16), z5 l3 B/ a" B) d  i" H4 K
close_positions = (mdt.min >= 30);
- d, r: @& Y# |+ z2 b% E" `- }/ A" k}
2 a+ b* N; B* h8 a}5 _& `. o8 _- ~- L- s# b
return ret;. F8 ~  v+ |+ S/ Y, I* S6 H/ L1 B
}
5 f/ I! @4 H) _9 V7 K+ M//---2 I1 D$ a' h- [; \' h! T# f6 B  H
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[]); y9 s; t8 `5 m' y6 K6 N
{; M/ s6 k7 J* K3 c
if(PositionsTotal()) // Is there a position?5 [9 h/ [5 g4 M+ v: I- R0 s/ }
{
8 S) k7 B# B" m5 Q, p& _1 `4 N, P- gdouble offset[1] = { 0 };2 x8 E& _: {5 r3 ]: z) F- Q
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?; H( C& F- \' V9 N" ^
&& PositionSelect(_Symbol))  // Select the existing position!
( q% [* I. x% X: P5 P2 _3 V{
( C% U+ `* e% B; c9 VENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
/ i) T2 G% j" y$ i2 b/ i. ?double SL = PositionGetDouble(POSITION_SL);
$ G  T8 U" z( R' ]1 S8 Hdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));; \" {3 O4 e  N# F
if(tipo == POSITION_TYPE_BUY)
; V3 [) A3 _! ~  ?( r9 _6 @6 ?: j{" |( O# v; z# ?6 r- o9 Q7 d
if (cotacoes[1].high > cotacoes[0].high)
1 b# O) e3 Q* h. Z" F8 B{
1 b% H4 f) y# L( f4 gdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];: C8 L, z# |3 L- v% E1 _5 E
info.NormalizePrice(sl);
' T5 R* c- v  [. p1 R3 nif (sl > SL)
4 h( k4 k% {2 d. b" r9 P0 v{( q& e3 G$ q: T: F
negocios.PositionModify(_Symbol, sl, TP);
  C- G: R; k; N4 z}* }# t% u* H0 p" W- k! S
}" y" X$ z/ {7 H2 q
}  z: ^* X- [: C( s. R
else // tipo == POSITION_TYPE_SELL
3 T" l; S: \1 Y: O6 x{
' U. ^4 ~$ A) W; D" Xif (cotacoes[1].low < cotacoes[0].low)
6 l/ j  ]5 _' E# j* }{
3 Z$ B' o+ M  s: {1 D) Greturn true;, G- E  z6 m4 p! D* }  }
}# a: x9 ^( C- ]; _0 x
// there was no position
; ^. |) N& U: c& {return false;  d2 v/ X6 r: |# Q
}
) [, X/ d6 m0 }1 Z% j我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。) J" `$ }$ F. F
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-9-30 06:49 , Processed in 1.226279 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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