私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
+ o$ F7 l; ?3 E2 A在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
; ^, f* N8 @0 w. D" A8 m为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
$ `4 L2 [" @' c& }9 p以下是制定这些规则的代码。
( p. f0 [; u5 m, i//--- Indicator ATR(1) with EMA(8) used for the stop level...6 G* o3 x- @# y" g) p0 U. I
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
1 Q4 X' U  a! q9 i( w0 Q& L1 sint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
1 X) m$ m( ]/ }' z1 _//--- Define a variable that indicates that we have a deal...
, P& ]2 K/ M% w3 k9 k' }/ [$ C+ L2 Dbool tem_tick = false;" e* j7 l6 V' u" W+ j" B
//--- An auxiliary variable for opening a position4 Z7 [0 |8 O5 M% v) s
#include<Trade/Trade.mqh>6 H+ O. X# J5 i; y; s
#include<Trade/SymbolInfo.mqh>
) ]0 D- _% E% i; x+ g  `CTrade negocios;
* B, n5 s" O; K0 G- YCSymbolInfo info;
$ ~  d& x8 p8 w6 R, r$ ~1 H) w//--- Define in OnInit() the use of the timer every second% s6 l6 w: V$ b0 G
//--- and start CTrade( t0 P3 s( U$ n1 ^! M. t
int OnInit()
& w% n$ ?: ^6 V) e6 c{
5 p% s, c, r, R7 r# J//--- Set the fill type to keep a pending order( w% U3 Q, h, h1 n
//--- until it is fully filled* ]$ p% y4 A( s1 L3 D, t
negocios.SetTypeFilling(ORDER_FILLING_RETURN);7 T) S3 p; k/ d8 r
//--- Leave the fixed deviation at it is not used on B3 exchange7 B- Q) R& r0 G# L# _
negocios.SetDeviationInPoints(5);8 q' i4 Q3 r7 c5 b
//--- Define the symbol in CSymbolInfo...9 s) ~( x# t+ R& c
info.Name(_Symbol);
! E. n! c8 C2 R; U* _- }  V//--- Set the timer...- `% K* Z- ?5 b1 C8 X* Q5 x+ R
EventSetTimer(1);
: E1 R. S# H7 s0 o+ b//--- Set the base of the random number to have equal tests...
: k( r3 M5 [" O  u% sMathSrand(0xDEAD);
7 I8 d( O; J* G. b! zreturn(INIT_SUCCEEDED);
0 h; t6 R" D' {/ I  \! S}; t8 {% q, S" k" Q) n  |! _
//--- Since we set a timer, we need to destroy it in OnDeInit().+ ]* f! f# f9 ?. t) H! s# s: f
void OnDeinit(const int reason)8 a8 K$ s" g  Q+ l$ e; J2 m( |8 g* m* i
{+ c' d4 E$ U+ n4 j5 H/ h
EventKillTimer();
$ d! m9 a. i6 ]) ]% E$ T}
: N; k/ ], q" E/ v( k4 |  j& k//--- The OnTick function only informs us that we have a new deal
  {# ]* N3 S8 W* A. w/ I1 kvoid OnTick()
. H3 e; i( J' s9 s5 m{" L3 H* T& d; P; i/ [5 W
tem_tick = true;1 s0 b# e7 t* p: a' D
}
% x# @; y! K! i$ }+ N7 i2 s8 y//+------------------------------------------------------------------+* N1 S/ v3 Z" ~3 t) w8 Z/ G- L
//| Expert Advisor main function                                     |
% m, I* T" y& _4 W" E4 d//+------------------------------------------------------------------+
$ s) V! @; l7 q) l% jvoid OnTimer(), ]- G$ M" L; [: F) U
{
, w1 c- K: Q+ K+ K. gMqlRates cotacao[];) d# Y; o; p; L; ~
return ;7 {: y" K- N& B. R( ~
if (negocios_autorizados == false) // are we outside the trading window?" [) _! D+ d; j7 X% ~
return ;2 {) ?4 Z/ `5 D4 z
//--- We are in the trading window, try to open a new position!0 g4 P) J9 f  r6 a# Q4 L
int sorteio = MathRand();5 N9 m. X) b4 D& W0 Z4 v- f$ B
//--- Entry rule 1.1; \5 d4 w/ R1 s4 d$ U" l- E
if(sorteio == 0 || sorteio == 32767)# M- d8 N8 f! a) ?8 I3 C
return ;
- E1 p, C. X  j  S7 E3 H8 \' C0 mif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
! Y. ?0 z/ V" q{
" s& [3 @8 t  q( a) T) gnegocios.Buy(info.LotsMin(), _Symbol);
* v$ M8 ?! _: ?  [}
1 p" Q) u2 O* A; ?& q, \- Ielse // Draw rule 1.3 -- odd number - Sell
9 w4 V9 |& o2 f; u/ c9 T) G{
0 a' e. c; j- s2 @9 ~  x9 G9 ]negocios.Sell(info.LotsMin(), _Symbol);
' c5 Z2 ?1 Z4 j2 k, u}
' F0 b" ]" U: ^3 }}& l8 x% B. h4 d; K/ i
//--- Check if we have a new candlestick.... Q* S7 ?$ ~% }& q5 S5 k* W
bool tem_vela_nova(const MqlRates &rate)
6 e  {) w# i1 `& y: x$ R  D{6 K) j7 l. S2 g+ \, \! g
{
- V$ Y+ b$ z  u% O  J) M1 v; \ret = true;
& @7 I: b3 K0 ~" s" b  Z2 yclose_positions = false;
' ~# k0 o) e% D' P% r# }}
. I# k9 W- I/ Y' Z- X- Q. s5 o$ kelse" `5 r/ r& B: |; }
{
6 H6 T% y" m1 @1 ?; ]- D9 iif(mdt.hour == 16)  ^6 a) n6 Q$ ?2 g# O1 @
close_positions = (mdt.min >= 30);/ @# V$ j: \( k
}
  K9 S; m6 ^9 ?5 ~- r}
