私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
% }- r' s7 D7 c& v  I% }在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
; l5 J6 y: b) Q% @! l为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。2 G  q' ~- l3 c
以下是制定这些规则的代码。
; z" {' D5 A# n1 Z7 A" |! T1 b//--- Indicator ATR(1) with EMA(8) used for the stop level...! u3 ^, X6 k* V% P  t2 X/ @
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
7 k9 Z. Z' |& F* G5 o3 c0 O: F2 }int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
; D8 T+ L- x3 E+ r7 ?* E//--- Define a variable that indicates that we have a deal...( q& \" \6 Z3 [, Q& Q
bool tem_tick = false;
) c/ x/ S) ]2 @//--- An auxiliary variable for opening a position+ ^* W; C* X+ C. ?3 l  t
#include<Trade/Trade.mqh>- q: _& ?/ `" c
#include<Trade/SymbolInfo.mqh>. L1 J7 j% r  k& h* a5 l. T$ |1 a+ B
CTrade negocios;) R# N. r) C" W7 l
CSymbolInfo info;8 C& ~- I* E% |! ?5 T, P! E7 W4 }
//--- Define in OnInit() the use of the timer every second* v7 J7 S+ v9 I7 Z, {  ~9 V4 `
//--- and start CTrade* n: @5 B5 p$ i; R& J
int OnInit()9 b# {0 u: A9 W. t+ H2 v5 _/ H( t- {
{5 g* L6 d; S/ y6 P( d) g/ v
//--- Set the fill type to keep a pending order) e4 B( X' e* O, F
//--- until it is fully filled
( }0 {" }) j9 r3 F9 }negocios.SetTypeFilling(ORDER_FILLING_RETURN);
2 r, b* ?9 T. ]3 `; T//--- Leave the fixed deviation at it is not used on B3 exchange
' d% z5 l0 i0 C, I7 [! ~: ]negocios.SetDeviationInPoints(5);' l% h0 U2 `) {( ^# q* X
//--- Define the symbol in CSymbolInfo...
$ l9 r$ `9 w0 W+ Z3 h% H. Uinfo.Name(_Symbol);
/ {' s! O" W* A  r, f* r1 u//--- Set the timer...6 a$ Y% G+ b0 I2 H9 k$ ?( U# Y6 D
EventSetTimer(1);
5 R" r1 v, |3 Z/ \0 x, t7 M" _/ ]//--- Set the base of the random number to have equal tests...$ F% o2 d, }6 R! m; O7 n  S
MathSrand(0xDEAD);' _2 t9 X6 N1 j
return(INIT_SUCCEEDED);
5 R, s$ e4 a2 x' X, I: h}4 r' W3 f5 h) z( @! X4 M0 G; U
//--- Since we set a timer, we need to destroy it in OnDeInit().
/ C8 z8 n0 T6 M! v- l1 R" Uvoid OnDeinit(const int reason)
" r5 K7 }  @" G9 O9 A1 ]5 U{
$ `. [- {: c) h# W9 ^EventKillTimer();7 N4 z% e$ j# @+ q, k
}0 B" P: e% c* r, R+ ?
//--- The OnTick function only informs us that we have a new deal6 b1 _4 k; n2 V
void OnTick()
* m( x0 S: E' c0 C; G" \$ _6 X+ c{
+ D% Q+ K1 `% `3 f  _tem_tick = true;
1 l: O" v0 _) d8 C; j( x$ g9 s}! d6 Y. i, A- J  d. g9 A$ L& w0 N
//+------------------------------------------------------------------+
$ B9 s2 S3 `# z& d2 |7 {//| Expert Advisor main function                                     |
% e# E# O7 a. v8 m5 C  x//+------------------------------------------------------------------+
2 E! u9 e" \) U% X8 c& \void OnTimer()& |# W0 ?8 M: w' I0 Q5 L9 F
{
# w% e2 o" d1 S! {, f5 R1 }MqlRates cotacao[];
" T) C0 z0 I! I& z1 E8 treturn ;
0 {% ?% }$ E0 v$ l, u& uif (negocios_autorizados == false) // are we outside the trading window?
( `6 r$ h3 @9 Q# _$ k% @' ?: Rreturn ;
/ m% x% s+ q- j2 p: m! f. k9 F2 ~" i//--- We are in the trading window, try to open a new position!' D+ {% r- E( W% m- D- \6 ]; I9 A
int sorteio = MathRand();1 ]* B/ R! L$ g  `: p
//--- Entry rule 1.1, Q  {7 ^+ n7 ^* y" d( i$ a- n
if(sorteio == 0 || sorteio == 32767)2 t/ e* `/ Q' o$ f1 p; r
return ;
: s/ I9 }* P7 z. l5 T$ `/ pif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy6 G0 Q, M( q" C' Y% e- l
{5 n$ U& a( B& t* J. d3 w  m
negocios.Buy(info.LotsMin(), _Symbol);: c/ @& W+ g: i5 V6 W5 `/ w. R
}
& i7 n; _8 ]  K( Q( c# Kelse // Draw rule 1.3 -- odd number - Sell
- B; T+ E  l/ q* ~/ b{; T- P9 a/ U  T+ b6 f$ w: D' N
negocios.Sell(info.LotsMin(), _Symbol);; [/ N! _& V* v* H
}
3 ]' e7 o* m/ }8 e}
4 z5 F( _) k8 z5 O# [  {8 L//--- Check if we have a new candlestick...& Q+ M. ?$ N" u. @" [
bool tem_vela_nova(const MqlRates &rate)
8 `5 [( m( x) W6 ]" @# j5 s5 [{( p5 O$ s) L  H, I* }
{
+ Q. f) d! c$ R3 x# c3 {$ Fret = true;
1 S* I" e9 ^3 ~" S4 eclose_positions = false;# I4 R0 y) z5 H5 Q) P" s
}
. }5 i6 {/ D- {else
0 a6 z5 z( [  d  o{% r/ W+ y3 m7 |; Q/ c; A7 B
if(mdt.hour == 16)* D% D5 j2 ]7 V: N5 y' B' z
close_positions = (mdt.min >= 30);5 h5 {/ u5 x' `6 i, o+ D
}
0 g) u% c0 I% j! F}
- Q5 ]* f6 o! qreturn ret;  A+ z# {$ j* j7 h. `2 W
}* v7 @- A, B8 J& C; W' S! f# P/ C% s
//---
" C6 H6 l: G4 C& _/ S+ Ubool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
! _1 U) o4 @) ?: z{: e% F6 c4 N' T- B6 C* S" c
if(PositionsTotal()) // Is there a position?
5 a4 A3 u/ v  Z5 d+ Q5 m{/ m- C$ o1 n7 q8 t
double offset[1] = { 0 };+ M  a5 z" E; B, x" `+ n9 }' ^
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
) M: i  X9 L" S  T: F( e&& PositionSelect(_Symbol))  // Select the existing position!! Q0 f  a) h/ {
{
7 H% ?/ ^7 \1 f1 I( P: ?! ZENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
* A/ b. |2 a$ ]" H4 _; V& L9 t' Mdouble SL = PositionGetDouble(POSITION_SL);+ E1 E$ C2 R$ z7 d) E
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));3 `1 P) |$ A$ c6 k$ L! s+ {3 Z5 U
if(tipo == POSITION_TYPE_BUY). w  I4 h: g: R( N5 Q
{
9 g/ V  U$ C5 Bif (cotacoes[1].high > cotacoes[0].high)
. s' I! n$ b+ L+ W" y) {{
& l. c: H  K# A* \/ u8 Cdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];' {& E. R/ x9 W
info.NormalizePrice(sl);# f# a; X  D- b, S& `  m" H3 R+ a& K
if (sl > SL)
5 L% h0 F1 T: i# G$ t3 {{* P6 `- Y6 C3 ]# i
negocios.PositionModify(_Symbol, sl, TP);
2 H' g+ w1 Q( Z7 V. k  Q}4 R4 j# q( t* h) E  K) \7 }: y
}) x6 p  n! _+ T: g
}9 `- H8 @. N. q) {$ N0 R+ _
else // tipo == POSITION_TYPE_SELL4 D5 t8 M: o  ?- S' B
{) G1 s4 U" h$ `- a+ d5 R0 y
if (cotacoes[1].low < cotacoes[0].low)
. I6 g* n  P# K4 ~. [% q{  k! Y7 p$ _1 w) A& g; x% N6 V
return true;
4 B  v+ i2 x& }; m; Q- d2 a3 U}
) z& N9 |% Z! e3 F// there was no position
$ u8 U1 R7 ]4 P' s/ b, Qreturn false;; k* _+ y* ?+ S2 R- N) z6 n1 N
}
7 M! C2 j$ a/ c我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。/ v& u  t, ]  Y! D; 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-5-26 20:08 , Processed in 0.997043 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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