私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA* K5 o: p% T0 D$ A
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。1 i) a; y$ W2 J0 ?; s  M
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。! K: R6 M+ \* M0 @- P2 V0 Q
以下是制定这些规则的代码。' m1 w5 G0 L7 R$ S0 B$ P  P
//--- Indicator ATR(1) with EMA(8) used for the stop level...7 y* N! q* b& V+ \3 s% t; q
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);- Q9 Z" F) \0 x  n( _5 q5 }" x
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);9 S! A# h) I* y& U
//--- Define a variable that indicates that we have a deal...  e) x0 O2 O6 W5 r
bool tem_tick = false;
  S4 i- X% [8 O  Z- E" i8 T' z/ n//--- An auxiliary variable for opening a position
0 s& s) c7 ~* @: K9 s#include<Trade/Trade.mqh>  p: o, a& N1 T# v8 I; s
#include<Trade/SymbolInfo.mqh>! F( m& k- N5 @* P) U
CTrade negocios;
% T2 j0 I% M" |) [* n. @6 {CSymbolInfo info;
4 e$ W  N% ]. q- T# P//--- Define in OnInit() the use of the timer every second8 n; ?: l% b% [0 I" K
//--- and start CTrade: U! g7 a* l1 U2 i% N
int OnInit()
3 {6 K! Y% U9 \{( y& k6 B3 ~& P. _
//--- Set the fill type to keep a pending order8 @0 O6 T4 \$ e2 o9 Z" f( Q
//--- until it is fully filled- w1 u* n" F- t& h4 l: M6 O/ W
negocios.SetTypeFilling(ORDER_FILLING_RETURN);2 C- y3 F) K8 f9 I5 n) {8 _! x
//--- Leave the fixed deviation at it is not used on B3 exchange
: l1 O# s' ~" c* Wnegocios.SetDeviationInPoints(5);' [1 ], N: C' ^) ]! [
//--- Define the symbol in CSymbolInfo...
4 q4 B. \, a$ l! E2 {info.Name(_Symbol);0 g* t0 B7 N' n* n
//--- Set the timer...% a4 g1 N" s+ z* k
EventSetTimer(1);
5 a  I0 T( [$ D//--- Set the base of the random number to have equal tests...  X7 ~' v. r6 I) T/ H' B' y
MathSrand(0xDEAD);6 @7 |5 e, d2 f% q1 d
return(INIT_SUCCEEDED);
& ]6 A) @# }, K}. V  ?' j' E5 K1 S7 ?7 \4 J7 f
//--- Since we set a timer, we need to destroy it in OnDeInit().! a( r1 O5 i  g+ w( k" w
void OnDeinit(const int reason)
* E% j/ n' S! ?7 m4 S{
6 ]; j' R7 Z" }2 S, X1 V0 k* kEventKillTimer();
$ a, D9 u2 T, c4 A4 ^: V$ o) _}
9 l' w8 U" F/ t: n' Q; U( S* x//--- The OnTick function only informs us that we have a new deal- P0 F# a' [$ V& t
void OnTick()
1 A4 Y6 w0 X) N8 N1 m( V+ z{, k2 j' k& [4 `5 e
tem_tick = true;
: [: C9 s1 ?1 u' d}
: k, M* i. m" u2 ^* H. m//+------------------------------------------------------------------+2 b# p; [2 a! L/ ?+ x
//| Expert Advisor main function                                     |
% r/ K: ~5 G4 i7 d; g//+------------------------------------------------------------------+) M& M$ Z* @/ k0 C( B2 N
void OnTimer()0 y* Y: k: N7 g  f9 w9 _* `# d& p
{
' S# O+ p8 _4 e# T  AMqlRates cotacao[];
/ j, X$ `/ W1 O. p  k6 q! j: mreturn ;
- \: [7 W; v$ @$ qif (negocios_autorizados == false) // are we outside the trading window?/ A8 N' H* n9 J& H/ m" I* W4 \
return ;. F, i) i# V& I* A( ]! ?9 B
//--- We are in the trading window, try to open a new position!
" G$ t# n. r6 w3 hint sorteio = MathRand();
6 }& e& g# Y( n- }' `4 f& Z! L//--- Entry rule 1.19 L1 Q. R6 R1 O6 h9 A
if(sorteio == 0 || sorteio == 32767)
! u# \9 U: m& K- v$ rreturn ;9 W* n' Y- ?' [" z9 C6 D5 j
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
3 s8 u  Y" g* X* i{
) c+ P" V5 P8 Z  z& f# s" xnegocios.Buy(info.LotsMin(), _Symbol);
5 Y2 T* H) j; w+ B3 K}
' l- r* }, H; E' uelse // Draw rule 1.3 -- odd number - Sell; X  h. Q- G5 E3 f8 K
{
9 X& q( r, E, [/ Q+ v$ Ynegocios.Sell(info.LotsMin(), _Symbol);
( m& @; {6 ?; z}) C- D5 w: B7 z- U
}% B& X& ]+ \; b0 t
//--- Check if we have a new candlestick...( R5 v( p, B$ `( D+ }7 L
bool tem_vela_nova(const MqlRates &rate)
3 ?. A+ e& _) O& p{0 R0 H  h+ U& s5 T9 A5 X& h
{
2 h( ?3 q. A) S7 W" f& lret = true;
. Q* r' _: J+ ^  e! H  Uclose_positions = false;
( ^; U9 C2 r, _* n& c- E; ?}# H1 E  b9 w/ W) ]5 [
else
+ e0 y7 Y  C3 W% w; W) H{& V7 r. P1 t+ n2 i' k' \( _$ H" s+ d
if(mdt.hour == 16)
! e, j* M4 }  z  I! w& r8 Gclose_positions = (mdt.min >= 30);
' `. [! a& H) x3 O" v  d5 S& p}
! D  R) j+ }- ^0 j$ L" Q9 A}" d1 v2 S7 u  r8 ]6 Y& ^
return ret;  d3 T: i, [8 ?0 j, W* `$ N5 k! p
}2 P$ ?, {6 R$ S: Q7 l
//---+ G$ ?) T( Y: P4 m
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])- \- j. F6 \& @; J
{& X% S% e5 w: K) O
if(PositionsTotal()) // Is there a position?# E+ F2 W( ^! A0 I
{
: l, P& ~3 b, I6 ~( Z9 Cdouble offset[1] = { 0 };4 x/ w. k4 _$ i" a$ R. v
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?8 U* U. n+ c6 j- G% o6 k
&& PositionSelect(_Symbol))  // Select the existing position!
0 g2 B; x& E+ ]; J& E7 O{
3 G! A% Q5 H' b  wENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
1 l  d2 k. l+ h/ D) C' Pdouble SL = PositionGetDouble(POSITION_SL);
; P8 P1 h0 P/ r. Adouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
  |/ Z- _1 v0 V$ g: ?if(tipo == POSITION_TYPE_BUY)  o6 T# ?  D0 ^" h6 N- F  d0 M
{
  W  M& T% l$ b+ A4 u  gif (cotacoes[1].high > cotacoes[0].high)
7 D2 }* a1 \' ?2 U2 B/ X% M{- U! H( S) z' H3 s" m( I' b9 M" @9 I
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
1 m* I) Y3 B- L/ O0 t! Sinfo.NormalizePrice(sl);
  v7 b( D% ?/ O4 F% {3 ?) F4 Pif (sl > SL)2 ^- {1 ~5 y8 S  K$ _! {! k8 N
{
! ]4 n% Q+ y0 S- ~5 g& E/ }' Anegocios.PositionModify(_Symbol, sl, TP);
0 L, \2 B8 b7 i, E}
+ k! z3 B0 j% R0 i) I}
, y) F9 \) o- w2 z2 `}% g0 `6 k# N: C) ~3 F# `' w
else // tipo == POSITION_TYPE_SELL
7 D1 K: W' I  p{
' H" m3 ^! \3 G* h" U9 c9 Vif (cotacoes[1].low < cotacoes[0].low)6 k- R( p* j7 I
{! S$ o1 Z2 |( X& S* [  {& q
return true;
* K" X% c$ B8 d9 Z) a" c) E}
& E3 v( B, T0 N$ ?' M; T) }# k// there was no position
4 u3 e; l  ~+ I( g' V6 j) j8 rreturn false;& {3 q/ e8 j0 o$ c
}8 ?. ]6 o4 p3 [% D, S
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。# ?: p% j) d. J# d1 S# h, N
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-22 02:24 , Processed in 0.414037 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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