私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
1 l) Q: D( v- r+ D* A% ~8 r+ G在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。9 Q* T/ y( C2 w1 G+ F
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
. t9 F* t0 q  w. Z+ Z1 _  m以下是制定这些规则的代码。6 @6 w9 R: H6 H  J' H' i" Q
//--- Indicator ATR(1) with EMA(8) used for the stop level...
4 w) H1 F7 A( \4 `' t! W1 v6 [int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);+ \# O8 z6 y  l& }
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);4 B+ K, A3 Z7 |: U$ E
//--- Define a variable that indicates that we have a deal...
  F8 i7 O$ O7 s) L5 p  Dbool tem_tick = false;
: s% n9 S+ M; B- O- Z# e% ~  x//--- An auxiliary variable for opening a position
. j; a( G( |" W. I#include<Trade/Trade.mqh>
% w& B- M0 Z  c( ]#include<Trade/SymbolInfo.mqh>
" S3 w, P4 t4 u0 B: yCTrade negocios;) @! {0 M, A( w
CSymbolInfo info;
+ ]0 H$ O3 [& z- I. f+ _$ _, R4 u8 \//--- Define in OnInit() the use of the timer every second
$ w% a0 b5 U% f6 y//--- and start CTrade
% h7 [+ |2 l2 q1 bint OnInit()
; P7 b) V7 Y& s4 S{
4 g9 R7 f6 z9 j0 {//--- Set the fill type to keep a pending order: O, U& W6 O/ j" U* \1 T$ G( o# @
//--- until it is fully filled  N' i5 H( g& e6 g
negocios.SetTypeFilling(ORDER_FILLING_RETURN);  Z# X4 \, e0 F' d! _3 ~* p
//--- Leave the fixed deviation at it is not used on B3 exchange
- d! p6 J6 d* Y- j6 tnegocios.SetDeviationInPoints(5);
9 j% U" b# @0 [6 I- [//--- Define the symbol in CSymbolInfo...
4 |- E. W9 p% F- Z% \- iinfo.Name(_Symbol);
& b9 |6 `8 z% Z/ }//--- Set the timer...6 c* t3 R7 l- l' r) R' R% I
EventSetTimer(1);
" J7 J0 m& F' [- w! ]( P//--- Set the base of the random number to have equal tests...
& o- c* I( m+ Y8 D$ s# G9 FMathSrand(0xDEAD);( P) Q( l. B0 c7 D+ Z( y4 c) j3 Q" \, Z
return(INIT_SUCCEEDED);
5 F* x  m8 _5 Q}
* Q; l7 d3 {# c! B. W  z//--- Since we set a timer, we need to destroy it in OnDeInit().
/ N& B9 S, ^% n& K+ F# Evoid OnDeinit(const int reason)
% ?# ]' {* |5 E5 |# e2 P{1 R2 Y/ Y& C& u# @  W/ B; H: d: W7 @
EventKillTimer();
5 ]$ z3 D' [( h: q}
/ }: D1 z# \0 k$ E" Y! {//--- The OnTick function only informs us that we have a new deal
. H, m+ g1 r- E: n8 vvoid OnTick()
( V5 q. X$ d8 ]. `1 \0 d" H{
' O: o; j; k2 e& m4 Ctem_tick = true;
2 z; G( J9 f: b5 t8 K- i}& {( D7 _  ?, f
//+------------------------------------------------------------------+& i' A# a$ v: o
//| Expert Advisor main function                                     |. N, V& u+ x9 ~3 G& i# B8 P
//+------------------------------------------------------------------+
3 K% a, J& ^1 C- c% Lvoid OnTimer()! b7 n, r8 L9 C; |' F, ~
{
% f: x& p9 l  x* h' f# HMqlRates cotacao[];' z$ U, P8 O' @$ I" i
return ;0 o( e; m( [5 b9 t3 q7 p
if (negocios_autorizados == false) // are we outside the trading window?5 ]4 ?; u; ~4 Q* W* ^- o$ J% \! S" ]3 }
return ;! }" e) `$ d* P3 v9 n
//--- We are in the trading window, try to open a new position!* q! j4 U6 m2 f& V, G8 Z3 P5 a
int sorteio = MathRand();
  {/ V1 j$ X7 R/ K) ^# Z//--- Entry rule 1.1
