私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
- \7 `6 Y- |1 g% N/ X8 ?+ H在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。- ^$ ^. G2 T! c7 ]4 a6 m+ L
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。+ k' ]$ b* |+ U; m8 @* M5 r7 f
以下是制定这些规则的代码。' b8 ]% f7 B9 ?2 S! [* v; ?6 r0 y
//--- Indicator ATR(1) with EMA(8) used for the stop level..." ^8 M# X' ~: e1 @# P
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);& U3 W2 _2 F7 @( o1 S! x5 f1 U
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
) s0 q5 e& F! u: ^) p//--- Define a variable that indicates that we have a deal...6 i) ]4 x" v" r* k
bool tem_tick = false;, e$ P' O$ Y8 ^6 |
//--- An auxiliary variable for opening a position% I' E/ W/ ^! b# j
#include<Trade/Trade.mqh>. E( G, y, b& Z. t- s! ?( ], j
#include<Trade/SymbolInfo.mqh>6 E& S% B1 y/ B) b+ b; Z5 i/ p# p
CTrade negocios;
, {5 y, ?3 O- X6 O# SCSymbolInfo info;
" W* t' Y) i5 p8 C//--- Define in OnInit() the use of the timer every second! ~0 O' R) O" S, n% @, O& k3 I' u$ {( b
//--- and start CTrade
6 B: w$ s2 O5 l  u. `int OnInit()7 Q$ L2 o) O) c' m
{
# q* T0 A5 a) n) K7 A" v$ c//--- Set the fill type to keep a pending order* D7 I/ o% b5 {, I
//--- until it is fully filled
* [$ Q# f1 f2 Q4 |  r; A+ x0 O  Unegocios.SetTypeFilling(ORDER_FILLING_RETURN);7 _* q! L1 W: M
//--- Leave the fixed deviation at it is not used on B3 exchange. f8 _; p) D. O0 F. |. C
negocios.SetDeviationInPoints(5);
" z7 c" |* c( _$ a1 w//--- Define the symbol in CSymbolInfo...
, ~! l- S7 {9 W4 s. O& l" j. U) g/ Uinfo.Name(_Symbol);% f/ s% d, Z3 G" p: o
//--- Set the timer...
, ?& D4 @9 k+ aEventSetTimer(1);
3 e' S7 U) Y8 `+ C% S//--- Set the base of the random number to have equal tests...! J2 S9 D  r( w7 b* b$ o
MathSrand(0xDEAD);
7 n, ~! P+ A" rreturn(INIT_SUCCEEDED);
2 p  x' N. r/ n" X4 c& L}
8 B1 {3 p; d' a0 M$ I5 r//--- Since we set a timer, we need to destroy it in OnDeInit().
1 A$ @# s/ T8 z& a- f4 `void OnDeinit(const int reason)
+ L) k( D: T7 Y$ c4 [' r% I9 m: L{
( Q( e$ n# f) Y! sEventKillTimer();$ ?* h5 Q3 ?5 F
}
0 H% I- p* M  A+ x//--- The OnTick function only informs us that we have a new deal
: K6 ~; z0 y$ D; M! e/ vvoid OnTick()! e+ K! e4 m; D9 j& d4 ]# k% N
{8 ^8 R& K% e* r$ x
tem_tick = true;
: K9 b. ~4 b1 ?( H) k* d' e}
3 h( ]0 T. C( }//+------------------------------------------------------------------+
6 A( |8 p# h0 r$ ]! Z8 L//| Expert Advisor main function                                     |' J% b1 ?2 P0 C5 D1 E7 l! {
//+------------------------------------------------------------------+, E& b( s. s) D" F/ t# x
void OnTimer()
: I/ v3 a, B# S{1 s' X- c5 _% Z1 p" D
MqlRates cotacao[];9 d1 L  t% u3 B+ p. ~$ X6 k
return ;
1 |, ~# ?1 t2 `/ a- Jif (negocios_autorizados == false) // are we outside the trading window?, P/ h2 _2 {. Q9 ]* D
return ;( F* B- E3 _2 l: C6 u9 n5 }
//--- We are in the trading window, try to open a new position!  T% k4 u! D7 w; N$ p! Q- z
int sorteio = MathRand();
6 o$ b9 P! k7 Z+ g6 W* O//--- Entry rule 1.1: u; X9 ^+ H8 R, G4 p$ G
if(sorteio == 0 || sorteio == 32767): s# C: c6 h. v9 A% z; d5 J. r3 y
return ;2 ~# S) }1 u$ [+ |+ j
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy8 G6 S" f; Q6 q# F
{
, A9 {; U/ H5 l3 a& P& c+ Y) gnegocios.Buy(info.LotsMin(), _Symbol);
9 k- O- A; O  M. P, ?}
: ?% J8 r/ o! g- \0 n% I0 x# u' ^else // Draw rule 1.3 -- odd number - Sell
7 Y8 m% D+ H) i! _# J{
+ m' z2 Q' \8 s" t( {+ knegocios.Sell(info.LotsMin(), _Symbol);1 f5 M6 k6 P9 P( m( u) \* h* |
}
% D: ?6 l! b$ r# @6 V}( t1 A% N) U) n1 a" W
//--- Check if we have a new candlestick...* F* P/ v9 H2 C
bool tem_vela_nova(const MqlRates &rate)3 @1 \' g9 r  {1 W
{
- c; E2 J* m9 o# m{2 l* e6 Z5 G0 v0 t
ret = true;
# P+ @* j6 E+ ~* l* C* w6 ~! r0 Tclose_positions = false;' G! E/ s/ f! h$ t
}
; U- P" b9 B% r9 s" Zelse, a% V7 B% v$ C9 m! v
{
: O# r7 R9 p9 T3 s& }if(mdt.hour == 16)
; |: B& h1 L* f) @' W8 ]close_positions = (mdt.min >= 30);4 |% g+ `3 r+ l, C% m6 |; w
}
2 r! }- g3 q2 o}" r: K% b3 b; E
return ret;. O5 `, S2 W2 }
}
* o1 W0 g' Y& y6 H9 N- I+ V//---, t* ?6 E8 n& Y+ K  b
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])$ c- V6 x6 ?$ K! Z! I
{6 G, O* ]6 E& ?& D, T
if(PositionsTotal()) // Is there a position?, \: v, X7 Q" e3 e% S4 j
{5 V; {1 u7 M8 U+ V1 z
double offset[1] = { 0 };& y& j$ x2 Q( K) Z
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?9 s' n  s; \1 g% C, B
&& PositionSelect(_Symbol))  // Select the existing position!/ [8 @0 `0 _( E! d1 l
{
: S; {! p, G" x: {( y4 cENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
  U0 Q6 [- R( }+ ?double SL = PositionGetDouble(POSITION_SL);
7 ~2 B) r9 A$ N% U2 {& gdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
% v% S8 P+ t3 R, Qif(tipo == POSITION_TYPE_BUY)0 B; R; e/ M2 t) [; I$ u1 l: m
{
  x$ T- w% q+ s( c! dif (cotacoes[1].high > cotacoes[0].high)5 }9 U8 d- ?/ C, `; D% o
{1 M5 Z% J& N$ H/ ^5 Q# t
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];4 @0 l! s9 x" P. F, G
info.NormalizePrice(sl);- M. V4 m; H, R5 i
if (sl > SL)2 D: [+ [' y/ X/ N4 E6 o  E; I
{! }  Z# m) K  S8 o1 s
negocios.PositionModify(_Symbol, sl, TP);. o+ y: S% O% @( U" X; j
}
# f" i0 ~4 Z* k8 t; D8 l0 C/ Y}) W9 S: Y. T6 u3 I; M$ P
}1 r8 o. o. r, o4 k6 L6 W% F
else // tipo == POSITION_TYPE_SELL2 P* O$ `8 [# w3 o. S4 ^
{
6 b: Q/ K( P2 h6 @2 E8 j# fif (cotacoes[1].low < cotacoes[0].low)
( ~$ k1 n9 D3 M, I$ g{' ?7 X; \2 n6 m0 L( T0 Q( q
return true;
9 F8 ^$ H: h/ p& u) W}
2 M0 O( d- i% K- `8 a% _8 w// there was no position
: B6 T+ N5 v% Q  D6 ]4 ]0 preturn false;5 K/ v: H7 O1 e9 J6 F
}
: a* T7 R7 a6 y, M; w我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。2 L" o  ~9 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-4-25 22:34 , Processed in 0.934236 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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