私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA: \$ Q9 N+ \5 }$ N1 D& F
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
& H3 `8 U) ^# x# \, `1 A为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。  E# t% A& ?+ U4 @4 E# P0 [
以下是制定这些规则的代码。% C/ J/ E" l0 H1 n% o. U
//--- Indicator ATR(1) with EMA(8) used for the stop level...
) z( v1 s3 @& b9 ^, h3 g" U, I' Iint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
7 x; Z; g5 {, |9 q! Wint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
9 U: {/ K7 k2 B6 b  O//--- Define a variable that indicates that we have a deal...
9 W4 l% N* a9 ]bool tem_tick = false;# e  A' Y" A1 U# t+ i: a) l1 }
//--- An auxiliary variable for opening a position& V* |: m/ K, B0 F/ W
#include<Trade/Trade.mqh>8 H0 u! M9 E' {: @$ ~9 e
#include<Trade/SymbolInfo.mqh>3 C' A# w; D% b9 u, B. ^
CTrade negocios;
+ l0 V6 O) w# j& R1 bCSymbolInfo info;
1 R- d. n$ x7 }//--- Define in OnInit() the use of the timer every second- [" b6 ]/ {+ x, ?* X5 s
//--- and start CTrade1 n, i0 U" V, Z6 Y
int OnInit()
2 R) W: S# I/ b. \( D' G4 r{" \# s" X: x  \3 e, G4 Z1 U! }
//--- Set the fill type to keep a pending order
' `2 Q2 m" W! v! r, C* ~: A//--- until it is fully filled
3 j. ]7 l# ]$ Pnegocios.SetTypeFilling(ORDER_FILLING_RETURN);7 K1 f* E4 a# T! R5 }
//--- Leave the fixed deviation at it is not used on B3 exchange) ?, J1 \' d- D1 W- A
negocios.SetDeviationInPoints(5);
# Z5 n# A: x3 W9 V" n! D/ E//--- Define the symbol in CSymbolInfo...
! g& ~/ U( E+ _) F. Pinfo.Name(_Symbol);/ ~0 M* L$ r6 B$ b3 P
//--- Set the timer...0 y7 `# i6 T. O3 g/ S" n6 ]
EventSetTimer(1);) m) \6 w+ x# [. T" p
//--- Set the base of the random number to have equal tests...
4 l  \9 }/ E, xMathSrand(0xDEAD);
- p$ D6 }* x' C2 ?7 X9 }return(INIT_SUCCEEDED);- k: j& j# n* @: y& F- b; [7 h
}
1 n* ?0 R) e8 B$ ?//--- Since we set a timer, we need to destroy it in OnDeInit()., P2 n. @* M" f3 X
void OnDeinit(const int reason)
" n* E1 n+ `% o! H0 f" X{; w: g* V$ T8 @% @6 Z% v$ v
EventKillTimer();
# I- O; H+ e) p1 v0 n}
: Y0 I: V) m- C& e$ J4 n6 Q7 h: n//--- The OnTick function only informs us that we have a new deal% o# A2 k1 N, t! [/ G( X
void OnTick()' I/ s0 N) o3 ?
{
: v4 @* g7 u0 p8 U8 a* k3 item_tick = true;
1 C0 ~+ W* U, `  Z+ ?}
, \& D! X1 w! z, y//+------------------------------------------------------------------+
( \) B+ w/ C9 T- o4 W, Z//| Expert Advisor main function                                     |
% M- N2 T' ^  r( s" s//+------------------------------------------------------------------++ s" S( U, q7 Y% F, v+ N; E
void OnTimer()
8 k% k  s# h+ [  m# u{
1 f% k# S1 P" x& J# m4 oMqlRates cotacao[];1 D8 g  M  f6 H. [3 I# Y! o" t
return ;
; `/ N! {/ ]( Mif (negocios_autorizados == false) // are we outside the trading window?# }6 Z6 T" p( T! I
return ;
0 ^4 h( i- r) C/ k0 @//--- We are in the trading window, try to open a new position!
$ S& d5 N& _4 o- ~9 w4 Eint sorteio = MathRand();
$ ^, U, a* z6 ]! [% b+ E//--- Entry rule 1.1' |* j7 _1 z. {9 B3 \, q
if(sorteio == 0 || sorteio == 32767)
2 @$ D0 f6 k4 i. zreturn ;" g  o1 `; A- D' l; L
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy$ x4 U' B& z: \% I, p
{8 |) P3 l8 w. S  z9 R! |
negocios.Buy(info.LotsMin(), _Symbol);
  P" ~) `0 S2 r9 V7 @$ m" n+ W" Q/ h}, W; t+ J: y: S+ ^) j8 ~# u* `
else // Draw rule 1.3 -- odd number - Sell
( j2 o8 x" S1 \0 g8 f{9 L3 k0 _/ O5 o
negocios.Sell(info.LotsMin(), _Symbol);
4 z/ y! T; _& }1 Y! {/ w}
: [  r; U7 g; l9 X}
/ Y1 W3 ^9 T/ ^//--- Check if we have a new candlestick...
3 U: M: ~. E, T9 `* ?, b! zbool tem_vela_nova(const MqlRates &rate)2 R- N% d8 N; p$ _! q- Y
{
! g4 n( w3 w5 z! {{
5 \, D% K8 ^1 p1 l* r/ q! Hret = true;6 e0 u$ }( n5 `6 s- R2 ^
close_positions = false;
8 w& P3 u3 E4 a; d- n; I2 o5 u}
+ C+ O( H( L! G+ }( J% G' u. ?7 zelse( @8 g; B5 O/ b% N# h* s
{
2 D0 o4 ^+ I7 F( nif(mdt.hour == 16)
) W) N. }7 T9 ^& `/ Mclose_positions = (mdt.min >= 30);
" O7 H" L( ~7 G}
) m+ W, X0 P& o/ Z# h}
7 F5 R7 r7 I4 P- V5 t6 `return ret;
2 b5 z) Z, `! m$ ]- _* y2 M}
* }% q) @) c& m8 A* Y//---/ Y$ e! T7 n( Q0 M0 C, t, b
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
" Z) x5 s& a' `& D1 ]# h8 y! y{
2 b; D7 v0 S& n9 h! Xif(PositionsTotal()) // Is there a position?
( U( X; I6 r" t% m8 w  }( r' X{
0 e6 T# c- X& _3 u! G2 udouble offset[1] = { 0 };
6 y, W2 O$ U& Aif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
/ U" o& \# E( S/ x% e&& PositionSelect(_Symbol))  // Select the existing position!6 ?% n  J9 m" y1 a2 b5 L; M: r" X
{& G' X- o  z1 p* l) q/ C+ w$ p
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
; M/ z0 a3 O: L" m/ M8 Fdouble SL = PositionGetDouble(POSITION_SL);
! E1 B# P% _7 |double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));+ N2 y8 _3 K) z
if(tipo == POSITION_TYPE_BUY)3 @3 P# L/ X" C+ ]# A' n1 h
{! U& E9 h# E- ?: T% _: H; }+ x
if (cotacoes[1].high > cotacoes[0].high), q7 a& d  c  c% z! u
{
  E7 X3 L/ X5 m( M- Ydouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];+ w! P6 _, |0 O
info.NormalizePrice(sl);8 v% n7 n$ p- v7 I6 f! a8 J3 a+ y
if (sl > SL)
4 T5 q4 D3 o% d1 O{4 [; O. c: u( i* D4 I# N  y3 |
negocios.PositionModify(_Symbol, sl, TP);) p0 M: _% ?$ ^
}* t! |$ @' S; x2 @, q2 _8 n: \2 ?7 k
}; T2 m  |# ?" w+ n2 ~
}
2 {/ t, R. f& R2 G% r! Xelse // tipo == POSITION_TYPE_SELL- ?4 q8 y9 X9 z% o3 f
{
; n; F- S7 @3 s. aif (cotacoes[1].low < cotacoes[0].low)
( c7 l: {3 C' j{6 B; h& Z8 S) C; m9 g0 M. F/ z
return true;
9 [' L* e  P. g' \5 K: ]}
: u4 l! A- f' X  d// there was no position5 L. U. w. ~; u
return false;
1 N9 k$ ?3 h7 j) [+ z}
+ T2 ~% o; m, E6 o. C我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。8 N, R5 D' m) M6 x
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|Archiver| ( 桂ICP备12001440号-3 )|网站地图

GMT+8, 2026-5-16 16:16 , Processed in 0.491229 second(s), 32 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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