私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA3 Z7 _2 B: j  N" Z, ^6 |! G% X8 D
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
" r9 P; D4 Y& Z% |  L6 ~为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
6 A' S) g: _1 c6 G/ m以下是制定这些规则的代码。
) `3 d' r9 ]( V  o//--- Indicator ATR(1) with EMA(8) used for the stop level...4 n6 K% B2 g# r" h9 o: {
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
7 g2 i' t" \/ a4 hint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
2 L/ k7 }$ \, [! I! s! y0 [//--- Define a variable that indicates that we have a deal...9 ~# _9 C2 o. F$ a
bool tem_tick = false;' G$ A: q% E! z4 T  V0 |
//--- An auxiliary variable for opening a position
) ^9 m4 k5 x9 ?  ]: ]* A#include<Trade/Trade.mqh>7 D2 a$ r  C: I8 k  @7 {  N
#include<Trade/SymbolInfo.mqh>
9 k, L- n5 p* F. iCTrade negocios;. h( l" [0 f! Z0 y  _' o
CSymbolInfo info;5 T1 A* B6 O9 L
//--- Define in OnInit() the use of the timer every second) r) h0 _: m6 i5 T" |# b* H
//--- and start CTrade
- Q! o: E) ]/ f. r6 iint OnInit()' H1 d4 C) f2 t7 N
{
$ }' s, K/ H& f2 H//--- Set the fill type to keep a pending order  S& w& c9 H8 j! q
//--- until it is fully filled" p# |; K- S' U2 O& C& n
negocios.SetTypeFilling(ORDER_FILLING_RETURN);, j; H* G- {' d+ |9 t/ a% j% {
//--- Leave the fixed deviation at it is not used on B3 exchange
. v+ t5 c$ A7 q- Y: W" Z. o# ^negocios.SetDeviationInPoints(5);7 e+ L4 [! t2 h5 j
//--- Define the symbol in CSymbolInfo...
& }5 l6 R0 a- K* kinfo.Name(_Symbol);
, D- D0 n  u& O4 n2 N' M6 B//--- Set the timer...
: N+ T+ d. h$ a+ n# h9 a& k7 oEventSetTimer(1);- c$ F' \$ d6 h+ M
//--- Set the base of the random number to have equal tests...' J( _9 Y0 w' H% l$ e4 Z$ }) }
MathSrand(0xDEAD);/ U5 q7 U* z; d' V( y) k6 g
return(INIT_SUCCEEDED);
. T& b8 m, E- X5 A* y( k% X  Z}& X! d5 m# I4 c1 ~9 }1 V
//--- Since we set a timer, we need to destroy it in OnDeInit().
5 r$ A! _  l7 n, g, ovoid OnDeinit(const int reason)) R" q  e: t- _" {
{
) X1 b/ v1 [4 D' eEventKillTimer();' u7 }" }% u+ l3 M, j7 U
}; r% p% s1 Z' n* s" Z5 R
//--- The OnTick function only informs us that we have a new deal
. @7 N" ^8 r! e" v6 [) h# z' Cvoid OnTick()
; r) w" E+ _: h& B8 n7 i- s" d4 @{
( i) L3 N) a* z6 O8 J4 [1 _2 Ctem_tick = true;
1 o) J. o) M7 q1 c- X  Q8 q}/ `7 f/ w7 W+ F% G; j. d: V/ y2 u4 Q& }3 B
//+------------------------------------------------------------------+3 V! N" A/ ?( \# e8 E* [
//| Expert Advisor main function                                     |
! U; B- q9 d3 r# n//+------------------------------------------------------------------+0 }7 ~9 N" |" n# c, A2 t. I
void OnTimer()
' I9 C# S- I6 k0 n# ~{
) w+ }7 H: _6 y4 I, Y. Z( k2 e2 PMqlRates cotacao[];
1 x! u. M+ _8 Jreturn ;+ [' m: f; Q: Y2 {1 j/ {9 t
if (negocios_autorizados == false) // are we outside the trading window?
8 ~% F) h, _8 y/ r1 Creturn ;  B$ n1 l% ~  \1 B( z7 u
//--- We are in the trading window, try to open a new position!
8 p9 \" x' |& ~int sorteio = MathRand();
, C4 L& m3 ?1 A; z. h% A//--- Entry rule 1.1/ D  F7 U- [. W# z# e' ?
if(sorteio == 0 || sorteio == 32767)
. A  W( n, }& B* T' D# I' sreturn ;% f1 D" ~+ k; z4 E. z( n; t
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy/ c1 H6 V9 {6 u. t! L" r2 T9 R3 p
{1 w6 ~$ R$ V* v8 ]" T& z6 N
negocios.Buy(info.LotsMin(), _Symbol);, |, `7 U% X1 f: @9 G
}* X: J; ^* c! u' G& G
else // Draw rule 1.3 -- odd number - Sell. O, Q% W2 u+ {, s0 N
{
( N+ `( r, Z$ y/ ]: B) b3 fnegocios.Sell(info.LotsMin(), _Symbol);5 j* s4 y; s" P  U9 b
}
6 D& |# q2 E4 M3 G' M! x3 E8 t}
; G/ }* e, d. b' \$ s  ~  y# e1 c//--- Check if we have a new candlestick..." V0 Y/ O: r5 k; p& B" l
bool tem_vela_nova(const MqlRates &rate)
8 X4 t2 h# u; \{( q+ K4 w; j" D$ C. L
{
9 _6 w* x, N7 P* p$ z+ nret = true;, d: n0 ?$ V% r9 S5 N$ f
close_positions = false;
, ~1 O$ k% S2 e0 U9 G, }5 a}
; d0 Z) x0 P( a: U" Belse* l1 L9 z# C" b( ^* q" V8 E
{
% P) O9 F0 u- e& |. ?# Bif(mdt.hour == 16)
5 _% b3 r' {. q9 k3 Oclose_positions = (mdt.min >= 30);( I; Y* J- w1 n
}7 r) x# K- L6 @) x: E
}! D0 u, p6 C" p" u
return ret;
  m. R# R, B2 x( K5 _}9 A* c6 c! j# o: @+ ?2 x2 R+ e
