私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA) p" C9 Y, m1 H0 O' h9 c0 E
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。- o$ T2 n. h. X* E( c" y1 e9 l4 Q
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
" ^0 a" ^7 x8 h; o/ T1 i以下是制定这些规则的代码。
3 V% ?4 L5 U. b9 r$ Y. @% L//--- Indicator ATR(1) with EMA(8) used for the stop level...
" f, U) k" v& l/ R* c% C; qint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
, r- p* }$ j) h) u8 a& e' vint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);# K) P+ b7 P6 @1 n5 c2 D& Y" u: N4 K
//--- Define a variable that indicates that we have a deal...
5 l& l- W" d9 e7 @. g! qbool tem_tick = false;
9 A5 O% Q+ m# C* u//--- An auxiliary variable for opening a position) I2 i( L  g  v/ f/ r8 X2 ~
#include<Trade/Trade.mqh>
- @( r0 I; k9 o1 {& ^#include<Trade/SymbolInfo.mqh>4 v4 U. T- J( D8 w) Z2 h' x: d
CTrade negocios;0 @. K$ l/ ]8 ^. V
CSymbolInfo info;
! C3 a8 Z1 R; ^) L5 e/ b) t//--- Define in OnInit() the use of the timer every second& x( T) C& g" V! U+ ^' e
//--- and start CTrade" l/ I1 m, W. }) `: |9 d% z, `
int OnInit()# s: q, g4 ^4 t8 l; c. w0 M
{
5 T6 l3 O$ s( \+ O, z//--- Set the fill type to keep a pending order
5 i. L+ i( y, f$ e$ G7 H. H//--- until it is fully filled
' g" f! J  f6 m0 P6 z2 A8 inegocios.SetTypeFilling(ORDER_FILLING_RETURN);+ H% d' j9 Y( Q7 N3 I) f
//--- Leave the fixed deviation at it is not used on B3 exchange/ Z( E" z8 j0 u5 o) S5 t5 _) x
negocios.SetDeviationInPoints(5);/ L' Q/ N" |: I9 V+ C% _/ f4 ~8 o  ~
//--- Define the symbol in CSymbolInfo...  H7 z; v5 G1 s1 \4 d
info.Name(_Symbol);
. x  I6 i. G- U; o- `//--- Set the timer...
) J; C& ?4 G' g' e9 rEventSetTimer(1);; ]" G+ e9 C+ b2 c. Y  E6 F
//--- Set the base of the random number to have equal tests...
+ h3 X0 R' e6 T' e  }! jMathSrand(0xDEAD);7 a6 Y( |( n) _0 w& h/ D
return(INIT_SUCCEEDED);
4 O/ _4 Y. {) ~- ?( h/ S6 h6 ]}  Y# l& B! E/ n- m
//--- Since we set a timer, we need to destroy it in OnDeInit()." N9 K7 p! F. z
void OnDeinit(const int reason)
0 M- n; v& V9 }* N+ B4 A( n& m{
: t2 Q+ @) P$ q; S( {EventKillTimer();* [% ]7 G& C- z* ]! y
}
  Z- F# D; I! H" U- n7 H//--- The OnTick function only informs us that we have a new deal
! ?" m/ c$ W8 x" w( Dvoid OnTick()* B- [) S- J( E% H8 K
{
& J1 A9 D6 \9 f6 q/ o2 \8 Q' |tem_tick = true;1 v7 q# s3 @6 [1 n: j
}: h+ d5 G# g9 p' Q
//+------------------------------------------------------------------+% ~5 E0 y# M8 \* E3 A& ]
//| Expert Advisor main function                                     |
/ u% n/ D$ `# ]8 c//+------------------------------------------------------------------+
8 H7 X. B* Z/ p9 E9 tvoid OnTimer()
* C3 B7 L$ d) N8 E{3 Q* l) t/ o6 N% T3 @
MqlRates cotacao[];
' F' M2 ?& d5 E) i" o" ?8 `return ;
, [! f1 k* ~2 D; `/ j# g. Jif (negocios_autorizados == false) // are we outside the trading window?* d2 B5 a5 j; w1 c) X3 S& ~
return ;
5 g) ]. c- H- Q& ]! Y//--- We are in the trading window, try to open a new position!4 F9 W# l7 @7 Q& z
int sorteio = MathRand();
' f* w3 x7 n0 N0 Z) E* z( ~, m//--- Entry rule 1.1
- |$ D. e8 l# V/ [if(sorteio == 0 || sorteio == 32767)3 r2 t' n7 F# z' e8 q! N
return ;* v" y# P1 }- Q; [6 d
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy# o: P2 N+ V; h9 ]
{5 e0 D5 K& f; f
negocios.Buy(info.LotsMin(), _Symbol);
' U1 {5 t. A' Z( t+ H* a2 S! y}
3 \: f3 B. }% ~- Q- D; t" gelse // Draw rule 1.3 -- odd number - Sell  g+ h, ~; Y3 W6 m' P# f
{7 m: r5 L7 E3 A
negocios.Sell(info.LotsMin(), _Symbol);6 N% t; j8 \3 i! A' v# Z5 u+ ^; }) N
}
) O3 a- z- d* j: Y- [) t}( z, P) k; ]! H; A+ G% S
//--- Check if we have a new candlestick...
/ D/ w* R+ P" ~" A6 a4 Lbool tem_vela_nova(const MqlRates &rate)7 o, v* ^# b4 n0 J5 b
{* m3 G& z( @( P! a8 e9 s( `
{
. d8 H7 E: f& \8 _0 [  wret = true;
6 E. H1 U) N( Z4 t5 M8 j2 Qclose_positions = false;' l  m4 o" q1 L  w
}
2 P& l, o( s% a  O& x" J4 Welse, V' x+ \4 d: o' j
{& y) Z. P7 o( t* G' @
if(mdt.hour == 16)$ W4 n6 D2 \+ N+ A9 G# g: M: y
close_positions = (mdt.min >= 30);0 }, `/ Q* F6 e7 l
}
! k. I0 {( }2 Q: s5 `- S}' @! I& w6 |4 h3 s
return ret;/ S: ?* K2 w" U2 j
}* b! ?, z  E) U* n- ~
//---
% a3 N4 x0 v1 sbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])6 `3 K/ j( ]+ z  Q& b
{- O% {& ~, i* q; l
if(PositionsTotal()) // Is there a position?
: o7 k( R' F/ [: Y{
! K9 c3 R& ?  l% c6 |3 Wdouble offset[1] = { 0 };+ i7 i& J, ~/ _$ r8 }$ l
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
) n( Z8 O* _: x8 R1 E9 s&& PositionSelect(_Symbol))  // Select the existing position!6 x" e- f( G' u# V
{
$ Y" P  R4 A/ R$ v" q: QENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);& ]* T6 ~" b# W: s9 ^* {2 K
double SL = PositionGetDouble(POSITION_SL);
5 J: s' l# v4 M2 Mdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));) H7 a5 j6 N  R8 g6 u: f. y
if(tipo == POSITION_TYPE_BUY)4 S  i) I* x1 k! i4 b, n2 v
{
& s1 @/ F5 ^. j- }/ S) p0 P( J6 H# Nif (cotacoes[1].high > cotacoes[0].high)
+ `0 N% V$ w1 P  b2 e{
. i  }" I. F4 l6 E, idouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
& D3 C4 Q9 @7 m) N6 minfo.NormalizePrice(sl);
) h7 Y+ G+ r4 a8 Q7 Gif (sl > SL)
9 O' F1 H: k5 n) g/ v6 ~3 a6 l( T{  L* L, I, p4 k4 B$ f1 Q
negocios.PositionModify(_Symbol, sl, TP);
4 C# v8 |% V3 k( U2 U}9 [1 Q3 G( m! f" \$ i% Q7 h8 I0 q
}( S1 g, J; \8 @, B+ X
}
) x/ [7 S. B1 H* z, ?else // tipo == POSITION_TYPE_SELL7 n: S) }9 k% Q- j) S
{
/ g4 |8 [0 b  d- dif (cotacoes[1].low < cotacoes[0].low)
; n8 X; n; S  d% w{
2 g2 j" Q& w( j3 N/ treturn true;: [2 j  Z2 B5 u& I/ K  K
}
' T% _0 [$ O; m8 |" G0 l// there was no position6 A- h! }. \0 V5 m
return false;; g) D% O; i' h& Q" U
}
$ U3 ]8 o* z: [9 z1 U$ H) Z我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。4 Y( K+ F# o  m/ C" |
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-25 10:04 , Processed in 1.312709 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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