私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA  z0 f" U9 V5 B" K& e$ _- U
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。* K4 z' \$ s8 ^+ j' }5 }
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
9 O0 p# a3 q% ^: Q! W& I" H以下是制定这些规则的代码。
* S2 l' W. o% P* n//--- Indicator ATR(1) with EMA(8) used for the stop level...
, _& Q! M' Z1 ^. c* ]( V  ^1 ~/ t/ Sint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
& \- K& Y# A1 T0 ^# rint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);( d: A4 R( B6 E8 k- F# ~9 A
//--- Define a variable that indicates that we have a deal...
- w- X5 |) a3 Y* E1 Bbool tem_tick = false;
5 R: x8 z2 W' h6 \2 m% H//--- An auxiliary variable for opening a position
! u0 \/ a* N  G; d3 c#include<Trade/Trade.mqh>. p- m2 Y% m5 H6 S3 s& M2 ~
#include<Trade/SymbolInfo.mqh>1 V3 M4 Z: M4 u3 O- {0 _
CTrade negocios;" a# l2 r1 e9 h5 @! ~9 f
CSymbolInfo info;
; P, g0 ^) k" n1 K! T: {//--- Define in OnInit() the use of the timer every second
7 l: \6 ]5 e5 S$ K//--- and start CTrade6 _' R6 u8 N" U0 Z6 A: l! g9 U1 c. G
int OnInit()
! A8 j# {& P9 d& j{
& k# W6 c5 ~" L0 O//--- Set the fill type to keep a pending order
* Q# {  b; A8 W! }& [7 f//--- until it is fully filled
" [% T1 f. y5 {, p2 z8 Y' y0 S4 @negocios.SetTypeFilling(ORDER_FILLING_RETURN);& }) g# N' b( ?8 n. b" u
//--- Leave the fixed deviation at it is not used on B3 exchange
: o+ i1 J# \: u0 V9 }negocios.SetDeviationInPoints(5);
" @' i7 j) `- _//--- Define the symbol in CSymbolInfo...3 [/ q! x, k0 r; v2 J2 w! J# d% K# T
info.Name(_Symbol);
- Q. v9 J4 M. F. v* j//--- Set the timer...
/ A; K; u& q8 v5 @. V9 M- `) DEventSetTimer(1);6 }' y7 G; s  ~4 f2 |
//--- Set the base of the random number to have equal tests...  @+ c, x: Q: W3 E6 B4 K
MathSrand(0xDEAD);4 I' N0 ~# C: j; Z  Z. Z
return(INIT_SUCCEEDED);' k4 e+ }4 s! F0 R) c
}
5 I7 [  n  Y3 H$ _//--- Since we set a timer, we need to destroy it in OnDeInit().9 y$ g6 e( I# M; w+ w. f* A/ j
void OnDeinit(const int reason)# D) e- s( o# \! r$ ]$ M
{1 a# v$ T/ x0 ]4 T" S' X' f
EventKillTimer();
  _/ e, d7 f: M+ E}4 z  \; D, K& {8 D
//--- The OnTick function only informs us that we have a new deal
, }# }; V7 F7 \, C1 Gvoid OnTick()
; E3 S: ?. M' F$ ^) r- }% n{
7 _+ ~  Y1 ]# S# v( W% K6 h7 P; ttem_tick = true;' {" S7 l4 I, W# ?, U" B3 p# J
}
. O( W; a4 S" \//+------------------------------------------------------------------+
8 E9 r5 Q) W$ s  o# a6 f% a& Z! ~% g//| Expert Advisor main function                                     |* Z& B. B: B  z; S
//+------------------------------------------------------------------+
. e) Q. r% r  ?) k' _void OnTimer()
. d1 k: l, _7 g2 s5 Z{
  S' I* q8 ~0 \6 N% x5 zMqlRates cotacao[];# N( Y; D1 q3 ?9 r5 O
return ;
5 j8 t9 `: w+ F7 n$ I3 yif (negocios_autorizados == false) // are we outside the trading window?" N0 ~. _* |" a& B
return ;
) Q3 I  Q5 U3 V5 A$ D8 ]4 P5 h' {( m//--- We are in the trading window, try to open a new position!& [+ X9 b2 G9 u5 x9 y
int sorteio = MathRand();0 P9 H9 y: d! b/ ?7 O8 R
//--- Entry rule 1.1
2 E8 V- I+ E' H$ t: ^9 [4 r# Aif(sorteio == 0 || sorteio == 32767)( a" A( s, P6 ^7 }: H! ], p$ H
return ;
- W  P5 n% s) I# ?9 ?if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy4 R/ U% I" H' Z0 N9 u+ Z( ^4 l
{
3 I8 V# y& k+ t8 p: Z+ ^  anegocios.Buy(info.LotsMin(), _Symbol);9 r% @' b7 A# C2 b) J. z
}
" Q4 u% P6 a8 ^4 D: Yelse // Draw rule 1.3 -- odd number - Sell, e0 j; |* C* V' Q) p  M3 e+ |1 {
{
# r; [. Y4 z2 B' N0 y2 wnegocios.Sell(info.LotsMin(), _Symbol);
4 l8 t! E* Z4 T$ v/ q6 A}
3 ?  v( n$ z' f6 z' z}$ z! K7 U$ ^# k  s0 ~* f; A
//--- Check if we have a new candlestick...$ u8 e2 U! F8 ^, K. M9 U# n
bool tem_vela_nova(const MqlRates &rate)0 Y$ s% d% t0 z) h0 G
{
4 O" i( k3 i3 t5 @{
! O3 `+ K7 i3 J% p* Tret = true;5 R4 n2 f* n, i
close_positions = false;
) K7 X7 l) f, H}
9 Q, T& s# K2 U4 f( d2 b0 l( }else
+ t+ q# R  o4 e5 \1 i$ }{
: X9 R: w9 [. D) N6 ]if(mdt.hour == 16)
# D+ u' _- U; r0 K; Uclose_positions = (mdt.min >= 30);
  r7 x5 ^" t) J0 @* h  r( n. X}3 s2 I1 Q5 m5 {
}0 ], z7 o* b1 V6 z
return ret;
! h& R  \1 H' R" ?" C" e6 l/ |: \}3 C% r$ i2 l5 D' z6 R( \! v* }2 M
//---6 A. M6 y% M) o) s: T8 g, |
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])! z' }% R& H6 M
{$ c; W+ L2 l9 {( ~
if(PositionsTotal()) // Is there a position?* r% f$ `9 w" y. W: i
{
( M1 F) U! t4 v' L3 k2 Wdouble offset[1] = { 0 };" C) [' z6 I1 Y6 O6 m
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?0 q* f& q  ^, \0 k7 }2 C7 S* G
&& PositionSelect(_Symbol))  // Select the existing position!8 S, C+ w6 u3 g1 y' n# e) E
{, G$ N1 i/ G) y
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);' D! F$ N/ q6 F3 R7 _) e0 t7 ~
double SL = PositionGetDouble(POSITION_SL);. C+ g" u8 b+ J
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));5 V2 l5 i$ b) D
if(tipo == POSITION_TYPE_BUY)) s- y( S2 J- \
{1 j# O: B) A8 P8 Q% j, y
if (cotacoes[1].high > cotacoes[0].high)) [0 |  V8 J  X
{
2 j0 _- F% J; c! Ndouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];! [& Z# A: z' E
info.NormalizePrice(sl);+ g  K4 C' T+ L) O
if (sl > SL)
3 [( K" |8 W7 _0 [2 u, ^: _2 {7 f" ]{- q3 D; X9 Y0 P0 N- K, |( C
negocios.PositionModify(_Symbol, sl, TP);/ f; o3 o0 ?1 V/ d- S: z( R6 ~, J1 M
}
# g1 s6 O; ^8 [/ S7 w: b}
% [$ j; S( a! u* A}
; t8 v5 ^" ^. I* {else // tipo == POSITION_TYPE_SELL
8 x/ u: @; q8 J{; Y9 j% D) y3 H# s4 R2 F& C
if (cotacoes[1].low < cotacoes[0].low)
0 l, O) k3 {6 F! F/ H% U{
0 x1 e- _2 ~* f- }8 r# h3 [return true;
- @" {4 N; @/ w9 I2 [" b  R3 P* K/ F}
6 O- v2 r. j) e% y. L// there was no position
# g, c: I9 D9 p) G" y- L* l! kreturn false;! w- H+ d  a( K8 B
}
8 W/ `+ [! b2 W. Y我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
9 @" P5 Q( B% T2 p到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-25 09:54 , Processed in 0.458655 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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