私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
! t6 F* s0 g: o, L$ `8 P/ _在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。" b- {$ b! W, O; J
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。# {! t$ O! y1 \3 m, E2 [9 x
以下是制定这些规则的代码。
: a/ W+ E! w% c% c& N7 l//--- Indicator ATR(1) with EMA(8) used for the stop level...# k  e3 g) ~% M! \* Z
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
0 e3 N6 Z4 r) e7 D. z6 [8 pint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);( B5 n) `) L* E
//--- Define a variable that indicates that we have a deal...0 D9 O1 \# j  s: m$ {2 S- ?. t3 H8 O- A
bool tem_tick = false;
- m9 ~2 B2 ]" D4 b- f//--- An auxiliary variable for opening a position
9 |$ c* Y9 j$ G- D* q! T) p1 Y, T#include<Trade/Trade.mqh># u) e1 U! b# A
#include<Trade/SymbolInfo.mqh>
0 u5 n7 ^! [& |. CCTrade negocios;8 j2 ^( y9 L. {+ C9 y+ l# q
CSymbolInfo info;
! K0 ^" b4 I" j' M6 q//--- Define in OnInit() the use of the timer every second* x8 z, |3 l0 C, i. Q3 w, h+ n
//--- and start CTrade9 e5 f3 F$ o" c) L
int OnInit()! A# J3 q7 G7 y, T  m7 M
{1 y2 K( i0 s2 c: {! I
//--- Set the fill type to keep a pending order1 _8 }# \( c. ?
//--- until it is fully filled
1 P5 {, G, _+ b  Q9 q9 s: p) bnegocios.SetTypeFilling(ORDER_FILLING_RETURN);! Y, Z6 q$ t' N
//--- Leave the fixed deviation at it is not used on B3 exchange
* ^, R# `: p+ c8 [  fnegocios.SetDeviationInPoints(5);9 d* g6 A5 w% Q+ [
//--- Define the symbol in CSymbolInfo...% Z9 x. x; |- h' a. G+ M# q
info.Name(_Symbol);8 J# [- K) m9 i
//--- Set the timer...& B* ?2 ?( u9 Z! e6 V7 E
EventSetTimer(1);
  {- J2 S4 ~% H; t  o( x# h//--- Set the base of the random number to have equal tests...# S/ q$ Q$ O1 T2 x
MathSrand(0xDEAD);
' N" m/ S  E/ c7 p. ?return(INIT_SUCCEEDED);
! e/ i" G4 X5 e0 f  g}
' i+ n4 N! n+ W* O5 ]/ m& `/ W//--- Since we set a timer, we need to destroy it in OnDeInit().! X1 U! z% e- j
void OnDeinit(const int reason)$ {% x9 J" g/ t7 @
{
4 l4 P8 U, M- y1 Y( g6 E) z# oEventKillTimer();
9 ]' }+ D/ Q6 h/ K2 N( k0 T}. E8 s8 ~  s' f4 s
//--- The OnTick function only informs us that we have a new deal! B' `8 T: E1 Y. j2 P( g/ g& G
void OnTick()- B' x" c( `. v) f' O
{0 p- y* L' L' t% v/ U
tem_tick = true;
5 S, y  V* D7 C+ h}/ I. c/ [8 |8 q$ P: r8 W
//+------------------------------------------------------------------+
4 b2 w6 s5 Z2 i4 C0 x* y) q//| Expert Advisor main function                                     |4 l$ G2 u( t5 C% g- Y
//+------------------------------------------------------------------+
- Q) n% C1 Y0 {8 S/ Yvoid OnTimer()
! N' n1 J5 V: x% Y3 ]{0 S% m$ B( N, U
MqlRates cotacao[];2 I) B& O1 P+ w2 s$ S2 S5 L" v
return ;
. _# u; a) S. k2 X* yif (negocios_autorizados == false) // are we outside the trading window?& L% B3 j- R( _& u
return ;9 M, M( J+ b/ |# s8 }' E) e2 w, c
//--- We are in the trading window, try to open a new position!, p, i, w. l) {: }
int sorteio = MathRand();
) h- J; \3 A0 _; d% p& h( k' p2 _2 i//--- Entry rule 1.1
, V7 F6 Y+ {0 {( p' xif(sorteio == 0 || sorteio == 32767)
) v; o" o: m" _return ;
/ }6 |3 e- l+ t2 ?* w5 lif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
2 N, l0 H* T! y{
0 ~- ?! \; ]( Knegocios.Buy(info.LotsMin(), _Symbol);, @' H$ z) _7 U3 p
}8 l5 p5 g/ U! p/ H( K7 n
else // Draw rule 1.3 -- odd number - Sell
2 {' O& [- g$ b: H% U{' ~) q  v# h) k" M) ]
negocios.Sell(info.LotsMin(), _Symbol);
7 z: t3 c' c4 w% N# f( H}
" T& c: X/ K9 h, h5 w) _. I}: h+ z( d, W& @% \) g; G  s
//--- Check if we have a new candlestick...
# V8 D/ V! n, Bbool tem_vela_nova(const MqlRates &rate)
( O, ^; g$ V) O; C0 `{5 @# o$ I3 v* |! M# J; e
{( |# U3 z2 W6 z# @9 |
ret = true;9 a& `, d9 A) @5 X# [9 f! f
close_positions = false;+ d4 ^" p1 P' H* R: }* n5 ]3 a/ N
}. J8 t0 m! ]/ c* `6 ]2 \2 N
else
+ V( E- T( u+ y* R) }, b0 ]% e{
% W* D! f( ?9 E& _" I2 Hif(mdt.hour == 16)
0 A: ~+ _' f- r4 r/ M& z( eclose_positions = (mdt.min >= 30);
3 ]8 i7 V  }# f}
& z( }( |, W5 I1 g: [+ x6 L}
0 D7 t" R; C0 R' W, G, {return ret;. V2 f. w- I- K2 h5 N6 M% C
}9 N6 s9 l/ j* D0 I) e$ A
//---7 W! Z6 |0 u6 j" }. q
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])8 d$ g3 Y; X- e! Z7 e' G1 `
{
! |4 P* u. n5 B1 u6 M* p( Z3 Iif(PositionsTotal()) // Is there a position?' [. R7 Z5 X  l2 G! L( I& q5 j* x7 Q
{7 a" i0 Q) F$ i4 J% E, X
double offset[1] = { 0 };
' N+ p, Z& {' j- |1 c6 y* Dif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
4 M, W* R7 I) W% K, ^2 `/ ^3 d& R&& PositionSelect(_Symbol))  // Select the existing position!. U2 _- S/ f' L' V7 T( Q- N: M* l, @4 U( R
{* `, T( W: u3 A6 [# o
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);- x: o8 q! U: J! m$ Y( k- B
double SL = PositionGetDouble(POSITION_SL);
4 Q0 N; Z( B4 i. I, G" ~- r9 jdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));5 }1 O: U: I7 c& w5 K& z
if(tipo == POSITION_TYPE_BUY)0 l6 r4 ]- Q5 P
{6 u. M. [6 V3 D9 g# m1 O
if (cotacoes[1].high > cotacoes[0].high)9 P! k: `% Y5 P2 o
{
8 W% t! l' M7 Ndouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
( B, L, H5 A3 |) ]9 `, uinfo.NormalizePrice(sl);- D. @! i: i, z2 B
if (sl > SL)
8 P, h% H4 L9 o9 `/ p% r{
9 s" z- E. y/ f, Znegocios.PositionModify(_Symbol, sl, TP);: ]  Z- H  t7 u' b: l+ r- w* b+ d
}9 I# E6 `) O" P4 h: j3 R1 F
}
& }3 B. i' L' _}
* J; P+ x  [/ s* M8 Uelse // tipo == POSITION_TYPE_SELL
% E0 Q) d; ]. B. G8 C4 |{. q; ~0 \2 n: _
if (cotacoes[1].low < cotacoes[0].low)$ Q" @' s  w# N
{# L5 b: Z( d& w; J* f
return true;, g9 G7 f6 |3 u! e: N5 M# e$ u8 o
}3 X. Z7 P4 q5 h3 I
// there was no position$ T/ W2 S/ ^/ D1 V) j# {1 x
return false;
8 X, U/ t- F' v# q: U1 l, q}1 A  s/ L7 \9 H) }: j2 O- j) v% p3 F
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
+ ~/ a8 p) g( Z; U  c2 |到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-15 03:16 , Processed in 0.401922 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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