6 j/ x# q9 W6 Hreturn ret;
* e' y, Y2 j, y4 T! C}
% l; j, P6 c: r# n3 R+ h//---
% Y7 ~* W4 S* Q8 I4 x" {bool arruma_stop_em_posicoes(const MqlRates &cotacoes[]). l% u/ c0 p' X9 Q0 O, d* t  Q
{
  j: g# e. m$ `if(PositionsTotal()) // Is there a position?, G/ x, w- r6 m. T2 M! N$ A
{
' \) d8 x! i/ a4 P2 ~: \double offset[1] = { 0 };
" F" z4 N3 l! Q! |0 I$ s5 Wif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
. s+ O6 _3 c3 u& q& m' {, E&& PositionSelect(_Symbol))  // Select the existing position!
7 x! `8 B& g$ v7 m{
' w$ r7 a9 F9 a0 sENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);( S6 t; a! M/ w5 m% f
double SL = PositionGetDouble(POSITION_SL);
- d6 x& U" ^; d; D" j0 _+ jdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));% h7 j' Q+ @- R. N% s% U
if(tipo == POSITION_TYPE_BUY)
  t' r3 q$ L, f3 O$ d" L" j6 A{
9 u# g1 x9 _: i& t2 ^4 x6 `if (cotacoes[1].high > cotacoes[0].high)
4 z$ ]2 E7 }8 E; {( s{( z  E( V% d7 |, L: X& p8 l
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
. [2 ^. Q2 Z8 P- f& `; w7 W& ]2 Zinfo.NormalizePrice(sl);% x# x6 ?. ]/ {4 }
if (sl > SL)0 R# [, f" C& F* H) W
{  J7 M! a" x2 @0 l+ {2 l
negocios.PositionModify(_Symbol, sl, TP);0 }) A$ C! |2 U8 u2 ~6 y1 e
}7 l* x, ^5 _1 Y) b* E
}1 s% N3 P; h' |
}
% U6 W7 H+ m, B8 Celse // tipo == POSITION_TYPE_SELL( i0 k6 F( W4 H" M
{8 |9 t( X6 s* Z9 g9 h
if (cotacoes[1].low < cotacoes[0].low)
0 `( H( \  _# x0 ]{
6 T  w1 `$ C4 H2 t5 e+ dreturn true;+ x/ e; z( K# B, T( g' s( y6 V  j/ c
}& z3 ^, I0 U, I9 B) `# w: ?
// there was no position. m9 }" c- R, `! J9 f
return false;! f/ }- n8 U" Y4 W+ V8 a7 Z( t
}% H! I; L; v3 K2 \7 Y' k
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。  j# V  {2 |+ ?% m0 Q0 }# S! 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-1-17 19:45 , Processed in 0.533293 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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