私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA) }1 I! y. l5 J  f8 \: G/ F
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
3 m+ D3 K; e2 T为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。) q/ a7 w8 V" ]+ w$ x6 [- w
以下是制定这些规则的代码。% M8 }( r) P! [6 }2 w0 P/ z" t2 M
//--- Indicator ATR(1) with EMA(8) used for the stop level.../ H; x' V( Q, e+ ?/ `& w! M
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
+ x, W+ g! v# ~. a, K& D0 d+ Qint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);: K- r) A. s' R; H3 w; ^% r' c" p2 A
//--- Define a variable that indicates that we have a deal...
7 ~! Z0 o( y% J3 O# o. bbool tem_tick = false;
8 a: S+ M8 D9 [' [6 @% E2 x2 S8 l//--- An auxiliary variable for opening a position
) ~8 D4 s" h; @! l#include<Trade/Trade.mqh>
+ n% W1 \. @$ s% w* x* k7 |#include<Trade/SymbolInfo.mqh>
% L9 j4 r- e( ]8 l/ [CTrade negocios;& U6 |) n0 q( P+ X
CSymbolInfo info;
/ e! r' q1 I2 x& R  b" Y//--- Define in OnInit() the use of the timer every second
# j0 g. q# y: G, n# b! S8 G  A//--- and start CTrade( g2 X3 @% z+ o4 r& F2 V5 r
int OnInit()
% S) L" W0 {* P# Q( @8 l: G5 L- A$ |{
1 M( e. ^! o8 o//--- Set the fill type to keep a pending order
$ S0 X) ?  w0 i//--- until it is fully filled
2 q9 P, h: ~% M- o' u7 P6 fnegocios.SetTypeFilling(ORDER_FILLING_RETURN);+ c( o' l  o# c- p  u/ ]
//--- Leave the fixed deviation at it is not used on B3 exchange& l$ g$ f: t& K. u9 D7 l
negocios.SetDeviationInPoints(5);- r& Y: F/ N$ i1 U2 N# L8 B. s7 c
//--- Define the symbol in CSymbolInfo...
. [$ L; }! o4 yinfo.Name(_Symbol);
/ }8 I  M8 Y+ ]+ R( O& H2 b. x, J//--- Set the timer...3 ?! u' ~) N% E8 p$ H0 J, r
EventSetTimer(1);
$ }9 }, D5 z. x9 p" H//--- Set the base of the random number to have equal tests...) D' ^+ @+ ^( t7 l: T9 g1 V" o
MathSrand(0xDEAD);9 ]2 L% c/ t( N* ]- A8 l, S  L
return(INIT_SUCCEEDED);
0 P& q6 n6 w  w# V}- u# K, ~4 l7 n6 V
//--- Since we set a timer, we need to destroy it in OnDeInit().* ?8 U9 I. T. c8 g/ f
void OnDeinit(const int reason)* B0 {+ N# H+ b% b
{
8 k, M( V( C  {8 IEventKillTimer();8 c* q" _1 d1 l1 p$ t) B
}
( r$ ]* q$ S  L9 j//--- The OnTick function only informs us that we have a new deal
. U1 W7 G2 O( `3 s/ S; w4 ]$ cvoid OnTick()
: Y1 ^" W) Z: r5 ]5 d' p% V5 @7 O  {$ u{8 v) u. [. T/ r
tem_tick = true;$ H! b( C1 _' x; j6 P1 {" W! U
}
  A2 c- `$ n# C9 Z1 b8 o) h//+------------------------------------------------------------------+4 O7 y' K" q) t( W; h- H7 P0 w9 ~
//| Expert Advisor main function                                     |
! ~$ _& T  P) b//+------------------------------------------------------------------+9 T2 I5 V; T. P' U
void OnTimer()0 N5 \) @" x% i+ Z" ^  ]
{
1 t; B7 e/ W. g; tMqlRates cotacao[];
% i  Q$ ^) y5 Y9 M2 N4 \3 T4 Preturn ;
) p+ C2 g  L: Qif (negocios_autorizados == false) // are we outside the trading window?
! P( `8 k6 ~0 t6 lreturn ;
9 e3 c/ @; z4 K- d" f//--- We are in the trading window, try to open a new position!1 ~) O/ M3 M# D5 w3 c3 S% w
int sorteio = MathRand();& D2 ~; S' @0 K2 d- v
//--- Entry rule 1.1  a9 R. i! J7 M8 C$ z8 V$ x/ J* k7 W
if(sorteio == 0 || sorteio == 32767): d! [$ @1 \8 G8 z
return ;
9 ?9 i9 C. F. _$ Y' d0 a& aif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
2 U1 E9 s. c7 N7 {4 k- H{& c2 I7 _( y$ u
negocios.Buy(info.LotsMin(), _Symbol);& L& S$ M( t% |
}
, G; y# ], P3 j) x. f) F9 d7 Telse // Draw rule 1.3 -- odd number - Sell
7 Q6 R3 |0 |, K9 y2 V{" K8 {5 ?# W+ A" {$ B
negocios.Sell(info.LotsMin(), _Symbol);& C. e  k4 L! k1 F) b" Z
}) H5 j; K! ^$ a( R' v7 Q0 B& K: o
}7 n' ^, h0 y- ]1 m* Q& F$ [: ~. t9 g
//--- Check if we have a new candlestick...
5 \. I1 l8 n) i9 I" r* Z4 xbool tem_vela_nova(const MqlRates &rate)4 Q) R$ I: Q9 ?
{
1 e3 C! A" N/ x* g$ S{
5 R+ X$ ?* j+ U  _7 sret = true;
+ w6 N6 v+ D! u' n5 Oclose_positions = false;( `3 J9 Q8 `/ C+ {. p5 j- f0 p. K
}
6 K$ h& U; G* @8 ielse; Q8 @& k) ]  T9 @" S" R- O0 D  ~0 H
{3 U( _7 s) A' M1 p/ Z
if(mdt.hour == 16)) h! k$ c) n: ?! R2 v9 G5 z. R
close_positions = (mdt.min >= 30);) `& S# U1 y7 X; ^# [2 F4 q* D
}8 S. H8 l* J2 y3 T) }6 f; D
}, N2 M/ m: R! _/ b9 t  O/ S+ q
return ret;
1 y( B( }( [4 ^: z' s; T$ U4 R8 A}
5 n1 V* E; N& t: [# s//---
! _1 w0 u0 i8 e0 }; v9 k  kbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
. a* T6 {! S0 }, j{1 o( Q4 p) s& R' Y2 {6 z: S' m
if(PositionsTotal()) // Is there a position?/ Z+ Q& L+ S- A$ r
{/ c. }" y2 y! x5 R9 v  D
double offset[1] = { 0 };; |! Y. X8 o' }" q+ t
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
% |6 }" J+ \- i" D9 s( v7 q&& PositionSelect(_Symbol))  // Select the existing position!& g" y9 H& \6 j1 ^3 U- d
{4 [/ s8 p* O& h, _
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);5 W4 W- p5 m( h
double SL = PositionGetDouble(POSITION_SL);
: L; O' ~7 w( V  u; _) sdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));0 |9 J8 S7 K1 L: X7 a7 g% P
if(tipo == POSITION_TYPE_BUY)
: F  p- \# x" e9 ~4 u6 o3 _{% ^1 B/ I, c* q8 o% ~' u0 i9 s
if (cotacoes[1].high > cotacoes[0].high)
0 @3 n' m7 P5 o& v6 ^1 [& A( f0 L{
6 @) L& c6 R$ W8 zdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];- R/ C# \  {% h8 _! k3 J& L
info.NormalizePrice(sl);
5 }+ V, z* @" U! {if (sl > SL)+ l8 W; s; e+ E5 c! x$ B7 A& D" A
{9 l: ]) |- l; v- O) f
negocios.PositionModify(_Symbol, sl, TP);
' o( \7 e3 Z( h, k. C$ v+ X2 ^}8 \9 M8 N  p) {; c; K8 A
}  r' {; u5 i' w( X- S# ]
}
2 ^6 @( K' N- e/ @4 |else // tipo == POSITION_TYPE_SELL5 O  ?& A+ H$ r* u9 l
{
; @" `+ R  @: N: ]2 m! z* [" qif (cotacoes[1].low < cotacoes[0].low)+ }: P) L) h5 ~# B- P
{  m- Z! S9 m/ z0 m
return true;
( |7 l6 u9 p8 }# _# D" R. P}" p3 n, F: W4 j) D6 s( N/ `! O$ e1 S
// there was no position
9 l/ b- g  N' W6 ~return false;* b5 e# r, M) X* K4 C# e
}% I+ r& ^6 z3 ^4 N3 e- K7 x. o& G
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。+ Z7 O% h" u2 z
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-21 19:10 , Processed in 1.263905 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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