私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
- `0 C0 W: z. f  e+ z在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
  y" Z+ v+ [0 s. A% k为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
+ q) z# [! ?% w! Q6 Z以下是制定这些规则的代码。
/ Y! o& g- Y; b/ Z! J//--- Indicator ATR(1) with EMA(8) used for the stop level...
0 {2 t6 T4 ~1 \0 U5 uint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
0 O3 u4 P+ ~2 |int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
1 k2 f5 `, I! d! A9 u+ b//--- Define a variable that indicates that we have a deal...
( d1 W9 ]" z/ E1 r% z: ybool tem_tick = false;
, ?" I  ?4 F$ z//--- An auxiliary variable for opening a position5 t! Q& N/ v; n6 d, ^; Y7 P* f
#include<Trade/Trade.mqh>; T4 S: v# f6 f9 V  i) T- m1 ?
#include<Trade/SymbolInfo.mqh>
& {6 w5 s9 v6 x; bCTrade negocios;! G0 k; F0 U4 Y; C3 p
CSymbolInfo info;7 F0 c/ Q( c' v. s' u6 u; t7 z
//--- Define in OnInit() the use of the timer every second4 Y! M3 E' }3 Z1 |9 i; N
//--- and start CTrade
0 u6 \& m3 O! Wint OnInit()$ F- n+ N9 P4 Z2 ^3 t$ Y7 T9 w! I
{
$ f/ ], \) J* r9 O& m//--- Set the fill type to keep a pending order
0 b3 p0 \# f! Y+ J! ?  q//--- until it is fully filled
8 m' }) m: l2 x! `negocios.SetTypeFilling(ORDER_FILLING_RETURN);2 w5 t* q; j/ [! Q$ \4 z0 s( d
//--- Leave the fixed deviation at it is not used on B3 exchange3 p0 z& ]) p6 v& _1 U5 T* X& L7 _7 ^
negocios.SetDeviationInPoints(5);
: H0 P* {- m$ G0 A+ j//--- Define the symbol in CSymbolInfo...; p& a! B9 }% ?0 ]
info.Name(_Symbol);9 ?' B. H  B1 k9 D
//--- Set the timer...4 O. g3 u& J: N% U! m( \
EventSetTimer(1);
% u* F4 ~( U  x  a4 s//--- Set the base of the random number to have equal tests...' J+ V! X$ R- [4 r; `
MathSrand(0xDEAD);& @1 {, O9 z# L* }  Y
return(INIT_SUCCEEDED);
7 V; s% G, c6 K* `9 U}
2 x( u# G, y8 H' F) Q+ w//--- Since we set a timer, we need to destroy it in OnDeInit().
! d6 A( {7 U( Svoid OnDeinit(const int reason)
1 d- u; i# Q* M{
2 N2 ?9 S: \( J. ^' P' iEventKillTimer();/ V" i" b1 n9 G
}" `! _6 v3 b3 P- C
//--- The OnTick function only informs us that we have a new deal/ @1 A4 w' _: Z/ |" E
void OnTick()
! ^% \5 U! N, l% h9 z' r{
5 p: ]3 |: X4 ?, K5 Gtem_tick = true;
8 b4 D2 r. k# d# I3 p}% |9 y5 e2 Q2 q% h" h6 P# B2 J
//+------------------------------------------------------------------+" h/ t8 d# f4 @: v  c
//| Expert Advisor main function                                     |
. x( X0 i+ L1 w2 Q//+------------------------------------------------------------------++ d) z- w0 R; U* S1 I8 J
void OnTimer()  C  C' E% ^, U: r
{  j% A+ t; h+ h4 G
MqlRates cotacao[];
9 r4 k- g: |) m' l( U4 Dreturn ;/ B# q" i3 S7 C! a0 i
if (negocios_autorizados == false) // are we outside the trading window?3 Z* w' L- k4 L. P1 A
return ;' g: X; B4 Z7 _8 K
//--- We are in the trading window, try to open a new position!1 B( ?7 B' A( ~. o3 j" |6 u' V; ]
int sorteio = MathRand();/ f. `% O* c# \) a
//--- Entry rule 1.1+ f; D7 S7 Q% t5 h
if(sorteio == 0 || sorteio == 32767)
9 x( l/ R) j/ yreturn ;$ P% ^6 ?0 X- a7 m
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
: P) m8 Y2 B5 l# R{
6 t( s0 k* h8 J; onegocios.Buy(info.LotsMin(), _Symbol);
6 m2 h6 w/ b  Z# j& C/ L}( Z% D, _7 o$ d$ s4 m
else // Draw rule 1.3 -- odd number - Sell3 Y" h9 {+ h# w# Y* I, d
{5 h  X) Y( [4 t- q- C
negocios.Sell(info.LotsMin(), _Symbol);  N. ]* f2 P# r, [: R
}4 J- [& h4 h) |1 b% l. k# X" `9 [  M
}4 q/ |  D, V" y! J: R4 `# b" V/ q
//--- Check if we have a new candlestick...
5 A+ K6 X1 I! ^& V8 X$ ?$ Ybool tem_vela_nova(const MqlRates &rate)
2 F% }, @2 e7 b) a4 }* e+ y3 N7 p{
7 G8 z( u* m! z' W{0 g  U  g8 [+ ^" X/ {+ F4 o5 D) {
ret = true;
0 V/ E) d" {! r& Q) {close_positions = false;
  u/ t. m2 q* D0 ^( @) {}
& r& B; n) ?$ u5 x# ^; selse
$ k) {* @/ g, _9 j6 ?* f/ \{6 t% X; P. `  V4 m- p; T% S
if(mdt.hour == 16)& U& D$ ]' H: M: l
close_positions = (mdt.min >= 30);2 \  x2 n+ J+ X# E
}# E& q6 ?* ]! t" E" H
}; m, r9 j; p7 w* J3 Z
return ret;
9 [7 q4 G; ~6 K$ c  S5 ?( Y5 R2 b}8 J0 K, I$ `! ~1 U" e/ k. R
//---
! y- _* f2 D( e' W& _6 e* \bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])4 \: e5 ]+ W" Z) r& |3 V3 j9 M" t8 M
{
% h$ r2 x3 J" Z; U; ~( c' @/ o6 T$ Iif(PositionsTotal()) // Is there a position?
. K! w7 j/ a% X) @{; D$ A! i8 U0 P$ h
double offset[1] = { 0 };
, x% W+ r8 p1 L+ l) l2 h0 k# h9 fif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?2 Z' ~1 y, D- D4 ?5 o% w
&& PositionSelect(_Symbol))  // Select the existing position!/ W, I4 G, v& h% |  |- B
{
) V5 b/ y! q' r4 c5 RENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);/ ~, v2 l* D7 _% A8 B: [, Z/ N
double SL = PositionGetDouble(POSITION_SL);
/ T8 k! E! ~* l# _7 l, s  D2 X+ f/ udouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
: K& h: v) R5 K  j/ {/ y$ H8 yif(tipo == POSITION_TYPE_BUY); {  r- V4 l+ c& _: X0 |
{
; i: o' X$ V$ [3 _3 a( mif (cotacoes[1].high > cotacoes[0].high)
' m& l( T$ g( V- }5 b$ Z/ Z0 K{& V( z$ @( q+ t
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
% ?2 u, H) q! Pinfo.NormalizePrice(sl);
4 P" v: t! g5 i  ?5 u4 X  K2 Bif (sl > SL)
6 ]) G: w1 I6 A" M{
8 d# i2 m" x# r, Nnegocios.PositionModify(_Symbol, sl, TP);
* f' ~- ~: x% `; Z0 x( L' @; s7 b6 e}! Y( Z' \% y% c+ ]; a% L
}/ d6 f: l8 i0 i" x6 j
}; [' b3 L9 T  j2 s5 z' v
else // tipo == POSITION_TYPE_SELL3 i4 O2 j! e( a: Z# D: B+ Y0 n( b
{( Y! ?+ O/ `4 @, Y6 I
if (cotacoes[1].low < cotacoes[0].low)
% G: \0 Q/ V5 l4 e{
  s8 K, p  `( rreturn true;9 }. _! }8 o  g. @2 O2 r) }9 G$ E% o
}, x- o( W" V" ?6 p
// there was no position4 {5 H2 z' Q6 q0 k- c: B$ \
return false;
; n$ ~- C$ I! K) D}
. I' N; E1 s8 `% c) K4 r' u& R我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。3 J- v. x0 e/ R/ ^
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-28 00:07 , Processed in 1.471951 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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