私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
- n# x6 }; ~; o" y& ?+ ~: ~在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。6 W7 |/ P% p. u
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
% v2 L1 A8 j0 s. L- W以下是制定这些规则的代码。
# v2 a1 @" Z# G9 j' D" j- J//--- Indicator ATR(1) with EMA(8) used for the stop level...2 J. E. n) m5 g; ]" Z; e
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
" H! Y, H, v' w( x& j1 Vint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);& E. v* d9 i7 c9 ^  {, [. C+ ?
//--- Define a variable that indicates that we have a deal...5 h9 K& o- X* E4 e0 r: s
bool tem_tick = false;
8 P, s# y1 R! u! m1 |9 o0 `' r//--- An auxiliary variable for opening a position6 X+ L- U2 r6 R- O% Q$ B) A
#include<Trade/Trade.mqh>
; [+ v  w3 M; g% I#include<Trade/SymbolInfo.mqh>
. E  y; B6 `/ ^. C! }, Q4 ~CTrade negocios;
4 o4 ^9 G+ O  N8 V/ J) oCSymbolInfo info;
9 O# A- Y/ q5 L9 q; d//--- Define in OnInit() the use of the timer every second, m& ~; K% m7 O$ `4 |3 c
//--- and start CTrade+ P: L8 l9 C0 B
int OnInit()# F9 j7 P; t( ]! K' K
{% m8 @. E; A" m+ ^, Y/ `
//--- Set the fill type to keep a pending order
3 C- V0 M, d0 P' M9 e. Y& d//--- until it is fully filled
  h8 |0 D1 \  Lnegocios.SetTypeFilling(ORDER_FILLING_RETURN);
7 w$ i! O. l* x! I//--- Leave the fixed deviation at it is not used on B3 exchange6 `2 O: L) k8 @7 d; r5 l  Y
negocios.SetDeviationInPoints(5);# ~) G2 {; L" E5 a9 t0 y, p
//--- Define the symbol in CSymbolInfo...# u  Q) T$ H, P9 l( l: S4 ?( L
info.Name(_Symbol);' R2 u% }" g, }- k1 B# Z- y
//--- Set the timer...! n7 F7 n+ W9 _! @. Q0 V
EventSetTimer(1);
" n% E1 R8 `) {% Z& ?2 C+ U+ U//--- Set the base of the random number to have equal tests...0 c0 |1 R3 \# W/ v: B
MathSrand(0xDEAD);
. X- o5 |7 A4 K7 }9 Q4 |+ Ereturn(INIT_SUCCEEDED);
' b( W' }( A$ o* g! ~}8 O/ A) I: I, ]' _4 X3 \) I
//--- Since we set a timer, we need to destroy it in OnDeInit().
4 F3 H( z- {2 |1 t* P# Hvoid OnDeinit(const int reason)9 F! T1 L: i3 A4 O- u
{; D; j1 c3 Q1 p+ I$ O, k! h
EventKillTimer();
% O/ O2 K7 {, Q9 a; w2 @}
3 w/ z% `  o8 F5 @//--- The OnTick function only informs us that we have a new deal
+ D7 n- H! I- uvoid OnTick()7 k* {$ T! H" F$ U9 i3 w4 v
{
. ]4 S3 S% _" @0 C1 r" ^  Ntem_tick = true;
7 F2 t0 b$ o5 a* Y}
: {/ v  F! g6 t//+------------------------------------------------------------------+- R: ]. J' u& W" }) a+ `
//| Expert Advisor main function                                     |
8 j( l6 I! M/ Z//+------------------------------------------------------------------+
7 S7 r* {9 o4 [% tvoid OnTimer()
* E* _; U# A2 E! B  z{+ L% K& i& w. s) Z  X6 X) `
MqlRates cotacao[];
/ T- y: B( r9 H* D! Ereturn ;
  M( W) t- u/ }if (negocios_autorizados == false) // are we outside the trading window?
& [- v& t# H7 P0 ]- H$ freturn ;% B, J# H  ^' F
//--- We are in the trading window, try to open a new position!
3 L$ ^4 k& d; f% [/ z1 uint sorteio = MathRand();
! [: U8 W6 H8 T. O. i4 \4 k//--- Entry rule 1.1
1 E3 }2 |+ y! n& a6 q- @+ aif(sorteio == 0 || sorteio == 32767)6 L* @- G, n: L3 q7 n
return ;
. u9 H  k6 P. D7 {if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy% |; |" X" Y# h6 b! ?  \
{
6 U7 v) Q* I& fnegocios.Buy(info.LotsMin(), _Symbol);
9 {. s& M# c- M! @2 L0 W}0 q5 u% X4 i; R- j& j9 ~: r
else // Draw rule 1.3 -- odd number - Sell
$ i! r; N! L( e' B7 ?0 ~{( T* u% p3 i6 P4 R* I, b$ C7 L
negocios.Sell(info.LotsMin(), _Symbol);
1 U# t5 O) r) D, M3 X" U7 Y}; W% v  k8 c" |9 P( v8 f
}
6 x: V) M8 S2 w+ I//--- Check if we have a new candlestick...
  I# ?& N7 S- Q3 i4 A5 d: Lbool tem_vela_nova(const MqlRates &rate)
* M& [! `* P$ F* f6 M. z{0 d; n0 C& v' x6 c& |! m! I% C3 _. @
{
, r2 W, e; x6 N9 b$ k6 d" {ret = true;/ m- q8 o  r/ \9 f/ j2 F
close_positions = false;/ {6 W& A2 z8 T5 O* o, p
}
. b* u! U- g/ y& k- Kelse
9 d' @- L6 L0 e) O* J{+ m) `) I! w* p% v0 R' ^
if(mdt.hour == 16)
6 O6 e' }7 W9 iclose_positions = (mdt.min >= 30);
' N7 f# }+ |0 Q7 v& o}+ I  q( ^" |5 K! K: D, E' G
}2 F  x5 R! M& i$ ~+ ^
return ret;& l3 K  T4 \# P# M; f- G" w/ ]; a
}7 a2 n% |6 @3 L* B4 {; g
//---
2 k3 X* v8 T7 s7 y' }% m9 cbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])! u7 ]0 U$ R; E
{% H9 t0 i- a8 e5 I( k
if(PositionsTotal()) // Is there a position?% P, v  J. n% l$ A
{
2 a  B: F2 O5 F6 Idouble offset[1] = { 0 };
7 S) M* D/ |. M0 }9 ^% Gif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
% t- L9 e3 E$ B3 o7 w5 R6 C' B&& PositionSelect(_Symbol))  // Select the existing position!
, F$ D- t. k# g0 D( u{/ h7 F5 c  j3 I0 l; `
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
3 _$ v0 |2 [5 m3 w9 ~double SL = PositionGetDouble(POSITION_SL);
* ^$ K3 f! T# [( F4 Z1 gdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));) i8 g7 @& U% W. z3 E5 w1 ]
if(tipo == POSITION_TYPE_BUY)
1 R) R8 u2 \1 w* L8 L; D/ {' c{
% M# x, K2 O: Z7 m" F( }if (cotacoes[1].high > cotacoes[0].high). h8 z8 [. \: s8 i
{; s7 ~3 q- F4 L1 U( d; R; a' N
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
& u4 ^3 W. J8 Q0 Yinfo.NormalizePrice(sl);
- a% r7 J& y+ s' vif (sl > SL)9 |1 x, J+ E2 U# \
{/ `4 R1 f- B- `: {. F1 R1 M
negocios.PositionModify(_Symbol, sl, TP);
4 |% I+ g7 ~5 t}5 g% c& m+ q6 m8 B6 P5 ?
}
( t' J+ b& t  t+ o; g9 z}  Z( e2 k. K: O
else // tipo == POSITION_TYPE_SELL
* d; Q: g9 _/ R{' a! [9 h9 _$ l: |1 v
if (cotacoes[1].low < cotacoes[0].low)
" T+ O  U1 g3 X& K* w8 R3 Y{( H$ v  J$ K5 i7 L
return true;) w3 g) k0 w! z1 Z, V" }/ n# d
}( S4 g. H* _3 v4 M; H: c6 P
// there was no position7 g& P7 u) j. E" ^1 {0 w
return false;' J' i+ D/ w. z* H/ P6 W
}
* }. Z1 K: b# E我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
8 P4 W. Z. R! X到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-26 04:03 , Processed in 0.421852 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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