私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA) C/ A# s) E0 G3 b
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。8 ]/ `+ h) r  O8 g7 i
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。3 R* O7 K% m7 B( r" V' I
以下是制定这些规则的代码。$ E' K9 |; o* f/ Q
//--- Indicator ATR(1) with EMA(8) used for the stop level...# \9 w: D. x& c& c: a8 l! F& x7 F, a
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
) ~5 H! u* [& Z8 c! s9 G! ]int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
2 G) o0 g$ y' J* I# w+ x. R$ J//--- Define a variable that indicates that we have a deal...
% m- x. g/ [7 N$ M* Bbool tem_tick = false;
) L7 ~9 l' Q. ^' p- t- U//--- An auxiliary variable for opening a position2 M  f" R7 X- I1 G3 H! T
#include<Trade/Trade.mqh>+ w7 c, M/ G' z5 k/ M
#include<Trade/SymbolInfo.mqh>
8 A" q% M% V* JCTrade negocios;6 I' w) u" r6 F5 G- R" I7 {& l
CSymbolInfo info;
* `, Z* L4 K/ i8 G5 F6 O# i$ B//--- Define in OnInit() the use of the timer every second/ g5 \# O7 @3 _  o; d2 Y7 c
//--- and start CTrade' U5 ]6 X, q3 I( o3 h4 m
int OnInit()/ g. ^$ y0 S( l& q8 d" u, a# t$ J
{) t& Z5 H8 t* M& w4 |& ^8 r: l, K
//--- Set the fill type to keep a pending order: ?9 D  n5 L# R: y# T4 q# G& [4 |
//--- until it is fully filled2 ~1 n+ R! D( S6 g' p5 r6 ?$ }' w
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
4 h/ _6 N2 C8 o, Z9 U//--- Leave the fixed deviation at it is not used on B3 exchange
9 E0 k9 m; T! z8 ?+ [: inegocios.SetDeviationInPoints(5);0 \' _0 W6 q0 s' p1 [% ~( s) p
//--- Define the symbol in CSymbolInfo...
+ q  I* s* G: A/ }! j4 d) P5 n8 F, vinfo.Name(_Symbol);, @" S, b$ x4 v, D  ^6 K7 e8 Y
//--- Set the timer...
' K7 N) x, Q. R1 jEventSetTimer(1);
$ x) y" ]: C5 P1 F. Y& k//--- Set the base of the random number to have equal tests...+ E3 G  e4 H+ W/ h& L
MathSrand(0xDEAD);
) t) C9 G0 `, @, [' i2 L& f4 ireturn(INIT_SUCCEEDED);' q+ g- R; P! o0 T" ~" ]7 t
}/ ?8 T) L' i' Y7 L
//--- Since we set a timer, we need to destroy it in OnDeInit().
) D% T# B* G* [0 b9 [& Q3 x# dvoid OnDeinit(const int reason)
* b# w7 V9 h( R; G8 v3 F{
' }! u3 j7 g5 h: ^' u, t2 k9 ^EventKillTimer();
3 u3 L; C$ E# ~  m% C" |}
) g- u8 ]+ m$ i//--- The OnTick function only informs us that we have a new deal
7 b% l! {5 X' Wvoid OnTick()
' t9 N5 h5 V4 b; ^4 v{0 a8 \4 O  P) m# q
tem_tick = true;
  }! [! O  S1 H}4 q2 b1 b* r2 {( W2 @
//+------------------------------------------------------------------+
9 f- p3 Z! ~3 v( V3 s3 y0 K. L//| Expert Advisor main function                                     |
7 w* Z$ `( o+ [& O1 ?//+------------------------------------------------------------------+4 |! Y9 u% j8 G7 H
void OnTimer()
; G' t( _3 C2 Z' c8 W' A1 _9 L{
% O" g; W2 C/ _9 ]  x/ q0 jMqlRates cotacao[];, Z- P( c) t/ P+ H) @
return ;
% u5 [# y& c! G% p. mif (negocios_autorizados == false) // are we outside the trading window?
( p+ p* ^+ L8 j7 C( lreturn ;
" G* Q9 P1 e9 u5 Z0 \$ R//--- We are in the trading window, try to open a new position!9 i- p$ l. u2 F0 b: q
int sorteio = MathRand();
' u+ y0 a1 F0 G( }//--- Entry rule 1.1
# Q1 N! C- O+ B6 C( P, P. V8 Wif(sorteio == 0 || sorteio == 32767)& q- j3 h- M! q# X' z
return ;( L, l8 x: Z' M
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
2 |* I* ~& Q* H/ e. F6 m{
; t$ g* @; _- n' Z- Rnegocios.Buy(info.LotsMin(), _Symbol);( j- p+ b8 x5 H* u
}' I% O3 `) X' \, x- |) x
else // Draw rule 1.3 -- odd number - Sell
; L" }& m' t- Z6 d) e6 R7 w* e/ @' w: d{2 O5 T1 q' U% x' {
negocios.Sell(info.LotsMin(), _Symbol);
) J* Z( n6 F4 r8 }# L8 x}5 h8 S3 V0 S% r6 }. ]
}0 p) C' F4 r( o! |7 W+ l# k
//--- Check if we have a new candlestick...' ]- C& M# p: @, `' s, _( W
bool tem_vela_nova(const MqlRates &rate); T  m& K( E& E
{
) |3 f8 v3 y6 |+ h7 @{1 }# A; @: D8 J" k
ret = true;
/ |) W7 s5 Y. ]' Q! B' \close_positions = false;
: J1 h) ?: B. h. w* n! z}5 O  k7 X( o: j  o- \* H
else. h9 b3 a( S' Z" S
{
4 d  V" |: k8 F) c, Nif(mdt.hour == 16)/ m! L2 X. h; m& V
close_positions = (mdt.min >= 30);3 v8 B, _" o) U# a8 D/ C* W
}: Y! D* L- ~2 ?/ u$ t
}! {8 r0 t# B% L1 F: }# L
return ret;$ t" A4 G$ I4 ?. z
}4 |8 C; _3 G  [* {) w' v. X
//---
0 @& S' g0 s& }' O: Bbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
# q3 K: L5 y2 R+ ~4 O1 K+ Z$ _1 t{
. K; f3 ~. k6 ]1 }+ E0 Y6 Hif(PositionsTotal()) // Is there a position?5 \4 r8 n: c/ h0 A- Z% E
{
) b8 H* B2 V: M" L% w& _; K. tdouble offset[1] = { 0 };4 y1 P" ~# a' l7 V0 \9 z! E3 Y
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?' s7 `; q% t# b6 w/ }& i
&& PositionSelect(_Symbol))  // Select the existing position!" p) h  B3 A( }3 g6 o1 a7 ^
{# L' L2 n% o7 C/ Y" k
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);5 J6 ^# ]" F7 N7 E& p* _
double SL = PositionGetDouble(POSITION_SL);" s- F$ Z3 E% Z# n
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
0 v6 O) I& O. d' F' L, zif(tipo == POSITION_TYPE_BUY); m/ r! b2 b' E/ F
{( s/ D% a, ^! c: w4 R$ W
if (cotacoes[1].high > cotacoes[0].high)
# ^+ l  M; e) T9 J% t{3 \% r: T! I( ]9 T; I/ Y! J+ o& Y) m) t
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
3 E+ \7 H$ }( e: m# G$ s2 [info.NormalizePrice(sl);
. Z! Q4 s4 G1 _2 c( J  _/ G& p' X; Gif (sl > SL)
( ^+ v8 o6 e+ U' p7 N7 a. ~+ |2 E{2 q# t8 C) j# E
negocios.PositionModify(_Symbol, sl, TP);, w* A8 F# M, {" \4 e$ x8 O  m
}
5 R8 t' T3 r" [}9 c7 l5 ~8 n  I
}
+ s0 O1 O# N1 G: _: delse // tipo == POSITION_TYPE_SELL
. ~! R+ B, g0 c" ?1 P, ?{
' a7 k+ |5 j8 m+ e3 L9 E- w0 o; C: `if (cotacoes[1].low < cotacoes[0].low)
/ J/ U' L% S: \  k  J{
7 Q7 K2 @; D5 O1 l( I) X' d4 Creturn true;$ B  ^( V5 M! \
}
8 S* t& ?) n  c' {( _// there was no position% E$ z6 ?6 H/ r  r
return false;
: U2 z! I- z* C* K+ G}- d6 y3 j' Y' A
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
7 N5 |2 t* e' G6 r3 v9 p; T  S* O到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-4 20:31 , Processed in 0.392156 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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