私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
0 w3 g/ n( z+ s+ z* q: j9 c在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
' K9 ?6 t2 ]' f0 j为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。  Z' H, f% o4 B# t
以下是制定这些规则的代码。. @4 C* E* z9 W2 t( @7 X) P
//--- Indicator ATR(1) with EMA(8) used for the stop level...
) o' i1 h9 y2 v  w! B, A' j9 Hint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
% F8 }9 ]% r' G8 `int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
/ x1 C( K- b  T7 L//--- Define a variable that indicates that we have a deal...
4 O7 X- d" ]: w# r; Dbool tem_tick = false;
1 R8 o. L; P$ z; G6 ~% i% ]//--- An auxiliary variable for opening a position+ V, k7 X: L& p* t% [" j$ e
#include<Trade/Trade.mqh>
' h  W" ~7 T1 \, L8 _$ t* H#include<Trade/SymbolInfo.mqh>8 d6 |2 i/ y5 r( F
CTrade negocios;6 i9 |' N- s0 I8 V. c2 U% F5 I
CSymbolInfo info;+ X& g% |  }+ C' P9 X
//--- Define in OnInit() the use of the timer every second
: e8 E5 w7 y* E7 U, V//--- and start CTrade5 h& ]5 V/ I6 Z0 ]
int OnInit()1 A( b1 t& _- U! B/ q, m
{: u% l) j( J+ O+ [
//--- Set the fill type to keep a pending order* {) b: K$ v6 }6 j. o4 s7 I
//--- until it is fully filled6 N* H5 \" a: k  e8 P* E
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
$ U9 z1 R/ {% z$ z//--- Leave the fixed deviation at it is not used on B3 exchange
2 v+ ^, C7 J- A& Mnegocios.SetDeviationInPoints(5);" H# S  @3 v" E% `
//--- Define the symbol in CSymbolInfo...$ S% k' ~/ x- S0 z; Y) _5 X8 p
info.Name(_Symbol);/ `9 m) d2 t) r0 u, F  \; c, y
//--- Set the timer...
  z: t, @5 N1 V: [& E0 K$ R9 DEventSetTimer(1);
9 W+ X1 K$ W& Y/ s3 {9 f) Y//--- Set the base of the random number to have equal tests...
1 k) b5 `7 I+ yMathSrand(0xDEAD);
1 {+ d% M: |8 I# L9 V$ b2 L  Dreturn(INIT_SUCCEEDED);
. o4 i# X" ^+ w}
( k" }) c/ c9 |8 B7 x  J* }//--- Since we set a timer, we need to destroy it in OnDeInit().. r2 K7 ^: }9 ~9 h0 v, Y; p$ A  G
void OnDeinit(const int reason)' R1 {4 F3 R2 Q) F3 }3 m. N
{, }9 u5 X: M& h+ c( P( Z
EventKillTimer();1 q7 P  e( u: D+ L  O6 U
}3 a+ l" H& S! ^1 n
//--- The OnTick function only informs us that we have a new deal
# @8 F! {# {" r' ?  Uvoid OnTick()0 q' X: u1 g) S3 @
{
7 s9 |" I: L6 @4 z9 ^tem_tick = true;" k. @/ E. Q  K  A6 S
}( X, N0 p4 u5 i1 d/ ~2 \( u
//+------------------------------------------------------------------+4 i; x5 S2 V+ P+ l
//| Expert Advisor main function                                     |
' c. y9 L* J1 x0 p//+------------------------------------------------------------------+. n5 O' o, x! T+ C/ \6 d7 }
void OnTimer()
( H- U& O8 o& Z0 v" }{
" _# a$ F4 H1 ]* fMqlRates cotacao[];2 E" ?+ m$ T/ c1 D% G8 w$ A
return ;
) o& l3 v: X7 x' N- t% o( Fif (negocios_autorizados == false) // are we outside the trading window?
* x( @9 ~+ j. Wreturn ;& X  _4 y- r& i9 H. a
//--- We are in the trading window, try to open a new position!
2 h# P% l* {+ I" Yint sorteio = MathRand();
  a) O; p1 a$ ]/ H9 c+ [//--- Entry rule 1.1
1 L% k: W0 V/ ~. A) Y7 k+ \if(sorteio == 0 || sorteio == 32767)
( o7 \5 J9 M6 p0 Kreturn ;
( U' q; E* T. H# E7 ~. I& Yif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
8 W$ ]9 `' |2 N, D{
! F$ w* v% `$ P8 i) |4 `' knegocios.Buy(info.LotsMin(), _Symbol);
: J/ b6 d5 @* j3 k! S, b6 E}
8 m9 J" `# g# Z, L9 Gelse // Draw rule 1.3 -- odd number - Sell9 X1 Z) M- O. W" ~6 c
{7 U# E7 B8 a# _" U% b* g
negocios.Sell(info.LotsMin(), _Symbol);1 E- R7 _5 y/ b& f! J) }. z
}8 B& c- \$ @4 p3 J# c
}3 Z( D1 A" P( ?2 C1 |! Z8 [
//--- Check if we have a new candlestick...
# O" @6 W/ l7 S0 i! s& j. _bool tem_vela_nova(const MqlRates &rate)) K$ ^/ X1 U) ^! D! V' b5 t' V
{
" `. `9 v; ?# b2 r1 g) |! \7 p{
6 b6 K" r) `! B4 Nret = true;
! q/ L5 R9 i1 g) K8 N( J1 gclose_positions = false;' F0 |7 v+ T# k# y- M
}
9 n4 ~- w) P; y) }! p5 t) Q7 h, ]else% v( Y: _% N9 V9 U+ e3 r1 D( R
{
& V& n7 C4 A9 l5 ]if(mdt.hour == 16). Z0 K3 _4 c* v. `. E
close_positions = (mdt.min >= 30);! o6 C4 K6 ?% M0 `6 s+ g' y
}+ E4 l' e4 [2 ~/ s& h
}; I, b6 @6 c+ N2 b; K
return ret;3 A" M# z) j$ t9 s
}
/ u: e3 E9 s/ y0 f6 \+ `6 a* m3 L/ J9 f//---
- A/ A; \8 M" ]5 f. E  ~6 pbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
& S- u" w9 j. u$ j* h{5 M+ X' j" N- H: j6 |1 G9 y( _3 ?
if(PositionsTotal()) // Is there a position?" S4 J! j* }9 x2 @5 d9 \
{: }- v/ e0 j" ^! f
double offset[1] = { 0 };9 t: t" A( X/ G" S
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
9 M- L4 {0 |; c3 }& N7 l" A& V&& PositionSelect(_Symbol))  // Select the existing position!
3 a) g. K2 u+ c% p1 Q{
& Q- h- T8 L2 t" L2 rENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
  t: `8 G3 @. }5 N  a$ pdouble SL = PositionGetDouble(POSITION_SL);" G, l8 C% v  i; E3 b* ^" u  ~
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));& {8 m/ d) K) y3 V' k# I; }( Z
if(tipo == POSITION_TYPE_BUY)4 t0 L" z. f( G$ V( g% x
{
5 e/ L0 J( G! o# G" u$ a; ^! y1 @if (cotacoes[1].high > cotacoes[0].high)
0 s! d. r7 h) P{
1 i7 P8 Z0 P6 U" I1 V5 `/ p5 Tdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
  A' ?( U# z3 j, r1 a" x: Zinfo.NormalizePrice(sl);
) y9 I0 C( f5 m7 m9 d$ [9 G+ Zif (sl > SL)
. r3 J$ ~5 W' h{4 l* z8 V( H7 H' E1 M
negocios.PositionModify(_Symbol, sl, TP);2 W! H: V( O& U1 p. \( m1 I
}# M! }! v2 d4 k
}$ k, S5 `' P9 X# s8 Y& e* @7 i
}
; {$ v( H! v( {3 q" Zelse // tipo == POSITION_TYPE_SELL/ ^. V* k5 q. b% `+ }
{
( D, U: m# W# t; f. u' yif (cotacoes[1].low < cotacoes[0].low)
1 P- [0 \0 T3 [{* Y7 j/ ]; T  m) V9 b  s7 M2 y
return true;
' j$ O' w& T) C; z1 ]}
8 F0 c3 v/ d0 Y1 n- }9 J3 p3 D3 v// there was no position9 b( N( D& m3 f- Z! _) J( l) \
return false;
8 k% O% G* G6 r# l1 w}3 c2 ?6 b8 }3 f# j, c
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。% g, ]9 U' `& E; g$ p
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-29 01:00 , Processed in 0.488638 second(s), 32 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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