私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
4 L9 S' M8 T: M+ P7 [% n在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。) f# I1 p+ Y+ O1 D, A- o" t
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
2 h$ E& o9 v! a, J+ g, W以下是制定这些规则的代码。2 Q4 K' U- t8 Y; `
//--- Indicator ATR(1) with EMA(8) used for the stop level...) _4 w, O" I/ f  L( q# y( ~
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);$ _  ~1 ^  G# r7 f  i( E: j
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);5 b& L  {1 Q; v! f# p5 z# p
//--- Define a variable that indicates that we have a deal...4 s. H' u: @& z/ J/ _
bool tem_tick = false;" B/ W" @) b9 R
//--- An auxiliary variable for opening a position
* ?# j: X: i+ g0 z) X: y* A#include<Trade/Trade.mqh>5 w$ M0 Q: N( _) F- o9 t
#include<Trade/SymbolInfo.mqh>
3 l: f1 G7 w, N3 ACTrade negocios;
! O" p: E( ~7 g+ A/ O$ yCSymbolInfo info;
% @3 q$ r0 H+ ]; f, p//--- Define in OnInit() the use of the timer every second8 \9 L# M& w+ t# |" K
//--- and start CTrade
: J$ B# {9 t/ K8 Z3 b- sint OnInit()$ a* d! A" g; o- K1 x, M
{  _8 v% y  ]1 s5 L/ p( @
//--- Set the fill type to keep a pending order
5 L* f6 A  G' i+ T! g//--- until it is fully filled
% o* r& O5 G. Y6 @) Dnegocios.SetTypeFilling(ORDER_FILLING_RETURN);
8 \" _$ e$ W! q' y//--- Leave the fixed deviation at it is not used on B3 exchange
: B: X" t& ]2 V! b! tnegocios.SetDeviationInPoints(5);
, j. F3 X- x8 O# b//--- Define the symbol in CSymbolInfo...5 \) U# G8 Y" W
info.Name(_Symbol);1 y4 ^& S/ |7 @! z
//--- Set the timer...- l) u5 }2 b' [  j
EventSetTimer(1);; K9 o$ u$ Y  y- X5 i) c
//--- Set the base of the random number to have equal tests..., ]  S& X, _; }" x& Z5 q
MathSrand(0xDEAD);
" W! H( }! t0 |0 [0 e. |$ `" m9 zreturn(INIT_SUCCEEDED);
5 a. e; g. S: J6 O* _}' g! m8 T7 N! J3 S% J) r
//--- Since we set a timer, we need to destroy it in OnDeInit().$ v5 ^/ H5 H6 v% w
void OnDeinit(const int reason)
9 [. F. u& O, E( i, ]+ d{# R- {! l. k8 @
EventKillTimer();
' [) t- G, x' U$ k$ p1 n. ?8 m: E- K}
2 w' U2 j7 }* C3 R//--- The OnTick function only informs us that we have a new deal# l4 x. o9 g% T. z! t
void OnTick()5 L1 ?1 h, H/ b& z; w% O
{
/ J" e& X1 L# O% T1 m2 ktem_tick = true;7 M9 ]9 I9 F6 z$ l& G
}
4 p# t9 f" H5 U. s5 S8 o* e+ n//+------------------------------------------------------------------+4 J  |7 u) e7 R4 l0 I
//| Expert Advisor main function                                     |
1 }* C& b( a" P' y+ z//+------------------------------------------------------------------+
2 L9 w5 a( u8 S& h0 gvoid OnTimer()
2 f( G5 B+ c* r# a; y4 B{4 X) L8 G% C( T% ^7 ^# q
MqlRates cotacao[];
0 S9 G- r/ D  n1 F) p/ Ireturn ;& j+ B$ ]# W7 M# l4 X2 Y& S$ Z7 G
if (negocios_autorizados == false) // are we outside the trading window?; ^5 Q; `7 N% q
return ;. s' U  G# O) u" J( D: n& p% w6 L
//--- We are in the trading window, try to open a new position!
' V) e  y/ G  _, a* k/ ]8 vint sorteio = MathRand();! `6 B7 d& l- Z
//--- Entry rule 1.1
! A: g, C' X! v" D9 }7 pif(sorteio == 0 || sorteio == 32767)
2 e" ?9 O% ]* e  G, breturn ;; K* L* h: T7 s/ Y' p
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
$ O, k) [! v1 T. `9 d( R8 Q{
3 Z/ S: K3 u# O7 `0 `negocios.Buy(info.LotsMin(), _Symbol);
, h- d& G8 H- Q5 F5 ^}. e4 l- E$ A* R/ K$ B  W& V7 ^
else // Draw rule 1.3 -- odd number - Sell& e! D9 k% r4 [1 s+ N9 H2 u
{+ t. @0 |9 n% e# I
negocios.Sell(info.LotsMin(), _Symbol);; g7 r. I  ~# @& a9 o7 C
}
! f3 b( k1 j& Q# f. L/ V5 S7 \# f5 v}, [( ^2 q4 |8 r8 M* C
//--- Check if we have a new candlestick...) |$ x, d! N1 `% o) u& m
bool tem_vela_nova(const MqlRates &rate)! F. o5 M4 Y% i  s9 c, ^
{
5 `! Y4 A  u' f2 Y0 J. B3 w{9 x4 O3 p  @% j, c2 r; {$ z
ret = true;9 i: R8 u* q) E; n" Y/ R
close_positions = false;( ]3 W' r+ \( u% @
}
, K6 X& S$ k; a8 _else4 X6 g9 B) k4 Q+ n, X
{" ~$ H9 G+ g2 G$ l: @3 I
if(mdt.hour == 16)
* p1 C9 I% u9 n4 {6 G% i! p: Vclose_positions = (mdt.min >= 30);5 i: Y- ?* }+ X( S( }
}
2 y$ T# {! S# k; L- C}: P2 r. `+ \7 `
return ret;$ i! _3 ]8 T. M
}: h  _4 @: d  I: e) e9 I# T# q
//---' x2 ^4 X5 z& c0 D
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])) u0 D! `2 J: V# R8 W) G6 M
{
  {' [; W2 ^4 t( N; A0 j' Cif(PositionsTotal()) // Is there a position?2 {, Z! W& H; w) a, i: e
{* D) W* ^2 q* W+ K% l
double offset[1] = { 0 };
6 X6 {1 ?, [: x. u  @: U7 ]) ~: zif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?. A% h  s. h4 u7 u! J& e
&& PositionSelect(_Symbol))  // Select the existing position!
* q7 }3 p7 q/ C# |+ |% g{
  \7 x- X/ @: ]2 s/ t- O( V, bENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);0 c) U. g# ~. H9 O" h
