私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
  z5 S  M" B1 B: D在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。9 q  k/ D4 P; W) T7 h3 d, F# ?! L
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。5 Q. n, g" y- ^+ T* @
以下是制定这些规则的代码。
1 s& R" l3 L* `: q* }% i6 n//--- Indicator ATR(1) with EMA(8) used for the stop level...4 X' e0 [6 u& u* W
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);( ]' Q# f' w0 Z7 \* K
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
. v, u- H4 k8 y2 ~# G* V//--- Define a variable that indicates that we have a deal...% `; v5 [  J& @8 f0 X
bool tem_tick = false;2 \: K% |! b% f$ R: C
//--- An auxiliary variable for opening a position
2 i. Y6 v  g2 V  ^; v2 v0 M  d#include<Trade/Trade.mqh>: D& X1 @  x7 R3 c0 ~
#include<Trade/SymbolInfo.mqh>
: ]' T3 @/ p% U1 s: m# ^% k5 DCTrade negocios;$ u& r' ^' q, V. A
CSymbolInfo info;2 ]8 l, S% d5 U# b& I
//--- Define in OnInit() the use of the timer every second
2 ~2 r7 E0 s' p6 z9 {. v, G( @//--- and start CTrade& j% p0 u% O3 x) y
int OnInit()/ G& a% ~. }* }( P# _) b
{
3 w" t& u0 C1 p1 v* c9 I//--- Set the fill type to keep a pending order
  J  l  M0 o4 J1 D//--- until it is fully filled
, G1 O9 n" X% Lnegocios.SetTypeFilling(ORDER_FILLING_RETURN);
% u  j9 u9 t$ ^# [5 T4 p//--- Leave the fixed deviation at it is not used on B3 exchange5 x0 o9 @) @/ ~5 Y* K2 v( t
negocios.SetDeviationInPoints(5);- `+ b7 a2 {1 e# R9 a0 [
//--- Define the symbol in CSymbolInfo...
1 Q$ H8 l% N4 O" l6 iinfo.Name(_Symbol);; m8 H! P  K: C6 c  m3 g2 t* n% v
//--- Set the timer...
# d2 k" s  \: h5 Z9 V, X% n* u! O: rEventSetTimer(1);6 _* F9 p' V4 w: j+ ?5 D
//--- Set the base of the random number to have equal tests...
( w, H/ l0 [) I3 y2 UMathSrand(0xDEAD);+ y* j7 Z% R5 g% z# a2 J9 c' X
return(INIT_SUCCEEDED);2 U: p0 F* a1 Q8 u2 V' F, H) w, |
}# ]) C5 C; b* u; N# x3 J/ H7 ?1 ]
//--- Since we set a timer, we need to destroy it in OnDeInit().
/ J9 K( \. a* R& e7 Vvoid OnDeinit(const int reason)8 t3 [. Z4 ^: j4 \5 B
{
0 A6 I5 d* X9 P* ~EventKillTimer();
/ @: n- o* M" b( ?$ d' H! x}
! e* ?& Z! `1 H9 m//--- The OnTick function only informs us that we have a new deal* w6 C5 F5 o4 i4 J) e% |/ \
void OnTick()
! |% J% h9 L3 P" C3 D  g, e{7 H! x/ I/ Q. i. S5 c
tem_tick = true;( i' X" n) v- S7 I, }
}
1 D& X1 x$ P/ ]1 b' u; \7 F//+------------------------------------------------------------------+5 I4 ]8 Q" X1 O4 J+ i
//| Expert Advisor main function                                     |
- t' x; j- J* d//+------------------------------------------------------------------+
7 P6 C. X) t! r5 ]( z9 ivoid OnTimer()0 f( _. ?  ]: |5 a
{
: s$ }' f! Y1 K2 ^( {& qMqlRates cotacao[];* _: q1 g9 s% }* R- `% |6 P
return ;
3 E  l) M; b, D8 Jif (negocios_autorizados == false) // are we outside the trading window?. l: _: P, J( A! p3 X. g6 G( V
return ;. q, L0 h( h* M% f" Z/ U: y
//--- We are in the trading window, try to open a new position!' {; e# K& ~% |2 f% d
int sorteio = MathRand();
! p. [, I: T6 l//--- Entry rule 1.1
6 M( [' [7 m8 S& G9 R$ Iif(sorteio == 0 || sorteio == 32767)4 E% G4 z2 `5 B5 e- I' P% m
return ;: y# k1 V8 J5 f6 t! s
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
: ^" J0 ?2 x  N' i  _, U' Y& ^- k{1 p% Z0 E9 r' z2 y2 Z' A; m' i6 K
negocios.Buy(info.LotsMin(), _Symbol);8 R, ?. ?, l; [3 _: W) n
}
* O2 e2 F! r9 T: ]3 Zelse // Draw rule 1.3 -- odd number - Sell
7 L; o& V3 ^) g( k5 R. V. ~+ q9 F{
4 S0 W3 |" _4 h: Unegocios.Sell(info.LotsMin(), _Symbol);
  y$ @. K7 e3 S1 u' }, e}
* a& u/ P% \" t) s}, o0 o) g% t+ J6 f6 O, e. }. S
//--- Check if we have a new candlestick...5 P/ g5 w3 I7 f% r( k" y( j  F
bool tem_vela_nova(const MqlRates &rate)
  R, m8 U5 M5 s. P9 z{
$ q" `% @8 k' _0 p4 z, e{# p2 D, s  U5 W
ret = true;
  h: J3 U" _' g/ [close_positions = false;+ @! ^" m9 g$ \. g
}8 e- o6 n  g4 w) t% i6 F
else& z( ^$ d2 G3 G% I2 c: x
{- ]3 `6 ]. x4 u/ z+ W
if(mdt.hour == 16)$ b2 y# b1 s# x/ B: U
close_positions = (mdt.min >= 30);
4 @) G0 J) p! K! A0 o- L}( {- y, j& r, c6 s! f+ B. G6 w
}9 G/ Z* i9 A) A
return ret;  b7 x6 C+ f* B/ W0 D- }
}- v$ O8 Q8 j& {1 W4 Y
//---
" P" |9 P" B6 [0 @$ T8 Hbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
  A" }. P, Z1 P9 y) V2 M{
1 z$ j( y. Q% b7 mif(PositionsTotal()) // Is there a position?) h0 ]% x, J7 L/ J
{
% x2 B6 o. q* B5 Hdouble offset[1] = { 0 };
! G0 D. W" N% X# x7 z/ bif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
  I7 b6 C) w2 j7 d8 Q/ o3 m5 c&& PositionSelect(_Symbol))  // Select the existing position!
& H3 l+ N9 z% W# {* c{
1 N! s5 c3 G2 V& j! ^1 dENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
! B2 N$ a  d( A5 a& D. p7 rdouble SL = PositionGetDouble(POSITION_SL);
( Z! @& {1 q& k3 k3 W1 ndouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
/ K  `! P/ k# V9 w# k" aif(tipo == POSITION_TYPE_BUY)
4 M5 [6 ?( ^2 Y2 b) l1 A# s9 x! S{2 V8 H; p; \! j4 i, d$ p
if (cotacoes[1].high > cotacoes[0].high)' r- [9 A$ c- e, f% V/ f* _( w
{
% ~. c3 D  X+ q+ {) w! Zdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
1 O! v0 V# s- E/ D, c* binfo.NormalizePrice(sl);. B4 I2 X- ?. [! l1 Y1 K# {
if (sl > SL)9 ?: m# s1 q% m3 D* L# z
{
, n+ j+ U' f. c8 z1 dnegocios.PositionModify(_Symbol, sl, TP);7 w& q$ V4 D1 [$ h. C, K
}1 T5 I$ `# T8 R
}
' V3 |; }! E7 ?6 f+ g}( C8 O; T% i6 h$ h4 y$ C, {6 \0 G& K
else // tipo == POSITION_TYPE_SELL
7 \1 f" k/ s1 ]1 B{' a9 B$ h( _: w, s
if (cotacoes[1].low < cotacoes[0].low): [9 m) n: l( r" r" l1 F
{
; A: x- a8 U6 u/ ^return true;1 W( L) L  o+ D$ {1 N( e/ k' A
}3 V$ R4 d) f* `# q, H& o5 ?
// there was no position
7 J3 k2 `( q7 u% vreturn false;9 e5 W& v2 ^  L2 C0 {) j) `, X
}
/ |" \1 V* r. |( y4 W  r  ]( j+ b我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。' O9 `& f5 ]+ q/ i) N: t
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-16 00:39 , Processed in 0.887583 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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