私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA6 @8 K, V! s2 w3 _' |, p" n
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
; R$ w5 b/ v3 {- `, _. X为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。; ~2 M# c  ]$ t' I2 |& ]& S. k
以下是制定这些规则的代码。
2 W$ ]+ U6 x) d% _! Y//--- Indicator ATR(1) with EMA(8) used for the stop level...
! S, ?( ^0 @! m) r; N. y/ Gint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);8 k* d0 ~  H0 o' W* n/ C: C0 s
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);% g' P$ Q: V2 Q$ m3 |. v
//--- Define a variable that indicates that we have a deal...8 x2 I3 p1 j$ ^* b3 b
bool tem_tick = false;
7 O9 I4 d1 s5 s) M, ~4 f+ W//--- An auxiliary variable for opening a position
7 ~! i0 O( z2 J: B4 N! `#include<Trade/Trade.mqh>
' w0 H5 e+ y* ]% {9 r; e4 H#include<Trade/SymbolInfo.mqh>' N4 A, x& l" M. A  [
CTrade negocios;8 c" g6 Z, G# n* K3 O+ Z. O  h
CSymbolInfo info;5 f6 f" ~7 X, s; j
//--- Define in OnInit() the use of the timer every second4 ?- n4 E1 L1 @  N
//--- and start CTrade) s* q: n, B; v/ i) `* m
int OnInit()% y, T' H4 b+ R1 z' ?; w( p( g
{
8 ^6 R. \6 r! \, F  [5 t/ ~9 W//--- Set the fill type to keep a pending order
3 O8 Z( ]2 j/ i7 c' z//--- until it is fully filled
7 N, g1 j  O  j4 U! |. u6 o$ V/ Knegocios.SetTypeFilling(ORDER_FILLING_RETURN);) i* N6 V$ ]: ~  H. x1 M4 n7 M, W* ^
//--- Leave the fixed deviation at it is not used on B3 exchange: O& R0 N. A5 b% h& v
negocios.SetDeviationInPoints(5);
5 W6 E( ]+ P  j//--- Define the symbol in CSymbolInfo...
8 M: o3 @+ u0 ?, O. V: E( o# G; i* dinfo.Name(_Symbol);& p& q$ b$ l0 I& s
//--- Set the timer...
# E6 k' t& j+ A' ZEventSetTimer(1);
0 i3 i8 d. ?# _, a//--- Set the base of the random number to have equal tests...
2 E. V/ F9 J4 R  HMathSrand(0xDEAD);
$ G/ G, s$ a/ h/ L% Y1 Greturn(INIT_SUCCEEDED);9 o1 c3 i, D( S3 {
}
9 |$ s& X. T: U: z: a3 U//--- Since we set a timer, we need to destroy it in OnDeInit().+ Y' m: L3 [" U9 r' L
void OnDeinit(const int reason)' d5 V  m& N+ q% ^" q, i
{( H: b. A/ z# Z' O
EventKillTimer();
+ s* B  k3 u0 @+ b}, S# ?  {/ s+ k) e
//--- The OnTick function only informs us that we have a new deal
, ]" ?$ J3 q) k1 _% gvoid OnTick()
) M; z1 u. q0 w% j9 G{
( m  u- D6 Y+ W5 s6 G1 p& k) Ztem_tick = true;; Q! V9 R2 P1 L
}% n. t, L' U( x5 D4 u
//+------------------------------------------------------------------+
) u7 C8 `& `7 G* v//| Expert Advisor main function                                     |9 Z6 c8 T5 d1 I# a5 W
//+------------------------------------------------------------------+$ n# _3 ~& B7 |3 B
void OnTimer()$ s% e; t" Q9 {+ t' q8 M5 y
{0 i( z& D: D; W& H8 a7 G
MqlRates cotacao[];2 Q6 L" B5 z: t3 x, S
return ;6 l$ K0 S* R0 _/ I5 J
if (negocios_autorizados == false) // are we outside the trading window?1 [& G2 h8 o3 B6 {# I, j
return ;
6 m3 m8 K$ }6 Z  Q: E, k) h//--- We are in the trading window, try to open a new position!: _3 B* {$ X9 [! c" v' h
int sorteio = MathRand();
. R5 u. H; J" F/ Z2 Q! V) b) y//--- Entry rule 1.1
# ~6 o5 b- d' g  e  W- P& e# eif(sorteio == 0 || sorteio == 32767)
0 r/ f) ?. T' j8 a2 l& \- rreturn ;
* b. P* _* x+ d3 p& h- @3 m* y/ Zif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
  @- f8 D6 i" X  d9 Q6 l4 {4 ?{$ C# |9 R+ d3 B1 P- o0 O
negocios.Buy(info.LotsMin(), _Symbol);+ X5 x  D3 D$ s; z! m  J, ]
}
, F+ `' L0 w* n+ ]else // Draw rule 1.3 -- odd number - Sell3 H+ E9 T. e  D- Z6 H/ i2 ^
{
4 ?1 h- i2 {: B) Q0 q) K; c$ gnegocios.Sell(info.LotsMin(), _Symbol);
7 U7 ~* d2 l2 F}
* G- p& X! l& H& M0 b5 ?# K}
! E+ K* u4 C. S2 y//--- Check if we have a new candlestick...
1 X' J4 q: a! g* _1 T: mbool tem_vela_nova(const MqlRates &rate): |. j  C& }4 X6 Q# {; g. R
{# a8 A* Q' ~' @- Z1 [9 t4 Y4 v
{: b/ f$ \! B* b3 ~2 |9 Y
ret = true;
4 R6 g5 K. m) p$ J" S9 m: j5 S% Bclose_positions = false;
# C3 d1 t7 U; n* _}
8 {, N5 a! J+ C; U  xelse. f# R- [# W; Z" ^7 ~/ r6 I- d  A
{
9 E  S5 _+ m$ n7 fif(mdt.hour == 16)
) j+ u8 r& x! ?+ n1 O7 j8 b4 v, uclose_positions = (mdt.min >= 30);
$ v8 l# M, S7 Y+ G- Z}4 L. [: a3 O- C0 E+ w% p7 {1 p
}
1 k9 c9 l- z: f/ w5 j. creturn ret;
  r# j+ P" B3 `/ Z. v1 Q}
