私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
5 f- f- _# G9 K9 s7 y* ]在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。5 |; A6 U% e+ p8 \: j; F. x0 d  w/ t
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。5 P) }, V0 \- u: E  ~
以下是制定这些规则的代码。0 I7 }, W" z  z; X
//--- Indicator ATR(1) with EMA(8) used for the stop level...
8 t& V! n) ]% j' B: wint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
; b# x- O5 T& `5 Kint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);7 ]/ B7 N/ z6 i$ M. Q
//--- Define a variable that indicates that we have a deal...
) ^" }% a/ a# s) b2 C; R5 ?bool tem_tick = false;; ]# z7 V* h5 u  \
//--- An auxiliary variable for opening a position
  s7 v0 m* g* j4 F2 x0 r7 ]#include<Trade/Trade.mqh>0 B0 U8 c; O# K. N
#include<Trade/SymbolInfo.mqh>
8 ~/ {, [6 r0 V& r; z) a3 FCTrade negocios;
8 L  S; S: k8 OCSymbolInfo info;% t* k, r/ S8 u: F+ q* d' z& X7 `
//--- Define in OnInit() the use of the timer every second
4 J! {' p/ A6 p/ i//--- and start CTrade
' E8 L# [( K; d8 G9 z9 H: g! [int OnInit()! X2 f/ E4 s$ P3 B6 w
{
1 T+ U* |1 r5 O//--- Set the fill type to keep a pending order
& o: l/ S1 t+ b  u//--- until it is fully filled* ?9 g0 ?+ }! A/ q4 \, d
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
' {* l$ o  `+ c: d4 ^$ v//--- Leave the fixed deviation at it is not used on B3 exchange
. |: {2 B9 V. g3 p# anegocios.SetDeviationInPoints(5);) K& v0 ^/ G+ a" n9 I
//--- Define the symbol in CSymbolInfo...
& k. R/ G+ w. g3 p6 l5 @info.Name(_Symbol);8 ?: H) R  h7 c; o8 P6 q; ?
//--- Set the timer...
# q6 k- q2 B6 A; B2 k5 WEventSetTimer(1);4 A& d4 j9 F# @( j) k
//--- Set the base of the random number to have equal tests...
7 t9 }$ \/ S" hMathSrand(0xDEAD);
  G4 [" @+ {% A5 M: f+ V- {return(INIT_SUCCEEDED);
1 u5 S4 U7 g6 |4 f- z0 i}
; K% V1 M6 m9 F( b//--- Since we set a timer, we need to destroy it in OnDeInit().
  m: C2 T4 ^/ Q- Gvoid OnDeinit(const int reason)% Y. h6 i) L% @/ z0 i
{
1 ]0 j9 P6 ]5 L* V$ H. B4 J4 TEventKillTimer();+ x7 a* c2 U4 T, t1 q
}
, k! m1 e" j  `! `8 k# s# t; Q//--- The OnTick function only informs us that we have a new deal# |# Z+ I7 l) x- I2 H! I
void OnTick()
( w5 q3 J9 y" l1 H# K, b6 _$ S{
' X# W/ B6 E& D- L8 y3 p7 w- B3 Ltem_tick = true;+ s( ^  L2 H6 [$ t
}7 ^) C. Q! S. x) N5 l
//+------------------------------------------------------------------+6 |" j) k- q8 p  N5 h, L/ D1 ^  L
//| Expert Advisor main function                                     |. A# [7 b  [( k) i# \& x
//+------------------------------------------------------------------+
2 u/ \' O# e+ ?" C6 s% ivoid OnTimer()
  H# f' r* p8 q8 F! R{
