私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA/ T, d* g0 l( H
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
9 X9 Q7 X" h1 X7 P) N为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。$ e" n+ C) J$ t% b; v
以下是制定这些规则的代码。
1 p* v$ m' Z6 t" ~# |- @0 j* }//--- Indicator ATR(1) with EMA(8) used for the stop level...
! y! s4 I2 i' T3 ~int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);/ K# t4 ?, ]2 k9 \% ?. X
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);8 h; D2 T, n0 N3 T( S# w
//--- Define a variable that indicates that we have a deal...
% x% B( E( ^# w8 i7 O" w( _1 @0 j0 @bool tem_tick = false;
. S  Z, F& \% P8 N# k0 J//--- An auxiliary variable for opening a position
" H) b  s: q/ `, v" l#include<Trade/Trade.mqh>
1 _7 V' M& x/ o/ w#include<Trade/SymbolInfo.mqh>* U) v, M) d' Q* x1 v
CTrade negocios;! B( r, [2 ^! l+ T! z
CSymbolInfo info;* m0 N2 ?0 }+ e9 `) r- T# R, [
//--- Define in OnInit() the use of the timer every second7 }" F: }- R& L9 p
//--- and start CTrade3 x' Q% W' H9 P0 g
int OnInit()
! l# F2 A& F, M{6 E# U8 w( h+ l* D
//--- Set the fill type to keep a pending order
* T1 f- {& M' \, x! O//--- until it is fully filled& |  S6 T  c) ]1 f
negocios.SetTypeFilling(ORDER_FILLING_RETURN);. S7 o5 n0 ~1 Z
//--- Leave the fixed deviation at it is not used on B3 exchange' B" B( B& q! `0 X, K* h! K
negocios.SetDeviationInPoints(5);, _6 K# J. h( W" n' x
//--- Define the symbol in CSymbolInfo...
6 a: e0 L9 [& o' o% rinfo.Name(_Symbol);6 m/ \( }3 D$ [
//--- Set the timer...
! m7 h' b2 M; Q6 B/ b+ ^EventSetTimer(1);
4 d$ i, l. o1 g3 P//--- Set the base of the random number to have equal tests...' U0 t% R7 f8 |7 d1 n( c( @( y  Z
MathSrand(0xDEAD);
, y1 x) V$ p3 Ireturn(INIT_SUCCEEDED);5 ]& y8 r) [' G, s9 U
}0 m1 e" Y# t3 p- l/ N' X  f3 i
//--- Since we set a timer, we need to destroy it in OnDeInit().; V6 L+ c' C, x  Q: V& [
void OnDeinit(const int reason)0 p3 I0 J, k7 w% i5 `
{
) a3 A, }! g: D) z5 U' ]2 A' eEventKillTimer();0 a2 G; {5 F. u( q, K$ Z
}* ~/ W. }( c8 b$ c% n( H: G
//--- The OnTick function only informs us that we have a new deal
; |! ?% q0 H! E8 l+ A5 {0 Hvoid OnTick()) |0 T; L( e: ~$ K& L2 V
{
$ n3 f  g2 D  m- _0 Jtem_tick = true;
6 W7 M) L: {. Q+ z5 @; W0 O}
8 S6 W9 ?% q5 B6 T//+------------------------------------------------------------------++ ^. h4 F  K$ ]$ t7 ~/ B1 s
//| Expert Advisor main function                                     |
* J) I* B/ `) g/ B# `//+------------------------------------------------------------------+4 v' ^2 d: R1 d2 r7 @% W, _2 k. A  A
void OnTimer()
" a) q2 S, h% _- s/ H# p{6 H1 J4 A+ T# l- H
MqlRates cotacao[];
! k+ e$ l% A4 jreturn ;
* A$ w; Z5 p0 [4 p* S3 Dif (negocios_autorizados == false) // are we outside the trading window?
& G( U1 x) v3 r4 S& p! R8 J. n+ b# o/ Yreturn ;
4 v  W2 O7 B9 m. Z% T//--- We are in the trading window, try to open a new position!. u: y' t9 \! L5 m; p( {6 |% G
int sorteio = MathRand();
. X* [" ~6 A( F) u8 d8 @% o; @//--- Entry rule 1.1
* Q) Y: N+ N& W! S- W7 eif(sorteio == 0 || sorteio == 32767)
* \* g1 o6 a, ~1 e, [5 jreturn ;
. [6 N% ~4 t+ y7 q! [0 u* Xif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy( q' ?) q, X6 ?/ ~# H
{
9 q# m3 {. F2 ynegocios.Buy(info.LotsMin(), _Symbol);' x4 j* l4 e. D. d' H' `, N
}2 k+ Q; E$ {4 I, b9 A9 g) N: p; O6 T
else // Draw rule 1.3 -- odd number - Sell
/ M. h" z0 D, f  p{
+ p2 ~$ Y+ f% x* v) ?negocios.Sell(info.LotsMin(), _Symbol);3 N5 k- E& F' x( i
}$ D- u+ N7 W# t* Z6 t6 m/ i
}
% x; d! j+ y6 U8 V9 p//--- Check if we have a new candlestick...
" P# N$ C' a8 U9 i+ `- S! ubool tem_vela_nova(const MqlRates &rate)6 m0 z8 t# m: U4 K/ K- j
{
& K: G# \9 f8 ?$ j: a9 c{
3 T0 j& |& C/ q! A* |, Jret = true;
0 J! f, [$ \7 U( T1 q- ?close_positions = false;
/ v; l! \' v" y6 D( }) o& {% W}* O3 z+ V' `. Z8 c/ x8 ?  L# n# P, C; M
else
2 j& R1 e* U4 ^" J  d{
$ V  x( Z; j% v$ yif(mdt.hour == 16)
7 X+ \/ R1 P1 R. j4 F) ]! n1 Yclose_positions = (mdt.min >= 30);
3 [  ^  v4 j9 K  k& Y+ d- w5 j+ O5 r}
) W8 I! u! {6 D% n- z7 u: t}& `, E- _1 A; n# O" ?5 F# F( Y
return ret;
* j4 P: s' F- G8 q/ x}3 I0 r) \) T8 E$ {9 w( R
//---
& _; a! w1 I2 [0 R/ G6 m* ibool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
" Q4 U/ U. O3 V/ l4 I{
, c. B4 o) P$ ~* I- P* t- zif(PositionsTotal()) // Is there a position?
5 a* `1 ?2 Z$ v) |{& ?2 j8 O* [0 B" I
double offset[1] = { 0 };2 k* n6 O+ B6 A# E9 g: c
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
7 ]2 y6 J+ }) T) o&& PositionSelect(_Symbol))  // Select the existing position!9 D/ O2 `( Q+ _
{/ q+ [$ b& A6 B6 i  o' b
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);# N1 l- q  o% Z' n+ r8 Q
double SL = PositionGetDouble(POSITION_SL);2 c" P* O8 A( E2 ?& w
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
% Q; ?1 N$ V5 E/ n5 Gif(tipo == POSITION_TYPE_BUY): x( s, J( A9 r; _4 K9 F" ^
{
; V/ H% F3 {! O: W- m- B  yif (cotacoes[1].high > cotacoes[0].high)  Q* q# ?& k7 Q% t
{
2 b: j6 o+ y6 tdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];' c1 `9 E' V8 c2 i- S+ k& `
info.NormalizePrice(sl);
1 M) M5 t1 r% e. R; P, O1 qif (sl > SL)
# z# k. @6 Y2 ?( e) ^; Y3 \: T; U{
4 f4 H/ M( q% p; Y. bnegocios.PositionModify(_Symbol, sl, TP);: \/ U0 H* V7 N) L; b" K
}
  w! R9 D; K# ]9 e3 ?0 ~}
4 j6 f* [8 @6 v2 T: X, X}
5 X9 S# {4 n, q) E( I: E6 p( Qelse // tipo == POSITION_TYPE_SELL4 q/ s' M: H- z3 ]4 |# `% A
{$ U# S& X- [. V
if (cotacoes[1].low < cotacoes[0].low)
3 I1 P6 h7 _+ e; y1 n{
7 l4 P/ d: B% Z4 c1 X  Rreturn true;
- N7 e$ g5 l- D" T0 Y+ X! j7 _1 o}/ z& x* c7 |- @% g4 m$ x$ o$ z4 Q
// there was no position; ?$ N7 _3 T4 Z9 n( c
return false;
- F& l9 M8 P; q* y/ k}
$ D5 C- M3 n1 r我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
0 N0 `' L( k: L1 Q9 L2 p9 \9 i到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-31 20:16 , Processed in 3.678085 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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