私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
9 p  G4 B1 T. N5 L2 ]  I在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
- r! _- j, G# f& q" d; f) d4 _! o5 A为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。) ^) f8 P, ]5 L7 ~
以下是制定这些规则的代码。) H) _$ h1 _4 d3 q5 ?" B0 U
//--- Indicator ATR(1) with EMA(8) used for the stop level...
9 I  e% V6 e) C3 G- u* A# U5 kint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);) B/ Y  k2 y, A# H2 E8 Q9 C1 A
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);! X+ J/ }- h/ k4 A( G5 E" Q& {
//--- Define a variable that indicates that we have a deal...
6 O, `  h1 H( \, I  Y- wbool tem_tick = false;
* _: A  Z* }9 b/ |+ L% s# H) s//--- An auxiliary variable for opening a position* e- x4 G8 q4 M: W) h( n& o, q, @
#include<Trade/Trade.mqh>
6 m2 c- C1 z9 q+ u#include<Trade/SymbolInfo.mqh>" O# L8 g9 \# U+ E/ J$ J
CTrade negocios;
' M- w* X. P0 w' [CSymbolInfo info;
3 V$ ?9 o6 J8 o5 Z//--- Define in OnInit() the use of the timer every second, H/ B6 L4 |& m/ \: n$ w9 s$ ]
//--- and start CTrade
9 M; B+ O- H- }! N3 m# l) o4 d- k! Oint OnInit()! H4 L$ V  U" r& d( a: \
{
1 \' l' n* ?4 I: T, C0 [//--- Set the fill type to keep a pending order
4 e" b4 d3 e0 L7 f//--- until it is fully filled
* w1 Q( F/ E. Z, ^* ^negocios.SetTypeFilling(ORDER_FILLING_RETURN);
6 ], v# j* g0 ]! ^  N* M//--- Leave the fixed deviation at it is not used on B3 exchange
+ e0 \$ G/ G! R8 I( [negocios.SetDeviationInPoints(5);
% N1 x* X& X3 _# ~" I" ~7 @//--- Define the symbol in CSymbolInfo...- w$ R4 G- B0 n3 \
info.Name(_Symbol);, v; {" }& N% ~! K$ H, u
//--- Set the timer...
' q3 {0 j  L% l* g2 GEventSetTimer(1);
1 `- g" c9 G3 H0 b, s3 t; l: ]//--- Set the base of the random number to have equal tests...- W5 f8 Y! E/ z/ Z
MathSrand(0xDEAD);2 Z( S. d+ X* s  \; X
return(INIT_SUCCEEDED);0 s, h3 F  R+ p! z9 h
}- [8 g8 G; P0 _
//--- Since we set a timer, we need to destroy it in OnDeInit().: V) H, o$ U8 l( r) A3 P8 E
void OnDeinit(const int reason)
- n7 a$ ~9 j% h0 Y4 N- t" C{
9 L* Q8 t, N; [' C4 b* P8 lEventKillTimer();
/ G+ d  G+ N8 F- v" Y}
0 e- I3 J9 z$ T2 v3 _* z8 S//--- The OnTick function only informs us that we have a new deal
% t5 O4 M) a/ r+ i# s7 }void OnTick()+ z) H0 F% ]9 g& a: v% p
{; Z4 `! m6 k7 y9 I" h2 m, r
tem_tick = true;  s- R' I9 _) o& X+ `
}% O5 q0 p4 d# y. r5 K
//+------------------------------------------------------------------+6 H! d9 a; {' Y) S8 o7 W7 A- i. @4 N5 F
//| Expert Advisor main function                                     |; h5 c9 Z$ {. {& r2 Z" _
//+------------------------------------------------------------------+2 Z9 f5 @& r3 ]5 a
void OnTimer()1 w, A- f7 ]4 N
{
0 o# j" n4 R( G$ c  y6 `! wMqlRates cotacao[];
! {7 t% h# J' R1 _& treturn ;
! t  D4 ]$ t) C! f& xif (negocios_autorizados == false) // are we outside the trading window?
0 m4 N8 p# N% m; v% R$ ~; Treturn ;6 p( i- U/ n  I* q5 P  [- v
//--- We are in the trading window, try to open a new position!  ~4 u! [" w4 I1 I! S! d& s( s$ M
int sorteio = MathRand();3 }. I( q6 r7 h' M  f5 F* A
//--- Entry rule 1.1
% c8 |/ V' g9 Q1 Gif(sorteio == 0 || sorteio == 32767)
5 i$ w9 W+ K3 v* P: @& ]( f- G/ oreturn ;/ a2 t" A* H% o# J9 V+ c
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
0 U% l" ]+ k/ [; C) H! [  a{9 A( s% L: C6 e8 o$ M: j, Y
negocios.Buy(info.LotsMin(), _Symbol);
) a: @/ i4 W" m, z5 v$ n}
! b+ d' V! e  y  g- eelse // Draw rule 1.3 -- odd number - Sell% U4 B4 {2 f/ O/ p3 y: M( R' O; ?1 Y0 T
{
( Z7 b1 l2 r# _/ Q5 vnegocios.Sell(info.LotsMin(), _Symbol);
& r+ q3 B. w" q+ ^}& b7 G) m6 m* I1 q2 D) Y
}8 M2 Q) \2 T6 a$ ~* Z# U1 @# ]
//--- Check if we have a new candlestick...
0 p( ?. s4 U! e' f/ cbool tem_vela_nova(const MqlRates &rate), v; j) R9 h8 h6 l& U# ?+ g! ]  ?
{
# W  n2 q* G' }. y{1 ?% t) z7 {7 b! b1 k
ret = true;& J' q$ e* ?6 x4 T: }4 m5 Z
close_positions = false;! R0 w& x) K5 ~$ ~, i
}
9 J& k0 W6 _  B' e+ E5 e2 I: celse" q1 b2 _) d- K0 y/ j3 I( w% T" l
{
& B3 Q* S7 ]9 ~5 K$ |* @3 Vif(mdt.hour == 16)
! x7 X; I- M: g0 x. Aclose_positions = (mdt.min >= 30);
4 N" ]0 C9 y  l3 X4 o% O}
  E* U$ K# E# Y1 S5 ]}' C; B& p6 _# U# ?  n5 C& i
