私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
/ q5 P3 Y5 B% Z6 Y2 K6 N+ Z在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
) `- @6 ^/ @1 |8 M+ Z为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。. F# a+ _4 `. v% u. |
以下是制定这些规则的代码。) `  N* W( W! }
//--- Indicator ATR(1) with EMA(8) used for the stop level...
" y: V7 `. l. L( D" g7 I6 J9 G) ?int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);9 G+ v1 O. b8 S+ m& T
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
3 V9 m; Y/ n+ l! _2 A2 R//--- Define a variable that indicates that we have a deal...
+ u' ]+ [4 v- ^. \( i4 W3 h. B! A5 xbool tem_tick = false;* A# ~/ p; Y' r- W
//--- An auxiliary variable for opening a position
2 L0 f$ ?  k- Q3 ~8 P( O+ M#include<Trade/Trade.mqh>
# c, e) `9 N/ x  c* J#include<Trade/SymbolInfo.mqh>
& A, d" m' L5 `) e# B$ {3 MCTrade negocios;
+ F/ @* v) E$ S, g' o) w  S4 hCSymbolInfo info;% o) a0 @0 v7 d
//--- Define in OnInit() the use of the timer every second
- t% \2 h. n: r, h//--- and start CTrade- I% C3 m) I7 s. ^/ w0 \  g( z
int OnInit()* L, f! @7 `7 e7 G8 T
{
' c' V, v/ z$ K7 u//--- Set the fill type to keep a pending order
4 s' a% M) R5 W$ Y1 k2 b//--- until it is fully filled9 D$ x* L, T6 s, @1 n2 s. W1 D+ [
negocios.SetTypeFilling(ORDER_FILLING_RETURN);! a, u; l) x* V% \. M3 D! _
//--- Leave the fixed deviation at it is not used on B3 exchange
* w- z9 M% M& F( L+ v7 Anegocios.SetDeviationInPoints(5);
: f/ }% o  c9 J& m, W% E//--- Define the symbol in CSymbolInfo...
* z9 _- u1 F- n+ {0 einfo.Name(_Symbol);6 l4 f' q# y( b9 x4 `
//--- Set the timer...
; o3 I6 b0 M$ H0 u4 @/ HEventSetTimer(1);
! i' `! O' }! d6 b- j: p//--- Set the base of the random number to have equal tests...
7 i+ D1 M8 E4 C$ PMathSrand(0xDEAD);4 k8 T. T% y* d8 B, A
return(INIT_SUCCEEDED);2 B; G9 y! `, _
}4 k) n" z8 n4 T# @6 }3 v+ Z
//--- Since we set a timer, we need to destroy it in OnDeInit().
; B- _5 Q) Y4 H+ N  Q' n* m, _# Hvoid OnDeinit(const int reason)3 q6 B/ \" h# ^# ?+ r: W6 d5 l
{1 v0 ^( W) U7 e$ N. }' ]
EventKillTimer();: J1 L) L, G1 f% b8 T
}1 C; n/ l& P0 h* X
//--- The OnTick function only informs us that we have a new deal
/ ^4 m- q1 I# b9 Y  lvoid OnTick()
- G$ z$ }) j" ?; ^* a- ?. }% G{
$ f. K# K$ M# e6 U$ dtem_tick = true;2 U7 W& B4 c  K( x' }2 @0 r
}
  U( \- }! P: m  u8 y9 Q//+------------------------------------------------------------------+8 E& y* x- o/ a- c) U6 L. `
