私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
% \& Y$ O: A, e6 _" i" E, o在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
8 n; f" x) D" i为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
4 k% H4 @+ z5 P! w以下是制定这些规则的代码。
3 A: P0 P: t' r) p  \) t1 g//--- Indicator ATR(1) with EMA(8) used for the stop level...
8 |  M- Z6 U( T( ?% ?& uint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
2 n6 b/ @3 I. o* E+ e, _+ Zint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
; j5 u9 ^; |3 A0 K9 \//--- Define a variable that indicates that we have a deal...1 \* Y. G% E0 M, Y
bool tem_tick = false;7 U6 P* X  M" y( P
//--- An auxiliary variable for opening a position8 I( \4 w! m1 ]8 U# f8 \) w5 ?. I
#include<Trade/Trade.mqh>8 h7 r+ T& k, j5 [
#include<Trade/SymbolInfo.mqh>
' {6 j5 V1 V# ?% P5 L! g1 ACTrade negocios;
" T  y" \. j! k' qCSymbolInfo info;8 P! U0 R& l  |2 R+ A" S  Y% L2 a
//--- Define in OnInit() the use of the timer every second
8 r1 |5 {; }) P//--- and start CTrade3 W+ c8 O5 \: X. ~; n
int OnInit()9 M$ ?) s0 a) n6 {* G! S6 T
{
  a3 L9 t- U% ~2 ]//--- Set the fill type to keep a pending order
& c/ k1 y# w0 c) w2 y//--- until it is fully filled
" O. O% r+ s* g  F) q; Hnegocios.SetTypeFilling(ORDER_FILLING_RETURN);% @2 _$ @# w; @' t+ Y4 Z$ n
//--- Leave the fixed deviation at it is not used on B3 exchange4 x" f# \/ j% p- s9 k' {
negocios.SetDeviationInPoints(5);3 F3 [1 V3 [1 Q9 e$ Q* q
//--- Define the symbol in CSymbolInfo...( s, o: S# e3 p& J- c3 w
info.Name(_Symbol);% N8 I/ ^9 O- ^4 M: H+ T
//--- Set the timer...# b3 h# X& t: _- D
EventSetTimer(1);4 s2 z$ k! Q( _$ A8 k- G
//--- Set the base of the random number to have equal tests..., ~; N* D: i* M# w0 G% B
MathSrand(0xDEAD);5 n, f( a& q% ?" U2 R1 W& r
return(INIT_SUCCEEDED);3 \( V" ~- G2 h: i1 v
}
9 K8 A+ H+ N; d  W//--- Since we set a timer, we need to destroy it in OnDeInit().
3 l9 v; i% F5 `5 r# X8 a) Bvoid OnDeinit(const int reason); w& y0 O6 R& t9 R# z! ]
{
# O9 ]( L/ u7 }* s; ?& ~' jEventKillTimer();. `" E  o3 p; q: x6 j- @4 T
}% F, W. l- k  s
//--- The OnTick function only informs us that we have a new deal. U5 w/ O. F3 M& L
void OnTick()8 e: c# J. M2 J5 z5 x- L
{" G. x5 R2 }9 C+ F! Z% D
tem_tick = true;
. X: a$ F1 [3 I' M8 a, T! n+ I3 W3 D}0 c( F2 f0 R2 M) c# _& d
//+------------------------------------------------------------------++ c  R) z6 ?, {+ Q
//| Expert Advisor main function                                     |' b* Y: `; m2 b* G" u
//+------------------------------------------------------------------+
* H* A9 ]: P( Cvoid OnTimer()
5 ]8 E0 B1 M' Y  {2 t+ I! ?( O{2 ~' G) d/ {, r
MqlRates cotacao[];" z2 t1 N+ C- H: K8 U+ g  H
return ;
. G8 B0 g' Y+ T+ x/ N& a) ~if (negocios_autorizados == false) // are we outside the trading window?
+ S9 U. Q) W  ]( A3 vreturn ;9 s! |, _: S+ u' L
//--- We are in the trading window, try to open a new position!
' H% t& D) N5 ]' S( |. Lint sorteio = MathRand();
" ^$ V; ^+ v1 F5 p- F//--- Entry rule 1.1, n7 b  L# v+ N
if(sorteio == 0 || sorteio == 32767)7 @$ p% B' @; x
return ;4 s! Y9 X& ?) K1 B
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy3 D- k3 P; a! Y7 [+ h
{
" V/ a- |1 ~: I2 j+ ~3 Pnegocios.Buy(info.LotsMin(), _Symbol);
! ^$ K" o8 i$ C5 B& E1 `3 A}0 |/ J7 Z8 @3 t. _, j
else // Draw rule 1.3 -- odd number - Sell2 F7 W+ z; ]/ a( g
{5 b/ U  c/ P% ?* D* }" a0 y$ ?' A+ i- K
negocios.Sell(info.LotsMin(), _Symbol);
  T4 K  P! s, \- X}
+ M' b) u& F, h, F}
/ w$ j5 Z$ }) F8 d//--- Check if we have a new candlestick...
6 n* y' O0 S* m8 ybool tem_vela_nova(const MqlRates &rate)8 t/ Q! H1 M" S8 h% W
{( Q1 E& A# \% L0 T0 e
{+ _0 k$ [& z0 j
ret = true;1 z8 e* D5 z7 |+ H5 ?/ ]5 E) Z
close_positions = false;
& X, N7 P7 G! G$ ]/ p& Z6 V# N( n}, |7 H: |$ s  T/ f
else
' R* Y  D, f# o6 Z# V2 i, A{
0 z4 o$ G' ?) U9 Sif(mdt.hour == 16)
5 U" s/ f# I! V" T" W- Iclose_positions = (mdt.min >= 30);
! X- {; t5 x8 y. ?}+ G, k# b9 p- }9 S- y; g
}6 [7 U2 Z  n1 k4 ~8 e' w) V) N: J
return ret;, q# h- m* {$ a" I- O
}
2 b* y3 S) i6 l7 }- \//---
# k+ W0 r$ k# v. K: _9 e& E7 Xbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
: i$ n; b3 z0 A+ u; F{: P) [6 f5 g  d3 W. g# l$ t
if(PositionsTotal()) // Is there a position?
; B' h4 p, l7 t7 {* G{
8 s/ c  `% c6 J& \4 ]: B5 B# Gdouble offset[1] = { 0 };
1 t$ M4 J) X8 t* a  ?if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?; P( |2 P! [; q+ y, m
&& PositionSelect(_Symbol))  // Select the existing position!1 k8 m- x3 A, B# Y, H
{) S$ ]% x) }& U
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
. X& `( P9 r- u! T* |( I% G, J, Qdouble SL = PositionGetDouble(POSITION_SL);
: G, X5 V4 _  cdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));7 K/ j3 g9 c7 @; a( n0 o8 f4 n
if(tipo == POSITION_TYPE_BUY)
' s& F  o) K; L5 b& y5 o{
6 y- h+ {1 f9 N) Fif (cotacoes[1].high > cotacoes[0].high)
* v3 _" \5 J  ~{" H& L& E: x) I7 l
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];; q% p( i% J0 ~( m+ b7 k7 j. I
info.NormalizePrice(sl);
9 q  b9 I! u1 A$ hif (sl > SL)% w5 o$ j! i1 V. c# {7 S- h
{
: F8 E* e. U* J; v/ Xnegocios.PositionModify(_Symbol, sl, TP);
* W9 P$ |9 F: C8 J}$ e) c  _; ^! c8 w) m: R3 p
}
3 j% B7 H9 D9 [" K}
$ O, R. o- g0 x0 S/ X( telse // tipo == POSITION_TYPE_SELL8 B6 v7 ~5 ?* z5 T$ \, H
{. F3 ]6 r' K' X0 A; E7 q
if (cotacoes[1].low < cotacoes[0].low)
' ?5 O( K; D3 H8 }9 q1 t' l: u{* }1 F% v; T* R* P, m# m
return true;! [, g& n9 \( W8 H2 B  h. I% C
}
  V9 Y4 n5 p& Y# Z6 F// there was no position
& F: K. P0 ^5 A- U/ e) wreturn false;' a" N4 G0 T/ J  i( G+ Y; @
}8 U$ M$ M' o7 `1 B: Q3 C
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。: N2 B- {. [. u' q5 r3 X% d/ 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-1-12 09:08 , Processed in 0.674553 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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