私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA0 X$ C' X9 p$ T9 |
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。2 ~, Z( x4 ^: ]
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
  ~+ m6 n, {8 i1 w% P以下是制定这些规则的代码。; E/ n  I5 R! J( I6 @$ `6 L2 k
//--- Indicator ATR(1) with EMA(8) used for the stop level...' @2 }* w* M$ q# S0 V8 I* f
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
7 r  f  t0 y* H) d$ C2 ^& Fint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
3 ?) K- B2 r! q+ _1 n8 Y//--- Define a variable that indicates that we have a deal...
- a$ ?0 M8 j$ B0 hbool tem_tick = false;( d+ D/ s) y- u
//--- An auxiliary variable for opening a position, E4 c7 w% z( I+ L
#include<Trade/Trade.mqh>/ ]2 X* F$ F: a, ^7 S9 L" [
#include<Trade/SymbolInfo.mqh>
9 V% j: @4 u9 G! x: |1 O/ W+ nCTrade negocios;
2 _' z0 o" w% k' o- yCSymbolInfo info;
( j9 z. Q9 O4 R# \//--- Define in OnInit() the use of the timer every second) e5 u% ~# E# r3 s/ T/ C
//--- and start CTrade& F  t3 c! R% `" [! \
int OnInit()
  m- @5 Q; _% k1 b1 }& `! \1 Z+ U{
% Z0 B: q% [5 z: e7 k% {$ Q//--- Set the fill type to keep a pending order
+ N! g  J( I# X8 a//--- until it is fully filled
# J2 [7 Z5 f, ]4 r. n* u' Cnegocios.SetTypeFilling(ORDER_FILLING_RETURN);3 U. J1 m4 [8 J4 x: O
//--- Leave the fixed deviation at it is not used on B3 exchange
+ C* ~0 j2 y0 m2 w- Onegocios.SetDeviationInPoints(5);9 B: R6 Z7 q4 U" d# c, r. K
//--- Define the symbol in CSymbolInfo...
7 i$ |" u; m3 ?2 kinfo.Name(_Symbol);
3 c. l$ B$ O* K) R//--- Set the timer...$ r/ {* R( u5 }/ b4 ~" o+ R
EventSetTimer(1);
) T, ?! e) X- r5 t! [4 @4 E# }//--- Set the base of the random number to have equal tests...
$ I4 h  O- v$ K5 c. U4 XMathSrand(0xDEAD);
  G) C% r* V- Y' dreturn(INIT_SUCCEEDED);
+ ^/ V, r* @8 c* t}
6 d1 q& j6 e. K% R& E: w( O( b//--- Since we set a timer, we need to destroy it in OnDeInit().
" a5 v. W: V( U8 ovoid OnDeinit(const int reason)1 ]1 u8 C3 M) k' b' w, [- J
{2 r" q% b* x1 e1 b( ]
EventKillTimer();, B' i! \" Q9 C7 a
}
6 H9 h8 S8 z2 S4 j7 N1 ?8 _//--- The OnTick function only informs us that we have a new deal( a4 E& k) G& a0 e# K( U
void OnTick()
. [4 s: N% z! a/ r/ `{
$ I, X! s) Y+ M2 w* j0 V; `tem_tick = true;% u" q9 _* k5 [* c6 j
}
5 ]( K5 i0 O- v. v6 W1 S//+------------------------------------------------------------------+4 r! u# `( P/ G& U! ^$ c: ^
//| Expert Advisor main function                                     |
8 |7 J4 o) n) B; I6 b+ S3 r- R//+------------------------------------------------------------------+
1 j; O9 F: w7 f: {* B1 w" \8 `8 {! Hvoid OnTimer()$ P7 B" T* A/ A" p) l( e0 ^
{
8 W0 O% h0 k# p2 NMqlRates cotacao[];/ k4 \7 ~7 y: n9 l( R/ @
return ;' K* G9 V4 V4 K7 v6 w
if (negocios_autorizados == false) // are we outside the trading window?/ [* f) z! x5 M+ {& ~4 G! ^, s
return ;
7 J2 f7 L, d+ z8 b3 G! B% j: N# G//--- We are in the trading window, try to open a new position!
' y9 l" c3 a5 g; o' w4 j: O; jint sorteio = MathRand();
, p( L' x, V2 f6 d, t1 ^//--- Entry rule 1.1
8 z; I& V# ]% [+ f3 u  mif(sorteio == 0 || sorteio == 32767)
3 m$ I& }& }8 A. |return ;
% m/ P% K) Z9 \if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
. q, r9 g4 S# G6 ]{6 h$ L- \9 V3 f% k- ?
negocios.Buy(info.LotsMin(), _Symbol);
% j$ T; ]$ m  e5 ^. \1 w3 w}) c" t8 z( c+ I- M
else // Draw rule 1.3 -- odd number - Sell7 [2 n1 {: o* x( Y8 Z/ |
{  ]' ?2 v4 u2 S( K" z
negocios.Sell(info.LotsMin(), _Symbol);8 ^7 |$ f/ |* B! r4 m
}
; @4 s" R6 s/ w# w, o) v}
4 K# O6 A# b2 `# z- n7 x9 W//--- Check if we have a new candlestick...' k9 Y, ~: Q6 ~# K# E* m2 B! Z
bool tem_vela_nova(const MqlRates &rate)( r( z4 H6 M" u- e; M) g7 h
{& g  a3 Y; k" D  F
{/ \/ a8 H( \4 L- {3 p8 @0 {- O
ret = true;
- r6 m- g7 h$ F/ uclose_positions = false;
/ {3 \7 B- B% D: @: j2 U}6 ~6 O! O* e7 n3 Z' N0 g% _
else
. ^& Q5 k. C' f6 C( ?! @$ H{
* f$ O3 T7 [5 M- s" w; ^( G; Dif(mdt.hour == 16)2 B- |) f' P0 p; Q" U
close_positions = (mdt.min >= 30);9 J0 `% O6 z. u2 E3 H
}: N& N/ c3 q  x7 \( ~. b
}
( c$ y% O% O0 areturn ret;
4 u4 X0 B) z) _' Z1 t}, F  l. a0 T) A& n  p
//---
4 a& `1 i  y! B6 i7 i; s. U4 R( |bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
2 j0 k: Y3 e, A$ E8 E2 t$ k0 j6 ^/ `6 b{3 Q+ e9 |9 g8 b  @- `; e& s
if(PositionsTotal()) // Is there a position?3 B* A6 D9 m8 D+ w
{2 D0 X) W' m; S( p8 P+ c
double offset[1] = { 0 };
# I2 {+ f! z/ K/ m$ Mif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
" \9 r' X5 |" K: B7 p2 p; j&& PositionSelect(_Symbol))  // Select the existing position!
! {- T- e3 [. Q% u- z{
6 F' _. C' ^! t) o: fENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
' i8 {* r6 P9 \0 N  Z6 Ddouble SL = PositionGetDouble(POSITION_SL);
- q' g3 v0 ]* F8 `/ e: Ndouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));2 ^1 Y0 ~  Z. _( O# h" b4 O6 e3 x
if(tipo == POSITION_TYPE_BUY); w! T. G  ^5 q+ V( ]
{
# y% T0 ~$ g: @if (cotacoes[1].high > cotacoes[0].high)
; P7 \% A, L8 L6 \{
/ f3 L! r4 ?% P1 t5 K8 ~( [- Ndouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];% m) ?0 Q/ l; _  }
info.NormalizePrice(sl);
/ y! V8 Y- R& ^, D  V& M" Aif (sl > SL)& c6 H4 X8 L4 i! c/ X& B* c- M5 N
{& |4 |% O$ E7 e' @1 d
negocios.PositionModify(_Symbol, sl, TP);8 B8 }, h) N$ ]
}
9 d+ D$ i- ?- ], s4 y}! |+ J3 [9 J: u+ S" I0 _
}5 S* v1 J% F8 e. L, b. s
else // tipo == POSITION_TYPE_SELL5 y" \% E0 [% G# S! |
{1 O8 |! r4 x( P: u3 Z
if (cotacoes[1].low < cotacoes[0].low)
7 c7 J5 @' L7 m+ v+ C{
5 l/ q, P/ t/ w8 I/ p; ]" xreturn true;
4 [) A+ `/ x/ I/ C  r5 }: W  V' y}
* r& n1 l- J3 S// there was no position: M4 P& G+ p& B4 d% Z  W- m0 v
return false;" n( E- {" F/ v. {: W* ?5 K- e
}8 `# ]! j6 a) y. q3 y: @
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
! k4 B1 h5 `: R' B4 M6 @" ^到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-8 22:13 , Processed in 0.385371 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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