私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA( Y5 d' {. `- i1 b
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
( S; w7 U+ i- X! h- k+ w为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
, r3 k. h3 l% i% v. X3 T以下是制定这些规则的代码。
0 ^" `& X& u+ H$ O/ P1 j0 ^, X//--- Indicator ATR(1) with EMA(8) used for the stop level...
  P4 r+ t8 }  J8 L0 ~* P5 @int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
0 Y) @4 G0 y/ D: X# Xint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
" R7 d+ O$ v/ q3 x//--- Define a variable that indicates that we have a deal...
5 M7 ?2 v4 p! C5 I8 Y% ]$ ]bool tem_tick = false;
& w4 ]% u$ h) Y/ C7 l, v; x! T//--- An auxiliary variable for opening a position' O9 ]  x3 e/ Q/ v1 O6 R
#include<Trade/Trade.mqh>% m  j$ X; V' G/ a+ c7 n/ x, u1 g. j
#include<Trade/SymbolInfo.mqh>" D; y/ `# `5 g0 E& f9 M  e
CTrade negocios;
% g$ y, ?" @& yCSymbolInfo info;
: S5 U! E& a0 a* y8 X$ g  p//--- Define in OnInit() the use of the timer every second1 v9 o) p# a9 f: A6 n1 X" F
//--- and start CTrade( T0 Q  a8 A& F3 |
int OnInit()
. J0 e! @5 F+ o% \7 o* U{
3 M3 L# ]; z! I8 l- i4 f+ H//--- Set the fill type to keep a pending order$ u* H7 k% p0 v4 Q, L
//--- until it is fully filled
7 r: w0 _) E' R: J$ hnegocios.SetTypeFilling(ORDER_FILLING_RETURN);3 @9 C4 H2 Q" a4 s& o, r6 q
//--- Leave the fixed deviation at it is not used on B3 exchange0 a! @# }) P7 _9 s+ b
negocios.SetDeviationInPoints(5);
" z6 W- `3 y1 L3 z//--- Define the symbol in CSymbolInfo...
' q5 M- T) m, U* K5 ]info.Name(_Symbol);. B% o; Z- B& S7 H  v& T
//--- Set the timer...
% D" X2 I" ^; nEventSetTimer(1);% v0 [5 P0 g, ?/ c5 a
//--- Set the base of the random number to have equal tests...8 P( q( ^0 t- U2 v" k
MathSrand(0xDEAD);% y7 h3 d' c3 X2 }
return(INIT_SUCCEEDED);
: S9 g: G+ G! \! ?; T. Y" ^! K- r1 S}
( Z7 u' l, _7 h; H6 V( H# R( ~/ K//--- Since we set a timer, we need to destroy it in OnDeInit().
! x3 O5 \/ b5 Fvoid OnDeinit(const int reason)
; Y, W$ J% g  d0 z2 Y) N{
# \9 P: ^4 b% {/ AEventKillTimer();/ _6 I3 L4 i4 P, j) `7 Q# p
}
  A! m( X" Y# W  h1 U" p//--- The OnTick function only informs us that we have a new deal
% r: q) F0 e: F0 Mvoid OnTick()0 e) M* X( y2 j# n) o2 [0 z
{7 o0 ~# g! \' u/ B  u+ N5 N
tem_tick = true;5 ]# _1 U# M/ k4 p! ]/ H- h
}
/ p" x5 K4 I4 U1 `0 B- X! Q//+------------------------------------------------------------------+
+ \" _5 S2 N$ I; m% p//| Expert Advisor main function                                     |: m" P) T7 t! z6 I
//+------------------------------------------------------------------+
# c+ T! b( I. B+ c1 Uvoid OnTimer()# {% _8 q6 o8 X# F' @2 O* i
{! d8 j$ t% y" B% v  I7 [' g& L0 g
MqlRates cotacao[];
' d, L0 C2 r3 c7 M2 X0 B/ L8 rreturn ;+ `. ?* U5 A0 a6 A" O! ]( ^2 Q
if (negocios_autorizados == false) // are we outside the trading window?
" R$ z& k1 H7 b7 M& Wreturn ;
, ^1 p! I9 }/ N! o, u- j& `//--- We are in the trading window, try to open a new position!
2 w3 F, N7 B  ?/ C+ iint sorteio = MathRand();  j4 @9 V7 x9 Q+ t4 ?2 u
//--- Entry rule 1.1' s' Q+ H- z3 _  W; K
if(sorteio == 0 || sorteio == 32767)& K9 ?' g: @' r0 \
return ;3 t4 v* P' f+ n% K. d6 k3 A& P
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
4 s) w: G6 e, Y2 O( d5 N{
. n* E/ R: N% c, U" B6 W' r9 Inegocios.Buy(info.LotsMin(), _Symbol);0 T/ m+ d: L! X' x
}
: C  d- X; W# n$ j' ielse // Draw rule 1.3 -- odd number - Sell
% F( m$ z/ u! E" Z( F{, U/ D2 |9 o! a6 m0 a5 _0 E7 z4 ~( {0 q& J
negocios.Sell(info.LotsMin(), _Symbol);, F  j! \, A2 ]$ ~$ g& ]
}
+ v% Q  u( r5 A! K1 j}9 B; v+ D5 D( x. h* _
//--- Check if we have a new candlestick...
  \/ I6 I' ?) s/ \/ obool tem_vela_nova(const MqlRates &rate)
9 r8 u; E; K4 O' _{, J8 `; C# t& ?$ e# B% |9 _- B2 F
{6 R; O2 C5 ~; l3 C) }& s2 B5 ]& _
ret = true;% p' f# J- q( J! {$ \
close_positions = false;
2 k1 M" `" J. k; J9 ^}+ @/ I  h8 k, H) U% a: j
else
# Q$ z' c: A3 F{0 F) j2 ^- a- `# }/ m
if(mdt.hour == 16)* K' Z' h% D3 U$ R/ m  j
close_positions = (mdt.min >= 30);
, k+ T; @' ?, K0 }* \/ {}
4 m$ e, y% ^7 H( L2 Z}7 _, w4 o6 c' g& H# |/ e
return ret;7 g$ p0 O: \' @3 G7 s  V/ G* N
}% C: l: B) [0 \0 g
//---
8 N! \0 w" a' I6 K2 l1 v2 c- ^bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])$ @( z7 Z: z4 N9 D( @  c
{
/ b; h1 F* e) c: Bif(PositionsTotal()) // Is there a position?
6 `- I6 Z( {, N8 @* {3 ^" w{2 U6 I2 c6 _9 s/ b! X' F% I9 \
double offset[1] = { 0 };
# T+ A/ l: H' g7 s/ {% J8 w7 Tif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
4 u8 ?8 `8 j+ i2 a7 S1 u&& PositionSelect(_Symbol))  // Select the existing position!
- x+ Q# @6 s0 M: ^2 r- R{. s5 Y4 N+ Q# ~; ^3 g
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);2 V- t$ G' N3 Q  S2 N; w# D9 s
double SL = PositionGetDouble(POSITION_SL);
, k3 d7 I. S% ?% B: bdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));( M* W9 |" K7 `$ B  S$ N2 c' ^
if(tipo == POSITION_TYPE_BUY)
) t; b* X$ Y: [, A, ^" @{" x! t, S; ^" H0 R4 s
if (cotacoes[1].high > cotacoes[0].high)
8 f# t. S- ]9 _{
8 b- l. A2 Q) |% [$ ?7 ddouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];( ^& B0 v, h/ p5 l& i
info.NormalizePrice(sl);$ G4 I6 V' U9 G+ d9 O9 C
if (sl > SL)7 o1 T# a( h: R8 \! h1 c
{) j" ]/ |: D, n  k
negocios.PositionModify(_Symbol, sl, TP);& f1 v7 |8 d1 F; J
}
- i2 p5 u6 m, F}3 s8 g6 w. d, F, _$ X/ }* \5 C
}
: ^2 A! T% a: A  I3 V# x4 selse // tipo == POSITION_TYPE_SELL# A9 a# e! |! A
{
. Y, {* h  p/ lif (cotacoes[1].low < cotacoes[0].low)
3 l& ]' w+ W1 Q+ d3 T0 Y# m) |& g  L: C{
5 n( W0 g* h6 V, u8 z; V; |+ preturn true;3 E1 Y* ]: \! E- K9 \
}9 U* u+ f9 j: }' H& r" N
// there was no position) X0 E2 u: C% |0 M* G+ I
return false;
' J# _2 X: C. R6 Z}/ G$ Z8 z& R8 c
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
  ]2 l. e3 j4 s到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-15 09:36 , Processed in 0.655733 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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