私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA5 ]% Q" o: w, i5 q  c* h
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
! h7 G: ^4 w& j! `- }' N为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。# ]: e$ Y3 V6 u) Z* r
以下是制定这些规则的代码。
5 C7 P9 o7 c, @; i2 }//--- Indicator ATR(1) with EMA(8) used for the stop level...4 b" e# N9 ^; G( p+ x
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
; v0 Q: a% i& b0 rint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);3 B" x* A3 |% @$ @
//--- Define a variable that indicates that we have a deal...
3 N% e$ c% }9 q7 w; n  J" tbool tem_tick = false;
  r, a; s( d9 N//--- An auxiliary variable for opening a position
, q& I3 P1 Q# m" N5 b- k#include<Trade/Trade.mqh>; P3 @9 P, _4 y) s1 H! [* A/ r& r
#include<Trade/SymbolInfo.mqh>  }, J7 N9 L: t* }! U6 A2 T
CTrade negocios;! a5 R! l6 H. d$ ]# Y
CSymbolInfo info;- E) t: ~2 X, o
//--- Define in OnInit() the use of the timer every second9 A& n* N+ \$ o% c; ~
//--- and start CTrade
. k1 D( B4 p! V  H3 S! Eint OnInit()
4 P' L! w" ?7 g7 M" q5 n/ D1 t6 p{( l: [/ H  J+ W3 T* W5 @- x
//--- Set the fill type to keep a pending order" E/ M/ U9 v" A8 R" m
//--- until it is fully filled* L  H, b& q" o( K0 l; j+ T3 j( Z0 U
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
# B) V) n# R7 b1 Q6 t//--- Leave the fixed deviation at it is not used on B3 exchange
) E! X) e- t7 t. C1 X. N( K# ~negocios.SetDeviationInPoints(5);8 x9 T7 j  M: K4 ^
//--- Define the symbol in CSymbolInfo...& U: z" E  Z; n+ j: g. [
info.Name(_Symbol);
) @0 }5 W; ]" p5 ~! V7 [5 ?) g7 k//--- Set the timer...
4 E- z  |  L/ F1 uEventSetTimer(1);$ M. J1 G  t1 P" G. C
//--- Set the base of the random number to have equal tests...7 @& I) a' }) i; y/ j  z
MathSrand(0xDEAD);- L9 t- [1 |# ]6 A% x
return(INIT_SUCCEEDED);
: |  z9 X; W' d}& ]8 F0 z/ Y  k
//--- Since we set a timer, we need to destroy it in OnDeInit()." w6 H0 l, g1 k1 V6 e
void OnDeinit(const int reason)
0 ~* l% S2 H# G{$ A0 J1 Q5 }* L. @
EventKillTimer();
5 n5 w; ?# H+ r5 R- u1 p: K}5 I9 {' N/ Z. X
//--- The OnTick function only informs us that we have a new deal
* T" K& r& c: k2 I; G4 v3 yvoid OnTick()  F6 j% z1 S& v9 m$ d" O. \
{
7 _, ]8 ]6 q2 g$ l5 Btem_tick = true;
9 I! V5 I$ Z2 l7 p& H}
3 V4 X( Q2 r) ?! x; q//+------------------------------------------------------------------+; c* v! Z( B+ E
//| Expert Advisor main function                                     |
: M( b6 ?! Z  V4 I# |. X0 k1 Q$ E% m//+------------------------------------------------------------------+
2 V) q! \: u0 [; p6 x& Uvoid OnTimer()
8 ^" y: J% o  `: G. r# N, J# p{
' O! A8 r/ S/ _9 l2 U' iMqlRates cotacao[];4 ?: e# V% \- z: W; b; L8 n
return ;
5 G: u0 I( T/ D6 v+ hif (negocios_autorizados == false) // are we outside the trading window?
! w% p, b  r  A& r4 ~0 V5 X# M! vreturn ;
' b. ^, ~: I0 A0 `- Y3 P//--- We are in the trading window, try to open a new position!2 z3 Y$ I4 e- e: i
int sorteio = MathRand();
. L# k$ l+ {6 h0 B7 r( r6 z//--- Entry rule 1.1* ~/ d) T) s. }0 B+ R3 \- L0 F
if(sorteio == 0 || sorteio == 32767), N) K- L) X) r1 J
return ;
+ ]1 T. `4 }& L* O: h, Lif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy# x0 @, h! \+ `$ x3 m3 e5 a1 p
{
9 v# _: J  Y- p3 Inegocios.Buy(info.LotsMin(), _Symbol);# D4 `" h' S" `3 ~- L
}$ E6 W5 S7 I% A; }* b$ F! Q
else // Draw rule 1.3 -- odd number - Sell
4 p' y9 ^, e& q8 R{
2 Z# l( [% ]( y! r: Wnegocios.Sell(info.LotsMin(), _Symbol);
7 B) O; f1 W0 R}
1 l! B2 @2 m3 P" j2 F. S6 t% ]}
: _( D  l- Y  z, e% _) u//--- Check if we have a new candlestick...
8 I( `! D8 j- a& |# vbool tem_vela_nova(const MqlRates &rate)7 `! G& Z$ M, N9 Q
{  y5 z2 W8 F: k. C* h& j
{
- x; l3 e/ |6 Y9 hret = true;) Q+ e+ O2 z/ P
close_positions = false;4 y' R/ E( y( r% x8 u
}' y8 [% a5 g/ W: |7 \
else
$ V# E5 V4 @: L{
# h$ f+ c3 E; u% {" Mif(mdt.hour == 16)+ s' U9 t' D/ S- C% Z
close_positions = (mdt.min >= 30);
/ _7 [; Y& d! c- y3 r& e* P# A: S}
1 P3 c5 |7 l/ L- v4 t}
/ R8 b/ S+ n, i( c1 wreturn ret;
" h$ O8 P, p6 m. L( E- V}
! \+ }3 d6 o  }- t; @3 J- }# ^//---& d5 ]& I5 W' P! M' M8 `1 d
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])6 F( Z$ a, }3 B6 m# ^1 E
{
  x- ?( X/ @/ L/ fif(PositionsTotal()) // Is there a position?
6 I6 S$ g% t9 ?  s/ O{1 [/ \8 w+ V2 f9 z' A
double offset[1] = { 0 };
1 _$ o+ K' M9 p2 r1 P: \9 c* |  cif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
+ {5 q/ b6 ?3 x4 t, x5 B&& PositionSelect(_Symbol))  // Select the existing position!
4 I: K/ Z' r6 X% O7 P+ r{
6 v8 n8 N- j, I7 [6 y* f' IENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);* b0 T& f6 p1 Y6 U4 M7 u' @9 a- u' H
double SL = PositionGetDouble(POSITION_SL);
( Y: v9 D0 f/ q) v/ P6 h( ?# n9 Pdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
3 ^) Y, ]( v- f3 y# q1 t7 Kif(tipo == POSITION_TYPE_BUY)( B4 s- r1 ?% m
{
& }- x/ ?! h! \/ ]if (cotacoes[1].high > cotacoes[0].high)  e' J; D4 W9 j. y9 y
{
( b  J6 q+ s& B0 Z1 [4 _+ t6 L6 C7 R9 qdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];. N& |5 Z* b: z5 k
info.NormalizePrice(sl);
% N3 ?8 t8 q) Xif (sl > SL)
; d/ e# `$ w4 N8 u9 _6 y$ o{
1 S- A$ F) X# X: ]0 b# nnegocios.PositionModify(_Symbol, sl, TP);6 }* K7 U1 x" `" s. I. `; C: K8 p
}
0 B% b+ p# `% R( @* D' A}
/ S* T' N- z( L0 t1 M; o}6 F* O% M/ Q% u8 }9 `
else // tipo == POSITION_TYPE_SELL1 E5 a8 }" W# L4 k$ V+ B% W# K
{
+ R: t0 L/ j* v+ ?; r; B+ F6 G( [if (cotacoes[1].low < cotacoes[0].low)
$ K* b, D+ e: D  I! Q{/ U# w% l* v) ^# O: v
return true;; J2 K9 j8 K6 h; {* A
}$ H. l: s" z/ c& ^3 q3 i6 J; v
// there was no position
1 c' R  o$ j- i& |! u- X2 t& Oreturn false;
) T  @  R! x: K' w7 g}  E# |6 u- o' B, H8 w# W* j
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。1 N8 P3 s) H( R& D
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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 10:17 , Processed in 0.451448 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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