私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
, N/ B- Z! W3 g' W, _, w9 ~, d. f. P在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
3 w2 V- s; h% k  g为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
) b& |0 \: H3 K% k1 ]以下是制定这些规则的代码。5 l5 e. d* s% V/ R+ F
//--- Indicator ATR(1) with EMA(8) used for the stop level...2 n+ {+ |& K+ o; Q  Y6 n) ]
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
. q: R, }. \! K+ K4 J" `int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);) O' V" i% A2 X( ]" E- Q
//--- Define a variable that indicates that we have a deal...
; h& {' c3 U6 dbool tem_tick = false;! p* j; P$ h" e
//--- An auxiliary variable for opening a position
7 d* e' m% c, Y4 r% u#include<Trade/Trade.mqh>
- n  o- g( a5 d# }#include<Trade/SymbolInfo.mqh>
. @2 D6 O: y* W6 h8 TCTrade negocios;
. N, b7 r8 x+ E( E$ ~$ jCSymbolInfo info;3 |7 @: @! `/ Q+ X; h0 o
//--- Define in OnInit() the use of the timer every second
3 Q; w6 t- j+ o//--- and start CTrade8 d8 `* A& s% w( q: B0 Z6 u
int OnInit()8 y* s( C+ ^- \8 `+ d
{0 Y- C6 y+ O; t& f7 y
//--- Set the fill type to keep a pending order
( ~8 |& W5 r5 f6 D8 R//--- until it is fully filled4 _5 U, M! F6 z# J7 E, Z/ K2 P
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
! }3 R6 Y8 |0 K//--- Leave the fixed deviation at it is not used on B3 exchange- i+ G' p5 p! Y5 ~. M9 g+ O
negocios.SetDeviationInPoints(5);
$ D3 s; H# {, }% A; I# N//--- Define the symbol in CSymbolInfo.... L1 a" q# B0 V7 j2 B6 A7 s" M
info.Name(_Symbol);
) d9 `) q8 T' ?+ |6 O//--- Set the timer...& I; y9 N/ {$ a6 |  G- l. n/ W+ B
EventSetTimer(1);  x: c) ?! @  ^& ~& W
//--- Set the base of the random number to have equal tests...
/ k% |, M$ Y! H1 A: z+ WMathSrand(0xDEAD);5 U, `- ?1 o& U! Y( ~- v. {) |
return(INIT_SUCCEEDED);
! d! H9 ^7 C5 S! [}4 D* q9 Q/ s, j6 J) f  d5 z9 |
//--- Since we set a timer, we need to destroy it in OnDeInit().3 F3 a/ w' c1 p% P% G. R# _4 d
void OnDeinit(const int reason)9 E" T5 i" B5 X6 t* `: M& L" E) q
{; o7 m8 ^% ?5 C
EventKillTimer();
9 m+ g% P& ^1 l9 L1 g. o- I4 X}
' d6 K' Z9 {1 H5 p3 ]/ ~" u5 x+ n//--- The OnTick function only informs us that we have a new deal' e6 q4 R: J% q
void OnTick()
# D4 |+ ^5 U5 g* w8 H/ Y{
, j, t" i4 t9 C7 j/ Y9 I6 I9 |% J' Vtem_tick = true;
( s; g1 u5 j# ?, |# N: u}+ Q: ]* p3 e6 F+ E  h
//+------------------------------------------------------------------+5 U" c# M! C$ g1 l8 f
//| Expert Advisor main function                                     |! p+ C+ d9 g# v1 O9 O* c5 K: _
//+------------------------------------------------------------------+
! U# G# X0 i* N6 Ovoid OnTimer()8 B! R; |3 F% }4 ^: T0 X  j
{
+ n' E5 A" d( t$ k7 l3 Q( C& o' cMqlRates cotacao[];+ ^0 F: v8 b& f4 X# Q7 c" T4 R3 d
return ;
- m3 }8 E  [  A; W2 X- O* oif (negocios_autorizados == false) // are we outside the trading window?
  b* B$ e- X* C7 g; ?; ~  ]: f3 G' t3 H# nreturn ;
3 j, K" T4 R1 P" \6 v: k//--- We are in the trading window, try to open a new position!
2 o; Y' f0 Z# e0 J) r; G3 L1 _' ]int sorteio = MathRand();
/ q6 G! i2 d9 G& A  Z9 r; ]+ M//--- Entry rule 1.1
9 Z9 Z9 u0 |2 O: n1 v. n/ |" Sif(sorteio == 0 || sorteio == 32767)
! {! @% p: ]( Greturn ;: Q' {. ?! s8 L2 d) j9 v$ g$ E
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy$ Q# Z$ |4 \* G  y. W4 v
{
8 Z7 r( x6 k9 E6 {1 rnegocios.Buy(info.LotsMin(), _Symbol);
8 V4 i' @! d5 h. _1 b, {}+ s! A8 @+ y  w
else // Draw rule 1.3 -- odd number - Sell
7 a) U1 T4 z8 [1 i% y{
- a; F, h2 a8 s/ r6 |negocios.Sell(info.LotsMin(), _Symbol);6 P; G0 ?% }; \: v1 Z- s) i) n
}
' p2 i' |3 I# ~}
' R- @3 Z0 k; p% ?  F6 G//--- Check if we have a new candlestick...
% F' i0 y) n+ G- H% r) q  \- Ebool tem_vela_nova(const MqlRates &rate)3 b6 z# h* E7 l" M$ t. V
{
! A$ F3 i9 D" a{
7 U. t+ w" _6 m+ P( j$ |* X" S& gret = true;
+ `  D$ ?% ]+ F5 k. M2 mclose_positions = false;
6 `& W4 P+ Y0 N6 L3 Y}6 Q7 P' E7 ?* ~1 a  t% g. f; h
else5 M# t/ f* X  d# u
{  D! P4 P& r; l5 C9 s
if(mdt.hour == 16)
/ J1 ^1 q+ w# g  `1 N/ sclose_positions = (mdt.min >= 30);
/ l$ v# A/ Z- _% F* i8 K}
0 y3 f+ N3 i2 z( k3 w6 L}9 T) r7 I# ?, h6 i$ @
return ret;
6 c9 X2 _4 ^8 O3 p- m% B}
: E7 ~- p& d$ O6 k9 Q3 C/ H4 T//---
6 W7 G: w8 X3 ?bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])' q+ ~& R$ M2 Q
{0 ~8 e, u3 T7 B) F1 q! W# ~% Q. [
if(PositionsTotal()) // Is there a position?
! c. C- @/ G! y0 L: i{! ]. B+ Y' C; T6 H# B8 a2 ~- L: F
double offset[1] = { 0 };
( c, D8 h7 e5 d8 b5 k2 J9 Mif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
# N2 A1 i$ s( F4 k( ?) s1 Z&& PositionSelect(_Symbol))  // Select the existing position!
) e/ F7 O9 |/ _6 B5 [. ~& [5 p{" _7 e/ C9 q8 Y( Z- N5 q3 U
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
2 F# R' @- g+ F# Ddouble SL = PositionGetDouble(POSITION_SL);  w: g5 V; _% }* f9 F0 B6 k2 H
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));* R6 P: I5 v7 W' o5 e
if(tipo == POSITION_TYPE_BUY)
/ G6 `- V0 {. ]; F+ v: d3 \* Y{; ]7 C. B4 M, g7 N& Y' A
if (cotacoes[1].high > cotacoes[0].high)
( n& i; q+ V# k2 A{" u7 |! o  h$ O2 W6 w6 U. S
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];; P7 D% K5 m7 E" c( ?( I! X2 C+ [
info.NormalizePrice(sl);
* p* p8 g5 N+ A$ r+ E+ cif (sl > SL)* u; ^1 O8 j* T5 O. ?
{6 j5 b& x3 ~" ]( _! ?5 Y- t& I( \
negocios.PositionModify(_Symbol, sl, TP);1 D/ `( T. W  [1 I
}4 J( p) v' Q" {# Q4 W0 p. Z! p
}+ C8 R) J; o7 W+ ^+ U( d- `
}
! f0 I- E. N! A8 J  ~else // tipo == POSITION_TYPE_SELL
6 I4 q  d  G( h. [{' G, ~7 z; M! L" {5 n0 S
if (cotacoes[1].low < cotacoes[0].low)2 v* s( a+ {, J/ f, x
{4 O" a  p! {7 B. V+ h* Y; W
return true;
+ ?2 Q0 F* M* X+ l}; ]7 {- M1 W6 F
// there was no position4 }- _5 N0 q2 o: a' B
return false;
/ `$ e$ M9 {6 C}
4 B' P# g7 A. O  {; O& N0 j3 r我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
  M4 n! A# W$ O0 a& P- u; `到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-9 22:49 , Processed in 3.683056 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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