私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA9 ^; Y1 C. H2 w" h( p# G2 I
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
2 t7 o, O# n0 U0 K0 q9 l# e9 ^为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。/ v0 |* p' c) p/ ~  z
以下是制定这些规则的代码。# a  s. F) @  O# {, V
//--- Indicator ATR(1) with EMA(8) used for the stop level...
( u  s7 T; J: e6 s( L2 S6 {int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);* @% F' ?" e; ^3 x
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);$ e5 f; T& _5 p
//--- Define a variable that indicates that we have a deal...
1 Y4 k; W: M, w/ dbool tem_tick = false;
$ g$ K, R5 b) F7 {# g' c% L/ l//--- An auxiliary variable for opening a position
* b* j5 C0 }/ h7 {0 H- o; H#include<Trade/Trade.mqh>
' t+ j7 T' z" T6 O* i#include<Trade/SymbolInfo.mqh>
( j7 D9 j" o& M' v- mCTrade negocios;
: p- A7 [! w$ c; f  w) ?) CCSymbolInfo info;3 ~5 Q7 H' l4 [/ B/ P
//--- Define in OnInit() the use of the timer every second* y+ A/ k+ h  o# y' V1 F6 g
//--- and start CTrade
, A( D3 W( Y1 r8 h* V- yint OnInit()
% v$ y5 }2 B$ S{
9 o" N, }' p" ^. {+ I7 c//--- Set the fill type to keep a pending order
! ^9 D  e/ E8 l6 J, c8 e//--- until it is fully filled1 T! @/ `9 |7 V0 K0 ^6 i4 O, ]. Y
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
, c- v) ~/ }3 G( Q4 g//--- Leave the fixed deviation at it is not used on B3 exchange( s. l! ?3 S# A6 G& o6 @3 R
negocios.SetDeviationInPoints(5);. S7 g. M  g7 ^9 A$ f& V1 a6 @/ k
//--- Define the symbol in CSymbolInfo...
/ H1 _0 o7 Q8 m- A% j8 I1 [info.Name(_Symbol);4 D6 t, P9 q6 c  s1 E/ `% G
//--- Set the timer.... J2 c. F, c6 Q4 A- P- Z
EventSetTimer(1);/ U! f5 e6 L/ D
//--- Set the base of the random number to have equal tests...
1 U  r) w3 e8 i4 fMathSrand(0xDEAD);
! ]! I6 F6 X2 v; t% P# h" Vreturn(INIT_SUCCEEDED);
$ m& g0 t9 _8 W9 K. W8 [  M  O+ P5 ~1 G}
) J! H! ^) _  i8 K( w  F4 F//--- Since we set a timer, we need to destroy it in OnDeInit()." b- O$ t* b- d. c
void OnDeinit(const int reason)
- p* t! U# Z5 J- H4 ~1 S0 \{0 a. N( Z! |2 k; a' F  v
EventKillTimer();
( E, G3 E9 s/ U' N! w}
0 _% H$ I1 c) ]- n3 R* Z//--- The OnTick function only informs us that we have a new deal
# U/ u& P# I7 s; T# |( O; nvoid OnTick()
5 P* F& ^) a6 c% d{
' d! }8 P4 e# R* k) Ftem_tick = true;
( a* u/ C. h% j7 c' U; e$ l' `}
( c, P: [3 H5 y& v9 m//+------------------------------------------------------------------+
* q1 c, g! i4 W9 n7 G//| Expert Advisor main function                                     |
8 L( l/ B" Z! O$ x( D//+------------------------------------------------------------------+/ p0 J" Y' ]; k8 T' v
void OnTimer(). d- j5 G; ]1 C* P. ^
{. c' m( h& K. L  O; d' d2 j! ?
MqlRates cotacao[];
% n, I& s3 e1 K' Ureturn ;
, Q5 u4 K3 a; j. aif (negocios_autorizados == false) // are we outside the trading window?
5 C3 b# h& |' w  g% W6 Ureturn ;
$ y, E0 D  o- G# [, `//--- We are in the trading window, try to open a new position!: Y2 X$ w; B* B% c
int sorteio = MathRand();6 L) r6 C0 {8 P% _/ a7 K  W4 S
//--- Entry rule 1.1
! g. U# a/ F8 Aif(sorteio == 0 || sorteio == 32767)
2 u  D8 j; {) @return ;
( t) K* ]. K  ]2 N8 e# \if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
, a# F2 C; a9 K2 I{0 J- D0 R, [2 w; {5 ?: ?
negocios.Buy(info.LotsMin(), _Symbol);
& U, ~% g. g5 i' T}
2 y- `2 ?9 v/ c1 |% Yelse // Draw rule 1.3 -- odd number - Sell
; }; t1 n- k5 ?! D; n8 j% |( c. c- M{
  i5 O9 ], ]) o: h/ Vnegocios.Sell(info.LotsMin(), _Symbol);* M" _) q* {, H- ~& A7 J1 m
}/ s! @6 t) l% S1 f9 @, C
}) z0 W/ M" x+ X
//--- Check if we have a new candlestick...
/ D7 W& q# C1 C. T) f+ H! M- r- |bool tem_vela_nova(const MqlRates &rate)
- S! m: N, t  R- S6 f" G5 I  C{
9 x( k/ A8 s2 m6 p# l8 c{, D7 a* ?* n+ y- M; y
ret = true;! R0 t3 A, W& C- C. n' K1 w( L# Y
close_positions = false;
& Q- |0 M' `7 @, S: p}
* n# S& a4 ?. A' s/ Qelse: _. R8 U' E+ v
{
( [: Q0 p$ M( R1 X$ Z8 L9 Yif(mdt.hour == 16)- r( G' q. |! y  X( @7 P  f1 r$ H: G/ y
close_positions = (mdt.min >= 30);/ O3 y' W9 q& M9 u  \$ _2 C
}
6 F( z. V) g2 q2 U" s2 J}* I& {: u4 d$ i. ?# @2 H
return ret;
0 K# M% O5 [7 t9 _' b. s0 U* Q4 e}; k) c; g- _( X: `& M; I7 V
//---
, M0 z5 t8 t+ Xbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
3 Z& J! ~2 ?8 Z{
2 S1 k6 x, ?# v8 b# U$ V: \: Yif(PositionsTotal()) // Is there a position?8 i. p7 c" {$ W" ]
{) o: e. a6 Q' C2 J9 s
double offset[1] = { 0 };0 ?! l6 i7 r5 I3 h7 M6 O, r( |# K
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
# B7 u0 d8 S, S, j7 s) \- [&& PositionSelect(_Symbol))  // Select the existing position!: C; Q# Q! B4 g# |4 N# F
{8 O- N/ W# w5 H$ `
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);$ q7 X: T8 q; t# {- w
double SL = PositionGetDouble(POSITION_SL);
2 h0 ?6 a, ?+ S7 E/ Ddouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));+ C3 y1 ]+ B8 Q* c
if(tipo == POSITION_TYPE_BUY)
; L" H6 d3 a! D1 ^% N3 K! w- U{, Z5 _& B$ }6 d4 o( D$ B
if (cotacoes[1].high > cotacoes[0].high)5 W& X4 B+ P9 c8 o! ~5 d
{
9 V2 }1 y2 V7 W" M5 Fdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];& M0 S" |9 M5 P% a# s7 v: ?
info.NormalizePrice(sl);! z- M  g; C" ?' T; e% g/ v
if (sl > SL)
5 B" p6 z+ z9 L$ D$ P- i$ V3 k" d{- F6 T1 t1 t1 I1 V4 ~- }
negocios.PositionModify(_Symbol, sl, TP);: f# X2 l5 {0 w; h4 m; ]: h& n
}
$ o  \3 n$ b2 i( {. l}
  `: e; H4 l* X) K2 D}
; G2 }) K9 G+ V$ D# j7 g6 ielse // tipo == POSITION_TYPE_SELL( P- E. V7 k, Z6 q8 V7 z( w
{
6 r- n2 G2 Z: m4 v7 sif (cotacoes[1].low < cotacoes[0].low)
) @0 t5 n9 F/ _2 t- T+ j4 g+ C{0 W/ Y+ H$ W7 w$ ~3 W
return true;3 U, }, Z' _2 J( h" ?
}9 O# ~. J; w1 F/ d
// there was no position
$ t' q% i. i0 u' ~& h% I9 breturn false;+ J- i+ e( I% x$ t5 G9 y
}
/ v1 o! E* f  F  }% `& I我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
& X' h! i5 Y2 ~, R5 S5 @. q' D/ h! ?到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-10 05:40 , Processed in 0.403569 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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