//---: B4 Q  r, i0 T" R2 J
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
( J  {0 y3 e3 t9 d8 ?  F{& S# O7 }. }2 Z& J: L
if(PositionsTotal()) // Is there a position?4 Q% M# @1 h" a! x0 h8 M5 W
{
1 \' I2 }- |# N2 p, U# U. V8 k! Hdouble offset[1] = { 0 };5 n, @+ i" l* O+ B& P
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
" P$ M1 j# F- E* [9 k&& PositionSelect(_Symbol))  // Select the existing position!0 X. E. X, ~4 C+ n
{4 H  i) w3 ]. c& z- K, Y
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);% e1 g' ~! T( V, |: F
double SL = PositionGetDouble(POSITION_SL);
0 n4 j4 l. d" \- c5 @' wdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
4 D; S8 q; B, I0 [& P5 Dif(tipo == POSITION_TYPE_BUY)6 @! N2 Y& b3 }- z1 A! b
{
; z$ d; a" Y# x, X1 {4 C2 |if (cotacoes[1].high > cotacoes[0].high)# c! p# e5 }/ S( A) B, q) Y
{
! @( C2 K! @+ u: l. G, \0 X5 bdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
% m1 a, ~8 _* }0 Pinfo.NormalizePrice(sl);
. s1 S/ G( q# i) v$ `0 K$ d& fif (sl > SL)
9 k  J5 L: Z, [2 ]8 d4 ~$ X  \$ ]. i{- o0 i0 W: G0 L2 ]; N
negocios.PositionModify(_Symbol, sl, TP);
0 I5 Q  b/ M* z2 S" a$ i' E}
6 Z6 [9 J0 |# x. D}
( W4 H- h0 l4 J}
5 u% Y5 O/ L, Jelse // tipo == POSITION_TYPE_SELL$ H% V' E6 U$ F: t
{  x9 s! @( s8 d
if (cotacoes[1].low < cotacoes[0].low)
  U( @# I! m/ i# w* E8 Q8 V{
$ O4 n7 y8 q4 }2 D; o9 W1 Q; greturn true;
5 `& b1 M3 h" X8 I9 M}' J) [2 g1 _" X
// there was no position
  o8 N4 l) w7 Z, A7 Kreturn false;
/ L+ [: S, j2 C, c0 W& y- A: L}1 E1 {0 T- e$ A# X1 w: v2 i5 L: n
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。6 i2 d3 v" m2 a0 {* z4 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-1 01:46 , Processed in 0.418250 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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