私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA; `3 t! f/ N7 r+ m& A  R( @  x
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
0 K2 P3 G% q% a; h  ~4 H为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。( f! k# g9 W( q5 ?
以下是制定这些规则的代码。/ k  q/ [) d( `) r: b  n
//--- Indicator ATR(1) with EMA(8) used for the stop level...
) f* k; ^7 K% O, m& }# Uint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
. _4 X4 o( i3 a/ T6 d- _5 jint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);/ ~. X2 [6 P. \6 _/ q+ L9 i  @
//--- Define a variable that indicates that we have a deal...
. i- X, l# j* c9 V, O: l; r" Dbool tem_tick = false;
$ I& b2 i+ M$ t& O//--- An auxiliary variable for opening a position
% B' h9 X& V2 N4 o$ v: O  ~/ \#include<Trade/Trade.mqh>
* J" a/ N0 c0 b; F$ |* p! I#include<Trade/SymbolInfo.mqh>
. p7 l( Y  S' O- r4 m2 m/ sCTrade negocios;
/ S& K& {. w8 @) H! D, @! w5 WCSymbolInfo info;
" [' z( I% @* {5 s) P# _1 |6 U//--- Define in OnInit() the use of the timer every second& h1 i9 x4 X+ V( l% ?
//--- and start CTrade: F9 x# k1 h2 i; L8 L$ H
int OnInit()
" l' y+ d, Z: u2 @{
- }' i& P- a' z! ^  r# E7 M, z% E//--- Set the fill type to keep a pending order5 X7 S' o, ?+ {: Q9 x' s+ q  |
//--- until it is fully filled0 Z- D9 |5 t9 R' ^3 o
negocios.SetTypeFilling(ORDER_FILLING_RETURN);* o; e* \6 c) G# d- h1 x
//--- Leave the fixed deviation at it is not used on B3 exchange* S$ R  h% W7 O% Y; O
negocios.SetDeviationInPoints(5);4 `5 J- M! G% _( X- `
//--- Define the symbol in CSymbolInfo...% f6 C6 x9 O1 y& i5 G8 y  v
info.Name(_Symbol);& x5 Y& e1 s9 V4 T) B6 X4 @
//--- Set the timer...1 ]/ [% @4 v: E3 O; E9 i8 u
EventSetTimer(1);) R. l( J, G3 e0 X, K8 R9 Z& S6 \
//--- Set the base of the random number to have equal tests...6 F) u1 t4 {- v( A2 A' X
MathSrand(0xDEAD);
" G% x; {" h; t7 N1 jreturn(INIT_SUCCEEDED);
+ x( h& i1 ?. Z7 t( ?+ ]: K}. R8 g' a4 C9 q1 R
//--- Since we set a timer, we need to destroy it in OnDeInit().
0 R5 K2 P+ Y- x! F" @9 jvoid OnDeinit(const int reason)
5 \2 P$ K9 U2 I% {% N: |' W{
1 }' O' Y- b* {* Q" V# {! XEventKillTimer();* Z! t/ t' w7 G5 F0 H
}" Z% m6 B8 C" B% {( L8 Z
//--- The OnTick function only informs us that we have a new deal
3 z3 _. k% B5 L% n# @) U  u4 I1 dvoid OnTick()" ~  z% k8 B9 ~& g9 a  S# c
{7 B& G) u9 T- N8 d3 m
tem_tick = true;
; V2 t9 e( ]9 i, D3 x% o7 s}' o3 S' M: \: a. X4 M
//+------------------------------------------------------------------+
$ ?* ~, q$ A$ {: z& m//| Expert Advisor main function                                     |+ }: P! I: d. X! e" x) E
//+------------------------------------------------------------------+
3 Y: ^0 G) G5 b' U  j; ?- Nvoid OnTimer()/ r6 X& v$ P6 y
{2 {9 s$ K" I. O6 C& ?# h
MqlRates cotacao[];5 O6 A  E8 [. Z0 s* o
return ;
  R+ T* L, r8 f9 Z. w3 r6 B' Xif (negocios_autorizados == false) // are we outside the trading window?
" q7 e9 L" e5 l) Y) c0 Mreturn ;3 c$ U" l( s) U* y! r
//--- We are in the trading window, try to open a new position!8 ^6 ~- K* Y) Z
int sorteio = MathRand();: F" L' q/ V  _9 d; w: k
//--- Entry rule 1.14 f0 O7 q4 ]/ @$ V4 k3 ?5 I/ X% {
if(sorteio == 0 || sorteio == 32767)4 v6 ]0 u! `# u! q$ r! _+ {3 n5 k
return ;
) d* J+ {# P1 y9 i2 [if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
8 g- R. ]! M1 d. l: e( L  m{
5 A7 n4 S; Q, W: y( i- g# gnegocios.Buy(info.LotsMin(), _Symbol);
5 k1 I# z9 P- ^2 T1 \' B}
- b' ?; u1 i. f6 Oelse // Draw rule 1.3 -- odd number - Sell* K" O1 Q" N. c7 n
{) P$ l% x( F- ]4 h) W, d
negocios.Sell(info.LotsMin(), _Symbol);; Y# Z( v9 \* a3 x
}
# m9 L$ T2 p7 k}
) n6 m- W6 F0 r! F4 H//--- Check if we have a new candlestick...+ [8 e* K- M2 h& @& t
bool tem_vela_nova(const MqlRates &rate)/ `0 y8 G' P8 l. f( Q- [) D
{5 Z0 i( d: ?  b- F' o8 P
{) O0 k/ m! m2 w9 n# s
ret = true;
$ `$ p' x, u, U- A4 b8 z5 @8 u7 sclose_positions = false;
4 H! H  K# n5 p9 _* }}5 g9 w- V% ]3 i( \& \3 ?
else
$ C7 ^' P  ]( x{/ S4 m( m3 W5 g; K* C" b/ n
if(mdt.hour == 16)( K! W7 _0 C9 t# u# S
close_positions = (mdt.min >= 30);
, M0 i! x: s2 V& K}
, Q! X$ s- h: b: X5 w- ?) Q% Z* T7 z}$ C# d! ]" @* y3 n7 Z& a* C
return ret;
: I8 C0 k) E2 d6 ^, B+ R" e9 Q/ a; Y}, _+ U# y1 s/ Z% s
//---" |! n$ k' j% S; |
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])8 t6 x  Y4 ^  u% R, F( Z" J
{% h) Q# J" s/ D: M
if(PositionsTotal()) // Is there a position?
0 I& s3 ]  _9 d3 b3 v{
- i5 `, T; B/ C2 R- C( ^1 g* K1 ndouble offset[1] = { 0 };
$ K; e2 x1 b. V' d& [8 |if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
0 }1 R4 K3 i0 l, h% O4 t0 W&& PositionSelect(_Symbol))  // Select the existing position!- V* J+ M3 R: t6 ^0 _1 {
{
/ l, j/ X! D% b6 yENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
: M# ^6 H* ^- q# xdouble SL = PositionGetDouble(POSITION_SL);
: J& Y0 y/ [: A- V, }2 H& F& }double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
% g3 M. }2 b8 n" x6 Q; A% Y7 X. {4 wif(tipo == POSITION_TYPE_BUY)
: f) T! |. ?% w5 `& V{
* Z- d& D2 i2 E/ gif (cotacoes[1].high > cotacoes[0].high)
* t7 t! t) H  j; D. t{! a, n/ p! K4 J; s/ i+ \2 \
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
* D( A$ V; \2 ^: yinfo.NormalizePrice(sl);
5 A' s* l: W9 J/ n1 p( q' y: p1 _if (sl > SL)! V$ @6 X5 t. b, }( c2 O% B
{, a- H- Q2 X0 u4 ?- E9 |6 q$ _# F
negocios.PositionModify(_Symbol, sl, TP);
* Z; C' W6 @0 C$ _}0 K- `6 Z1 x0 k; G! F
}
: R7 {% s) P! E! N: v+ V}3 H# t2 a1 {, Z4 V& A# m
else // tipo == POSITION_TYPE_SELL8 {: B9 @7 ]: T4 j7 K. r$ a' I
{& c/ p- R1 q" B/ y( P) O5 s
if (cotacoes[1].low < cotacoes[0].low)
, |9 {, w2 s1 N$ M& k) i/ K9 C{2 b# g) z3 S$ o3 X
return true;4 U5 d3 G6 I# F3 s* A( ~! u0 A
}
- f5 _4 h1 D8 k: B5 w: I// there was no position3 R- l3 I( Y* G% w$ p
return false;
: d; X4 T9 B1 J}
( e! g( B- {$ E我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
4 ?7 L* ~: N$ p  H: h! [8 x& n到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-8 13:58 , Processed in 0.404350 second(s), 32 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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