私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
; M" D' D; ^8 L1 h5 Y在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
7 _1 J& V. A- K% F% t3 U) C为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。8 E0 X& G- q3 p9 J2 b
以下是制定这些规则的代码。" ^. |$ Y& H: m: Y$ M2 N
//--- Indicator ATR(1) with EMA(8) used for the stop level...
7 s8 {8 H$ |1 t1 c$ ~: dint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);, a$ |1 s. ?; \7 f5 r; a7 L
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
+ b4 j& @, Z" G" F6 w' O3 d//--- Define a variable that indicates that we have a deal...; [" g* s5 E8 v" i& J) m. f6 P& N
bool tem_tick = false;
- C# W* D+ \) C9 s1 w//--- An auxiliary variable for opening a position) G. i7 |( W) M, k% r
#include<Trade/Trade.mqh>
8 H( O2 `8 O2 _' d#include<Trade/SymbolInfo.mqh>& {2 j; f2 a$ I
CTrade negocios;) L3 I+ S  g! ]7 I( H7 G1 p
CSymbolInfo info;' s" X: B, ^) x1 I
//--- Define in OnInit() the use of the timer every second/ F! k# t+ [9 A9 g4 Z! f
//--- and start CTrade
$ y1 z3 T! W0 K7 Tint OnInit()  h& h& f0 b" k+ V  I, h; j" y
{* q; t  m0 j/ s* ^. r0 u/ t& w
//--- Set the fill type to keep a pending order) v' z0 h% F1 m" U/ j# I, v
//--- until it is fully filled
1 i) g) H& R( G5 O1 f+ vnegocios.SetTypeFilling(ORDER_FILLING_RETURN);
# ^8 q1 g* H( f: S5 m2 i//--- Leave the fixed deviation at it is not used on B3 exchange: Q, Q3 Z+ \( T8 Q7 C* c9 B
negocios.SetDeviationInPoints(5);' @, H- Q  Z$ w* F' J  ]+ f
//--- Define the symbol in CSymbolInfo...
7 v  r  F+ Z0 W# Sinfo.Name(_Symbol);! P. Z+ }( V$ V, n8 h
//--- Set the timer..., }/ z& G7 ]3 `: b+ }; R% u
EventSetTimer(1);
; Q! y8 y! u4 M% ~# E# h* P//--- Set the base of the random number to have equal tests...& Q: A, n+ R' h& s& a
MathSrand(0xDEAD);
0 V3 v0 B/ U% l! \+ W7 U! Z; x" S* |return(INIT_SUCCEEDED);# a' S) ]# _0 t8 L/ O6 H) U% B
}
) n4 A# c  ^  l6 F& Q, n& @4 j//--- Since we set a timer, we need to destroy it in OnDeInit().3 X" i- q) t0 |, W& C
void OnDeinit(const int reason)3 B/ ^) v5 d9 u. d
{6 ^' B% }8 q: B4 j* I2 `! m
EventKillTimer();
/ d) ~; ^. o! a; o}
; O* C# t) l# U3 H) {7 f  |6 ^//--- The OnTick function only informs us that we have a new deal
9 y5 Q7 n! B& s* Nvoid OnTick()
0 a5 s0 p8 U0 b) r& R  c1 ?{
* n. D, a4 S, M7 Z! }tem_tick = true;0 T. Z' e8 F" M% m$ s+ O
}
: D7 \1 }: A3 d+ T: j; M0 u% u4 Z//+------------------------------------------------------------------+
( }5 r6 B9 s4 S/ p//| Expert Advisor main function                                     |7 N  n1 _9 {1 ~6 }- ^+ Z/ [" q% f8 G
//+------------------------------------------------------------------+5 n) W+ `$ C4 G" Q* G: v) |# d2 y- G
void OnTimer()
# F  k' B( v5 O3 H: R# p8 w{
# t" x6 i) E8 ?( G# V: fMqlRates cotacao[];
; Q& h: V0 O# R3 K2 u5 greturn ;* f& w, k5 i5 L# }
if (negocios_autorizados == false) // are we outside the trading window?
% S  q2 j: K, I* a5 f8 D" P. Greturn ;5 ]( N9 o9 a5 z( V% [7 O( Y
//--- We are in the trading window, try to open a new position!
1 @& u" f4 U9 o& T& Z/ _- Rint sorteio = MathRand();
9 J- `5 q1 o4 ?8 ^3 I9 C//--- Entry rule 1.1
1 ?: x" J. c' i0 }# n' E# m/ _if(sorteio == 0 || sorteio == 32767), b  j" G7 T" q* y
return ;
5 i6 z! D5 k4 p: r6 Gif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy8 G" N' P6 a+ y: y
{9 {5 a) e/ |% t
negocios.Buy(info.LotsMin(), _Symbol);
, ~3 L8 T- ?) p}
5 v0 b5 g4 v$ @. h5 S  Velse // Draw rule 1.3 -- odd number - Sell# Y, k' A7 N& I: `
{
5 T  }, Y3 a; N2 c- x& Q  Pnegocios.Sell(info.LotsMin(), _Symbol);. E1 O# }0 r* j4 [1 V
}
* Q$ q- j" e% E- t2 H}+ m! w) `& q$ Y- H
//--- Check if we have a new candlestick...
4 {: Z0 o# ?. Q& [, K5 Y3 s- @bool tem_vela_nova(const MqlRates &rate)% _8 u5 i0 U$ F$ n: W
{
$ l, P% M6 S8 z  m, y- [, L{
& E' o3 p! p3 A9 U( \ret = true;
) x& }6 F# I% a/ s5 b0 }  ^* R/ ~close_positions = false;
$ A% ?0 w! C! D* K6 _, D9 P}
/ S0 g; V# _/ d# Selse: ?5 ?. U( ?" Q; W
{' t4 D7 A8 Z% U( N  V6 z4 ?
if(mdt.hour == 16)
1 [3 X% s: Y6 c$ W. ~close_positions = (mdt.min >= 30);$ j* f8 X; \! b% l! f
}% S8 s  x# P" A* ]
}! k) u$ Q1 M2 W  y" s
return ret;
6 U& a' _/ }; ]; {' W. B}
: g. g0 q% l3 H4 }! t4 R0 s8 ~//---
  w. E, O. W8 o1 t4 Lbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
