私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
9 m5 @8 X/ m( Q$ a9 ?& l0 `, F3 k在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。; J" I5 `7 [# W
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
& `" O8 |; A2 j8 K; F- y3 Z9 u以下是制定这些规则的代码。* ?- Z& ^9 h% J9 o$ c
//--- Indicator ATR(1) with EMA(8) used for the stop level...  k' X) Z* r! A0 j( v
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
, M  v! X7 S! H) Dint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
2 h6 _9 c+ a, m//--- Define a variable that indicates that we have a deal...
1 D: c+ ?! }, L; T# mbool tem_tick = false;
/ l2 w" |: Y7 q3 t//--- An auxiliary variable for opening a position
$ a4 N4 W) W2 E9 c3 Z. f* S#include<Trade/Trade.mqh>
! d, ?+ J; T6 D* \! ~. g#include<Trade/SymbolInfo.mqh>
# \+ C5 P" S$ l8 g0 QCTrade negocios;. E; b4 i( v7 b6 x* T
CSymbolInfo info;5 I4 t% ^: Q1 E. u; F5 t
//--- Define in OnInit() the use of the timer every second
, R8 u9 _1 C6 i5 k//--- and start CTrade: V+ p" P  \* `2 o1 `
int OnInit()& t8 j: F& ~# }/ @0 T$ ]
{9 N' j% V0 x$ W% i! f9 \. L
//--- Set the fill type to keep a pending order
, \6 j" b0 f1 L2 k, m//--- until it is fully filled
- P7 }# f/ v* i! e, Knegocios.SetTypeFilling(ORDER_FILLING_RETURN);
" ?* Q' s' Q6 r+ j8 y//--- Leave the fixed deviation at it is not used on B3 exchange
$ D0 T7 @2 _) |; x4 H8 _2 a7 fnegocios.SetDeviationInPoints(5);
- }1 s( R; r; r6 _//--- Define the symbol in CSymbolInfo..., y* Q. [+ x* {& F3 x
info.Name(_Symbol);  |. l& J0 |1 K) @  `; ~& G  I
//--- Set the timer...8 k% Q( m. j& I" Y; v# P( }
EventSetTimer(1);
" d/ X" b. E% n//--- Set the base of the random number to have equal tests...2 }/ q/ f) l: S
MathSrand(0xDEAD);
3 G  M$ z% p- O; `  I3 Y0 \# |+ S0 Breturn(INIT_SUCCEEDED);: G- e& j1 x5 \3 _' Z  Q1 r
}% z, A/ G- U4 a/ U: h
//--- Since we set a timer, we need to destroy it in OnDeInit().
9 [+ n4 r7 d0 `  O7 n) pvoid OnDeinit(const int reason)
! k. D! k% X, Z) _, n( R7 O* f+ A{  J8 z2 w; m6 f6 h2 ]- l
EventKillTimer();" r" e+ O9 [9 y) `' T8 z
}
( c( T8 h% b9 N% M//--- The OnTick function only informs us that we have a new deal
8 m/ n0 H2 K! n5 a/ l. Qvoid OnTick()1 [  {$ Y7 v$ I3 T
{
8 p4 B2 b/ S( f+ utem_tick = true;
6 ?% a: p% E9 s4 F& e4 ^4 s}: ]" @& s7 H' `2 J9 `6 x0 B' ^
//+------------------------------------------------------------------+
! P; w" L( D' c) A//| Expert Advisor main function                                     |
4 D1 J. y7 C3 I$ _//+------------------------------------------------------------------+  A, z) O4 G6 ?% W
void OnTimer()' v& X3 t/ v2 ~' B" \, B: Q& A% z# l
{
1 o8 ?# o) P' ?. F3 \) c. EMqlRates cotacao[];
) {( f; G7 @5 a8 Mreturn ;# L& n6 T. a/ p( a  \# ~
if (negocios_autorizados == false) // are we outside the trading window?) s1 @/ a, L& }
return ;) N) M! B7 d% i9 S% e4 w1 }# V4 ^
//--- We are in the trading window, try to open a new position!8 Q: m+ e; L5 e% @5 v: m6 H; l6 U
int sorteio = MathRand();6 ]6 \  _9 U5 N- y( ^8 O/ I
//--- Entry rule 1.1
, s- _% e" I5 e3 kif(sorteio == 0 || sorteio == 32767), d$ T& |. W* R3 o' S
return ;8 I4 o* d- l8 T5 Z( e; ^2 t
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy6 E7 F7 r/ ]. I' l' A
{( U- G, S' q- h
negocios.Buy(info.LotsMin(), _Symbol);+ ]9 |& {4 r* }3 {
}
/ f/ t5 S$ J! F8 Helse // Draw rule 1.3 -- odd number - Sell8 @( \2 n$ y( z& m" F# V, ^
{2 ~( t' w5 B- L2 _3 m4 `( r
negocios.Sell(info.LotsMin(), _Symbol);5 X: v" E1 \" X* v
}* J0 y) b1 N9 B- q$ V2 }& Y. X
}$ z% d$ }4 M' k1 v# y
//--- Check if we have a new candlestick..." V$ T4 h  F) q
bool tem_vela_nova(const MqlRates &rate)8 o9 t; f$ u, }+ K0 e+ T
{% e  L) S5 p% w- |& k+ L) M7 z
{
# s. }7 K' y% D$ L3 G' @5 iret = true;
5 h% s! y. J9 ]/ I( b6 T% E' Uclose_positions = false;6 B6 I9 R3 p( G6 S  g& _3 \8 C/ h2 o  |
}- z( \- o! n& `$ I4 p$ H' G
else
  F* H; O6 r; i* X, p{0 R6 y5 V  b3 M- C/ t' L# k
if(mdt.hour == 16); ?! G! g: u* ?8 d' T
close_positions = (mdt.min >= 30);
9 v" ~2 _, L& F- c* }2 n}
9 B. X; H  u1 L/ p1 A. d}% m, H" N/ g) z$ D2 L3 A- l& o
return ret;
1 u1 C1 D6 x  N( P* W5 c}- Q0 f8 _& U. U0 f
//---
2 k7 a1 N3 Y; \$ v, S6 x6 fbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
$ ~+ S. c. v$ O/ R+ ~2 A+ ^8 {4 k{
% f% }) }  W# I: y6 s8 Z1 ^if(PositionsTotal()) // Is there a position?- `* I4 [$ F! ?6 m9 F$ J
{
1 y5 f! u) k% U( a7 Z: n; O5 Fdouble offset[1] = { 0 };
! o( @0 F4 x4 |* J" }( Cif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
" ^1 e) @* i" y# ~&& PositionSelect(_Symbol))  // Select the existing position!3 w& t. l6 e  y* Y/ L. Y
{
6 A7 `1 z/ Q" H( c( ~ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);& ~# j4 G# ]' B) W
double SL = PositionGetDouble(POSITION_SL);
5 \8 D) \5 u( z/ H/ Edouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));/ C8 @/ l  I" t: j6 w4 W6 |
if(tipo == POSITION_TYPE_BUY)
/ a' o' [& t; [' c8 p) Y{
3 t4 n2 [" c5 b. B+ M/ Bif (cotacoes[1].high > cotacoes[0].high)# E7 _  [( M) B; k. s5 u/ Q
{7 ]. M) N! Y4 z! I* @$ U
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];3 I# L  j; D; c. I# _
info.NormalizePrice(sl);
0 Q) D- Z" @3 y; Fif (sl > SL)
9 W9 n* A+ t/ [% Z8 ?  Z{
# n5 R! d  R5 g4 V. K% U, Knegocios.PositionModify(_Symbol, sl, TP);
: y; c0 {1 m% s% z  T/ E+ _}
$ O+ J5 W0 i; W$ C; ~}8 b) f- }8 K# A; v
}
! A, `  Q  Z; B3 Helse // tipo == POSITION_TYPE_SELL
) K) U7 d0 |4 E' V{
9 g, H% m6 _  m; A' X! m: D) aif (cotacoes[1].low < cotacoes[0].low): z! n5 p7 q: b: Y
{* [# N% d! l+ a/ _
return true;, a/ s( o* t0 a; P2 P0 A
}
5 L2 t& |' \6 i; E// there was no position
4 n! v) v1 n6 r* ?return false;9 i0 G0 t& t) s
}
1 q  y$ L8 j+ R( e! o! _我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。( t+ u0 f9 ?( K8 B$ K3 j& v
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-24 05:02 , Processed in 0.438875 second(s), 32 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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