私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA. R0 {2 B) [, C) F/ F
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。# B. |8 E9 f" }  x
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
3 {. M1 K! f4 X' }8 j以下是制定这些规则的代码。) s/ ]# A) W7 R+ ?3 s
//--- Indicator ATR(1) with EMA(8) used for the stop level...4 e) E8 D2 _: q/ _- [; S
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
% g$ A0 w7 S) l7 |! t0 Q7 Jint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
# L2 i$ @" d$ p& q//--- Define a variable that indicates that we have a deal...; m% A( i# n, d$ b+ H1 a
bool tem_tick = false;
  \" s+ u! T% P/ B8 r//--- An auxiliary variable for opening a position
, \: t; d6 y  G; [$ g$ |8 t#include<Trade/Trade.mqh>
/ T0 {% M0 z3 ~5 Z# ]' [* I3 J#include<Trade/SymbolInfo.mqh>5 W* u+ y3 B) F" O, \
CTrade negocios;9 G4 Z1 _( H5 ]$ \1 Z+ }
CSymbolInfo info;
) L! z, z: {4 D! J7 {( p* B( b//--- Define in OnInit() the use of the timer every second
( A1 p3 y8 q8 N! [5 {5 S  e//--- and start CTrade/ G3 |- S! G: N: u
int OnInit(). n; o% Y- R, [
{* G. \) I1 ^( X
//--- Set the fill type to keep a pending order6 v6 p: {5 l  Q: C! V3 B
//--- until it is fully filled: x% K; F  `1 N* O- b8 F
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
) s0 u" Y4 T1 R4 F& I: ^//--- Leave the fixed deviation at it is not used on B3 exchange
% y5 X3 \: Z; j, l+ O$ A9 Knegocios.SetDeviationInPoints(5);! e4 k/ E: e: z9 b& g
//--- Define the symbol in CSymbolInfo...
: }% W" A7 i8 u5 W  M5 Binfo.Name(_Symbol);, ^+ T/ N8 L! A$ h: J6 [& C8 Q
//--- Set the timer...
) n7 E. @6 L1 t( D% IEventSetTimer(1);. x) a& M0 L; K9 U* V  l
//--- Set the base of the random number to have equal tests...7 u3 s5 w/ B# M* t
MathSrand(0xDEAD);
2 ?4 N' M+ ]2 y' j5 v. V( n. areturn(INIT_SUCCEEDED);9 N4 |: g. @$ n  f, F
}! @1 B# W. F: _. r3 A9 Z# r
//--- Since we set a timer, we need to destroy it in OnDeInit().# t# L2 ~2 z; W7 ]
void OnDeinit(const int reason)7 S( K: Y4 v& Z! F
{, J, Q- p( M  r$ A
EventKillTimer();
( |& j9 s) g: p* u}
# x4 i/ w) w6 E& n, z2 w//--- The OnTick function only informs us that we have a new deal& _8 R' D& W: K% p- o! r' M3 ~
void OnTick()+ D" Z+ X7 c# h9 ]% w; H% Z7 u7 c
{- x. @4 j) ^  d
tem_tick = true;
4 f( ?  O. E  F( x% O}! ^+ r4 _0 D+ g2 V5 o; s$ P9 H8 {
//+------------------------------------------------------------------+
4 z3 x  A# b* _6 r2 l+ O& p//| Expert Advisor main function                                     |  b$ x% z/ i# ?$ O- ]
//+------------------------------------------------------------------+" c& e1 K: m+ v3 j. R. m* e, U
void OnTimer()
/ `3 @4 M# M* `' e{
+ m9 u4 E7 v9 V, X; F$ SMqlRates cotacao[];
' y* U; C9 T! ^5 J" `1 |return ;# e# g+ Q, V4 l
if (negocios_autorizados == false) // are we outside the trading window?
/ c6 d+ m6 r6 I" Y3 G6 dreturn ;
$ s/ f, \- @/ T* N% D. g' h//--- We are in the trading window, try to open a new position!1 O8 O9 w5 d; o
int sorteio = MathRand();
+ w4 I( l/ s6 d8 k. }. r, F//--- Entry rule 1.1
) ?+ B- j+ U- n  D- }if(sorteio == 0 || sorteio == 32767)
% T6 i9 o/ S9 E' K$ i% Sreturn ;4 T: _4 U( P( r/ K5 ?8 O
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy7 f7 T  c* n3 F+ s
{
5 m& c( F# C; F* U/ Onegocios.Buy(info.LotsMin(), _Symbol);
; }8 [. c- a# M5 B# K# h}4 F! Y* U" L  y* Z% q0 I4 _2 G
else // Draw rule 1.3 -- odd number - Sell! S" o& Y% l1 h. t
{8 s. v( p6 b& l
negocios.Sell(info.LotsMin(), _Symbol);
/ a( J, R" \) B}
0 S& z) @  n3 V# M3 j6 H}7 E. {5 ]4 t' j  ^4 K/ r8 U  W
//--- Check if we have a new candlestick.... H% S3 W( Y% s$ F
bool tem_vela_nova(const MqlRates &rate)
9 A4 x" ]. w4 g{* n3 Q2 x0 G+ \' t  {
{
( B7 {) F; n) ^) B/ T+ Nret = true;  E, B4 Y# S" |$ C
close_positions = false;
& g  J3 [# z2 j0 `2 ^5 D0 m- b% j}; ~8 Y8 e6 ]2 T/ e
else$ ~4 u6 L+ E) j$ r
{( @  w! z' |) W$ x0 i
if(mdt.hour == 16)) Y9 |' `; X6 p$ |/ U
close_positions = (mdt.min >= 30);. i( Y2 J$ k' U5 w7 d' P
}
0 A6 n+ w* M% I* e) H}
( f. ]$ q8 @% V( creturn ret;
% }% F0 m7 G1 X% x7 K4 J: m}
; P  v$ }4 Z+ i. N7 A% T//---
% X. G  g2 v; z/ H, vbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
+ ?* F) f# S# o( ~; M+ w# k: X: P{3 x' f: o9 h4 ]0 g
if(PositionsTotal()) // Is there a position?
' n6 n' i6 }9 y( Q{9 n7 W3 }9 `! ]
double offset[1] = { 0 };
/ I# w& @0 p/ o0 }7 Sif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?* b* J+ N2 w  t: s, P1 w* z
&& PositionSelect(_Symbol))  // Select the existing position!
/ j* c5 x0 w, M: X% ?, w/ ~{
' c3 i* q5 L5 b' k1 v5 YENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);8 O2 o/ g5 n( T/ I- C# t) T$ t1 S
double SL = PositionGetDouble(POSITION_SL);
" T& A' z) P' b" ?) v8 I# |' rdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));4 l: d0 ?9 d+ S# |
if(tipo == POSITION_TYPE_BUY)
6 p; j0 a& t" f! Y# [/ R4 D5 _{
) t2 a9 ]% H- Gif (cotacoes[1].high > cotacoes[0].high)' Y% H+ ?" W% J; n
{
2 _5 T# e6 c- K  n6 gdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
/ N# C( a% ^. m0 d5 X- H% @info.NormalizePrice(sl);
; L" |6 J- d& W3 hif (sl > SL)
3 y5 M8 Y& R2 U( i{( u& J/ \$ E9 m2 x" a' ~( s
negocios.PositionModify(_Symbol, sl, TP);
8 n, H1 l  ]; p7 t$ N( k! o}
" \9 h9 D" ~0 n5 L/ j! v; |- q}8 ^9 W) @4 e' @7 I$ @# Q( O
}
6 i4 q% P0 \- a2 ^* Telse // tipo == POSITION_TYPE_SELL% u& T: q9 a% J3 I) y4 c
{
2 p* \( X9 d1 @if (cotacoes[1].low < cotacoes[0].low)
% M6 O0 I: U# e5 m: o3 b. X+ R3 j9 w{
8 B; ?2 ~5 u" L7 _. Oreturn true;+ B5 Z( x" O0 M0 f7 l
}1 Z/ r6 P* X% D* v
// there was no position! P/ c5 H# B0 z4 ^
return false;3 d7 g% T" B' I) @% t" v7 x
}
& Y$ F1 x& j6 }1 @' Y我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。' O) s# t% h, m0 `! M, x, K
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-27 15:10 , Processed in 0.618327 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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