$ z6 T7 ~& j1 f6 }5 _{
1 \$ w! x1 x. G* S: G; f. ]if(PositionsTotal()) // Is there a position?
* q5 q) \6 g' H% ]1 E{
1 K" B6 s, h; Udouble offset[1] = { 0 };
1 z, z# V6 S( ]  f* ?if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
0 t7 @0 G+ v7 h&& PositionSelect(_Symbol))  // Select the existing position!
( B( N. z/ f/ b1 y# Q9 j9 F# i  n{
. }! q( Z/ s. E2 p* VENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);" A+ q$ ~7 w' z' I
double SL = PositionGetDouble(POSITION_SL);: F3 ~  N1 m( K3 q: g
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
0 u$ T, s" I7 \# L/ Y5 Tif(tipo == POSITION_TYPE_BUY)% X; M& O$ s: G2 C8 f9 w7 H
{$ f7 l( u( [- s
if (cotacoes[1].high > cotacoes[0].high)
4 }$ f7 y1 [: v) P* L{
3 O" ]# _8 i4 `" c; Y8 K* gdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
# h" S* `0 c8 {$ i9 T- d! {$ ninfo.NormalizePrice(sl);
5 H4 r( C6 |9 H1 D' Z3 U: N" _if (sl > SL)( f  S: X9 c5 {5 a" ^
{
8 M! p& x+ x  i# }& q4 Znegocios.PositionModify(_Symbol, sl, TP);
, x/ M8 w0 p% `) r}! \: r& H) Y* W/ R7 v* l
}+ ?7 l# U0 z' w( U
}$ I' @' h6 G- O: _1 e
else // tipo == POSITION_TYPE_SELL
  W; }8 g: T3 [5 m& x; b{4 Z# M1 v$ h$ r$ ~( ^( {
if (cotacoes[1].low < cotacoes[0].low)7 M+ K' X  G2 ^
{
, f' X! ~2 N( Q; w) [% O2 ]return true;  w4 I4 X3 U. X! E( ~
}
& Q& s) k) C5 N3 O2 p7 q// there was no position
" V- W* g# i  u2 h3 lreturn false;
" y7 P: }* B5 q0 D( G! c& h7 k}) _4 V; k4 M# }, y0 \
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。$ `. r5 d5 a: \3 B: n' Z- H$ Y  l
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-28 08:20 , Processed in 0.643634 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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