私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA: J, \+ r% ?9 _' k8 J( T
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。! B  q& d0 O  e' E
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。: @  J" n3 T8 ~/ ^
以下是制定这些规则的代码。5 `5 E: l/ |8 }2 }
//--- Indicator ATR(1) with EMA(8) used for the stop level...4 Q9 L. N- Z% X9 s4 v7 \
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
# L- W  D* n% y) {% pint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);/ O9 ~/ U0 @/ N$ y& R8 I( F3 H' v
//--- Define a variable that indicates that we have a deal...) P' {; m0 Y$ P6 o; q
bool tem_tick = false;
0 I, D* A+ M8 o6 y//--- An auxiliary variable for opening a position
' o5 o& m9 [7 [2 s: C+ A#include<Trade/Trade.mqh>
5 c' k# G  N9 o$ T#include<Trade/SymbolInfo.mqh>
$ i1 ~( I) {4 A$ f8 j3 o* zCTrade negocios;3 E* `. `) N1 m
CSymbolInfo info;
8 N( P6 @/ p$ `6 |* X% ~//--- Define in OnInit() the use of the timer every second
% h$ U3 T  x2 [3 F1 d//--- and start CTrade5 ~& [3 A! l3 l- J5 S
int OnInit()% y3 Q$ d0 Z8 L' w; h+ Z
{
% C1 m0 t3 y) u//--- Set the fill type to keep a pending order
9 n1 ^; |2 Q- A% _9 Y//--- until it is fully filled7 V: N4 ~% b6 t# @# o
negocios.SetTypeFilling(ORDER_FILLING_RETURN);; J1 b' S! r6 K, b; c
//--- Leave the fixed deviation at it is not used on B3 exchange
0 Q7 X$ `+ Z) f* M* c$ S9 Lnegocios.SetDeviationInPoints(5);; P+ Y0 l1 d- Z1 I. N
//--- Define the symbol in CSymbolInfo...
% r9 |5 v1 S$ ]. b) k$ Xinfo.Name(_Symbol);( u/ W  |7 I1 c+ p( p
//--- Set the timer...
; T; x7 p& q9 [. U( V) gEventSetTimer(1);/ q# z+ @) d& d9 f  u6 M& G( `) p
//--- Set the base of the random number to have equal tests...3 A/ n$ F( f4 g* [6 F
MathSrand(0xDEAD);! z  z9 b, P! z
return(INIT_SUCCEEDED);% Q) m6 X# o3 O8 i$ |9 |$ Y
}
' t0 a5 `: N2 u! R1 y//--- Since we set a timer, we need to destroy it in OnDeInit().5 O5 u. J# s- \% W: ^+ a8 Z
void OnDeinit(const int reason)2 K) ?& T5 Q+ |
{+ B4 X0 Z" }7 z- E  o
EventKillTimer();
2 g1 ?8 o. E9 @5 M! e}
: ]2 [$ ]- Y3 c% ^9 |//--- The OnTick function only informs us that we have a new deal
4 e' {, ~3 X9 |1 Qvoid OnTick(): c1 h4 C: S( \6 z+ m' d; x$ ^& U
{" C0 ]; w5 f  u+ @
tem_tick = true;; ^. [" S  K2 R3 W1 Q. J( o
}
6 R% G4 Q- r* x3 B1 X4 |- `% ~% c# h, ?//+------------------------------------------------------------------+5 g# e3 \1 `8 x/ j
//| Expert Advisor main function                                     |
& P( E: y2 `0 k! g8 U//+------------------------------------------------------------------+
) |$ t5 n4 ]1 G$ t; ~$ E+ }, d$ Dvoid OnTimer()* r: F; @: T% k. }0 }" V
{% @0 l! B1 z+ p6 _7 q$ Z/ {- `5 J( _
MqlRates cotacao[];
" \- J9 }9 g  X+ N0 r! j' ereturn ;" v' u3 Q9 \: s
if (negocios_autorizados == false) // are we outside the trading window?0 E% t, A0 k1 }" R& m
return ;' \) _( Z+ h0 g: r( r
//--- We are in the trading window, try to open a new position!! i' v) y2 o3 G4 M" K
int sorteio = MathRand();" m4 Q( e3 j4 g
//--- Entry rule 1.1
. N* H: ?/ C" \: Q% Y) c0 s. M) ?if(sorteio == 0 || sorteio == 32767)
% h2 x# {6 z9 v; g" s. s6 m( V  Zreturn ;/ f% I! O- r/ W% O4 ?4 Z! O, H
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
' g) _3 ]# b" Y8 l' ]: U{
8 ]4 {- A% o3 w* K, anegocios.Buy(info.LotsMin(), _Symbol);
- v' C! m4 d, M( D: v$ n6 U}$ T1 l% ], |  l
else // Draw rule 1.3 -- odd number - Sell) ?: i3 R# y) t
{8 W% k/ L: z2 m, P0 j( }7 l8 _
negocios.Sell(info.LotsMin(), _Symbol);6 R7 r" z6 n) |
}
) m# u6 V( p% l' Y- a}
1 ^1 O( w; G# N0 J//--- Check if we have a new candlestick...+ U3 x9 Q& n: D0 I
bool tem_vela_nova(const MqlRates &rate)0 m4 c7 J3 T" s5 p
{
$ U6 d) n: [- b{8 g- l6 ^$ U( u# e3 M1 W) R4 ^
ret = true;
/ o; E- P( {& [% ]7 zclose_positions = false;
. R1 L" v5 `( F) I}- C5 G; v8 M" F5 A; c
else% w( z9 e! f  P: w
{4 T" \# h# t! r. @! j
if(mdt.hour == 16)
/ ~. a% d7 t4 d4 }' Rclose_positions = (mdt.min >= 30);9 X! t1 M% s- L# T+ g: O, ]
}4 I; y3 Y5 h; A1 [7 c- D7 e" }
}/ i* ?6 b  A$ D' `- I0 _; d
return ret;
2 m5 u4 m3 [; W) A  u& h}$ b5 b/ x; v. `0 G
//---
1 r+ P) h8 n8 S) j7 d/ [( Sbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
$ {; {9 a. V8 O6 b5 m0 @0 g& l; d{
/ l2 k- w% f) |1 Bif(PositionsTotal()) // Is there a position?
# y" U; A' O' K3 S: G{
6 A' S& \; u. J: Gdouble offset[1] = { 0 };; T1 T5 i$ j9 m4 w" I# ?
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
$ M, i( d5 {: X) B2 Y&& PositionSelect(_Symbol))  // Select the existing position!
& Y0 |/ @+ W5 |0 N' [{
" l# p: M- k! n$ @7 NENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);' o: u8 f+ O1 f& J- F
double SL = PositionGetDouble(POSITION_SL);
6 {4 s/ a, h/ U& i8 tdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));; G( d- A+ [3 t$ u4 `
if(tipo == POSITION_TYPE_BUY)
* L" D/ Q2 i" n, `3 }& Q{
3 a5 C8 E; r8 \. u) `if (cotacoes[1].high > cotacoes[0].high)2 u' G7 l* b) D/ }  q
{  G9 ], S( y; o# F
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
8 p- Y( c  F1 N: R+ _% V* l2 ?" O7 Ainfo.NormalizePrice(sl);2 A$ z6 V9 s" B8 A5 J3 T  b
if (sl > SL)
' ~3 i8 a6 m; e2 A: m{
; M- S; n5 L, i6 unegocios.PositionModify(_Symbol, sl, TP);
. K- z6 n! Y" j; D/ C}
# y+ ~6 S& ~+ Q8 x' X; f}
  O: }4 Y7 N% G5 E/ x1 G}
: u. j' m- f+ Z: b/ t% @else // tipo == POSITION_TYPE_SELL
  [$ k" K# S, D9 N9 a/ U{( U+ g, Q. `8 [4 }
if (cotacoes[1].low < cotacoes[0].low)
9 t* @/ R, Q8 e( Q7 I' C{+ `* J' T6 q# D7 Q9 t5 F
return true;- q' M5 Z. Q" b; I9 d; Y" d" K0 u
}  _  e4 ?) K. [
// there was no position& I7 E8 b( F! O% W$ \; v7 u
return false;1 h: {* ?; ^* V4 i6 e* {/ W- K0 ?  K- M
}
7 h7 E7 U: G9 S1 U. Q我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
5 v7 p& z- a  o9 z# 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-25 01:33 , Processed in 4.992090 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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