私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA6 S5 J* u5 ~$ P  Z- C5 ]' @& o9 Q
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。9 I3 F$ D) \% I; J' R- e
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
$ O* j3 B9 _3 }  ]- e; G以下是制定这些规则的代码。* J* m1 y+ D1 B" ^
//--- Indicator ATR(1) with EMA(8) used for the stop level...
  {  q! G) g# Z8 \  @int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);2 }( }/ K* ~5 u% G4 D/ _
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);9 s0 I# G# ^& J5 Y) a: u7 T) J" a
//--- Define a variable that indicates that we have a deal...8 ~! A; \3 E5 E  ?4 T
bool tem_tick = false;& [2 D! @% s7 l) x$ L
//--- An auxiliary variable for opening a position2 M" \' c1 c% J/ T* C' o+ m
#include<Trade/Trade.mqh>! T$ U/ K9 ?  `4 Z. ~8 h
#include<Trade/SymbolInfo.mqh>3 c" g1 Q- x. V7 g
CTrade negocios;" L. C+ p/ `1 }+ H
CSymbolInfo info;
6 V& D# u  ^7 D, B# l( |//--- Define in OnInit() the use of the timer every second
- c: _6 m* C" Y" {; K1 I//--- and start CTrade* I/ K7 `" f3 X% r7 s+ d. s
int OnInit()* K. Q! O  C9 s+ @9 h$ B
{' w7 P# @, ^  T
//--- Set the fill type to keep a pending order8 U2 |7 P) R# w4 o
//--- until it is fully filled
* y. m3 \' Y/ z" nnegocios.SetTypeFilling(ORDER_FILLING_RETURN);, N: Z' k+ c6 p* L7 F
//--- Leave the fixed deviation at it is not used on B3 exchange
/ m; x3 P: I$ z0 |0 ?negocios.SetDeviationInPoints(5);# o# U; n" P8 L) w+ p5 A- d1 C  |
//--- Define the symbol in CSymbolInfo...4 C, _6 G- H, a; B$ I5 j( v+ }$ @
info.Name(_Symbol);
, r7 s; _& U5 Q/ U$ a& \! d: p//--- Set the timer...
) j! Z5 Z" c" a4 D1 PEventSetTimer(1);
0 g) I7 b# O9 w' u, b//--- Set the base of the random number to have equal tests...
" M- ~3 w) O* _* fMathSrand(0xDEAD);
; ^3 W/ g/ }6 J$ E# R$ Q: dreturn(INIT_SUCCEEDED);) ], R" b; M$ H& c9 ~# _% U; K' B5 o
}. z2 i2 L$ E! c
//--- Since we set a timer, we need to destroy it in OnDeInit().
8 b+ m, m1 w8 M3 K. w- {% Svoid OnDeinit(const int reason)
/ A8 F6 j6 _% G' K8 @{
: K3 P! Y& W) p" U7 }. zEventKillTimer();
7 T4 {( C& Z! t: h% e}5 |; A' w4 ?8 k% P& h5 P: f+ d5 c
//--- The OnTick function only informs us that we have a new deal9 F# W3 M: L' _! Q. _/ x
void OnTick()! M2 r5 m# P. t; J5 Q4 D6 w
{, M& {, Z6 j/ ?
tem_tick = true;; j  I1 A. q  f1 f! W! l) i; Q: Y
}
, z: z: T+ i* x3 q' X6 q) O! F0 P//+------------------------------------------------------------------+% B* y) V& f7 v+ y; b  a& z
//| Expert Advisor main function                                     |) z' E8 m; }( \( r1 M7 g. z
//+------------------------------------------------------------------+  t2 U% ~% X/ x9 M/ X2 @- |5 M
void OnTimer()' h6 v9 F* O( F; N
{6 D8 i+ S  S, E  r, x( J
MqlRates cotacao[];
1 z2 `; Z( q! P- ?# mreturn ;2 g2 _" a. B/ G( H$ l% Z
if (negocios_autorizados == false) // are we outside the trading window?; [" t# g: l! C  D" a" A" c
return ;
* l9 q/ w9 y- @* R% y. w7 Z. z& k0 D2 N//--- We are in the trading window, try to open a new position!9 S8 {$ _1 {  \- u) f
int sorteio = MathRand();
5 s$ |7 V: J9 Y//--- Entry rule 1.1. O) c$ H  P. b1 ]9 Y# t$ d
if(sorteio == 0 || sorteio == 32767)
$ J) k6 Z! L5 g- p/ J0 k% S9 ~return ;
8 j! L- m, u( x8 [" m! W3 R. o3 q; X5 aif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
9 a0 Q4 U7 J  T3 G, r; f: T{
1 B( b$ g" F$ ~. q) [4 y! }; ^1 E/ Ynegocios.Buy(info.LotsMin(), _Symbol);
" ~" K5 D- e& P9 L( h}
) ?* l/ D* j! H: x8 B( R, kelse // Draw rule 1.3 -- odd number - Sell" @/ h" S0 b8 P1 j- [) V
{
) Y0 d  o- x& M4 Y4 lnegocios.Sell(info.LotsMin(), _Symbol);7 \$ H& v( G4 w  r  W
}
) P+ x. o2 N  t' n}2 c/ V, Y/ U  Q3 S8 R$ R
//--- Check if we have a new candlestick...3 u, K/ q' D9 I) ~7 W/ g( C
bool tem_vela_nova(const MqlRates &rate)) q" Z" j* N2 _/ g( e7 t
{7 n# N9 o' I- ?: M" D; `
{$ N- U. ], K, u+ M; s# l# }
ret = true;
" n  K: k; a" D* p5 Tclose_positions = false;0 K6 K1 g: C  |5 w, Y6 G, @7 N
}
! b, s2 K$ p: `% u9 U3 O- {else
0 r6 V* K8 ^2 {- K  n{% m1 W; V7 Y0 ~7 N
if(mdt.hour == 16)
1 d5 q1 e9 n$ K) p" i4 wclose_positions = (mdt.min >= 30);
8 Z  b9 \( Z: g9 {# @! W' d5 U. ?* c}
  v+ U% Z& F. ]}
% |' k# t, E- |) r- zreturn ret;
: }9 C0 I7 n! E$ ]! M4 V; ?}
% O5 S" q: d0 @( }. T//---) ^/ u1 g! l# J. W
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
# I: C2 R5 P0 r3 |/ Z# x{% B# f% }$ v  m2 B4 R
if(PositionsTotal()) // Is there a position?0 h$ A- E- b) _: J9 U$ c
{$ F; I. ?, A4 f% R+ z  X+ c
double offset[1] = { 0 };' o$ n9 M. _3 l$ V' S, a
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?4 _% p0 s" c7 E- {. @
&& PositionSelect(_Symbol))  // Select the existing position!
8 {/ l2 w, P, m5 s1 F" s7 x0 E{
5 X7 l; }! b; F/ n' ?. U/ |ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);4 w/ p4 L( _) _  i* L: X
double SL = PositionGetDouble(POSITION_SL);  p0 O5 \4 _- s6 k: d2 _
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));! R( R, f* a, y* B
if(tipo == POSITION_TYPE_BUY)& z, v( z+ b1 Z, ]( i4 X5 P
{* I/ b* O! X9 k8 k$ q5 H) \$ _
if (cotacoes[1].high > cotacoes[0].high)) v0 ~" f1 A0 J$ R7 C
{
# Q& \% T. O" G0 b# R8 Ddouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
+ \6 N' {* O/ kinfo.NormalizePrice(sl);" l) t3 r5 B' p6 [! w7 \( m
if (sl > SL)
/ }# n$ m) J9 h9 I" `{7 u$ ?( W* V8 p# G* ]
negocios.PositionModify(_Symbol, sl, TP);
' }  [) Q) E( R3 L% g}/ z- O$ B% ?, z3 U% M/ {
}
8 H, s% R  a9 h( D) g}
9 F6 b( ^* ?& F4 j# a. t% kelse // tipo == POSITION_TYPE_SELL8 ]4 H" e  M7 p/ A/ }) D
{
4 |. e1 |/ z) z% s# |7 F$ Z. Dif (cotacoes[1].low < cotacoes[0].low)! m' r- e& d" _: s  P
{) O# U7 ]5 T) D6 P( I8 B" d5 E
return true;- E$ m! q+ l/ c9 C
}
% I$ |2 U# b2 [, p// there was no position
- F; Y  k7 Y% X; mreturn false;3 T- v3 v. |# W% N
}6 T* q6 g1 z! k) d7 t! _# b
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
% R$ q( v7 ]/ v, ]" L0 x0 k- J到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-2 14:54 , Processed in 0.979668 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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