私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA) L$ d& k' f* ]3 Q9 m( Q5 Y7 f
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
$ f% p( L3 Y# {2 }! s# c为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
* g- W1 i8 q! p2 d, u2 e以下是制定这些规则的代码。+ I; @$ H5 P9 {% [9 s$ h
//--- Indicator ATR(1) with EMA(8) used for the stop level...4 o" W) S7 a, P8 m! v# Q( T
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);3 W& G$ }; S1 x- Q" s
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
" {: k# |7 I% b. A% q//--- Define a variable that indicates that we have a deal...* B9 v8 f2 G' a. d( [; t
bool tem_tick = false;
0 l1 r$ S9 O& C9 ?) [" u# \//--- An auxiliary variable for opening a position/ Y! e2 y/ N- z' @2 K4 _
#include<Trade/Trade.mqh>
& k  v/ X, J1 X" @#include<Trade/SymbolInfo.mqh>
' L$ o7 p- r( i7 [CTrade negocios;; e0 W: @9 |0 h
CSymbolInfo info;
) d  t- e$ X- Z0 D' ~7 E//--- Define in OnInit() the use of the timer every second
5 r. E9 `0 Z9 s4 h8 s: P3 Y7 [//--- and start CTrade& T8 R) i' h7 p
int OnInit()
: X" M7 b3 k& S8 F7 O* A3 K5 u$ C{2 r, y# g% }% D% ~
//--- Set the fill type to keep a pending order0 l0 E! A- c1 F' N
//--- until it is fully filled
2 y3 u5 x+ F' g/ m1 ^/ lnegocios.SetTypeFilling(ORDER_FILLING_RETURN);* d3 m" L. @: t7 c, \# {
//--- Leave the fixed deviation at it is not used on B3 exchange
4 e' e3 i$ M" V( I0 Q0 Z! Xnegocios.SetDeviationInPoints(5);9 ~9 U! z6 S2 z, C* Q
//--- Define the symbol in CSymbolInfo...# V1 D0 ^% s- V: F# ~8 A& V
info.Name(_Symbol);% k1 h$ ~% R- V# ?
//--- Set the timer...0 U( z. g" N' T4 z
EventSetTimer(1);
" \3 k4 Q4 P1 E8 ^* L  N8 B4 {//--- Set the base of the random number to have equal tests...6 {( `* Q/ N, L; v. e' w" t, ?5 e
MathSrand(0xDEAD);+ J0 m4 {. h. b& N' U8 c
return(INIT_SUCCEEDED);
. X0 r! @/ L; S4 i# ?" C* E}' v' D: g) p, M% W) L* k
//--- Since we set a timer, we need to destroy it in OnDeInit().
$ v3 c3 i1 o/ }1 D% g& q: |, \void OnDeinit(const int reason)6 N3 n1 h5 O0 t4 m6 \
{$ P; Q) |; t9 V/ A
EventKillTimer();
3 w6 |( T- Z9 r% Z. \1 e}$ G2 f# d9 h$ A- G9 b
//--- The OnTick function only informs us that we have a new deal/ v$ ^; z8 U' A0 o5 K
void OnTick()
1 Q2 h% o" r) q) W{
9 B6 K) M. x, L" T$ L/ ]5 Q& qtem_tick = true;4 G2 }" X  M1 h' p. ^2 X! c' j# Y
}- G! `8 w" J0 J; q1 b8 W
//+------------------------------------------------------------------+
! \- V* X( T. O" \, R" l//| Expert Advisor main function                                     |
" i! w$ t. F# n5 X7 t# p//+------------------------------------------------------------------+9 H8 F, \( }( A6 ?5 d$ g
void OnTimer()
$ _' x- r4 I3 }+ a6 R6 k{. V/ d" ]1 b# q; g, V
MqlRates cotacao[];+ `- F" Z) |" s, j/ S5 r% Q! }
return ;9 \3 ?6 g9 s; W4 ~
if (negocios_autorizados == false) // are we outside the trading window?' T' y7 H, P  [& e7 c1 B- S
return ;7 N% J( f$ d4 Y
//--- We are in the trading window, try to open a new position!8 A1 ~' @; Y" r% S2 P$ M4 w9 ], T& i8 d" o3 l
int sorteio = MathRand();4 g0 R+ @3 g, g7 m
//--- Entry rule 1.1' Q3 O9 P3 s' Y: }$ N
if(sorteio == 0 || sorteio == 32767)
+ Q) U4 Y# C& g2 p/ {return ;
) H  G8 s  V: B1 T8 ~* U1 Rif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy" V9 U6 a7 W+ e7 @  _5 h
{6 p. j* z! E9 c( x* p) e0 f
negocios.Buy(info.LotsMin(), _Symbol);5 L4 s& `2 e/ D7 Q- ?" q6 W4 O, U, o
}  c5 I. \- ?5 l( u! V
else // Draw rule 1.3 -- odd number - Sell( o0 n" w- e* M+ q1 d9 z) R
{
7 I+ I4 j' {! ^. q# N' tnegocios.Sell(info.LotsMin(), _Symbol);' }  V9 c' _$ Z" }2 a
}/ [& m& \9 Z( L5 w! n* Y& @/ |
}
1 o# ^0 {* `7 B& P//--- Check if we have a new candlestick...
! ?; [( k) P4 t7 M+ ~& ^0 n5 _5 A  hbool tem_vela_nova(const MqlRates &rate)
4 D# b6 a' Q6 n7 \4 s9 v6 [{
( N+ E. m0 D/ _) v' x{
2 I& Q- N7 H$ g" i+ Y  j- uret = true;8 o, Z' d( p5 y% i! F3 a9 E
close_positions = false;/ p: \) d# P; r& `
}
3 b  o' T7 L/ I# G6 aelse) u# `6 \( [7 W7 R! d
{1 ^% M& Z0 p% c- _) e) O- o0 g
if(mdt.hour == 16)5 y1 `. s9 B% f7 W+ _8 |
close_positions = (mdt.min >= 30);6 ~# ]% }, u* {2 B& p* Z1 ~
}% e. C! L' Y) U% E. F" {8 ^. @
}
" ]/ o/ v- H4 ]return ret;1 A3 I3 v% f5 k* [/ c% p
}+ Z& d' s8 t* ?4 x! V: d% z
//---, b9 J" r7 `0 r& U, E. _3 \
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])5 m1 i. H  e. ~8 ]0 A1 F5 H# }
{: _# z! y, }! W
if(PositionsTotal()) // Is there a position?) W5 }: `6 m6 @7 C; t+ g7 W& W
{
9 H( k0 o- |1 o; Q& N& sdouble offset[1] = { 0 };
3 H$ T/ `3 I- G9 S' }if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
6 v8 f+ `8 ?* U&& PositionSelect(_Symbol))  // Select the existing position!
  c( N/ s0 r7 l7 l. Y: [( e{" \, w! V3 I% k9 ~8 ?
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);. i( o* f6 G& x6 |$ q1 O8 Y
double SL = PositionGetDouble(POSITION_SL);) t9 L4 [, G+ ]3 d
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
# @; O9 K) p* N7 Pif(tipo == POSITION_TYPE_BUY)
; x$ f7 V* f+ u2 P7 w* q{
2 o8 j& V0 f4 qif (cotacoes[1].high > cotacoes[0].high), X0 x' r# o0 u# G% K; X! v; L; k5 _
{
  A* \$ r) I1 Odouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
9 X3 \4 j7 Q0 ?0 ^' {info.NormalizePrice(sl);
5 T' y* @4 v0 Q: Qif (sl > SL)
- j7 X1 g& s# B" w7 ?/ q; J" ]{
# B' q3 W( F) Unegocios.PositionModify(_Symbol, sl, TP);4 O, Z1 ?' O  X4 Y0 @$ z. |
}
% Z/ U1 f- a0 ^}
) O& f* ?; ^7 `}
- f) \! |0 H8 v8 Y8 M2 i0 r( ]5 Felse // tipo == POSITION_TYPE_SELL  a/ X; M! n3 ?7 `7 k: Z& s& c6 \' `
{# \) ^; j  K( z4 x, I6 E
if (cotacoes[1].low < cotacoes[0].low)# i3 c6 {6 `' e+ n& G" e; c
{
1 E$ ~/ d3 B  W6 a; Q( p: sreturn true;
' V0 G; m( A* |. e! X1 U}; \7 X9 M* a, Z7 d3 m! X0 K0 Q
// there was no position6 Y/ I1 D' k3 [( B  K' W
return false;
  d3 w" x" K5 m- A. `* g}# V8 p1 {6 Y. y0 r8 I$ S! G$ L2 p. C
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
) v* w9 Z+ p9 C& n3 W. L到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-2 12:29 , Processed in 0.433250 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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