return ret;
& f5 N. F) Z6 q}
" s" k, G6 B( Z9 c& L; t//---# p% Z1 E$ g3 ^9 E. C0 N7 m
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])( f% L9 Y+ L9 q) ^8 K2 ^
{+ a1 T. B; R$ e  r2 G6 Y: x( v
if(PositionsTotal()) // Is there a position?
8 m9 M( J* A5 j; s7 c3 M! g{
& N5 L4 @' G  Q2 z3 M: Zdouble offset[1] = { 0 };
- |6 G6 a2 A! S: ~8 Q$ q6 Iif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?% B  d9 |% a# x  q3 w' V
&& PositionSelect(_Symbol))  // Select the existing position!* Q; D: V+ o4 O9 _( `; y
{; ]0 _  l0 Z1 |4 q
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
: L5 F# h; h1 r4 O. ?double SL = PositionGetDouble(POSITION_SL);
2 w' P: P- `$ z1 xdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));" j  o$ D; O* d6 n
if(tipo == POSITION_TYPE_BUY)
- r9 \: U; T3 y: s7 ]{
& N! x0 x  r0 D6 a! K% D# h+ Xif (cotacoes[1].high > cotacoes[0].high)# Z( h4 k  N8 d: B/ ]7 t6 }3 d
{- ~) n; O# o+ ]% t
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
- u% Z' D/ p* \4 J6 s, I- Rinfo.NormalizePrice(sl);
; T; l! u  F2 i: {1 H5 I6 |& E0 [if (sl > SL)( S+ d+ _1 u) E' U; L
{# d7 E1 Y. u- {( X9 {" z
negocios.PositionModify(_Symbol, sl, TP);
' x% U5 {: G# |0 j; h3 L( _6 o8 s}  N, W/ o  u# j" v/ ]
}
4 c6 y; X3 Y# t  d}! D% }; d1 m3 g! C* C3 i1 ?
else // tipo == POSITION_TYPE_SELL
6 M! R2 X4 a8 L{
) r1 M* J' i: eif (cotacoes[1].low < cotacoes[0].low)4 h; w& B9 B$ @
{
6 e$ S; ?+ m/ u* freturn true;
( ]3 r  i1 Z5 |% i7 o9 v8 s, s}3 Q& L% S% a. c2 m4 S
// there was no position, P: [+ i/ I$ }1 U* I7 t
return false;
4 L4 I; X* \4 \+ {9 v4 t. \+ D}0 b$ H8 A6 U2 o( v- T( ~
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。) a2 p. f! H  W
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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 14:06 , Processed in 0.465668 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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