私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA4 T) W* ~2 p) \
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
7 H/ f6 L  {! O, y5 |. U为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
' F5 t0 K  q. F' q5 \! D以下是制定这些规则的代码。) g- A" q2 i% Y& R- ?4 y
//--- Indicator ATR(1) with EMA(8) used for the stop level...$ B" S( @9 ]6 |& `, [! w9 j3 U
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);( v' V' W2 C: S, L# {0 `
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);5 R, _% S1 g. _/ F+ G% r
//--- Define a variable that indicates that we have a deal...- A8 t' W! G2 Q6 [+ j5 o
bool tem_tick = false;* r+ V1 h7 Z4 w) |6 @' f. r
//--- An auxiliary variable for opening a position
+ ^# ]$ H* a7 F7 v& }#include<Trade/Trade.mqh>7 H+ X  B5 O' z- f
#include<Trade/SymbolInfo.mqh>
3 ^, d+ j: W9 d+ U1 u! U5 JCTrade negocios;
$ i' r) T  K! D6 KCSymbolInfo info;! K% O( y. R* G" q5 ^  T4 I3 @
//--- Define in OnInit() the use of the timer every second
: {: t, k4 `! x$ k% d6 G# b, B//--- and start CTrade
, }" n4 t6 F; [2 t" X3 [5 \) Aint OnInit()
* p8 M' J* n+ U8 ?7 L! \; [* [{, \, v: [+ H) ~, O! Q# N
//--- Set the fill type to keep a pending order
% O# C! ?: w6 ]0 }4 g  K//--- until it is fully filled6 X7 a4 I3 I4 m6 ^
negocios.SetTypeFilling(ORDER_FILLING_RETURN);8 p8 O0 b% d1 ?$ p! q/ _8 X; h5 E
//--- Leave the fixed deviation at it is not used on B3 exchange( T. F- c/ `5 w7 Y; P" g" v
negocios.SetDeviationInPoints(5);
- H1 ~5 e% n2 r; [* d2 S/ K//--- Define the symbol in CSymbolInfo...1 M3 x" u( B( f, {  Y( w, q$ P
info.Name(_Symbol);
; n2 n3 k# L2 V: e//--- Set the timer.../ }/ Z7 d5 X) P7 }4 W
EventSetTimer(1);
6 q' M& U; u8 i0 ]) ^  o//--- Set the base of the random number to have equal tests...
3 ^+ j$ U% j7 s3 yMathSrand(0xDEAD);
  b1 E7 s# I( r6 Z3 B5 Areturn(INIT_SUCCEEDED);) t/ v8 k$ b+ D- `0 ]6 v
}
' `" Y3 |: A9 H7 k//--- Since we set a timer, we need to destroy it in OnDeInit().
. d6 i8 l, e. h7 ?void OnDeinit(const int reason)  m$ }) H2 Q: F" I, b; [& o/ V
{  T) Y2 R0 U" _7 N! Z- j
EventKillTimer();  ^* }' t2 _. t' x+ ~6 N
}
- O5 p1 Q5 G& p1 I# H//--- The OnTick function only informs us that we have a new deal
3 t: |% N6 @3 b& M* m( [' Jvoid OnTick()0 P, M- E' d7 Y* O% e1 H/ n* I
{1 U' d6 w  l  u2 Z  ?
tem_tick = true;+ u) K9 f( D' L
}
/ o  S  f) I- u1 U* ]//+------------------------------------------------------------------+5 o( p6 t. w1 @  T8 P. C
//| Expert Advisor main function                                     |
& E8 h& j8 t" Z* D. \//+------------------------------------------------------------------+
" |/ i/ S8 B' D0 N; ^0 {, ivoid OnTimer()5 `+ ?: I" w' i% |& P, y( F
{
& t0 {' m, ^1 q% x6 e& [+ y9 U5 Y' }MqlRates cotacao[];  @  B, K9 r8 ^. Y
return ;
, x1 G0 n4 N$ {8 N; O5 j: i+ dif (negocios_autorizados == false) // are we outside the trading window?
1 ^8 V) R6 u: U, ~7 Hreturn ;  G) v( G1 H+ M' T6 r$ _
//--- We are in the trading window, try to open a new position!  X% k* j9 }! ?9 t1 \- h2 h& l
int sorteio = MathRand();1 P1 F2 T& X" f
//--- Entry rule 1.1& R( S9 w& V& T5 @' I' _  o' b
if(sorteio == 0 || sorteio == 32767)
0 N( S4 D% X% v6 D# hreturn ;0 t% Q2 O1 @5 {
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
- @) w% ^; \7 H% g5 I2 U2 _{
/ R2 f% G& p; _) ~negocios.Buy(info.LotsMin(), _Symbol);
1 _2 u& ]. }( V& d, L4 `1 F}
" [3 q( Z0 M* x$ w: |  P4 F( m% I& B2 Jelse // Draw rule 1.3 -- odd number - Sell8 T6 S, a4 K9 ]6 H+ v6 l
{% E0 Q" {3 e$ X+ J7 h4 N
negocios.Sell(info.LotsMin(), _Symbol);
0 u+ h, R$ V' |- ^) G3 z  C}( o/ x* d- e: c' F
}: ], O+ C, P1 Z0 R$ ^1 {
//--- Check if we have a new candlestick...: c: r; a+ l4 X$ |
bool tem_vela_nova(const MqlRates &rate)6 {/ G6 H6 ]9 x4 J/ @$ s9 X2 C  L
{& ?1 G* M. ?2 ^# S
{
. n+ }% R, U% B% F/ \ret = true;
& _3 Z0 }2 c! V; |7 `; S! wclose_positions = false;
' F: U$ d5 }# l7 |}
4 a2 R( a: Z, G: n7 x2 k: ?0 P0 |else
+ H- A; _) B  b: g, K; G0 q{& \* C; N" r& j) I% y
if(mdt.hour == 16)
  m* L" X! \3 G% uclose_positions = (mdt.min >= 30);
+ N- W0 |( g7 ^% n0 z. F  K- e# g}
: E9 l! i, p6 S+ W( O8 S. @}
) n9 r  T, M9 G4 d( W7 `9 wreturn ret;$ u6 P& e5 I, j( u
}
- H# `4 G1 ]) k9 P//---
# z6 u6 A% A( pbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])0 X- x8 e8 f( P
{
7 l. L. C/ x4 p: D3 C+ ^- Kif(PositionsTotal()) // Is there a position?
/ Q+ O8 X/ ~( `0 t/ B, ?{8 o/ I' J0 v7 }% k
double offset[1] = { 0 };
1 Y7 f0 S5 `6 u' @if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?- F: y: [9 X4 o& M" ?$ ?9 Q) C$ D
&& PositionSelect(_Symbol))  // Select the existing position!
! }; X( X3 ?+ Z' [( }! E{
' N3 j- T3 [* g! Z+ v! \  WENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
* r6 Z+ p. Q# V5 c: n1 wdouble SL = PositionGetDouble(POSITION_SL);+ Y- X) d6 h9 d- Z& g$ X2 J
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));! e+ V, w! n$ f
if(tipo == POSITION_TYPE_BUY)) v" E; [2 q. Z6 {4 Z* c
{) C. _/ @3 U& Z2 g, [
if (cotacoes[1].high > cotacoes[0].high)
/ T1 l5 K  e" W8 F{
0 }6 u3 i2 S; ?! ~( o" Cdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];0 n$ U  V2 G' M1 E2 b2 v9 r  p
info.NormalizePrice(sl);4 ]$ k* I: Y' _, s1 M
if (sl > SL)( f! ~' Y) d" n6 W, ^
{  S: E/ ~+ S$ I9 M7 k
negocios.PositionModify(_Symbol, sl, TP);
$ W  ~5 A6 q  g2 @0 d( G. d0 w}5 o6 r9 ?6 Y7 W7 F; z" f- l  r
}' X2 O, f! K; g2 J  k# Y
}
# A6 g9 m# I$ g# a: E% T2 Felse // tipo == POSITION_TYPE_SELL% e5 ~$ o( f1 `
{; l4 I. o+ t5 }! g/ g
if (cotacoes[1].low < cotacoes[0].low)2 D+ F, ]: P& S" p8 u
{3 P- E  j& A, \
return true;7 C3 U9 L: p% Q7 [% {2 F' M
}
+ z2 r" i! H: O2 {/ V" i// there was no position
* o. _2 B6 w% _7 ?$ Y* Ereturn false;' w4 r! h" v; `  `7 k5 W0 w  z
}& t9 O! `# T3 O9 E  F
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
5 y/ Y8 `! Q4 L) j( _9 l0 m到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-17 06:37 , Processed in 2.256491 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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