1 X& M$ j7 m9 ~$ Dif(sorteio == 0 || sorteio == 32767)
! N, V+ G7 ~' |return ;# M+ Z& m# ^- p8 t' h4 M! M) G
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy7 c( J$ B- d. n4 E- b
{/ v: V  n4 N0 h$ R/ r6 g3 e. h, @
negocios.Buy(info.LotsMin(), _Symbol);
7 y9 N$ h& I9 \, y' w}) o- V1 I0 v- _9 s( }$ n  X6 I7 H
else // Draw rule 1.3 -- odd number - Sell
" c/ v: v1 ~; \+ N{) k7 X/ G7 j! A$ ~
negocios.Sell(info.LotsMin(), _Symbol);' W9 V) B1 _" b- s6 V
}
+ o" y4 d) K' v  ]" ?) N/ A; h}( {7 n# W3 H& b# O# c2 `
//--- Check if we have a new candlestick...
# I# \% Q+ v: V% D, F# Hbool tem_vela_nova(const MqlRates &rate)2 [- S% X- B  e8 z
{
% r: G$ N" ?; P8 E+ p5 S{
$ c  g5 ]* d7 ~# b1 D5 Qret = true;5 W3 W* S1 t1 C4 Q2 ^8 t& b& t
close_positions = false;
  S) e7 j# v9 X}& U8 W% x' s/ W4 m) R! X& a
else$ L) u, v; Z& T- E
{
- d4 h* Q; \+ v+ i- M, k- dif(mdt.hour == 16)* M6 w: t1 S' e; V( F2 [, g
close_positions = (mdt.min >= 30);1 f: G1 t- W4 B' X/ m7 i: V
}
1 Q! {7 f$ q# p* i  F9 }}
8 B! a0 D9 P: }1 A+ q* breturn ret;) D3 {" J/ B. |5 c
}( y' |) H/ l& S; o/ ~  R
//---& |, N# M' n: d) z
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
' V2 ^" r+ }: l3 m3 R{+ X7 v1 Q! V. E( ]. O+ R# V
if(PositionsTotal()) // Is there a position?
; p% N- J% K1 W  w0 q0 V; K) w2 n{
3 [" v( |" ?6 B  o# mdouble offset[1] = { 0 };
  N" w- W" o! c. n0 T; W7 Hif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?2 k6 p3 d1 D! p, H" e8 K/ {4 {* ?
&& PositionSelect(_Symbol))  // Select the existing position!* D1 M- M- R) {8 o. Y8 v) z( C1 V
{
+ M  a5 l  y% Z  Y: J  jENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);9 _8 _$ x, G6 s
double SL = PositionGetDouble(POSITION_SL);! D3 Q1 w. M  X
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
( V9 P3 S% [+ ]if(tipo == POSITION_TYPE_BUY): ~. _( C7 w9 P$ K
{, t6 W, }% k' u3 I. e/ G( m
if (cotacoes[1].high > cotacoes[0].high)
! o2 S8 N4 Y9 Y- _5 f, e) b{
8 m* r/ ], a8 }  G3 x4 Z, Z. kdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
: }) D+ [' a$ t  Z* j2 \. Ainfo.NormalizePrice(sl);' `  x  C) ]& {2 p/ s
if (sl > SL)
/ p) d& K/ P) L8 |2 y! Z{
' H) X* Z6 C5 \$ _: Snegocios.PositionModify(_Symbol, sl, TP);
6 Y- w7 N+ F! I2 S9 [. K2 A}1 S8 B3 n0 `. p6 t' T5 T
}% I# }" v1 ^6 X- ^' c7 G
}
5 Z2 }2 G; Q! E, U6 C/ oelse // tipo == POSITION_TYPE_SELL
* ?& L3 ?' q8 E/ S. j; M{
* r- `$ E8 q5 s! |# n# x+ pif (cotacoes[1].low < cotacoes[0].low)& ?8 g$ Y0 q8 v; ^
{
- |6 d& m( \4 Z' V9 v( l  Xreturn true;# }; U& q# N) z$ [4 [+ `! ]% I
}! |7 T0 @6 [; A7 R3 P6 ?
// there was no position& [+ A$ E( n* f  Y- k1 D
return false;! q% X/ L2 z1 h+ v; I2 @* L$ d
}
7 ^+ J1 t' n! B# O/ p! j+ I我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
) @5 `. ], W0 Y7 x7 I6 s到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|Archiver| ( 桂ICP备12001440号-3 )|网站地图

GMT+8, 2026-4-25 16:23 , Processed in 1.344838 second(s), 32 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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