私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA# e" ^7 N: l/ R1 H8 ^
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
8 `9 g& E# ]- e! ~2 t+ g为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
4 ?8 w% t+ k1 u% y以下是制定这些规则的代码。. W% z" [2 v9 Q0 z4 O* U
//--- Indicator ATR(1) with EMA(8) used for the stop level...
8 L) ~! q7 E; Q. Cint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);! t4 A' V7 u, d& U8 o0 j
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
6 A0 B9 U+ j7 A4 v9 u. o, G3 w" M//--- Define a variable that indicates that we have a deal...8 A6 p2 Q# u( {6 @3 L
bool tem_tick = false;3 f* k& E; [' s, |, _" l  Y
//--- An auxiliary variable for opening a position' r! S' k. V( ^3 J# W) o
#include<Trade/Trade.mqh>
9 V- A7 B, b9 n- f#include<Trade/SymbolInfo.mqh>) ^, |- t* w- f9 ^7 y5 V$ s
CTrade negocios;
9 k, K/ t, }  `; F" E2 BCSymbolInfo info;$ N! e, T5 L, p& t# V. z8 y6 V
//--- Define in OnInit() the use of the timer every second
* N# t. C: F+ m+ L//--- and start CTrade
. f6 b1 ^' u' P- xint OnInit(), e% N" _0 n7 E5 O) z# E
{- H% f  p# \  V( i; d
//--- Set the fill type to keep a pending order1 m0 q/ h6 T+ d8 c9 G2 F+ r# b
//--- until it is fully filled
/ ?6 E5 Y3 k9 K. a3 wnegocios.SetTypeFilling(ORDER_FILLING_RETURN);! Q% A$ [8 }, Y& ~5 i* A2 f1 u
//--- Leave the fixed deviation at it is not used on B3 exchange
; N8 A3 Q( l8 e9 b6 y1 A$ snegocios.SetDeviationInPoints(5);
" ]9 }1 A( b* r- [2 }; d5 i//--- Define the symbol in CSymbolInfo...3 w' v4 D& W, k( ]+ q% c5 T
info.Name(_Symbol);
7 U5 x. P* U& n, T, D" P. |//--- Set the timer...# h5 s2 _  z' C4 n* C* M
EventSetTimer(1);' L0 y' i- ^( p
//--- Set the base of the random number to have equal tests...
- S/ R" a/ d% b! |  qMathSrand(0xDEAD);4 t/ n& [$ W$ O& A
return(INIT_SUCCEEDED);
7 @+ P2 G7 N; n( O" w( e9 K, T}
9 i3 y- _) D) e/ c5 Z( O//--- Since we set a timer, we need to destroy it in OnDeInit().* b  ?. f2 v+ z$ k# l. {0 z
void OnDeinit(const int reason); |1 \0 a' r+ M6 A8 g
{5 i) e( }6 m1 |  [7 _- s8 u
EventKillTimer();! g  ^" a* l2 N3 q( h
}; ?8 I" I2 m2 N3 O, V
//--- The OnTick function only informs us that we have a new deal0 _, b& ?7 ~4 c( u
void OnTick()
) i# j" J5 J) G2 {7 X{2 J* V* U! N# I4 O2 l$ \8 S
tem_tick = true;
. e- [! P) u* B; K8 N( k! B  r: f}
6 l( d$ L  X; A" X- {) u* @; d8 z//+------------------------------------------------------------------+; m" ?5 E0 {" B+ |, P) {* {& b' Y
//| Expert Advisor main function                                     |
! w" t) z$ B5 |/ u  s0 ?//+------------------------------------------------------------------+0 T' K' z$ ^1 h* S- e6 I/ o* h
void OnTimer()
( o2 [% k9 k% a+ w" i{2 Y( ]* N: W9 t+ d( k
MqlRates cotacao[];( q# g, \5 C- P+ `4 W$ F/ r
return ;1 ~' l* d/ n6 D: x- w% @
if (negocios_autorizados == false) // are we outside the trading window?- \% y9 b  w% v2 X8 ^1 e4 D0 H
return ;
6 g0 `" a; `/ ?& Z: ~- a//--- We are in the trading window, try to open a new position!
5 B7 D! W  m! y' V2 H' |int sorteio = MathRand();
& p+ f) K. I6 J& \. C//--- Entry rule 1.1
. |- o. n: C8 B$ b- R! Wif(sorteio == 0 || sorteio == 32767)# H* m: i9 x; D" C. ^
return ;( y, b1 T6 T5 g+ F
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy' r( d: k! K) ~
{0 T2 G5 V. a' E9 v* a; e
negocios.Buy(info.LotsMin(), _Symbol);+ O  S% D% M# _( P
}
% d/ f4 u) Y, M/ ^7 Nelse // Draw rule 1.3 -- odd number - Sell/ r% @, u  D' h
{
7 _  u( K% ]5 \* `/ M4 Dnegocios.Sell(info.LotsMin(), _Symbol);
. b; _% T# k; @0 V0 ~9 o. A}
- B9 M' \% J  M& q: P}
# `& j) ~  Y- a/ a* b# H3 z$ j! J5 L//--- Check if we have a new candlestick...
' Q; P- F+ N. Y# m- X( J, M9 @bool tem_vela_nova(const MqlRates &rate)
% s; i* G' M1 f3 d8 M% @) W{
/ b4 M8 }5 `$ b( {. g4 e{
3 f9 P+ T6 a7 G; mret = true;
5 A9 F: _- Q" n/ ^close_positions = false;
" C6 U) u/ d" X: e% ~; f4 s3 K0 ^}, c+ e% {/ |5 C- x7 C+ A, l# d
else
8 @$ l/ Y; c( `0 m2 k{5 S+ m" p- B2 R3 s  O- L2 i. e, o, d
if(mdt.hour == 16)- [, l# e* n# F( i/ [/ Y0 o
close_positions = (mdt.min >= 30);
& D) {8 k7 w( `- U, T! t1 `}
" {- m0 y& I# ^3 D' E+ e% X}
8 [5 G" o8 f7 A8 Wreturn ret;
, t3 O+ \( j! Y1 K}
" E7 h" D1 B- k. C8 w//---+ A5 Z, w: U' p1 H
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
7 d" I0 ^  q, \  T, ^& F# e% e  ?{5 v6 F) S: @; N3 B, i
if(PositionsTotal()) // Is there a position?" N. j# x9 P: ~+ I0 p' h2 Y0 n# N3 c
{
$ E% H/ J7 f# [1 s! hdouble offset[1] = { 0 };
, G6 f, I7 O0 w! W+ Qif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
% I# j- o9 D, h. Q: Y7 A$ w4 s8 }&& PositionSelect(_Symbol))  // Select the existing position!. c  l+ {4 T$ f9 Y
{! J7 U& v' F8 |( U3 B9 R
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
" C1 q; N( n2 M5 i7 pdouble SL = PositionGetDouble(POSITION_SL);
7 S8 ~9 e- d. e4 g% l% p. p% Xdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
; Y  R) x& H) A2 n* e& F0 Nif(tipo == POSITION_TYPE_BUY)
0 ?6 M/ s8 v% K3 K' {" l, L8 j{% H, z7 T1 `- X) c% o) G  p
if (cotacoes[1].high > cotacoes[0].high)8 n3 G8 y6 T3 U+ s
{' Y8 Y- H3 @4 ]/ q( ?6 U6 Z
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
! a+ i' m( E( B0 i5 Xinfo.NormalizePrice(sl);
. }9 R) X  J+ o0 g+ g0 v; |! G  Gif (sl > SL)# h6 q/ v$ a0 z1 a% K0 a
{
0 b" M! d9 ]8 {. {negocios.PositionModify(_Symbol, sl, TP);
8 a$ G5 N- O; u+ X$ \+ q}$ c  Y6 g" o( J
}
8 p" g' }% z' G) T- h8 p; p" X) N}' P3 O2 ?) [8 f$ l$ Y
else // tipo == POSITION_TYPE_SELL) D# o' Y; C2 M- O1 u# [2 j# ~: p' ?
{
) P3 G! P. j6 E! zif (cotacoes[1].low < cotacoes[0].low)7 |" y$ C1 J' W3 I8 c
{
9 E0 D8 o4 f( j: o" y# N! y" c0 ^return true;
4 V4 {! Q( b" l6 T7 |}, l+ A- O6 q0 d' v+ ~% n
// there was no position  n" q) \1 s2 b: d8 s
return false;
# t7 X1 E% b# d' o  s}
) S" n2 K2 R* p" ^) }3 P2 `我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
( ~! w. ^) w# y8 \$ B到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-31 14:11 , Processed in 6.430608 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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