//| Expert Advisor main function                                     |
4 n8 a" O# w4 d* t1 \2 d% [//+------------------------------------------------------------------+
0 I0 H& r  u; k6 Y* Wvoid OnTimer()# o/ Q& Q3 o) I& k: K
{
: I0 h- e. D% p) u2 O. X- P" qMqlRates cotacao[];+ W9 ~6 z9 Z# f: n/ f) Q  @" _
return ;0 G: b9 A5 M$ q. K3 e7 W
if (negocios_autorizados == false) // are we outside the trading window?- T" O) Y3 v5 \: p& u" {
return ;+ ?- C: n+ y* A4 B* l
//--- We are in the trading window, try to open a new position!
9 g; ?6 u' p- l9 Nint sorteio = MathRand();! n4 S! R% q& x4 P1 b) s9 }
//--- Entry rule 1.1
& m' o+ W' W) `5 e" Z2 uif(sorteio == 0 || sorteio == 32767): F5 V- ]  ?8 J4 x9 `% t$ b4 w
return ;
" ]8 @  ?# F% w5 G9 `/ _if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
& J8 D( f1 m. X' A; ^. I{
3 |7 G4 d  y8 @/ d+ `  v# ~1 b" |- t) F+ Vnegocios.Buy(info.LotsMin(), _Symbol);0 |4 s5 q/ ]" i* h  R# D3 V
}
% c" U4 Q" @: M- ^& [$ l0 Qelse // Draw rule 1.3 -- odd number - Sell
3 [* H8 K/ k1 V! G{
  d9 T( d5 G0 \. V- F+ o9 U) hnegocios.Sell(info.LotsMin(), _Symbol);
& I6 S" p9 W* F6 F0 z( K4 r) p}
0 E1 q& \% o3 Y8 H5 L}
9 S: z5 a" z9 Q" m7 C2 @//--- Check if we have a new candlestick...
: }2 U3 m1 a9 }! ]  w; Fbool tem_vela_nova(const MqlRates &rate)0 |+ w' G: ?5 Z5 \# t
{- \7 q1 F8 e: `/ N& g' R
{/ j. `4 t, w% C3 P" O# f
ret = true;
5 [/ Q% N0 Y2 ?! r8 ~close_positions = false;+ i7 D8 X9 a0 ^( u2 E" u
}: j4 p7 w# O4 d6 T2 g
else
( |& p4 U' s( p8 l# y& n, L5 Y. b{0 t, q2 y4 O/ V6 c# v+ X, A( \
if(mdt.hour == 16)
2 g+ F/ S$ `* Y4 {close_positions = (mdt.min >= 30);
) h& ^' Q: q  O$ I6 v3 l2 o5 v1 @}5 y$ P& y$ v- D) ]" W
}& A/ @* U8 }! }# D) o! J
return ret;
' B& F1 d& f' p}
. _. c% V. i7 e$ Z8 L//---0 S! d* {. J0 c8 k' z6 Y
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
' V& \- t6 k5 p& k1 Z  |$ w. b% f{) B4 G5 T4 ^8 w0 `, P6 k+ }: f
if(PositionsTotal()) // Is there a position?
  _0 p$ Y6 n# T. d3 j: L{; g9 `$ h, [- B% z: C$ Y" ~
double offset[1] = { 0 };/ O) w# X9 k( d
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
  k4 D# d0 a4 n5 W) N  ?; H0 i&& PositionSelect(_Symbol))  // Select the existing position!5 c: ^7 U) @/ w9 ^) T
{
2 ]3 u9 A" ^) b# g6 o( }8 IENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);  F0 t  f9 N1 f6 U% d1 I
double SL = PositionGetDouble(POSITION_SL);: |& f4 G+ u: b' r2 C* O
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
2 o( s2 B# }$ b/ I% b( H. e1 Rif(tipo == POSITION_TYPE_BUY)
1 T6 {+ b* y& |9 d, x{
7 |# h# a. ^9 |$ ^+ q  Zif (cotacoes[1].high > cotacoes[0].high): i3 Z- D" E$ K( Z( d3 D  r! ^. F3 F
{
4 n4 k$ o5 E- J! d: q/ R  L; Ydouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];% W" n  Q( A% z$ F4 p( U/ P+ j
info.NormalizePrice(sl);! m/ Y. x2 p) }" |
if (sl > SL)& n' \" f( j3 {0 ^' V7 H' S6 u6 H
{) l  ]9 u! y0 r6 m
negocios.PositionModify(_Symbol, sl, TP);
- ?' z" f! S- `% D4 m1 f}% U( j% a0 V1 P/ T+ l  p) j& B# |) G
}3 ^; {" m( O9 p, P9 p1 M9 z
}
" Z2 v# }0 D$ Kelse // tipo == POSITION_TYPE_SELL, c& O  W7 N  q, c3 g/ n2 z8 z
{, m6 [- ?. H# _: K2 g0 K  A: v
if (cotacoes[1].low < cotacoes[0].low)
$ m- p: H( |( h. }* r/ `7 B{4 P& \& |1 m, }0 x* B
return true;3 {5 w$ u  `/ Q  U: D" h" _
}1 l% r6 E6 a- s& g/ r2 ?0 K
// there was no position
3 j" g; d3 i: {: d/ [- A) greturn false;3 ]+ Y- X: N$ |! z5 N/ J
}$ `  F4 J6 J2 e& I4 g  I
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。9 z3 ?3 f1 u9 M+ b; I& b% s$ G4 g
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-10-21 19:23 , Processed in 0.380930 second(s), 32 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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