私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA/ R" C1 ^3 u" l) a( O  D
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。% l* A- T2 G" ?  q/ U- f8 ~& P
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。; @8 \  P7 {# K0 K$ B
以下是制定这些规则的代码。
. N; ^  F0 D! W" h7 w//--- Indicator ATR(1) with EMA(8) used for the stop level...
2 P. G  c; [/ q/ @int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
7 d' k6 D7 w# R6 Tint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
% M( P! f. S/ n  {2 x- w//--- Define a variable that indicates that we have a deal...
, r: B( e& W' p2 K: cbool tem_tick = false;
% L7 [' E3 l" J, `//--- An auxiliary variable for opening a position
6 @6 i" S& q) W8 u& ^- q#include<Trade/Trade.mqh>
; E: @4 ?: K7 b3 Y; i2 E% d: f#include<Trade/SymbolInfo.mqh>
0 a% S9 N- C& _$ B6 v5 ?+ c/ {  kCTrade negocios;0 e" {( S! u' `: y& \9 `( ?
CSymbolInfo info;
; P' G% }) m! ]//--- Define in OnInit() the use of the timer every second
* S+ c; a' q8 U3 F+ E2 A//--- and start CTrade: E, m- N8 x; q; c/ k
int OnInit()+ }1 v5 n, K$ @% Z! w- V
{( k  w' [# g# v+ _6 F+ k: O+ N" T
//--- Set the fill type to keep a pending order! L3 H/ w+ ^4 \
//--- until it is fully filled
4 [* F6 @; ]; k, {7 f* i3 q# A% enegocios.SetTypeFilling(ORDER_FILLING_RETURN);0 y6 c# [+ ]1 e% O5 D! n, I# o
//--- Leave the fixed deviation at it is not used on B3 exchange
! K0 u' K! ^, t) r0 w9 L8 d% xnegocios.SetDeviationInPoints(5);
2 {$ }; I5 P! j' _/ k0 @' S//--- Define the symbol in CSymbolInfo...
! O. M) C' C: B6 Einfo.Name(_Symbol);$ j: ]* Q9 O3 E* h2 i
//--- Set the timer...
) N3 Q  F" Z6 t$ `0 C8 ^0 TEventSetTimer(1);
* i6 U, d0 l6 h//--- Set the base of the random number to have equal tests...- t" W- J7 U0 C; o5 S
MathSrand(0xDEAD);
( {0 }+ c. r! K# ereturn(INIT_SUCCEEDED);0 Y( H6 q0 M) f4 X. ~! G! E  n
}# ^; P# n! X3 \& u: o: f
//--- Since we set a timer, we need to destroy it in OnDeInit().; ~4 w$ L& T0 g* ?6 n: i
void OnDeinit(const int reason)
3 J! A5 U& I6 h{/ l  P7 w8 Q, ?" S& M
EventKillTimer();
- @- G* P8 P8 W5 i. R& y* `- R}5 t1 [0 `5 L6 l2 A( K& x# P
//--- The OnTick function only informs us that we have a new deal0 s# D8 B$ p% A, u% ^
void OnTick(): S; `3 Q. W! x6 Y  G
{
1 Y& h* p- k- g: etem_tick = true;
/ }) j. K; [7 }2 x0 j}
, q6 ^3 u2 p: W- x- q8 s8 Y  y//+------------------------------------------------------------------+$ @9 m/ Q* @% q  t! g0 h  [! D
//| Expert Advisor main function                                     |
( H1 A$ n# V" D5 G9 @' i2 z$ V//+------------------------------------------------------------------+& M& v! c; H2 C# p) L* N/ ?
void OnTimer()
* K  V$ r2 Y- O) x{1 S1 [- z3 u. [9 W% T; V* S
MqlRates cotacao[];# ^4 w2 a2 j# k
return ;
$ F1 {$ I  P( Y' L  {3 u! t0 K% Pif (negocios_autorizados == false) // are we outside the trading window?( H0 L, a) G3 ]) K9 R
return ;& {( x: F2 T# D8 ~7 m! C8 M" K
//--- We are in the trading window, try to open a new position!
- C/ |, y* W5 l3 p: Eint sorteio = MathRand();
0 J( O& U# H, a5 M3 F* a//--- Entry rule 1.16 q: u1 r/ Q7 [% I: |
if(sorteio == 0 || sorteio == 32767)7 s6 p) U+ f  b. s" K
return ;8 J  @$ v* f7 {* ~( T; D/ R4 V
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
! V# g9 z6 |  ?) l% H{3 x! S5 r( O) f
negocios.Buy(info.LotsMin(), _Symbol);
  {/ B. T& b8 D! D2 e}  S# ?# @. m) }
else // Draw rule 1.3 -- odd number - Sell
. e. k6 [; o6 H8 q5 T2 r{
/ k! K' x+ j1 O- J' c' H% wnegocios.Sell(info.LotsMin(), _Symbol);9 o/ g- O8 i5 D( f
}
$ o. F9 K- ]7 \& D}+ l5 H9 i) J8 j/ e) J
//--- Check if we have a new candlestick...2 x( z9 i" A! E
bool tem_vela_nova(const MqlRates &rate)! u+ O5 x% h$ F' _
{
! Z- V4 X. f3 @$ r/ Q2 V2 E% d{4 Z, G; ?3 r/ y' v5 a  h
ret = true;
' ?4 D5 S9 T  A6 j+ [; L5 U& x, uclose_positions = false;' I: H5 J8 F/ F7 X' h, y$ |
}( F% h) T3 d' X+ X* Z- u1 a/ D
else. G/ `' d  p- j8 m) G) P
{3 T1 E  V0 t$ |" ~: g
if(mdt.hour == 16)) ?9 T% w7 S: ~; @% N
close_positions = (mdt.min >= 30);
1 z) y5 Y, ^" ~3 X/ w+ `/ A}2 j" a, ?2 {4 R6 }4 V
}* L7 ]4 }- o8 C
return ret;
( i3 h7 @- W0 y* a7 L! |. W8 h' q+ \}5 ]' I) l- t0 t* T, N# i+ D. w
//---3 J7 u/ y6 @3 s0 Q( H3 d8 e
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
: w* i6 H' s( Q+ J" F' Y{
; Z+ m2 W1 @' E4 h2 ^if(PositionsTotal()) // Is there a position?
7 d$ k: T# ]8 z0 j6 t{1 K8 T4 S4 Y7 S% _% Q, }+ V
double offset[1] = { 0 };
# G" M( I; e  A# ^if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
( w# f( i  M- p&& PositionSelect(_Symbol))  // Select the existing position!
0 d3 t* m  Z, F: ]4 O: O{
3 F: G& B0 I( P5 [ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
6 C- U! }' P. R/ q% B, Mdouble SL = PositionGetDouble(POSITION_SL);+ P) ^- k0 j* c' u
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));8 u8 Y( w% |/ K. T6 i
if(tipo == POSITION_TYPE_BUY)" n$ |5 ]0 |; @% r1 e
{9 C3 Z8 b4 }$ y: p; j
if (cotacoes[1].high > cotacoes[0].high)! j* G) m; |4 N8 I, X# D
{
7 T4 O$ z& Q' v; l5 s  G) vdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
) S/ \. p/ V2 i( tinfo.NormalizePrice(sl);3 q7 n0 H0 m, b  s6 C8 k
if (sl > SL)
/ z4 d* G7 p& q2 s8 c& Q{" i+ b. B3 T7 ~! `6 z- Z4 ~
negocios.PositionModify(_Symbol, sl, TP);5 R! a8 x( a% ^% f1 [
}
8 j3 C$ C5 O/ B8 S, F6 V}
" ~5 k- Y8 O/ B4 [5 {}3 V- U+ w: h* c# k
else // tipo == POSITION_TYPE_SELL* L  z2 ]6 h9 u2 i! v; a9 f
{
, \6 ]2 E4 X1 Gif (cotacoes[1].low < cotacoes[0].low)
: [8 P7 j) o9 o. ^{/ r# I' u8 J1 N/ p3 f: t
return true;
% B! a2 u' B9 W, q}
' M' \$ p3 e( m// there was no position) Q) [1 `" p  h; d: s. u
return false;; f* t) X. B9 ^( O/ l9 d
}# X- l  ?4 S9 c' Y
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。: M* L, T" J$ R' E' `/ V. B
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-3 20:12 , Processed in 2.525385 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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