私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
$ z! f5 l3 X' d' @/ F在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。2 ]/ @  |$ D8 q% j
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。; F& i, M, w+ K8 ?# s7 x
以下是制定这些规则的代码。
" \& h. Y' [- I//--- Indicator ATR(1) with EMA(8) used for the stop level...0 D. V+ L. y; w  Q! ~& U
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
  p" K: [' [5 ]( C, V$ Q2 vint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
  N: {" U; Q! ~; A  _//--- Define a variable that indicates that we have a deal...
  E$ x" ~5 n" o9 x3 e2 U7 qbool tem_tick = false;
0 O% c  j1 [6 g2 d//--- An auxiliary variable for opening a position
" |9 k4 D5 ]2 U- C( ~#include<Trade/Trade.mqh>0 g# v$ j5 B) ]: g/ c6 o% j
#include<Trade/SymbolInfo.mqh>$ ^2 _& w6 a7 z
CTrade negocios;4 O" B- E, \/ \- y
CSymbolInfo info;
+ w) y) N4 D) a- N& ]  q//--- Define in OnInit() the use of the timer every second8 W/ p! L4 s" ?% {5 _
//--- and start CTrade
% t% r8 z1 \* f6 V7 f/ [. lint OnInit()
" R+ u9 I. C* u, m( B' I{
% n' `9 t5 b" w( M: k' }//--- Set the fill type to keep a pending order) m: `9 u+ q8 L, b, f; g4 e
//--- until it is fully filled- C, r$ U3 C. R% m; G
negocios.SetTypeFilling(ORDER_FILLING_RETURN);$ n+ w( S3 x  e% S* T
//--- Leave the fixed deviation at it is not used on B3 exchange
$ o5 j& S  S) J: ]9 e2 [negocios.SetDeviationInPoints(5);
/ L4 _& ^" W7 }1 p//--- Define the symbol in CSymbolInfo...
  y: N5 k; N) a9 [# b! ^info.Name(_Symbol);
+ p1 r2 h2 x$ N/ w//--- Set the timer...
0 |1 r8 w4 f' r! P7 |EventSetTimer(1);
! q- B% e# h9 K& P  g4 R//--- Set the base of the random number to have equal tests...
3 O9 @7 X1 p6 p9 A3 s/ uMathSrand(0xDEAD);6 z; I7 E( \2 T4 T! c- k* E
return(INIT_SUCCEEDED);
  V4 m8 S& e# g; o- `}
/ T) X8 c- |7 h9 k' I* \//--- Since we set a timer, we need to destroy it in OnDeInit().
! D0 F) o# _4 w! m, Kvoid OnDeinit(const int reason)
. g2 A* E1 D% o9 }0 k2 K{
) ^, j" O. P8 @7 MEventKillTimer();  Q- t0 F# R% l
}
) z/ |9 g7 M3 E3 ?1 d& J( V! I//--- The OnTick function only informs us that we have a new deal
% b, ]7 ~- c, Pvoid OnTick()9 Q3 N+ t+ g3 ^% K. P: Z: e" [
{
& E! P  t) d7 h% S& q) z. Qtem_tick = true;
( _1 s$ G" B5 x* z/ R$ H% g}
5 ]1 g+ |, b( Y//+------------------------------------------------------------------+
" O& r( C' R/ r0 R5 j3 u. b+ }//| Expert Advisor main function                                     |, H8 _. L5 P. z+ I4 b- I- L
//+------------------------------------------------------------------+
* @9 p; {* D  W3 |3 rvoid OnTimer()
6 ^: E( l( @* k, _3 C{
# T$ r4 B0 q0 y; ^$ RMqlRates cotacao[];7 o9 I  j0 j; p0 q0 U: a
return ;
( N) F& P) t$ S9 Yif (negocios_autorizados == false) // are we outside the trading window?
4 d/ |  D. Y8 {9 i3 W) oreturn ;; R' Y$ H) a( O$ G. A
//--- We are in the trading window, try to open a new position!  ~4 n! x8 H6 W+ E8 y
int sorteio = MathRand();$ ~% k1 B( I# {5 E
//--- Entry rule 1.1
& W- u, c2 q( ~+ Q+ n, Z$ Dif(sorteio == 0 || sorteio == 32767)
' |3 z8 h  D" O5 Q6 g& nreturn ;
- M/ g9 W. a, ?8 a8 E8 fif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy/ O; [# l, d9 L! V" A, N5 A5 h/ c
{' ]( v8 ]: t5 V; K3 v0 s- D$ Y$ `
negocios.Buy(info.LotsMin(), _Symbol);$ d+ h$ L1 \9 S3 V! L  |
}
9 m1 @/ F6 d. A* O8 ~( felse // Draw rule 1.3 -- odd number - Sell
5 e( ]- }; F' Y  t0 v{) e* q/ M  l! z9 {$ `0 K
negocios.Sell(info.LotsMin(), _Symbol);0 q3 ]; G; H& P+ u& V- a$ g
}* }4 F' B5 N7 n: x0 {7 {
}
5 S% S! |2 m6 _//--- Check if we have a new candlestick...  E! e  K6 h: h
bool tem_vela_nova(const MqlRates &rate)! ~( x/ D' \2 u
{
# R5 g2 R( c/ {* L1 Z- ?{
; A; l& o7 K5 Q* T, o  i& `  mret = true;
4 n! s: M7 D7 Z) qclose_positions = false;) R9 v1 X5 r3 C$ I
}
0 X, o5 C: v& nelse$ G4 Z, x2 x# U! W
{
2 K& q* D1 q& o. ]1 i+ oif(mdt.hour == 16)
8 J3 \. C4 _: c( @4 g5 vclose_positions = (mdt.min >= 30);
3 B' L7 O( d1 a& u' _* C}2 Z- V# [, v# T. k; I( G# W0 j; H0 j7 H
}
' c$ s- b( R: ~8 n/ S3 T7 hreturn ret;6 q4 Z- d- o; ]8 r9 F, u
}
3 H7 w- b; ^8 V( p  H- y# y/ z1 A: i//---
' `& j) s( s8 Y9 x- ?- Ybool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
: L5 G, g; q  d8 d7 y6 X7 L- g3 M7 [{: c( Z  |/ b" ^; {
if(PositionsTotal()) // Is there a position?: ?, ~! [+ T% z7 `) Q
{. F' p' p6 W; j% Y6 T% a3 h! z/ Y
double offset[1] = { 0 };" E3 H& p+ y9 l/ N+ C
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?$ `( q6 _4 ~' L# |
&& PositionSelect(_Symbol))  // Select the existing position!  c/ U% X/ p: k- `; _! P
{1 c# ]' g! p' T$ [5 F
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);8 ^& J# L3 R; {$ ~! j* }0 A. _
double SL = PositionGetDouble(POSITION_SL);: g: ]2 f& g5 s: l! j# W4 W* y
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
4 O0 q3 m2 W( Q' S* `/ _if(tipo == POSITION_TYPE_BUY)
9 n$ A& V$ ]7 Y3 r{
& C" u+ x% |9 i7 [if (cotacoes[1].high > cotacoes[0].high)
: g* O) G/ ?7 o  l4 y{( L( Q; i$ m6 O$ Q
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
7 v4 @3 r/ |2 M! Y5 T' y1 O1 Vinfo.NormalizePrice(sl);) k6 Y1 d% Z1 u
if (sl > SL)
& _0 M! ~( S. C# Z6 U; ?8 X! ?{
2 G8 e; z) j7 E- a: s- tnegocios.PositionModify(_Symbol, sl, TP);
$ e" F, o- b  S0 W3 f}+ x! w4 S% c: F0 A7 r
}* ]6 N/ q2 n4 X( R5 J# b
}
4 x& ]$ F, u5 A+ Welse // tipo == POSITION_TYPE_SELL
8 a! M1 R  A; \{+ ~9 i% ~2 n4 u; I
if (cotacoes[1].low < cotacoes[0].low)  `2 e: O9 O9 p) i
{
! a/ S3 O! l" U) l" ]4 `/ M8 Ereturn true;- O% }1 h) U4 k% h: z5 W
}  }" V0 i! S3 F% R
// there was no position; @+ W. x2 f1 y. }. b
return false;
6 j0 o, G- ]. |/ J9 D( S4 E2 A}
. x7 v1 W2 T: o: p我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
# X" S* a* w% b6 Z4 b" `6 B到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-18 19:10 , Processed in 0.807593 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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