( |2 ?0 S7 `8 r# r/ tMqlRates cotacao[];
& X- h# p$ m, w8 Nreturn ;, W8 P. z$ k7 Q3 G& s( r. R
if (negocios_autorizados == false) // are we outside the trading window?
7 i& c- l' A. X9 a4 creturn ;1 k% T& ?& j- r, U
//--- We are in the trading window, try to open a new position!9 l4 e2 D6 U' S' O
int sorteio = MathRand();
6 a; U. b1 E* A/ D# g9 R  e; B( y//--- Entry rule 1.1) r) h% {  H. j! R/ p! M* e/ c
if(sorteio == 0 || sorteio == 32767)
- z7 H; C9 U- K7 u, j$ Wreturn ;
+ ?6 ?, a1 d' M0 B3 V( r% Yif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
# G$ p/ f4 _) U8 j- ~! c4 P/ o% H( o{) ^* `) d  p  m* M+ v: C
negocios.Buy(info.LotsMin(), _Symbol);
2 t% f& A' H" Y* n}
- {! z6 M) ?) J  ?# T# x% v( [: Lelse // Draw rule 1.3 -- odd number - Sell; J) q6 x4 A8 k# C! l) k) h& H
{. U! _7 w4 _8 W( ?) P& Z4 d- [
negocios.Sell(info.LotsMin(), _Symbol);
% L1 s) ^5 _! K. @# L}
/ ^& |' J! W) {4 x% m, }* x}9 s5 J, i- h: J# g8 j
//--- Check if we have a new candlestick...- G- n" a) _# n% j
bool tem_vela_nova(const MqlRates &rate)5 f! O5 s9 J3 X6 O! U, A
{6 A) L* g) v1 \- u  O
{( E; g6 q( J3 G9 t
ret = true;' ~- t# U3 ]* k
close_positions = false;
2 Y, u& g* L- o! m+ @4 s}! ^7 N) b$ }% n" Y7 J
else+ ]& `  M  J2 ~+ e
{5 W5 e0 E" n/ h+ H
if(mdt.hour == 16)
: s2 z' t0 d4 P' g0 jclose_positions = (mdt.min >= 30);
; U' o' q% e& U6 f8 u}
; y# M  P: B" F( p}
* L; x( n; m$ h: ]1 ureturn ret;' z/ K9 \( R3 d$ j( Q& A0 f
}
3 b2 u4 Y; e$ S/ S; j8 t//---
4 T& B5 y% E# T" N6 Fbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
' [. d5 P! B# u0 d7 h{% B9 P( {3 G; n- O& R% j1 O
if(PositionsTotal()) // Is there a position?
- I: T; w* c9 s4 m: D8 v0 Y7 q{- U7 e" k$ M- `: V9 o$ N
double offset[1] = { 0 };/ n4 m9 Z) ?0 N( K" j( @- i
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?7 ^- M! Q, y/ S1 X$ p/ i9 Q: k& a
&& PositionSelect(_Symbol))  // Select the existing position!" y7 D$ `+ u# F8 E2 K
{
2 x2 `. C$ T0 O5 C& i& qENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
+ F9 M/ s6 e" b# R! _8 }double SL = PositionGetDouble(POSITION_SL);
  y8 g. Q4 F6 l' R1 w, {+ \9 sdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));! \+ k9 g7 ]( S( x4 f8 ]" ~
if(tipo == POSITION_TYPE_BUY)
9 p% E' ]( N! {{5 y$ t6 f: E/ D0 i; o
if (cotacoes[1].high > cotacoes[0].high)- q3 z, i2 y5 S( h0 a
{. @3 q0 C" f8 T
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];1 M3 n( b6 ?/ ^
info.NormalizePrice(sl);; U2 _, o( _) {
if (sl > SL)
& t% e1 S( W4 p: Z{
8 q2 L. R3 z+ M% {, h* ^3 s7 Onegocios.PositionModify(_Symbol, sl, TP);
0 i0 M: S1 [2 h4 K# F+ x7 a}
# n& \; w; M) \7 ?, D( N8 T$ j2 v}& D: ^8 s% u5 k( l$ P/ `+ b# C
}% M0 i# U4 z9 X$ `
else // tipo == POSITION_TYPE_SELL0 G. e" a2 [- n1 L+ v, y
{
, W' @  N6 i! G" Pif (cotacoes[1].low < cotacoes[0].low)) p8 [2 _! R% ~  z
{' m& j$ W% y6 ^, L2 Z3 `- C" a' z. A
return true;" M% P0 c9 z* {! R5 f5 O4 k
}1 N1 p7 h+ B$ T5 G2 m& W! \
// there was no position
' Y: n9 e* ~- S* M& Vreturn false;. q* S) Y) ~3 F. m
}
- I* J& p! n4 A0 e' g& P3 [# a我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
& c8 d# r- U6 Z1 O6 E% p" Z0 c6 H5 W到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-25 17:38 , Processed in 2.706179 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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