私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
, Q  j" B7 r+ a* F8 q6 [) T/ B在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
$ A1 Y# {7 S# v为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。( m( ~' R, Y/ g" S& `) V) \
以下是制定这些规则的代码。/ c- M. P% u0 I& `" R2 M: x, {6 A" z
//--- Indicator ATR(1) with EMA(8) used for the stop level...# E: K. G. j6 |; V5 s5 X
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
& N/ H8 Z6 b* b2 G% J6 ~int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);2 y5 x/ K9 @; m/ g8 A
//--- Define a variable that indicates that we have a deal...8 K2 r: w+ ~% ~& ~( L- N( z+ r' M; N% V
bool tem_tick = false;7 F: A; X  l* V" V, I; V8 ?; R
//--- An auxiliary variable for opening a position0 c$ G  A5 P" [
#include<Trade/Trade.mqh>7 r2 F) X2 {, n( [0 S
#include<Trade/SymbolInfo.mqh>
) b5 g" `! Z& [: O; HCTrade negocios;+ c) X9 {" x. g# S' R7 b* \  u
CSymbolInfo info;% g4 ~. P  |$ B7 T* q" K9 b1 ^
//--- Define in OnInit() the use of the timer every second+ f* \$ V1 Q: g' n; c  j
//--- and start CTrade
) C) o8 Z8 T! Bint OnInit()
( P2 D  ]: O1 N{2 q, q6 j5 }* U3 \7 w2 D
//--- Set the fill type to keep a pending order
1 n$ l1 Q, @$ z- m6 D8 U//--- until it is fully filled2 C2 \% v0 H  O# U3 P
negocios.SetTypeFilling(ORDER_FILLING_RETURN);7 p4 |# @9 G* d; t
//--- Leave the fixed deviation at it is not used on B3 exchange. X9 W! D$ F' y: M: o. N  J  B- u# }
negocios.SetDeviationInPoints(5);
5 `  g$ `% ]8 w9 X% `$ c//--- Define the symbol in CSymbolInfo...
( E% X" ?9 P( m3 rinfo.Name(_Symbol);
7 W4 _! k. `: D//--- Set the timer...3 c$ x$ F% r6 q, T& G8 L& ^4 A
EventSetTimer(1);7 k, t5 |3 |( g  D. N' v+ ]
//--- Set the base of the random number to have equal tests...5 N. y6 C1 l2 }  h. o7 `* c
MathSrand(0xDEAD);6 j2 F: P& |3 ^% u; ?
return(INIT_SUCCEEDED);8 E0 Z6 z% b" ]6 p: ]( g
}: U9 O4 d6 p' V3 k* p% z
//--- Since we set a timer, we need to destroy it in OnDeInit().
2 u+ `! _. Z. L* N* \  Qvoid OnDeinit(const int reason)% \4 X  X3 p4 u9 y0 c. b* T
{+ S1 H) ~. X1 ^0 D
EventKillTimer();) A% p, Z, j% F" h7 k5 p' b9 F! d
}, f. K( X5 N/ P& l1 K* E
//--- The OnTick function only informs us that we have a new deal1 ~/ V; i1 L; s0 e
void OnTick()
# Y4 w' m# h5 ~% N: l1 v{, c8 E0 |9 i2 P: k) h
tem_tick = true;
$ N/ x4 M7 g) i# g9 d}* ^! [& q+ W- E" F& l) P
//+------------------------------------------------------------------+
& d) H! ?; r% b( C//| Expert Advisor main function                                     |
+ \$ M; U1 I: Z% p7 D//+------------------------------------------------------------------+
' a5 e. G/ y* c/ qvoid OnTimer(): t4 q  C9 S3 s" N, I1 N5 X4 E4 `
{$ I: r8 U! H0 ]! e( l2 J4 j
MqlRates cotacao[];7 M8 m) b( L4 T4 [1 T/ D% z" {& H
return ;
( z* d! f  Z+ k  `, hif (negocios_autorizados == false) // are we outside the trading window?
( ]6 E; p/ v  J  {return ;
5 j% q7 ]# a$ n# x" l- u9 ~//--- We are in the trading window, try to open a new position!
0 k: R$ j" d% W& Vint sorteio = MathRand();
) c# D0 y: ]4 P& D$ a9 C1 Y//--- Entry rule 1.1! n* W; O$ L7 j9 j
if(sorteio == 0 || sorteio == 32767)
7 ?  q. k' z9 U; Hreturn ;1 d0 X9 E, |* A! c1 G# B
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy# V* p- j0 C0 j1 {3 a2 T
{& a6 g7 Y1 W+ g/ ^% a/ p  {" `
negocios.Buy(info.LotsMin(), _Symbol);1 q" [. J4 [5 A
}2 X0 h4 P9 E5 V2 S9 H; H
else // Draw rule 1.3 -- odd number - Sell; }3 A5 E: s' ^  U; D1 u
{
" e: G8 y5 s# F' t& T+ Inegocios.Sell(info.LotsMin(), _Symbol);6 C: j4 D/ u/ n% z" g0 \6 B6 ^' ?
}) f; H- f" W& w$ u5 k
}& {+ B. f: ?) y
//--- Check if we have a new candlestick...
, }" p# M1 u' ?9 W8 l% s0 F" tbool tem_vela_nova(const MqlRates &rate)
( B8 I  U& Y" D6 F+ j* m{
/ U" T4 I1 k! C{
- W0 |: c2 F/ I$ k+ qret = true;+ ^# a8 P3 g- U! _  Q
close_positions = false;
* ?0 g; [: B+ R  q}  f* s' E, U  X- p
else
2 h0 A  K( k3 h# ]+ ]{  h8 v" w4 R$ O
if(mdt.hour == 16)# `+ x: i3 o' R6 t1 A4 y& b
close_positions = (mdt.min >= 30);7 a+ C% o( Z$ w% g0 h
}
0 R' c* k' m2 y6 _# D}
2 ?6 O: T1 ]& A! Kreturn ret;0 J4 w2 ]6 e% B, \- d% b
}* u' _: l! p8 q0 p3 |9 w
//---
, D+ u6 p  ^# H* S" w$ Wbool arruma_stop_em_posicoes(const MqlRates &cotacoes[]); Q6 q) w' l( ^# {8 F+ f" O7 J
{
5 y" K, \8 l" R1 d- nif(PositionsTotal()) // Is there a position?
8 Z* K. {" _5 x0 W3 n  X* u# s{
; M& x) g. u8 L" x5 m3 Vdouble offset[1] = { 0 };
  i7 V% s% U3 [9 m% Kif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?/ N: N. S6 j0 @' @! z
&& PositionSelect(_Symbol))  // Select the existing position!
; Z- G! o9 x& X4 L( L{
+ k2 S% {0 X9 k  C2 ^8 K/ uENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);% [+ x% E2 n0 F. W* c$ C1 A
double SL = PositionGetDouble(POSITION_SL);
) a/ `2 f- O2 Z: \double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));+ U" \: c, l' p+ V+ t2 {
if(tipo == POSITION_TYPE_BUY)
0 R# o; j9 t' @* T9 W1 j$ V{
4 W0 T) r& W3 @* C  @( xif (cotacoes[1].high > cotacoes[0].high)& J5 M" m( o( H; i
{
; D- Q  y2 \4 Q/ O. g" M; K$ |double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];% R% Y& [0 D# L, f' N* P) ^" i& S
info.NormalizePrice(sl);
( E2 w9 l7 \6 Sif (sl > SL)
) ^- [( Z' }" G6 X+ A% B/ H{# O/ T0 H# s" r/ y: @+ G
negocios.PositionModify(_Symbol, sl, TP);/ u# G0 M& U7 |* s$ k
}8 k+ @, V# d6 q8 l
}0 q  Q- b6 z& |7 e
}
, a' t( {. [% S- J: lelse // tipo == POSITION_TYPE_SELL& \0 h5 S% r! m4 n
{
" C( x( K5 \4 S' n0 B$ u: C! B% h( g6 Jif (cotacoes[1].low < cotacoes[0].low)
- v, r0 s9 i: |) \1 A{
9 j, ?& ]3 [2 G6 ^. |2 o) }return true;1 s0 n$ f& Q6 ]/ E1 M; O
}
) F: H# i# f( b// there was no position
* J5 z! ^& v& E+ c# Ureturn false;2 I7 W  w( l% {& W4 l
}. ?! j( ~( q+ N) q! i! X# T5 s
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
/ ?9 y9 v6 J( r3 {# d7 W, X! d+ r7 q! M到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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 00:00 , Processed in 1.038846 second(s), 32 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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