私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
7 t* a# C. h3 Q1 b+ F& Q$ C" \在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
! g- e2 E6 \0 u" V' t! Q+ J为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
! g! q* ]. @3 [% c& E+ A以下是制定这些规则的代码。
# m: F5 X$ `0 `2 m. \& i//--- Indicator ATR(1) with EMA(8) used for the stop level...# ?2 I) _+ p0 |1 m
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);" D% J4 k, t% p4 e
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);+ f% z" L  O0 \3 l5 Y  V7 P! z
//--- Define a variable that indicates that we have a deal...% N* y" Q& F+ v0 `7 H. k- h4 }% E
bool tem_tick = false;
+ E! d' s( Q3 ^. X+ Z6 h/ Q//--- An auxiliary variable for opening a position
  V5 q) w$ ^. e4 Y% U#include<Trade/Trade.mqh>! Q$ r1 u: B$ ]- ?
#include<Trade/SymbolInfo.mqh>
2 D4 m# g. Q% PCTrade negocios;
. L7 n: p0 y2 |6 ~3 ?7 [  X' s  UCSymbolInfo info;9 E+ {4 D+ G  C/ A. ?# H
//--- Define in OnInit() the use of the timer every second7 Q, H4 H# w. B) m
//--- and start CTrade, M- T/ O" L# w7 g& x
int OnInit()
+ S0 ?2 ^  \1 k7 n  Z& }{
* U8 S2 Q, F; x) V- {/ s% J! D//--- Set the fill type to keep a pending order- u+ d2 Y6 ]+ H" M1 X6 c% Z
//--- until it is fully filled
: {) }9 D+ J1 B/ P, f4 w" W4 W1 g! L% Bnegocios.SetTypeFilling(ORDER_FILLING_RETURN);7 I( L' U9 {3 d. D
//--- Leave the fixed deviation at it is not used on B3 exchange5 a& Z2 m& b4 |$ u- i0 U4 h
negocios.SetDeviationInPoints(5);
8 f& X( u* O' e//--- Define the symbol in CSymbolInfo...
6 S3 r1 e* s4 s0 Z3 Finfo.Name(_Symbol);
% ~4 T  S4 E7 \8 A. O//--- Set the timer...' u. D6 X3 f/ q7 w2 f
EventSetTimer(1);
. O& l  X$ I1 N//--- Set the base of the random number to have equal tests...( g+ B2 y1 b% L5 n. u
MathSrand(0xDEAD);8 {2 l3 t/ \9 e8 k
return(INIT_SUCCEEDED);
. M9 U1 I4 d: N3 I1 {}& ]; B5 e6 u8 o2 Q: D+ R: [, [4 A
//--- Since we set a timer, we need to destroy it in OnDeInit().; b. O, Q1 L# J7 a
void OnDeinit(const int reason)
: P7 ?( P. V! e6 Y{
$ t1 |: |0 R( m! n% W8 eEventKillTimer();4 d4 e2 ~' w/ z$ h: J8 S" k
}
9 b9 R2 A) q9 Y4 r//--- The OnTick function only informs us that we have a new deal8 i( r0 x3 N# ]# A2 F
void OnTick()
+ Y% a& z4 i, H{$ l; v( m7 b7 d
tem_tick = true;
# t' b' r4 b  T9 P}  c6 l+ h3 V; W7 K' Z
//+------------------------------------------------------------------+
' h+ @; X; T7 F8 v: J( U: k0 f+ H//| Expert Advisor main function                                     |6 r; ]) c! N% r$ }
//+------------------------------------------------------------------+
  {0 e0 g6 F" y* p$ ivoid OnTimer()% g) n! Z& }. f7 H5 P
{
( C% B. ^+ b$ h; g2 ~: n/ L; wMqlRates cotacao[];
/ S/ J. X7 \% C  treturn ;
+ c" I5 N/ U, |5 l" ^9 U" G# K6 yif (negocios_autorizados == false) // are we outside the trading window?
: X# x/ S* }  x0 D5 i3 J) U" ireturn ;- n6 w) U/ Y* Z' G1 s
//--- We are in the trading window, try to open a new position!
( X. f; c! u8 ?int sorteio = MathRand();
0 ~$ P& i! y$ [+ B/ Y5 E//--- Entry rule 1.1, W- S% B- A; B
if(sorteio == 0 || sorteio == 32767)
, e1 U5 _% T4 P7 t+ g0 M% ]return ;+ @, C$ N% v6 h/ p7 C# c" c) X0 r
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
9 `! @- s9 q3 z{- t6 ~6 O# C; V# c& l; p
negocios.Buy(info.LotsMin(), _Symbol);) j% X% _! `; x& Q- s
}: O4 k2 ~! H7 V
else // Draw rule 1.3 -- odd number - Sell
7 [9 j: b8 U, U2 ?{% ]8 h8 p- l8 t9 L
negocios.Sell(info.LotsMin(), _Symbol);4 s3 E' q& D8 E7 \5 f6 M6 Z  B
}# t! |* X% f$ D# B7 P
}
9 d/ X7 i: I( p  Q' X//--- Check if we have a new candlestick...% h# ?4 ~/ x# F8 h6 e' G: w/ n
bool tem_vela_nova(const MqlRates &rate)6 B, j( N. H! B  }% l1 e& \
{, w: T" E; v; a. F4 ?% v
{
! ^$ a  D0 K- z, c1 J4 }% wret = true;: e) u/ E) \! r. [* l! U+ ?- T
close_positions = false;
" g+ N) t& z% f  k0 k# |}
) Y3 R5 V) ~; r# K% |else
: ]( g' {* w! |; O, J7 ]: i& ^{
+ h! }" ~% q& G& z: L8 t. Zif(mdt.hour == 16)
! X  L: F' c2 U! f: a3 e: M  eclose_positions = (mdt.min >= 30);6 _! L4 w6 q+ v+ m' ]) T3 L/ X
}
9 I/ p4 c- y1 ^" L}
2 r0 T# R% o6 W$ z- Dreturn ret;
7 i$ Y' p7 k( d1 L" |}
2 p" p+ O# @4 @8 U//---
! l  k" U) c3 z, ^% hbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
6 J6 z3 D3 X4 N  I4 m- u9 o6 t: f{
5 }& S& J" Q+ V: vif(PositionsTotal()) // Is there a position?
" R0 N; G3 K& k, g8 I, }4 S* h7 A{
' _6 g' m% [! a1 ddouble offset[1] = { 0 };
! G5 {2 d8 P" y/ r6 O2 yif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?& I3 ^% b* k0 v; [, E$ H: ?' w
&& PositionSelect(_Symbol))  // Select the existing position!
) u3 i7 R/ I% _3 ?$ V{
# e, B8 l; K0 \4 k8 ~) F) ^' e! k/ ~ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);$ w9 N' Q- |9 i7 Q8 t9 h
double SL = PositionGetDouble(POSITION_SL);
& Y2 M* x* s1 y+ m+ Fdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
. B4 E3 u2 g7 F9 _if(tipo == POSITION_TYPE_BUY)- b) |% ~9 x6 Z1 t& h4 V
{" G8 _# ]4 T" _1 X! O# `  Y; R* Y
if (cotacoes[1].high > cotacoes[0].high)4 E2 k0 }+ H5 q
{% U  P) X( X  [+ X
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
( b" y, l( {6 tinfo.NormalizePrice(sl);1 r2 C1 g3 P' r
if (sl > SL)0 d0 |) O7 {( B2 b2 j, J
{
6 C# V4 I, x( _, E9 f0 I9 L4 m; ^  znegocios.PositionModify(_Symbol, sl, TP);
$ m- Z* ^9 F7 i! B1 k* l2 d% k& o}
3 }7 O( |! C0 u( L- f$ m: }, n}  [, z6 z) {9 c4 ?5 i/ I$ E
}
3 S& Q1 a$ M1 g& m3 ~( h4 D5 z! nelse // tipo == POSITION_TYPE_SELL* S. L5 G& h3 O7 ~7 M, @
{9 n, \9 v$ h. _& s
if (cotacoes[1].low < cotacoes[0].low)
2 q# G7 K4 T0 V7 {7 M, {0 k. I& H+ g- j{# |# {; ?( T3 G1 \% V, b
return true;
; A1 O6 C/ \0 k  j- N' U}8 f$ X4 f3 B' l
// there was no position4 a. O9 e- A0 S6 l
return false;
% E1 j: u: [/ l( D}
' n  S# c7 }0 H# p我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。# E' O& b1 a' Q* |
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-2 04:46 , Processed in 0.388403 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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