私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
" }- r) a0 k9 H' B" B( m$ @在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
1 Y0 U) \5 E8 B! [5 z为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
) ]" R: a; e( Y1 A0 |' w1 X( J. e以下是制定这些规则的代码。' W: g' I- N. b. a( }) D$ S3 O$ N' X
//--- Indicator ATR(1) with EMA(8) used for the stop level...
$ `7 w* B6 k! W6 |int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);# W3 J  K4 ~  v! s
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
" P* r+ J+ e2 W, p* R4 C* V* f//--- Define a variable that indicates that we have a deal...
. M4 L4 [# A. R0 Q" p7 ~: ubool tem_tick = false;
" u2 t2 d0 t. @) j' |6 s, L//--- An auxiliary variable for opening a position5 I7 B4 s. J& \- X
#include<Trade/Trade.mqh>
" H6 }4 V, a0 ^8 V#include<Trade/SymbolInfo.mqh>
+ p3 m$ z) L  V+ I( tCTrade negocios;
/ ?8 g* M# O. m4 _2 n; _9 t* W$ yCSymbolInfo info;8 }7 _! ?, {4 H3 e- a! z, ^& r
//--- Define in OnInit() the use of the timer every second
7 y8 F; @0 s# h) P' M. @//--- and start CTrade# u: q" @/ x+ z% ?
int OnInit()& ?; \$ G# m; a  U6 g% Y( E9 R
{3 T' J+ F& ~- b3 g
//--- Set the fill type to keep a pending order: [! G/ d3 Y1 Z, P7 v! ]" j# u
//--- until it is fully filled; c; H, Q" i1 T$ k+ N, r5 J% K# ]
negocios.SetTypeFilling(ORDER_FILLING_RETURN);! T) k: h! l% y" z5 ~$ _
//--- Leave the fixed deviation at it is not used on B3 exchange9 L- d; V. @8 L2 K6 E
negocios.SetDeviationInPoints(5);+ Y2 K1 e  e8 [, F+ W
//--- Define the symbol in CSymbolInfo...$ X! S2 F! B2 C7 u+ m
info.Name(_Symbol);, U. m; `5 H/ C/ ~$ Z
//--- Set the timer...
" f9 }4 Q/ F8 L. f, W  vEventSetTimer(1);1 W  ]1 Z" z7 C1 I+ h$ Q
//--- Set the base of the random number to have equal tests...
9 g1 b4 H% W( A2 C" h/ YMathSrand(0xDEAD);: b( ^5 i" r  u8 i" ~. v' J
return(INIT_SUCCEEDED);
$ S" I0 K2 j" x$ ^2 X}6 R8 j0 g- h% C) m; B0 Y
//--- Since we set a timer, we need to destroy it in OnDeInit().2 t7 f5 J1 |1 [- P
void OnDeinit(const int reason), E" Y4 C7 c) A, [
{
* d( @/ j8 |4 J+ @" [! ^8 FEventKillTimer();
0 `- S+ t7 l- v4 m+ x# f}6 v( V5 L) K0 o3 y! z
//--- The OnTick function only informs us that we have a new deal2 G  y% \& u' g* s
void OnTick()
2 G" K6 [5 x4 l: h+ h! o* \! [{
  M% Q* [- Z$ Z, Item_tick = true;7 k, x, H7 L" R3 F. f+ M: D
}! v, Y1 n) e' l) ~; Q  H1 s
//+------------------------------------------------------------------+, n$ n9 Z5 ^: s0 D$ [5 ]
//| Expert Advisor main function                                     |
$ V0 _3 [3 \8 \# p& F! u/ m//+------------------------------------------------------------------+0 {' `, F5 l  z% @2 G3 t& N
void OnTimer()
$ \9 ]' M( S* s% v7 O{) K) p# u$ f" U: x* a
MqlRates cotacao[];+ i# v7 {: [1 j( M
return ;+ }- X8 k) {; v
if (negocios_autorizados == false) // are we outside the trading window?
0 \, j5 y* k/ Y3 W) a& m, h1 h# G2 U# areturn ;
& y) ?. h3 p* O1 L//--- We are in the trading window, try to open a new position!
( f5 e: n$ X/ H% Y8 u2 Yint sorteio = MathRand();
0 V! r; ?: u% W# M/ P//--- Entry rule 1.14 X& l+ o( r( K) Y
if(sorteio == 0 || sorteio == 32767)
& Y' D' P$ Q1 H$ Breturn ;/ L% A. h5 S% i+ [2 d; k6 t
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy1 b0 T% x) ?6 i/ D
{# i" [7 p7 {( W. a" o+ @
negocios.Buy(info.LotsMin(), _Symbol);* \8 X; \; p' W) ~" \; }
}
/ v) ~# v7 g* s1 c4 t! pelse // Draw rule 1.3 -- odd number - Sell
; B1 j* w8 M0 i$ v$ O+ {{: f6 R' X2 y8 h( f( e+ N% A) n
negocios.Sell(info.LotsMin(), _Symbol);5 b6 l$ z2 t: @) B
}: G1 o$ l4 Q1 P- f- q
}
' f, G* w+ r3 @7 ^8 I* }7 P0 f5 G//--- Check if we have a new candlestick...
/ ^' n, Z. I' x& x6 u6 xbool tem_vela_nova(const MqlRates &rate)
0 i/ r7 z; @8 A! M* X{
% F9 I5 {0 I+ H; A: o8 p{* \5 F1 W2 }1 v' S4 g/ e
ret = true;
. b* [& I8 b/ p) A; w. k) K3 Nclose_positions = false;
: {/ |8 k6 X; a. {" W4 s& h. N}/ d& n4 |1 A7 I4 S3 a9 A2 _
else( r/ p1 `, l" D+ I+ T
{' g* M+ s, i0 l
if(mdt.hour == 16)
# H2 b% T; R; ~# n9 Z  yclose_positions = (mdt.min >= 30);" L# @- K! `9 @  N. `. ?$ i  g3 u
}
4 a. o. G2 x9 @}
( b% ~5 [" |" [/ Breturn ret;* ]: d# u; D' i  I
}
* n8 B+ w, f6 T% y/ f' E! _//---9 H. ^+ h0 i9 o! ]/ B6 r6 `
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])* Y" T! l4 J3 Q. q  _
{
% N, u3 O6 e1 X8 zif(PositionsTotal()) // Is there a position?
( b6 ^# D( L( D2 |5 O{+ ^* A1 X& Q: U7 G9 a4 N  X5 \
double offset[1] = { 0 };7 q0 D" n/ x! c8 B; ~
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
  e6 Y& {: {: `, v&& PositionSelect(_Symbol))  // Select the existing position!
, J% D0 z1 E2 {4 ?{4 a; {* V0 c- v
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);6 ]3 d% d' v/ i( Z  O' C2 M- Z' I
double SL = PositionGetDouble(POSITION_SL);
4 P$ |4 }/ E. W9 Z+ C* [double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));! F; f0 i/ Z. v$ o" v# I
if(tipo == POSITION_TYPE_BUY)- y) `3 m7 j9 F3 S2 z) ?! H
{6 T) J  Q4 ~: F. D  ]
if (cotacoes[1].high > cotacoes[0].high)
* G/ x( \: w; ?{" Z7 V& P  ]" J5 N) T2 E6 \" c* ^
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];# i5 c5 q* V. |
info.NormalizePrice(sl);
: o4 R$ k* ^7 J3 Sif (sl > SL)! }6 R) ^4 Q1 p# C6 g, Y  ^
{- n  ~* ?; C6 t- O% E, p
negocios.PositionModify(_Symbol, sl, TP);: i: V+ p( w( i
}2 u6 @- s7 R8 y, w5 H
}4 h' N8 d1 {# a& u
}
8 L. o9 ]0 \' [0 A( [* ^, @else // tipo == POSITION_TYPE_SELL- F1 a9 l4 C8 Z8 D8 R
{
5 g9 S' j0 w2 n/ m  Dif (cotacoes[1].low < cotacoes[0].low)
: N. x+ E7 b* Q, H{
- N; b2 q) i2 l* d$ K# ?return true;4 v( H0 g* I' A
}7 ?0 _2 e0 o; F# e& n
// there was no position
( o2 _* T9 F; y  H# G6 Greturn false;( F5 o" A, P7 G& r- c
}' Y0 \1 B6 Z$ |" ^6 f$ j
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。* ]; `& B8 r/ a) ?- s6 ]
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-4-11 01:57 , Processed in 0.527913 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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