私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
* @3 h' }. e+ W- ?在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。' d# r" Y+ h7 L/ g! t
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。: k; I7 ^5 r/ U4 J7 k/ }
以下是制定这些规则的代码。7 }' ^% @* {6 n) {
//--- Indicator ATR(1) with EMA(8) used for the stop level...9 O, N' f; l+ g* Y. X5 z3 P
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
& @' n+ y: F  [, b7 {int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);; ]& }/ M5 w3 J5 R
//--- Define a variable that indicates that we have a deal..., Y9 k3 f' o5 x; m
bool tem_tick = false;
0 |# B  o0 _2 k7 Q6 U4 {* I/ |//--- An auxiliary variable for opening a position. g& E9 o# u" n8 }& z$ d5 d
#include<Trade/Trade.mqh>
2 Y  C4 y. R) q! X( `#include<Trade/SymbolInfo.mqh>( \/ |; o; l" T
CTrade negocios;1 z* B' T+ K/ ^( W& e2 W; m0 M
CSymbolInfo info;
3 ~5 h) Z$ w) M$ r4 ^. x//--- Define in OnInit() the use of the timer every second  G0 E; Q- t1 ~6 L. c" D
//--- and start CTrade
" s! R9 l& b4 e1 m- a9 yint OnInit()8 r5 U) H5 E6 `0 k
{: l* m4 V, d2 w4 i% p) R" V! N3 ~% e
//--- Set the fill type to keep a pending order' ~/ {, R% O, a  q# l' |
//--- until it is fully filled
( y5 M. V6 |+ f0 X# {negocios.SetTypeFilling(ORDER_FILLING_RETURN);
+ A% L$ p% ~1 S+ `. L% q//--- Leave the fixed deviation at it is not used on B3 exchange; O3 T& a3 i& e4 V- o0 A' u, L
negocios.SetDeviationInPoints(5);7 f3 @% Z! r) ~" C
//--- Define the symbol in CSymbolInfo...
( c& K7 P5 L2 R& M, B, ?info.Name(_Symbol);) k0 t. d) @. B3 n* v' f1 ]
//--- Set the timer...8 s/ R- ~9 f: m; f: x
EventSetTimer(1);" l2 V( Q5 V3 d) H  d
//--- Set the base of the random number to have equal tests...
+ m2 T  v8 L4 n. a  p. s% z0 p4 qMathSrand(0xDEAD);0 ^$ P& @' @; c9 j# J
return(INIT_SUCCEEDED);' n; K8 b& W  @% A
}: g- [8 t; P4 t5 m
//--- Since we set a timer, we need to destroy it in OnDeInit()./ y& o* M9 Q9 E! Z' c& j5 E+ w
void OnDeinit(const int reason)
- O' A% a, T, r, z{
6 R! u% U6 N% pEventKillTimer();
& t2 N0 U  ?" ]6 w3 q}
1 o, Q1 p, T" h8 _3 _, {//--- The OnTick function only informs us that we have a new deal; u) F' }/ }9 H! d/ k  T) ~: A* a, x
void OnTick()
: V7 I1 c, r' P( j9 r{* N3 f$ N2 g6 W
tem_tick = true;: m  W$ v0 J$ k9 A8 e1 ~$ ^( ?
}* q5 [* R3 a) I% u# \( p7 t( G8 I
//+------------------------------------------------------------------+
3 [3 ]& N  t8 L% x//| Expert Advisor main function                                     |4 a4 }  b6 p, N/ s* D9 Z
//+------------------------------------------------------------------+
: P. E+ [0 w( O4 M8 w% u9 [void OnTimer()" e. B  X. c" Q* ]
{' e+ Y* ]% S9 P# i. F' t
MqlRates cotacao[];, P. J5 o1 Q( W4 I5 z; l
return ;
9 a5 ^# i6 T, f/ S( Xif (negocios_autorizados == false) // are we outside the trading window?% C: R) q- C8 P$ q: e* v/ B+ q8 N2 E
return ;
; B1 C( F1 I0 U/ Y, D$ t//--- We are in the trading window, try to open a new position!% ]3 t6 f8 N' x1 \
int sorteio = MathRand();7 Y( ?! V$ G) ?
//--- Entry rule 1.1$ f: L& j  N7 @
if(sorteio == 0 || sorteio == 32767). M7 y( W' b, w
return ;" R: t* E  R" Z) i
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
8 [1 D& Y8 D8 b. w- l) g{
! O% f- @4 C5 @& F( c- Knegocios.Buy(info.LotsMin(), _Symbol);3 |: ?. W. [8 v9 G. l$ M( {% Y
}% b0 H* j$ X( n" S# K$ ^% w7 g& b6 x
else // Draw rule 1.3 -- odd number - Sell2 B0 Y; L- J* o. j- V- a
{
0 H. y* z+ ?7 u& ~5 g: X4 Lnegocios.Sell(info.LotsMin(), _Symbol);: c" K+ J4 c8 A  K" S7 x1 n9 F& y* j
}: o3 M* s' T% r; d$ N$ D" Q
}
' Y1 A+ e1 P+ I$ [: m& T. \//--- Check if we have a new candlestick...
2 K+ n. T8 F2 `$ i. i( tbool tem_vela_nova(const MqlRates &rate)
6 }$ @- z( h1 u" m{5 B8 W  ]' h: K' H0 I- a
{( W; `3 X& O' P  n
ret = true;* F7 q0 n. Z& i; w: [
close_positions = false;# b4 M9 c' H. m) g0 z2 ~$ `3 A4 O
}
2 g2 J* A5 q1 g4 {* |else
5 k" d* n: K( }2 C) L) p{, R9 c* Z/ q0 |5 a* {
if(mdt.hour == 16)
# G& |) ]$ A& x' ]close_positions = (mdt.min >= 30);
  J# {" U4 |* z5 s. Z" C}
) T, C: s8 o; Y, h7 f; ]' f}
$ S7 C1 {7 q3 q4 L$ s) H+ vreturn ret;
, \4 y4 p! `* s* F& u3 r}: ~" j8 L% p! s* c) ]! @* [
//---
  Z( d! `1 m4 X0 i. M. Q7 ^bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])* i0 y/ l7 M/ ^+ b# ^8 p
{, G* B* l; ~! }1 B1 R4 i- }
if(PositionsTotal()) // Is there a position?3 I: b- ]( n3 y5 N8 L  h% `$ g) v
{
4 [% S# v7 V0 Xdouble offset[1] = { 0 };; ]% f2 a! `7 v9 z& E% e
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
  W, c" B& g2 F: {8 F! A&& PositionSelect(_Symbol))  // Select the existing position!/ A; G( z  F9 o$ r. Z
{' J  G4 A5 c& }( o* b! _. D
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);: R. K3 L7 Z) A) t! f8 y1 O
double SL = PositionGetDouble(POSITION_SL);
2 ]' U6 ~- P/ v1 b$ ddouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
9 f- S+ K+ B$ I& O/ ?  B2 |) J. Kif(tipo == POSITION_TYPE_BUY)  r1 S/ q0 P7 Z7 p4 E9 P
{, j/ H: R8 u- J. R! }
if (cotacoes[1].high > cotacoes[0].high)3 B3 \5 C* N! n3 i; V1 D. q
{" X8 E/ \( J) }: l8 r6 y8 M+ z. [
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];4 Q3 a* T$ C1 {1 l: T; X5 s3 w+ D
info.NormalizePrice(sl);
. s& o/ C- R/ B* \% l( r' b1 |if (sl > SL)
+ M& D; Z9 w* g+ z# p# }' X{) z! v1 g+ m8 Q$ z8 Q; |
negocios.PositionModify(_Symbol, sl, TP);
4 Z/ p$ N: w& F# ?: ]# A" A6 A}4 i3 P. x7 _; r3 B4 ~! }% T, I
}
* w5 M4 U0 @3 \7 y}
/ C4 s7 T5 B1 D6 `else // tipo == POSITION_TYPE_SELL) L7 ^9 B) V7 O, J% u. N
{
& S1 }; a3 e1 b- iif (cotacoes[1].low < cotacoes[0].low); U/ s$ C: M  K4 D' ?
{' j+ z2 J" ]; b& u( _
return true;6 m) N# ?1 j, M+ t# {
}
" r* ~- P4 x3 y3 E. d8 Q1 g// there was no position& r+ a5 D- b, I( w! Q# A  a& x; Q
return false;
2 e7 x7 x' I7 q+ O8 V% }% N5 {}
+ E) e% V, N. `$ q  ]' Y我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。" H3 k2 K* M: O0 f, 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-4-5 10:14 , Processed in 6.738095 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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