7 `9 h) h& d0 x- Q//---2 `* ~1 A# N: ~
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])- t( q+ t7 p) E" ^5 W
{
# `, ^& l$ b9 p7 H3 X5 Eif(PositionsTotal()) // Is there a position?
9 b3 `9 j" o0 Q( \+ X+ c8 v; L{" j) C4 E9 Y/ I" l4 z- G8 j# G
double offset[1] = { 0 };7 v. {* a3 d: u! q4 I
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?2 j. ]5 q+ A5 l2 s% b
&& PositionSelect(_Symbol))  // Select the existing position!
9 G( p6 h( ^6 z) f$ J2 ?{
7 h3 ^0 T% [8 m. {5 k) E! LENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
0 Z9 t5 g7 v8 u- I) b6 Jdouble SL = PositionGetDouble(POSITION_SL);, j2 U; S7 B1 |( G9 k
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));* H; {6 a  K( O. u
if(tipo == POSITION_TYPE_BUY)
1 @7 E8 {0 P' u/ f! x{3 d. N6 ~8 V0 W8 a  ^0 w1 P4 Q
if (cotacoes[1].high > cotacoes[0].high)0 H3 [+ u2 C3 p) C% x3 ?3 i
{
, U: @& K0 Y# ~5 W; p2 {6 Wdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];5 X- b: X( M: v* J7 J
info.NormalizePrice(sl);. N8 t+ k& [+ x8 E$ }% b
if (sl > SL)
2 v+ B) J( R# r' v0 L{# P2 B9 l6 ?# r3 @4 ~
negocios.PositionModify(_Symbol, sl, TP);
1 V# P, `+ C% [' {}- N* U5 y$ X+ w+ e! s
}
7 A4 D& l3 i$ V& y4 w- e}2 F4 n6 @! o3 o. u0 c" E
else // tipo == POSITION_TYPE_SELL
+ k" x5 K* Y+ i, R9 G{
9 j: t) C2 O* V9 p7 L" m4 N" k, K+ g' Fif (cotacoes[1].low < cotacoes[0].low)" D' N5 D1 f- ~0 E
{  ]1 W: {$ F  U! S
return true;
5 Q- |/ k: l/ Q}
: F. j2 w- G; _, ~6 {// there was no position
3 U, c" C4 \: L" Jreturn false;1 v  s9 J5 A% {: R0 u
}
, H2 W' q5 \3 O/ I6 X我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
' p6 i" U7 b8 i# }3 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-10 01:07 , Processed in 0.852832 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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