私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA2 [/ I# m% Y" ?+ D4 K! e
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
& ~/ L! G" l+ ~2 Q1 Q( y为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。% I# `: t- |+ i* F
以下是制定这些规则的代码。* d+ a  p0 Y& S9 U
//--- Indicator ATR(1) with EMA(8) used for the stop level...! q( V* G: i  y
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);. c" P6 o( o5 d5 |4 H5 Q
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
5 P' U  ]5 Y  t) Y//--- Define a variable that indicates that we have a deal...5 P4 ~# U  K- B3 |9 q
bool tem_tick = false;6 {0 k8 Y. m. `! _6 e0 e9 i2 v1 K8 K& }
//--- An auxiliary variable for opening a position
: I* d) G0 \& t7 N& V; m- n! M" F#include<Trade/Trade.mqh>
7 n# Y5 q4 y* d! u1 h#include<Trade/SymbolInfo.mqh>
- j9 Q% N) h8 F: MCTrade negocios;
; P; X! O6 g/ c: vCSymbolInfo info;5 L) j& j2 c4 R. ]* M, l, B, k
//--- Define in OnInit() the use of the timer every second
# I* K/ C0 J" @3 ]2 _' u//--- and start CTrade
4 u0 w0 I+ ]% a6 Bint OnInit()' {1 K2 X$ j' N( Y8 x, d
{
+ ~2 M4 P; a( x% A* J9 Z9 X2 }//--- Set the fill type to keep a pending order
' K6 Y2 k& ?% F  p  P/ |, q3 X//--- until it is fully filled
+ l6 ^* n/ @9 e/ V0 Ynegocios.SetTypeFilling(ORDER_FILLING_RETURN);
9 q7 ]# p) u# O, l9 P//--- Leave the fixed deviation at it is not used on B3 exchange
( L1 w, u/ ?$ n( ^# s/ Vnegocios.SetDeviationInPoints(5);8 ]* A5 l" e* i4 q+ {% n
//--- Define the symbol in CSymbolInfo...
& U8 V5 I4 e4 t: minfo.Name(_Symbol);* V# ]0 L8 t, l$ s& j. e4 C. N
//--- Set the timer...
  X& H0 H5 C! Y) R8 B: [EventSetTimer(1);6 c; N1 Z+ M& l! ]1 M: ~
//--- Set the base of the random number to have equal tests...
3 [, c# i& V" F# wMathSrand(0xDEAD);
: T3 u0 N* K5 I% ureturn(INIT_SUCCEEDED);) F9 O' S! v5 H7 i
}' }7 w* x" G: z! B. J
//--- Since we set a timer, we need to destroy it in OnDeInit().
& c% y0 l: r; ~$ gvoid OnDeinit(const int reason)7 H. _7 w1 X2 C, ?5 K" Z
{' h9 {. P& ?5 L5 M! l0 q
EventKillTimer();
: f! Z4 |" T& [& S  P7 S}  q& [5 U+ l$ j) b4 z
//--- The OnTick function only informs us that we have a new deal
, f% w+ I8 a% H% E; G9 Gvoid OnTick()
" D5 X9 s( N5 u{
+ U/ j1 R; ]6 N  r8 c3 e* }tem_tick = true;
) m' Q1 m  j" y) ~+ h- G' C: t  t}
# b; I* O% |( s//+------------------------------------------------------------------+' C1 i- o1 P' m6 ^
//| Expert Advisor main function                                     |
( z$ L# ]! h* X, T- z- A//+------------------------------------------------------------------+
0 b; v5 s" `; T. I0 S  f0 c/ rvoid OnTimer()
' F4 F( O1 d0 c. ?# C& q{
. Z! f6 X+ ?/ `MqlRates cotacao[];
* c9 h7 k; ]- zreturn ;7 Z' [$ H$ G; r1 A* u
if (negocios_autorizados == false) // are we outside the trading window?
0 `; R% G; S# M2 Kreturn ;3 B3 Z7 I% Z3 E( H7 J
//--- We are in the trading window, try to open a new position!% H; F" ~2 K7 }/ h2 p- H
int sorteio = MathRand();; G* _! O% b/ y$ V+ C" Z9 u0 _
//--- Entry rule 1.1
9 x. t9 K( D% W5 Bif(sorteio == 0 || sorteio == 32767)
6 f/ g( n* Z; p1 u+ U  areturn ;& K* C6 j& s: V$ r
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
8 z) i( n  [3 o) R4 N' B{
* @; k8 ]: z! c0 ?; D* l; ~. Rnegocios.Buy(info.LotsMin(), _Symbol);
9 }0 _! O& J0 @5 ]/ n/ Z% o) L}
0 E& x  J0 k% `* e! Q; l* Ielse // Draw rule 1.3 -- odd number - Sell, h' i7 ^4 x- O# s% j- j; C- K
{- [3 S1 t  R6 r1 f8 ^) P
negocios.Sell(info.LotsMin(), _Symbol);& n* N) m& ]3 f. {: u( g
}
3 D5 a  `  g, N2 ?4 i* T' v}- ?3 e$ r) j" y
//--- Check if we have a new candlestick...6 i: ^- a* J0 x  {3 t
bool tem_vela_nova(const MqlRates &rate)
# n- o3 r* S0 Q, J! w, l- f{( S. b7 s5 G6 i  o/ a/ M+ u
{3 M0 B( P- j' T. r- T4 U* U
ret = true;  x# K/ M+ H* g, c9 l: y, X) ^- I
close_positions = false;5 z2 W6 S1 b2 d9 M0 ]! o3 I" X& w
}) A: T) n6 _5 x' d' d5 D3 k
else  I0 N: C5 E) u2 ?+ o
{. }1 U+ O" ?- _. x# m9 E  d$ F
if(mdt.hour == 16)5 j2 z7 T5 M: [5 ?! G: _2 ?
close_positions = (mdt.min >= 30);
9 j7 U" w- F6 i& b; O}# r1 U0 T( a% M# P% d' c) M) v
}: n) H0 m# F$ s& F
return ret;
: S7 O. l: v. [4 C( c9 q3 X}
+ f$ F  H2 T; r. M& L//---
9 F% c" d; f9 ]bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])& M- F: Y+ X! n
{' f  |1 e' h7 m1 D7 C& h
if(PositionsTotal()) // Is there a position?8 Y2 F5 E" @( \4 R. e* U
{
) t+ X9 i" `; @( \( ?+ Ldouble offset[1] = { 0 };
  ~" a% ?; a8 A" g( @7 @; C/ mif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
* w5 ]  y# n+ T, v7 ?  o&& PositionSelect(_Symbol))  // Select the existing position!
1 K* t/ {: s( V$ q2 p4 B  V% M, _, F{
& u% [7 ?" p5 S& k4 {ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);7 l0 O  k% u: `+ ~  p' N
double SL = PositionGetDouble(POSITION_SL);
* v7 C6 D5 v1 T# k( i6 Ydouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));5 E& Z, u2 X" z* G& r2 g. {- e
if(tipo == POSITION_TYPE_BUY)
* O% b0 V4 f) S* K& \2 z{" }( t$ m; t% ]  W3 w5 \5 U" g
if (cotacoes[1].high > cotacoes[0].high)# T8 x# `8 ?7 \) Q% {/ L; ], M
{
  u7 b; w1 G+ D/ s  H( \4 Mdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];% D3 D& z5 G9 }2 B: E
info.NormalizePrice(sl);4 @+ b" |4 s) {& z
if (sl > SL)
9 @7 F4 N- w# z( J; W, j0 Z7 h{
0 X9 a3 O6 T; |) [8 ]' Y* Y" e0 vnegocios.PositionModify(_Symbol, sl, TP);6 X* J6 A( ]  Q- M2 |' J4 n0 e$ Y
}
# j& i. t6 B# h( X+ L: {) N}: D: ]/ ~1 P: i! q
}
+ u( B2 S( c3 E4 i$ p; Gelse // tipo == POSITION_TYPE_SELL; I% }0 D6 \# x& t) F
{2 N1 F. F) p0 \4 s
if (cotacoes[1].low < cotacoes[0].low), ?7 L" Y, }, H+ _+ \
{
, D* H  S4 U9 v7 ?5 e3 z. J* p2 Lreturn true;
3 T' t4 c3 H  T" u1 d. Q  D' J- P; C}
4 Q4 [+ }3 h) @4 O0 a// there was no position
- l+ u% l" x' i6 \) Q0 h) t( Q8 |+ zreturn false;
/ f9 m* g# Y, ~4 h) T- C4 P9 \/ V}
& x& `8 C7 E. i! W1 I我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
, D3 J' ^; Q- E3 i3 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-4-22 22:05 , Processed in 2.200042 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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