私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
  ^' Z( a  m. M$ ^0 r. A5 `- V在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。) W. x$ z9 n$ d% S2 i4 |
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。( J$ q! D0 x" o! \# x) f
以下是制定这些规则的代码。
: f* A7 V4 T9 Y% }. V+ [) C//--- Indicator ATR(1) with EMA(8) used for the stop level..., X# n  W$ k, _3 ?
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);0 H7 p0 R6 M; u* V! S: \% X; g
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
0 ^1 L9 ]8 B; B$ ~//--- Define a variable that indicates that we have a deal...$ D5 M# A9 I2 ~: T( v
bool tem_tick = false;& \, F$ @! [: |$ v
//--- An auxiliary variable for opening a position
! g! k4 x. H6 h" D# U) k0 a+ D: u#include<Trade/Trade.mqh>% [1 K: M3 P# a4 @
#include<Trade/SymbolInfo.mqh># d1 D% u1 ^  W6 Z3 o, e& Y
CTrade negocios;
. i( x4 N' T% z0 PCSymbolInfo info;# R8 r$ j0 W' j! C* g# v
//--- Define in OnInit() the use of the timer every second& L& M# i6 T% @" @. D( v( D
//--- and start CTrade
- R! h( T/ ^$ |% w9 n& X8 jint OnInit()' v) f: _, }* Y9 b: U9 k
{
, c' L# U# a2 E) J' y7 S) }7 B//--- Set the fill type to keep a pending order
2 e, z7 {( L' J2 o: b( b5 F//--- until it is fully filled. n. r2 X# b( e9 C) S
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
. C, l0 q8 w+ ?- d1 J$ Y6 @//--- Leave the fixed deviation at it is not used on B3 exchange
) ]1 }* U. E' rnegocios.SetDeviationInPoints(5);
, J3 k  r# Z/ `( t, _. p* K//--- Define the symbol in CSymbolInfo...
7 H3 N+ i/ Y7 q$ a" Y1 |info.Name(_Symbol);
, c2 L& ~( V! b! P7 g//--- Set the timer...
* `+ C! e2 M* G7 Z6 M6 F. jEventSetTimer(1);
6 H/ D" }1 o& f2 B3 [. r//--- Set the base of the random number to have equal tests...8 ~7 s, H' P) u6 V. b
MathSrand(0xDEAD);
' Y' {  ^( Y3 S; p* [return(INIT_SUCCEEDED);
7 p$ v# V8 T: s  |5 v! u}
2 r3 D" B6 v3 k5 x//--- Since we set a timer, we need to destroy it in OnDeInit().5 W5 U+ L' E/ F+ x
void OnDeinit(const int reason)2 y- C. v& _+ v, d5 u
{
5 r; v( _& w( {! P, [EventKillTimer();7 y7 T' K7 I8 A) T, q/ \
}( f! |) ]7 l3 i6 ]) Q7 r
//--- The OnTick function only informs us that we have a new deal/ q9 B4 Q+ p; q1 R* m
void OnTick()5 |% M- N% S. a& c5 }" X. u, M7 _' d3 ~
{! G4 j3 X- }- B6 _" `0 c1 W  U1 s' v
tem_tick = true;" w4 h1 h! o; a. O1 W* R
}6 C2 T# Z. N8 h) Y6 c4 h$ v
//+------------------------------------------------------------------+
/ i) _& G& j0 |- }2 K//| Expert Advisor main function                                     |" j2 @$ ?+ j) g( l
//+------------------------------------------------------------------+( G3 }* I2 J) T9 L2 G3 A- A$ t
void OnTimer()# H% q" T2 L+ A. [' b6 g
{+ Q! p4 T" n( _3 a1 q9 E& @& y
MqlRates cotacao[];
0 J& g; W  w! e4 Yreturn ;- n( O% `4 a: B- @+ `- w" R; f
if (negocios_autorizados == false) // are we outside the trading window?
% X/ I# g; v4 t& y- Z# G: E$ Yreturn ;
0 W8 C: m  G$ M, n1 H& C//--- We are in the trading window, try to open a new position!
! \+ X8 v# i$ Uint sorteio = MathRand();
" G" L1 @/ U2 t0 y/ O% g//--- Entry rule 1.1
2 t$ e- Z7 x& N& j2 Mif(sorteio == 0 || sorteio == 32767)
# a# a( x' ^! _1 D) _' oreturn ;) }7 s5 Z9 M, a, b5 h7 u6 P, r
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
* S7 X( v4 q6 W3 x* N8 W' g{
! v, h& D- N# J1 Dnegocios.Buy(info.LotsMin(), _Symbol);3 f6 j% k/ E9 b, n+ C& [
}
1 ~3 j) v* p. x3 H% S# F! j% Selse // Draw rule 1.3 -- odd number - Sell
% R# W: u& F  @{
4 I  U0 B% g3 Y# T) ]! c. Nnegocios.Sell(info.LotsMin(), _Symbol);
. |% S7 U8 E) m2 G. @  n}0 ^9 W; R2 b8 H. E' ]6 g4 i
}
# f/ e8 r% u6 R1 x4 J, j//--- Check if we have a new candlestick...0 D, @1 g  V+ k/ z+ A9 v3 m
bool tem_vela_nova(const MqlRates &rate)
% m/ f' _3 H# n$ O/ Z+ ~{1 O* s2 J6 @5 {4 A$ v$ V
{# v, w# @5 n, `2 [6 I4 i
ret = true;
7 P8 X! y# h8 F1 e  F) I# Dclose_positions = false;0 K' H( Z/ B3 d; q+ s* x$ Z5 r
}/ m5 I( [2 S, X- o* w# j8 s
else2 j5 v  Q8 m$ ~
{4 }6 x- ~  g7 C
if(mdt.hour == 16)6 n4 D  J, }$ r1 X; j6 V
close_positions = (mdt.min >= 30);
" ~4 y, ?" X# f2 R# [+ O; M& k4 A% J}9 n$ ~* ^2 J5 H# a; e4 |
}( C4 Q2 T# w% [
return ret;+ v$ x9 B! I; `  m8 r! H# Z
}
% X% K7 I9 h. l+ J5 b7 Y. n5 `//---: Q) |* k( V, |# N! c& c# W
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])' P  u! @8 T/ I
{
8 X" ?5 g! X7 F$ D3 v, v2 mif(PositionsTotal()) // Is there a position?
0 F& {) Y+ @- s* B. O& L5 G  c, A{8 a( e: X( \& R5 ?1 D" `
double offset[1] = { 0 };
. g" Z; u  y; O. K. R$ r" {, A: H& aif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?( Y9 R8 W; F( h/ \0 A2 G
&& PositionSelect(_Symbol))  // Select the existing position!/ V5 M0 U" p; e8 M8 s
{) c: R9 t; [" s6 m' B) A3 e) M
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);7 ?; f0 s. v- f/ I, C& d4 S, `
double SL = PositionGetDouble(POSITION_SL);
1 \/ ?4 F, ~- o) N; V4 {% Vdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
9 H; F+ r7 v( D# rif(tipo == POSITION_TYPE_BUY)0 B8 \; P8 d4 y! G% C4 a
{
& o" m  z: h, hif (cotacoes[1].high > cotacoes[0].high)- D0 r  i0 ?5 l8 \$ @# B" ~! o
{
1 q, x4 d5 M# p3 `3 b. x: Ldouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];( |& x5 y8 J% [& h7 o: Y0 g% [
info.NormalizePrice(sl);: N$ A; H4 s+ q" u# l2 ]
if (sl > SL)- p/ }+ E/ I. W1 s6 @7 D
{
9 S8 C, D2 s3 T) d: Bnegocios.PositionModify(_Symbol, sl, TP);
6 f( f- g# ^8 p: I  E0 x% S& [( O( Q}( u: H. @2 j7 R  T$ x( Z
}4 K$ W$ X0 B9 Y% ~
}
# s4 m5 v! V1 }- Z4 F6 {else // tipo == POSITION_TYPE_SELL
- C, |7 l! l; s( k+ c: b/ |{
/ Y  c0 q. K+ a* S+ H+ jif (cotacoes[1].low < cotacoes[0].low)
! s2 X1 b( d/ @' u; O  y. z5 q{8 x, e& H1 ?7 ], i
return true;
1 S6 L$ T: U" x. U4 O}1 s, k- o& H0 B: ]/ i
// there was no position( J% q6 C8 d: h6 d9 G/ |  u0 `
return false;8 C% t/ {! v) G3 s, }
}
$ ?3 H" B% O, S7 ^1 P0 k我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
4 j& Q) ?: Q, u' K  f) @% Q3 C到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-23 05:17 , Processed in 0.502797 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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