私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
' X2 E( E5 R8 E  @9 q. ?1 M在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
& s& U" H* C$ ]& J为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
, y) `+ E" `0 `5 _8 J( o以下是制定这些规则的代码。
& w7 v; S! A  J0 c; u" @3 ]//--- Indicator ATR(1) with EMA(8) used for the stop level...$ p0 s% L+ l0 u
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);3 m$ ?9 H- V9 G; O* {3 f
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);9 I' R0 c) u0 W9 P7 h
//--- Define a variable that indicates that we have a deal...; K5 E, }( N3 A9 [/ f9 q& Z
bool tem_tick = false;7 Z( A2 W. H/ B
//--- An auxiliary variable for opening a position- _% W' d* k1 F( n, J3 w5 m) \
#include<Trade/Trade.mqh>" h# G3 ]# `0 K
#include<Trade/SymbolInfo.mqh>) [& P# d( c, s
CTrade negocios;2 e: i9 t! ~" Z. c" g: O5 a
CSymbolInfo info;% s8 a- ?3 a- }7 P( d# V
//--- Define in OnInit() the use of the timer every second
0 {$ z( b8 t/ B. t' K: o//--- and start CTrade/ Z: [; v- e2 Z- i+ j
int OnInit(). [& Q: @7 |6 a8 R$ ]1 c0 g
{& h, y$ N+ G1 H9 x+ k
//--- Set the fill type to keep a pending order
/ `7 |! F* P+ y1 H7 {, P; _( D8 C//--- until it is fully filled
+ Q. e' o: j4 v" p1 wnegocios.SetTypeFilling(ORDER_FILLING_RETURN);
, |& a' f% X( w' r8 f. C: r//--- Leave the fixed deviation at it is not used on B3 exchange
, S, r0 L; S% A9 B2 r7 wnegocios.SetDeviationInPoints(5);" x9 P- Y1 G7 B
//--- Define the symbol in CSymbolInfo...
1 E9 ~* B2 [- \% i0 {( H5 ?: kinfo.Name(_Symbol);
& S  n, g& f4 _+ G0 }//--- Set the timer...  u0 G8 S' ^& D- H- P$ X
EventSetTimer(1);; l( c- x* Z6 N# [
//--- Set the base of the random number to have equal tests...
" K1 V. d5 O, BMathSrand(0xDEAD);
9 B' @  q" c3 N3 g) ^return(INIT_SUCCEEDED);/ f1 Q: {( {% R1 r% x4 O$ j
}) l7 p. }# v1 O" {& J6 x0 E
//--- Since we set a timer, we need to destroy it in OnDeInit().' l4 L) ^/ C+ c! O% @3 C
void OnDeinit(const int reason)  s4 T7 t: y6 B  ^  j( I' ]" A
{6 l7 x  P7 K. W/ y2 w& [+ Z
EventKillTimer();0 g2 z7 Y# V& o8 L
}
  n& D9 I* [  m* f. j  a9 G. \//--- The OnTick function only informs us that we have a new deal
4 F8 }- B7 I; {# W/ ^  U- Hvoid OnTick()8 b# p2 D2 W) x2 f& E8 _
{
1 m3 G* F& [' {tem_tick = true;8 O. k4 {: g" o" ^8 t" M) z7 O
}/ u. B& \) q- ^2 s2 T, R- o+ f
//+------------------------------------------------------------------+
2 a$ _6 [4 G! d4 I' @2 V4 \//| Expert Advisor main function                                     |
4 i. F& J2 ~2 a; R//+------------------------------------------------------------------+
" P0 ?$ B3 J6 }' A; _$ Evoid OnTimer()% b6 d$ M6 }. G+ ]
{
: [) B1 X. ]- a/ TMqlRates cotacao[];
2 o8 q, U+ G- D4 ?! o' p4 p2 Ereturn ;3 |0 Y$ H" M' L# H3 A
if (negocios_autorizados == false) // are we outside the trading window?
+ m, h5 P% f7 zreturn ;
' Y: Q0 J8 p& S  r  T$ M8 J# t//--- We are in the trading window, try to open a new position!+ K# F5 r# m* }: o* `  `3 V
int sorteio = MathRand();3 D: w. x; a7 P/ H# X8 `8 O2 K7 t
//--- Entry rule 1.12 d3 [2 J1 I  K0 }& C6 y1 `) J, |) \
if(sorteio == 0 || sorteio == 32767): }' b( r  _- q; |( w$ p
return ;( g' B+ k4 y; k, r
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy8 c+ z8 h1 O; p& X% G2 D4 ^! V/ Y
{8 P8 _# C8 [8 Z; [9 }2 j* S
negocios.Buy(info.LotsMin(), _Symbol);
5 r& G' n6 ?5 i}
" v/ @  L6 [5 L4 l" q# h; melse // Draw rule 1.3 -- odd number - Sell: |3 o6 P. A/ @, w  L) a
{" w  ~4 I! n8 E8 X1 W0 O3 f
negocios.Sell(info.LotsMin(), _Symbol);7 g( k8 r/ a0 k4 J5 g# p: x% k& T
}: \! s9 R7 Y6 a, r& I: P( U
}: E2 ?* B1 ]- b0 i9 }
//--- Check if we have a new candlestick...
( R" V) ]. W4 obool tem_vela_nova(const MqlRates &rate)
* S. v. s9 o7 {! V0 H{
$ Q. r6 @! K2 ?1 o* N/ q{
$ K- x) Z: \, t# c! vret = true;
9 K7 E  G# \+ a1 G2 k- v4 G1 }close_positions = false;/ @* G1 R; W! f7 ?2 r
}
4 m* o: h5 s" W; |& u! ^else
/ c2 R% j: T6 h" I{, j6 W3 `. T1 l, t' w( T( x! @
if(mdt.hour == 16)
2 Z  V! y( M0 ~0 }) @$ wclose_positions = (mdt.min >= 30);/ N" F# Y. C6 C; R
}
( l0 {" h3 t7 l3 d; U}/ \8 N, z- f* o- P& \% ~  Y
return ret;' B. ?0 t/ U4 c& T1 {, ~% r+ w
}
& P! s$ l6 [' o6 G( a//---
" \4 {2 y/ w" a) x2 |4 ~$ ?bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
( e$ j, c% g8 R) ~7 B5 s2 F{' A/ C, ?( s4 `' q% q
if(PositionsTotal()) // Is there a position?
' M' S% g! X: ^/ R- q{1 v, L( ]% N* t# `$ x4 b
double offset[1] = { 0 };' _& n4 k8 f+ b: N: A% _
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
4 [5 K5 L2 ?& _. I2 Y, e2 q&& PositionSelect(_Symbol))  // Select the existing position!8 C  N4 h% O' V/ C; p
{9 J* Z) {' p0 w
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
- ^2 h& d) D3 X5 T) |double SL = PositionGetDouble(POSITION_SL);3 ^/ m2 U8 s* q3 b" g9 x
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
  H7 K5 w( S' m% n! eif(tipo == POSITION_TYPE_BUY)) u3 g7 F- \# x' D5 L* t
{) G/ n$ F1 R0 S1 \( l' h# b
if (cotacoes[1].high > cotacoes[0].high)/ ]/ s$ y7 z3 Z* V$ ~$ W
{
* p$ f5 p% D% S; G( u8 i% {, ^9 hdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
. I, H9 V" t0 |, O; Linfo.NormalizePrice(sl);* ]/ Y, s/ z! O) r0 e
if (sl > SL)
& d! \5 a% V% e, O{* O! x( h% M' n( D! j/ Z: N% k9 M; Q
negocios.PositionModify(_Symbol, sl, TP);8 F' ^4 V0 J9 t
}+ D" O( X" G8 L" w2 w* H' f! i4 x
}7 Q- K0 |$ ?  r. \  ?
}, Y$ C2 r9 \( i, A4 D
else // tipo == POSITION_TYPE_SELL
, O4 D0 C2 i, I3 q* a{+ @8 b; i% k: T$ N8 `. Q3 Y# e& n
if (cotacoes[1].low < cotacoes[0].low)
# J1 g! d% p4 H6 t' B{: l! t  R6 m, L4 i
return true;& m( e& A; j+ |, i3 s
}
& c) Q* w. w2 r% e% u/ n; p// there was no position, Z- H% }. i  X/ |
return false;9 P  x6 f, d* a! v& x& F" J
}
8 j0 ?# Y, t7 x6 _- g' B* n我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
3 \5 O: O. ]( W2 \% M8 }" l到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-5 09:26 , Processed in 0.592423 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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