私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA' A; g: W7 W% s( G% q9 e# z3 r
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
$ N; X  ?3 c4 `& @为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。/ D; s" f) j1 H3 L# Q8 ?
以下是制定这些规则的代码。
/ |$ R  ^! T( N: b: ~//--- Indicator ATR(1) with EMA(8) used for the stop level...
' ]' M* O: p4 P: Q- x+ v0 Zint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
! V8 H% N0 a5 G7 G( a+ S' T4 Xint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
. K9 _: x1 Z% j* N//--- Define a variable that indicates that we have a deal...
& m8 ~7 k- M  d1 zbool tem_tick = false;
; z0 {/ [/ o6 d$ [//--- An auxiliary variable for opening a position
5 H. m* P& C2 M* ^% H5 x1 I#include<Trade/Trade.mqh>
! _+ m& [0 g3 P  W$ V#include<Trade/SymbolInfo.mqh>7 {4 s( _1 {6 E1 q5 _
CTrade negocios;
0 H  E$ x& F. t+ BCSymbolInfo info;. y2 Z8 F6 Z$ l  X/ j
//--- Define in OnInit() the use of the timer every second
4 |8 S3 k6 z# e* ?! k0 M//--- and start CTrade
4 r6 `% Y& [& E* Rint OnInit(): g- y: ?0 A8 z  b
{
0 e( S2 X0 d  f//--- Set the fill type to keep a pending order! X8 b9 d% W& K' w
//--- until it is fully filled
% n6 l) ~( |0 ]7 \6 k5 [negocios.SetTypeFilling(ORDER_FILLING_RETURN);$ i, i3 c1 {6 \. f
//--- Leave the fixed deviation at it is not used on B3 exchange4 m0 ^/ o+ M% u4 q; G
negocios.SetDeviationInPoints(5);
8 q# U+ X- o# v//--- Define the symbol in CSymbolInfo...8 j+ p, t( Y7 d+ f& m
info.Name(_Symbol);! J1 N( E8 Q$ {% c4 Y( J  v
//--- Set the timer...
% b% C6 P/ y  D) \7 D- y- |EventSetTimer(1);$ ~7 H1 J% k1 C: y
//--- Set the base of the random number to have equal tests...% u# a) X6 ^. k$ g# s
MathSrand(0xDEAD);6 O: Q+ A4 h- }
return(INIT_SUCCEEDED);3 y: ]# `' A4 B4 J" x
}
. I$ y5 L' S3 N' b5 ]+ Q6 E! j//--- Since we set a timer, we need to destroy it in OnDeInit().  u( \' K) V( F4 H* @! n& p: h
void OnDeinit(const int reason)
1 I) C3 T! J' e. Q( s{. a) Z' n! a2 A
EventKillTimer();
" e2 V0 W* [9 Q}
* }: r" P2 G8 u# T//--- The OnTick function only informs us that we have a new deal0 ?  D' Z3 N) z: D' b5 j- U$ m7 m" d
void OnTick()5 J" y& M. [+ J% Y3 ?) i1 |) ?4 @
{
$ F5 f& R6 @2 @8 }  ytem_tick = true;
  R3 {' F$ `9 z}' |- Y! Q" x2 B% C1 ?4 M. W
//+------------------------------------------------------------------+4 n' a, ]9 h( e9 T. l  b
//| Expert Advisor main function                                     |
3 L3 {  L. b: `) _7 U8 B//+------------------------------------------------------------------+
2 U3 Y4 G" B- z5 m! D5 \void OnTimer()
& _" V8 N0 N6 ?7 M{7 z4 I  l+ A0 d" b; x
MqlRates cotacao[];/ v0 i& V: b1 ^7 c1 H& N: m& h
return ;
3 B& J8 Z" v1 \0 A* ?2 x0 Vif (negocios_autorizados == false) // are we outside the trading window?
/ \7 C& b0 G% ~+ |return ;- r8 O( [! U5 M7 f$ N) S3 r' A
//--- We are in the trading window, try to open a new position!# g4 B: v& H7 \% w, _( r+ t$ ?
int sorteio = MathRand();
" ~  ?7 p  v' V! s+ s9 F; n//--- Entry rule 1.1
# Y3 {8 u5 p! \2 j: ]3 L( [if(sorteio == 0 || sorteio == 32767)
5 s, b/ u9 ?' Y8 v- z. H: Lreturn ;
8 E! n6 V9 y5 }" F+ hif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
0 B( O3 c# [6 ~, v/ b% R( M{
! i5 F) T7 v: i3 ^/ Anegocios.Buy(info.LotsMin(), _Symbol);0 H$ c( J: v" o: r
}7 X# D* m+ Y* J
else // Draw rule 1.3 -- odd number - Sell1 F* v6 F5 n# c7 B
{
$ U/ L1 U0 \1 `; H3 A; Knegocios.Sell(info.LotsMin(), _Symbol);
0 N5 h8 {% v- Q}& T& {9 L$ `, S& w# Z% @2 ?
}2 s  D7 ?; L+ W
//--- Check if we have a new candlestick.... F( ?# x1 p+ X/ q5 q6 b9 c9 f" A
bool tem_vela_nova(const MqlRates &rate)
/ g9 C) j9 k/ R* e4 i) }{
+ l! l# P% R' Y{
8 K# c3 n' N- E2 j+ Oret = true;, u% v; f* c3 O' h
close_positions = false;
# x& d& [" S' w! t3 Z0 U. A5 P}
0 v, Y5 l5 J, n) G( d) V/ D7 a7 relse
9 [3 }9 ~: u" f$ `7 R{# `% L& u# W- A# E
if(mdt.hour == 16)
% o/ Z$ c; |/ V  v7 R7 Uclose_positions = (mdt.min >= 30);6 d) J% Z$ @( N& z, F, N
}2 V2 ^" w0 a/ z1 n2 e. L2 X
}
. q4 m! F  R, Yreturn ret;
/ S$ v5 b' m- k" k5 C) d}
. u3 p: y% `# l3 S$ K6 ?5 e//---5 j) z) d8 ?+ g+ S
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[]); Z6 K2 Y# W9 Z! f# b: C! p/ Q
{
) L3 N# E: v: Tif(PositionsTotal()) // Is there a position?; h- |, I) R$ l& E/ v) t
{+ J8 c2 L# W' T6 E
double offset[1] = { 0 };5 d/ S+ p. w8 ^) ]- e' @: J
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?) L. f; z# F: k% k( I9 P
&& PositionSelect(_Symbol))  // Select the existing position!' k/ B4 [1 K6 y5 f. u
{2 h2 `4 |# D3 h* R! c0 A: X$ i
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);1 d: v( m- N  f: R, W
double SL = PositionGetDouble(POSITION_SL);! m+ Q5 x- L" L5 o: J5 ?# F/ [
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));9 n, u7 {2 B- D# w/ C  m
if(tipo == POSITION_TYPE_BUY)8 l1 E" M. A  Y, k( A2 s( N1 v
{1 X& i; x/ }$ k4 B0 u. W0 n
if (cotacoes[1].high > cotacoes[0].high)  f) R$ W# Q2 [3 W, p
{
; R7 T7 j, Y& a6 W( L( Z# ^0 O; Jdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];8 W# L  d2 R5 L8 ^+ g
info.NormalizePrice(sl);
3 Z7 `$ z# {  R$ w5 Z$ w) h' Wif (sl > SL)
% v4 W6 Q6 |: y! z0 s{
) O* ~7 w8 g' `. Q& y# Xnegocios.PositionModify(_Symbol, sl, TP);
% X+ ]2 w4 W  _- a}- F$ j/ x4 P5 ?# m6 _
}
$ Y3 X) g7 Q+ V; x% q}! y2 d! E2 W3 s, R
else // tipo == POSITION_TYPE_SELL
8 d- Q$ m( G0 u6 I* X+ `: K{! t8 y3 |0 w. ~( v' r
if (cotacoes[1].low < cotacoes[0].low)& T4 O0 P7 T% v' B
{# W* Q; T3 u: f  O6 K
return true;9 r3 E, f# t/ F$ @' _: z& }
}) [+ b+ t% L. D# c
// there was no position2 l) t9 N# {# m! J8 c0 e' `
return false;
1 }' w5 l) F: B- m}
/ f8 S. M7 E3 `% y我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
6 \: w+ i9 y1 L: {. ~到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-4-27 15:41 , Processed in 2.708327 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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