私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
& m# T" T% X" A8 d3 I在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
  x& u/ G: x3 c为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。7 e+ _9 A4 y& d- v! ?
以下是制定这些规则的代码。' k3 f' h4 j! k  M+ _. l9 w0 t
//--- Indicator ATR(1) with EMA(8) used for the stop level...
0 j5 W. B8 r  T* gint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
! |0 y; Q  `& C' Cint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
& ^9 t5 k* o$ W# b6 M. o: r# q//--- Define a variable that indicates that we have a deal...! \; V  r5 Q# |8 f: @) @
bool tem_tick = false;( R* K4 @% V7 ~
//--- An auxiliary variable for opening a position
! i/ d% l& q2 X; v! f2 m  Z#include<Trade/Trade.mqh>
9 ~, \/ S; ^7 d, h0 t$ D+ L3 Q! s( ^#include<Trade/SymbolInfo.mqh>
/ t( r4 h( T: g1 Z5 ~+ bCTrade negocios;# w5 P% K: o; K. d2 t
CSymbolInfo info;
  G0 A8 e2 `/ i. W//--- Define in OnInit() the use of the timer every second) B6 Z" g' B8 m4 G( V, N7 v5 }
//--- and start CTrade6 r7 @. K0 w/ `
int OnInit()
* K% r- n) \: D4 U0 g- n{, X. {$ T0 ]4 S; N% e4 P% p4 \
//--- Set the fill type to keep a pending order
. F7 z" a8 Q( R8 z) Y. z0 e//--- until it is fully filled0 ~  L- a" y! f9 U# l) G+ T4 P
negocios.SetTypeFilling(ORDER_FILLING_RETURN);' k* l% T; I  u( H  C( L1 o
//--- Leave the fixed deviation at it is not used on B3 exchange
2 B8 G$ ]4 s, n, P+ a4 @2 Xnegocios.SetDeviationInPoints(5);
4 \# O4 _  W, k  D- `2 t1 w4 k, j//--- Define the symbol in CSymbolInfo...
* R% o" A1 G3 v1 I7 f2 B) c; d1 K3 g1 `info.Name(_Symbol);
! b+ ?" W+ s- ^. T//--- Set the timer...# n& E' q$ i3 o9 R/ K
EventSetTimer(1);, p8 _7 {  f. `/ W; L3 F) {
//--- Set the base of the random number to have equal tests...
# \  h' W- Z! m9 f' x+ ~MathSrand(0xDEAD);
; B, P) ]3 ~! S! mreturn(INIT_SUCCEEDED);6 i! E( R3 J; R, K6 D6 e
}
# R: ^7 ]3 |/ H/ F# k//--- Since we set a timer, we need to destroy it in OnDeInit().' Y. A' a, j, Y8 n
void OnDeinit(const int reason): k! F% J7 F$ n" r/ f" V
{
* ^2 y6 v, i* a) O0 C& g- O' fEventKillTimer();1 h/ Z& O8 C" o6 O4 F# ~
}
. g- E2 a, D1 b2 c6 N2 w//--- The OnTick function only informs us that we have a new deal3 V# m. x" |) L+ l- F
void OnTick()
5 v0 ]# l, \6 a* f' R- U{$ M  X* p$ x1 ~4 _2 b& m8 u
tem_tick = true;
9 E0 _5 |- i) S2 z! R}
) {; l2 f4 R4 `* w( O; V//+------------------------------------------------------------------+2 P+ @; B5 {) W7 A( ?4 _. q9 U
//| Expert Advisor main function                                     |
$ N  d. ^/ Y2 j; n5 ]$ s//+------------------------------------------------------------------+
# {1 ]; ~8 ]: o# z- u; C$ M! k& ]void OnTimer()  [! `/ d2 }3 v  D5 h) _1 i
{+ T! q0 J5 G$ t9 l
MqlRates cotacao[];
: f& {$ `6 a' U# _return ;
, A2 [9 u9 J# x% cif (negocios_autorizados == false) // are we outside the trading window?3 j, H- i4 O+ e- }6 f
return ;
; h7 [. j/ j" f6 m. {  Y//--- We are in the trading window, try to open a new position!
+ v) M: T# z4 `: Nint sorteio = MathRand();
9 R" D. H  x1 n4 o//--- Entry rule 1.1
" F. ?' A* C1 r7 D. Z) K0 F* `if(sorteio == 0 || sorteio == 32767)5 g3 e# Q/ x9 Y9 L% M$ b- f
return ;
* Y2 [# x6 C- ]% c  I# vif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
) p2 |' ^- U$ a2 H. i& q- ~{7 M4 U0 F) r5 ~" A4 b" D
negocios.Buy(info.LotsMin(), _Symbol);
) `* j: f" c( ]! {# K, Q}
3 }% T- ~5 D1 w) y% m. Felse // Draw rule 1.3 -- odd number - Sell
. Z' L/ V/ Y% c6 a{
' R& ?* k3 Y0 w% c# C9 s( Z  lnegocios.Sell(info.LotsMin(), _Symbol);6 r/ O  U) m/ k0 ?% H
}7 F/ j6 Z* m7 Z( ]9 P
}
4 n# K; Y8 q3 U9 W//--- Check if we have a new candlestick...
! S6 }! w( Q  @4 x. E% H6 s7 wbool tem_vela_nova(const MqlRates &rate)6 i. x$ ~# N3 v! F$ s
{
) ^+ q9 c' _+ e& Y2 L* D{
# }: J- z, S+ {; V' j5 N$ Tret = true;
' c& [( X+ j, t9 u# }. h% \close_positions = false;) G% m( _4 ~( ?6 B
}
1 |$ B. [/ s/ y1 Q2 Zelse
# K* |2 P$ N$ f$ p1 T4 ]{0 Z0 w4 v0 i( z, z/ q/ n
if(mdt.hour == 16)& K. S. G! ~+ l2 V
close_positions = (mdt.min >= 30);
3 l# C4 y- C+ m) N% _}+ @9 K# K( }0 L9 Y& R2 p8 Q' p* W
}# P. B9 w' W$ b- K8 B$ ]: L
return ret;
# h: e$ ~' D$ ?0 d}* I8 x' t- e" I) G
//---
. R, g* K8 {3 Xbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
. U! f0 P8 z. J7 Q4 |  h9 [" X{" O. T! j4 Q! d
if(PositionsTotal()) // Is there a position?
( f8 e9 e# t" s$ z' O( t  V{( A; j0 K3 f( t- G$ d% ?
double offset[1] = { 0 };: [4 D( M- w' O/ w3 g( m
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
- A8 T9 V$ f4 r) \, u9 f( N&& PositionSelect(_Symbol))  // Select the existing position!; N- |4 h7 W0 ?- H/ v+ S
{
- B$ X! I1 C! b0 G2 y+ rENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);9 S* P; O$ R$ x: m# S
double SL = PositionGetDouble(POSITION_SL);/ z) ~9 v' u: K; G3 b# a0 u5 |0 U' i) H
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));/ ]/ S7 Z. P, U
if(tipo == POSITION_TYPE_BUY)
- y0 X1 _* n4 f" m  r% b6 m7 g{* f& z/ u% D" W) K
if (cotacoes[1].high > cotacoes[0].high)8 s5 m* x+ W. L8 O
{) X; d2 }0 P+ g0 N  {4 j& t* N
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
& e' A0 j  i& I6 e. m+ minfo.NormalizePrice(sl);* x' c- I4 _9 o# _
if (sl > SL). G" H9 o0 H" n- {7 U
{2 R4 a) U: E( l# b5 m8 [. E
negocios.PositionModify(_Symbol, sl, TP);$ X8 W+ d) z' Q3 w6 Z
}% _  I( w, k1 s4 v: ~! O
}
. W# t/ L: e' b. `5 Q8 ^}
& ^* X2 |& I4 S% h' telse // tipo == POSITION_TYPE_SELL5 p# M( [& |: h- G7 N$ o" a* B: m
{
! I. c8 K# h* j( C7 Rif (cotacoes[1].low < cotacoes[0].low)
2 D; G4 \; A  i{8 X/ P  g( m/ l+ J/ T2 H; c
return true;
9 C+ _0 B( A, h- z- Q( n}
' Z$ Q) ]3 F) T) ?// there was no position
5 Q! b# j" q2 m$ C: ?2 j7 ~4 \# P5 Creturn false;5 m( L$ K! c- T
}
/ h$ ?5 S8 J3 E# u1 u) F$ t8 G我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
' \0 S, M( L7 B. C, o" T% J到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-31 16:42 , Processed in 0.499201 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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