私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA% l0 I$ [  z3 h! f/ ?
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
: X- }7 `9 C: o! R% P1 q为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。( W) ^+ I7 g+ t: i( W
以下是制定这些规则的代码。
7 ^" ?0 x9 `6 B& Q3 i4 m! k//--- Indicator ATR(1) with EMA(8) used for the stop level...2 i3 [$ c! k$ K  a$ G8 j! g5 U
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);  c5 R% Z( I; i9 q
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);' I7 H/ _" t! A- F. f
//--- Define a variable that indicates that we have a deal...
/ M8 x. R, y, @4 d% {* wbool tem_tick = false;" S8 r1 J$ ?" R
//--- An auxiliary variable for opening a position
6 w; k/ x# |: q. c#include<Trade/Trade.mqh>
$ l& G0 {+ F3 U3 }, w8 a#include<Trade/SymbolInfo.mqh>% i4 P* M0 K( f4 M# \( D9 E. i
CTrade negocios;- h2 \( i" q6 a
CSymbolInfo info;
  W, Y% Q7 R3 [( ~  i//--- Define in OnInit() the use of the timer every second; p' y; H1 f" E4 _
//--- and start CTrade
7 p8 \) d; v+ z# S# v6 ^int OnInit(), q$ ^' X  I% ^' ~
{6 O+ Q9 `+ k' t) ]' H4 [& c. i0 @
//--- Set the fill type to keep a pending order) {" n8 _* K+ ]$ }
//--- until it is fully filled+ E3 q1 a4 R- }2 `8 y
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
' S  {% |. x+ X" L; a( a//--- Leave the fixed deviation at it is not used on B3 exchange
) w" a7 J) m8 a" cnegocios.SetDeviationInPoints(5);
# D- c, ]& S9 v; a$ [% D//--- Define the symbol in CSymbolInfo...4 ~4 J, f- T; ]# f7 X: d! |
info.Name(_Symbol);9 T% F/ _4 K9 R7 M
//--- Set the timer...
3 L" |1 ?4 [5 [. [& T0 A8 ?, tEventSetTimer(1);
  b- B9 N2 D  y# T8 k! Z" R1 h( n//--- Set the base of the random number to have equal tests...* L8 _! ?9 M6 w
MathSrand(0xDEAD);
& D* k2 F. d8 `) ^# k* B) ireturn(INIT_SUCCEEDED);% w- l2 L7 f5 @$ w* T- E0 R1 j4 C& P7 [
}' x$ z! |6 D+ V* a0 l
//--- Since we set a timer, we need to destroy it in OnDeInit().- \: {2 h. \. \
void OnDeinit(const int reason)( K/ U7 F" K" t& ?6 C
{
; C0 P( ]7 w- \8 QEventKillTimer();; g) f9 X0 O; ?+ G; E6 Q: @) l4 Q
}
" F( _, \1 g2 j9 F6 `//--- The OnTick function only informs us that we have a new deal( T, |. c# A  A: q8 M1 \
void OnTick()/ E3 R  R4 L* D9 x5 p" S
{
% D# W: `1 Z, a: ^: Ktem_tick = true;
1 ^2 y: P& r& q9 I& ~  ^, A( e}/ J( F/ b9 Q8 R/ ]2 u( G
//+------------------------------------------------------------------+
4 Y6 m9 i( i% b//| Expert Advisor main function                                     |
5 s- r( u1 Y; R. }//+------------------------------------------------------------------+, U" ^& F4 }1 A2 [0 n% {$ J, Y
void OnTimer()# M8 J% [+ ?6 d/ |& F, M! K
{
. u/ l" s% Y+ A" s& I$ S- p2 j0 dMqlRates cotacao[];% L0 V1 ]- [. o7 E
return ;
" z3 q5 j0 v* ^  T* k, N+ e+ dif (negocios_autorizados == false) // are we outside the trading window?8 a6 o/ E+ E( q3 i' o) A
return ;" K  g- H# R) _( J% |
//--- We are in the trading window, try to open a new position!; o4 _& F" `0 E6 Z- P! `
int sorteio = MathRand();
5 |1 D; Q( J- ^//--- Entry rule 1.1
" ]& |% p" n. z* F  Jif(sorteio == 0 || sorteio == 32767)
2 j8 V! U5 Q! @* D: a4 jreturn ;. k8 O5 z& Y& ^, c0 K5 k" O  h
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy. z4 w2 S% x) d; X; v7 F
{4 P9 a0 g" }# o; q% Z. i* D' Q3 C
negocios.Buy(info.LotsMin(), _Symbol);
/ y3 g+ Z9 ?, k$ M' A/ s* b2 b}: y5 R% c% B9 W7 d/ [
else // Draw rule 1.3 -- odd number - Sell
( ~( X6 C  `0 n* l( E{
  J9 G7 P# U  i% ]# znegocios.Sell(info.LotsMin(), _Symbol);
( A6 l& |0 r+ i$ a}
8 }' p* {5 L$ ^% M, f}3 U. g) A. W% A/ ~$ J
//--- Check if we have a new candlestick...
  j: |" ^5 B# `bool tem_vela_nova(const MqlRates &rate): R& \7 Q, F: I( {; ]/ Z) L
{: m2 P9 J( \' i& T; X1 W$ V$ d. y
{
2 v0 `5 f8 Z, \+ x* g! E4 B. ]ret = true;6 R) \- F; z, K* m
close_positions = false;# d9 a3 e* _; }1 Z& X1 C# M
}
5 m2 ]2 P' b7 L7 C1 Jelse
5 P! l! R$ o: }3 A{
- }  x1 E& j! }4 T' k8 rif(mdt.hour == 16). p9 [0 r) m5 n6 A, A+ \: @
close_positions = (mdt.min >= 30);
& b/ b0 C2 q- H) H: J1 t8 w}
' N  N  `% o; v}
, U2 r3 f4 W- b% [1 z4 m& F$ a$ [return ret;: ]+ ]: R+ N. e/ P; A+ a
}
' J' x6 M1 r) h  d, ~. E//---% c' I! P* a& X: N5 l2 m
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
" b/ `. T8 H1 e- j& C5 T$ j{& A" @  d# F) }* K
if(PositionsTotal()) // Is there a position?- \" W0 [3 ?) U7 |. ^
{
4 E. Q0 |6 e! K9 V* o6 mdouble offset[1] = { 0 };
$ c% n1 k2 L/ `- ]! m" I6 }$ u0 cif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
- v4 k% _1 @1 X$ `&& PositionSelect(_Symbol))  // Select the existing position!
7 n8 L$ g2 L) g0 n* \& b7 E7 R{" J  v* u& l5 t/ C( z. p
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
5 p! v8 O4 h: X- `3 sdouble SL = PositionGetDouble(POSITION_SL);% y: N7 t, q2 H9 r+ o
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
5 C, T% ^# I1 l. g/ P1 hif(tipo == POSITION_TYPE_BUY)& B7 w  U& {& Q: j
{0 }  G; e3 r4 l/ ~5 t1 G+ p8 ]
if (cotacoes[1].high > cotacoes[0].high)
! U! S/ u# b2 Y: @0 y; a{) A/ T7 C, y/ @# F* f
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
4 R+ K+ y* p" Z, Linfo.NormalizePrice(sl);
; s3 v$ [/ L% Y: ~* f& @if (sl > SL)0 p! y4 L/ F- B1 I1 F
{
6 A3 u# r  {; ?/ Pnegocios.PositionModify(_Symbol, sl, TP);, e3 z% j, F- m2 n
}
3 e' d% B4 R# P9 b8 L}3 {5 u! b) j; W7 _+ P
}
* K8 Y) R1 A8 velse // tipo == POSITION_TYPE_SELL# ?, V% T: ^! J9 i- m" o2 d; Q
{/ K0 \$ A4 X  f9 U& \3 K' Q7 Z6 u# S
if (cotacoes[1].low < cotacoes[0].low), ^) W' s4 T7 p. m; A
{+ }; I/ z$ S9 s& B5 ^9 N
return true;
& j+ U9 `/ U; ]7 Y+ P+ k7 I/ x}
2 [0 j. I! m& g  {! P" l; ]% c" t$ c; Q// there was no position2 q2 y( z! y6 k
return false;
* h$ S* x, r0 x8 M1 y}7 u& q/ |3 K# _# S3 w1 T" T  O9 N; \
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。$ n* a1 c+ ^4 R- E3 V3 u2 z
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-7 19:49 , Processed in 2.184830 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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