double SL = PositionGetDouble(POSITION_SL);0 ^& u; ?0 y4 Y7 h  U9 E
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
6 F+ b# C! V9 A8 F3 _+ ?  tif(tipo == POSITION_TYPE_BUY); b: \" [$ t& x# p& s& H# c
{% \& _; z# ^( X8 |! U
if (cotacoes[1].high > cotacoes[0].high)
! |3 \/ n* t( O$ g{
: j% t) u! X# _; d5 gdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
* f* H2 T: A" G3 h5 @& U8 p' Qinfo.NormalizePrice(sl);2 d! f: U! }; e4 p% K8 E( O
if (sl > SL)) _4 }# v& w1 y! z2 b! q: P
{( S5 j0 ~3 ?  O) Q" g' _
negocios.PositionModify(_Symbol, sl, TP);
" z% v( O' {) J$ \) i$ s}
% A/ B  G6 Z; p" ^}* [5 a$ W  w8 e' d9 F! y
}
0 N0 x" M' _  ]9 }0 [" i* D% Relse // tipo == POSITION_TYPE_SELL' _6 ~, ]+ D" @  P0 G. g" u
{1 m; C0 [& n) G
if (cotacoes[1].low < cotacoes[0].low)/ Y: o0 a& w: g4 [5 P& f
{9 Q8 T2 H: E9 w$ a4 h  L# B3 [
return true;
1 i3 k: ?% K$ [! Q; l/ c}; P; u3 R7 N4 [2 g) Y9 g
// there was no position
0 s: B5 e8 I7 j* |  greturn false;/ C0 a( A0 h  s* [/ l
}
. P3 R) j1 ]4 |1 B我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。/ [; g: J# u" R; J- S
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-3 15:27 , Processed in 0.415636 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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