私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA- [* H) i' D0 N2 ~, E7 R
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
( x9 o  }( n% C3 Y7 B为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
0 P  _' A- i7 @' W6 p# H" n以下是制定这些规则的代码。0 L  u( {' q* d$ C. ?" k% ^
//--- Indicator ATR(1) with EMA(8) used for the stop level..., h$ \9 d. `9 d) u- S. T
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
; g9 a6 d" x8 G* O7 D0 ?1 U! w. _5 ^int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
( Z0 A  y. r7 P4 t9 G//--- Define a variable that indicates that we have a deal...
1 q5 m' a/ p) C' lbool tem_tick = false;
  @# C5 s  c4 d3 P& }1 l//--- An auxiliary variable for opening a position* z/ I' w9 |8 h1 U! b! G  \: W9 t
#include<Trade/Trade.mqh>, B( f, h+ N; G; T9 t" r. E, R( p: p
#include<Trade/SymbolInfo.mqh>( B! G8 t  F/ ?/ }8 u7 T$ ^! \
CTrade negocios;/ P& O7 d, W& N! y3 t; H& Q. L3 x
CSymbolInfo info;
% S) X) u( i9 m) \//--- Define in OnInit() the use of the timer every second
0 e2 y- Q) f9 T5 O5 D% D4 ?//--- and start CTrade# d/ `/ ^. B% G3 q2 R; e( k
int OnInit()8 ]! ~# \" O. d# g7 |3 H0 d
{9 G4 Z8 ]. Q9 H) W' L. r
//--- Set the fill type to keep a pending order6 \. i# E8 m3 I; s" e/ i# a
//--- until it is fully filled7 [1 j$ E; X( O* l
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
" R+ v, K8 b: J5 ]: z# |& ]//--- Leave the fixed deviation at it is not used on B3 exchange' h+ {6 ?" j8 d( E  j3 |: w
negocios.SetDeviationInPoints(5);( M. z) S+ ^% \, [. _( n
//--- Define the symbol in CSymbolInfo...* g" w0 p& i2 K, I; P3 _" q
info.Name(_Symbol);
+ ^) H  \* J6 d7 Y0 v' j* Q( Y1 z//--- Set the timer...
' B4 F+ ~3 F8 v" IEventSetTimer(1);: S* c! ~3 C0 o7 U; A
//--- Set the base of the random number to have equal tests...
# [7 C  \4 X& q* o3 s* J* j  HMathSrand(0xDEAD);# h! O* A2 P3 G% [7 F, _# b0 B
return(INIT_SUCCEEDED);* W7 A1 o8 W0 n8 x) x& ?
}% M$ C7 H, F4 k: B) |" Q7 e1 f% d0 G) ^
//--- Since we set a timer, we need to destroy it in OnDeInit().
1 P6 c( J! W' a. G: \void OnDeinit(const int reason)
/ P0 ?* q0 m2 L{* `* F. j4 j) @: E: W$ q
EventKillTimer();
4 ^' e& K: k9 C8 C9 |}/ i1 G4 I9 Y5 w& L
//--- The OnTick function only informs us that we have a new deal) t% i1 u9 ?2 R) o; z- M1 Q3 I# Z
void OnTick()
; O3 F* Y: S8 c{
, V( [5 y0 ]4 {tem_tick = true;
4 {7 j) `% G& X* T! I  T" d}
* l/ M4 o9 N9 Q! y" x6 ^3 Q- p5 e//+------------------------------------------------------------------+
4 x/ Z# P+ C5 m* a0 x//| Expert Advisor main function                                     |$ e9 p- o  J8 G7 _8 d7 G( y& p
//+------------------------------------------------------------------+
: o& x$ W' Q5 r$ S& n4 X9 W( Tvoid OnTimer()+ C/ W4 V5 ^# l, @8 w. z
{
, C' C: p+ v4 BMqlRates cotacao[];
3 G! T; l4 N) P0 |- Breturn ;
2 I2 h6 g1 \9 D# x6 T/ Bif (negocios_autorizados == false) // are we outside the trading window?. L6 Z3 v9 X) l4 H8 Y6 T
return ;6 }/ x* M+ A2 G$ k
//--- We are in the trading window, try to open a new position!
' E& W* \3 l+ _7 G1 xint sorteio = MathRand();
9 }# O8 j7 L1 X: C1 r: s//--- Entry rule 1.12 K+ D" k8 A0 F2 x4 m; g
if(sorteio == 0 || sorteio == 32767)) h8 m) I+ e( _" a* E/ c3 _
return ;
( R8 J$ g4 U1 v1 z- J/ ?2 Eif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
; m6 W7 ~: a8 ~5 B9 ?{- Z0 E' P9 p. Z5 s0 m4 B9 P
negocios.Buy(info.LotsMin(), _Symbol);
7 J: ^& n% V+ X# ?( c* h2 p: Z% E}2 P+ H* b" r8 o1 V2 z
else // Draw rule 1.3 -- odd number - Sell8 g' G$ e* E7 m& u: v" ~
{2 f' |$ |% u8 q
negocios.Sell(info.LotsMin(), _Symbol);3 ~' r  C1 d9 f3 x; y# Q+ M
}
0 z4 ~7 s7 u2 l5 B}
. A- O2 V" Q* H) q2 J//--- Check if we have a new candlestick...( u1 {" }7 E: ]; S* Q! |
bool tem_vela_nova(const MqlRates &rate)
$ Y! s$ f7 s. Y) }{
6 Q" l- D' e# B: e{3 {1 L$ Y3 s' |0 i) U
ret = true;! t9 \' Z/ x! A' W, Q* G
close_positions = false;
* d* T/ M8 u. H: h  s4 [}
  ?. {) [$ |1 {/ Z( Aelse  V4 q$ `4 h  m5 ~$ ]
{
( j5 _/ ]( _4 \* sif(mdt.hour == 16)
. D# S" {1 Y  a" F1 mclose_positions = (mdt.min >= 30);
1 T) w/ v0 @* y& a4 l+ U) L' o7 k}
( u- E: Z6 ^5 n4 E2 }' H/ U}% ~3 s  f0 ^& j' x9 s! o9 Z! z8 a
return ret;/ A: f8 Z3 f2 d4 B9 q
}
6 y! X( K7 q7 T, h- H) ?# q//---/ i' t/ n& d7 I$ E# n; W
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
& f3 o) z$ C4 H! |{
) r9 j  V) T  P/ Qif(PositionsTotal()) // Is there a position?' b" r# q; S2 x; a6 f
{
( R) _) i5 g+ M$ S# W! J0 Mdouble offset[1] = { 0 };( C* L' c! J/ C4 W+ `
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
; a+ M5 @7 X  `0 `2 D: o& X&& PositionSelect(_Symbol))  // Select the existing position!
9 G- i  A, z  n3 B4 h{# r9 a( h9 t4 x- h0 q% I
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
( f' `9 W3 @5 ?* R- Q" n& Hdouble SL = PositionGetDouble(POSITION_SL);
2 `$ f9 ~! g& [+ y& p5 T3 gdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));- p+ T* ~  G* I. p; x: r4 m
if(tipo == POSITION_TYPE_BUY)" x0 b- k0 S) Y! s) y
{
7 B% V4 y! X% q2 q  M$ g7 ^if (cotacoes[1].high > cotacoes[0].high)2 k; r, a# `' L1 U$ P, b
{5 R1 w2 n6 t6 x3 D! r" b
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
1 s9 D9 ]% \# ~0 E; p5 [9 U2 Ainfo.NormalizePrice(sl);7 u$ k7 X2 G$ ?* E5 Y
if (sl > SL)
7 G+ T8 n! `6 x4 s, j{7 s2 q# `& V/ w+ T1 t/ O- d! a
negocios.PositionModify(_Symbol, sl, TP);) h: S! J0 W3 D
}
2 O  X2 W7 r5 c+ y! @% F}
& q( D5 j; n. `1 c}
* V% `3 a3 o6 Melse // tipo == POSITION_TYPE_SELL
- m0 z9 `( u0 ]# l2 }& [( e$ v. N8 |+ j{
" C$ D" m9 s/ ^" k4 c* m" D3 Mif (cotacoes[1].low < cotacoes[0].low)' Y4 q% z- y: W9 `; z
{
  u. w- o8 i( Rreturn true;) l9 P7 \' M2 \! Z8 A) @* Y7 S
}7 C( u+ Z4 y7 {3 _( O8 Q4 m( A
// there was no position  x" H9 Z; N+ N- v
return false;$ z* H9 n- W) R7 w4 [9 Q" S8 a
}) [/ p& I( e$ |2 ~8 j
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
: k1 v; \5 y& l2 K) O3 l# i到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-20 02:24 , Processed in 0.801951 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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