私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
; A/ V, l) q3 K在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
) a& p: {2 {5 v9 r5 D为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。) h1 S* f, G7 t- l0 q2 E1 P% i
以下是制定这些规则的代码。
4 Z; e5 s' \7 J5 }1 T3 I2 Z, L//--- Indicator ATR(1) with EMA(8) used for the stop level...
) s8 L6 Z9 u/ bint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);& N5 s, D) u7 ~: r9 u/ q
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);4 O# P% w. l. t: f9 i' V+ W6 ^
//--- Define a variable that indicates that we have a deal..." L6 y% K% x' B5 P
bool tem_tick = false;: L" b) d$ F: N& o8 @, ]
//--- An auxiliary variable for opening a position8 ]6 J5 i# a% K* p
#include<Trade/Trade.mqh>) J/ s1 p' v- u2 S% A  N
#include<Trade/SymbolInfo.mqh>
6 E$ H( r# y% `CTrade negocios;- ?# K9 I3 g( s! K
CSymbolInfo info;4 Z" P' m4 s4 ?+ R1 }
//--- Define in OnInit() the use of the timer every second2 w+ R" `% W) {$ m$ s3 O% _- y
//--- and start CTrade
3 m; z" L' a4 j' [1 vint OnInit()
4 ?) h' s  h+ [$ T8 I4 S{' X* E( F! K' R, C1 ?
//--- Set the fill type to keep a pending order
8 z" k: Q8 Q# G3 A  w1 z//--- until it is fully filled
1 n. h2 n' O2 t+ Onegocios.SetTypeFilling(ORDER_FILLING_RETURN);; g# N* X9 @' V/ y
//--- Leave the fixed deviation at it is not used on B3 exchange0 q( r% x9 Z7 B6 q) g
negocios.SetDeviationInPoints(5);
) e4 A2 B5 Q; m3 L. D/ t//--- Define the symbol in CSymbolInfo...
( n. c$ W8 s# G- ginfo.Name(_Symbol);0 r6 ~/ z# q6 P3 q9 j  X
//--- Set the timer...
$ J2 H% ]: a3 \3 _( q* MEventSetTimer(1);
; H5 z, T4 p8 \; N+ a//--- Set the base of the random number to have equal tests...
) T9 Q) R( q7 _- y  o: K4 FMathSrand(0xDEAD);6 x! A8 M, R( J* z* |; u& j9 u/ h
return(INIT_SUCCEEDED);- G2 r/ f1 d4 J% K
}
# Z' B/ ?. p9 `4 {& {//--- Since we set a timer, we need to destroy it in OnDeInit().% K8 q$ q% W" N. ~7 q7 @+ Z
void OnDeinit(const int reason)( v) ^3 c( Y' w/ a: K
{; G* Z8 ~* |$ r" N% a
EventKillTimer();
6 m( q- U( z  ~+ l$ @}% g- x8 n6 c9 w' {  h- B9 d1 m% p
//--- The OnTick function only informs us that we have a new deal
/ f: Z+ J5 X5 gvoid OnTick()- W! d- p% }! @4 g8 s# Q
{) F- n8 r" m) e% q. c* Y
tem_tick = true;
3 \# |0 b5 P. L! y, D4 C! G; e4 L}  |8 q3 D+ C2 q' v; T4 q" T
//+------------------------------------------------------------------+9 S4 b( A0 a  ^' P
//| Expert Advisor main function                                     |1 }% ^. f" \# V( q$ }& Q6 h0 M9 V9 I
//+------------------------------------------------------------------+0 {4 {% W3 b4 T# X
void OnTimer()
* O/ @. f% v, U9 U: x  I: r{
3 w+ Y/ j" H; \0 pMqlRates cotacao[];. E3 A& |4 t. X! y' o3 {  K# e+ h
return ;
: A0 y6 v/ a5 E( x% a" C0 ]if (negocios_autorizados == false) // are we outside the trading window?% {, H# |2 g/ K: L1 j5 I
return ;
2 r  l' I+ ]3 o% f//--- We are in the trading window, try to open a new position!
: {, m2 c+ _$ F* v$ r, cint sorteio = MathRand();, O; v* l/ D2 N/ F) _- O
//--- Entry rule 1.1
1 h, E! X6 r, p! H: e) |if(sorteio == 0 || sorteio == 32767)3 h1 S6 U; e+ F; {- J
return ;* |$ f5 V) i6 U$ B% L, u9 L5 W
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
& B$ ]9 b1 x) D% x9 r2 C3 l- T{# P% }! p4 M& L1 v. ~: e" u* t
negocios.Buy(info.LotsMin(), _Symbol);
& b) b  k' g3 n  f3 @! Y}- Z' q& V4 k1 T) h6 t3 y
else // Draw rule 1.3 -- odd number - Sell
5 ?8 J1 }- L* }+ o{( ^2 ~1 X: Q3 l0 ]6 \
negocios.Sell(info.LotsMin(), _Symbol);
2 U3 N4 `( z8 ~4 ]! W  N}( `/ G( l" @% c# C& W
}
: J: h" N1 f% `$ G3 B2 X//--- Check if we have a new candlestick...
* m6 V: w7 e2 |, l/ {! Jbool tem_vela_nova(const MqlRates &rate)# B: r6 Z5 D$ {) ^+ Q  x
{
# X2 I- q/ P/ j" }% i$ Q9 k7 @{
3 k( R" O2 N" I3 Yret = true;8 A! O5 E$ Y5 J. W4 i# H- t
close_positions = false;7 \9 T+ O+ s$ J$ U; a/ E) f
}
3 b$ k; Z! K$ H. v# nelse
8 z8 F# E" q0 O- H4 e{
, V1 n+ Q' l# \2 N3 l: A5 g: Kif(mdt.hour == 16)
' N4 r$ i( N# L5 S2 S' w7 gclose_positions = (mdt.min >= 30);
, F' l  _1 _+ e/ h6 X}7 S7 E0 _2 ?4 |9 y0 u; P# @! G0 c
}' w; B6 b* v2 i3 P9 r
return ret;
+ h9 i$ ]/ k! e$ v1 c}0 |6 V, n1 {1 m: R/ B
//---7 J2 s* |- ?8 M
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])! \3 s! b! K# _
{
8 D- Q* d" K) ?& q4 a2 o0 y, gif(PositionsTotal()) // Is there a position?
5 }, i6 s% U7 K5 z1 }: C{8 u/ R: [! c" D: X
double offset[1] = { 0 };
: ?: Y5 k+ ]* ?' D0 m0 _2 kif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?8 `" {0 f8 t5 ^+ J% v/ n/ u
&& PositionSelect(_Symbol))  // Select the existing position!8 h; e2 Q5 q3 @; o( `9 \, x$ t7 e
{( v' c! |7 K. |! j) R
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);7 h4 X( t1 c* f, P7 G+ M
double SL = PositionGetDouble(POSITION_SL);* `; w: ~( z6 R, z2 X
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));% {0 G8 l, h4 Q
if(tipo == POSITION_TYPE_BUY)/ v) v. R) W( ]) W: `# g
{
' ]) M# l+ K) `9 A: q7 i) }+ hif (cotacoes[1].high > cotacoes[0].high)
3 S0 }/ P$ O$ ?+ O( ?& O  \4 q{
5 x" d% W0 y- x& r6 q, n5 u, B, pdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];& O5 _2 `& a6 m6 D  @' ~
info.NormalizePrice(sl);" ?& X( ?, X0 H
if (sl > SL)
% D% r8 @  q4 q$ R% x{7 o8 x6 g6 |  v8 `9 D# I. [8 f$ k3 \: Z
negocios.PositionModify(_Symbol, sl, TP);4 x: O) u/ X* r$ l1 ^! X
}
: g' D; A7 Y6 N5 G}
' s: T9 ]! z6 D* e, M/ c}0 M- @( ^! N2 m: c# {# M/ f
else // tipo == POSITION_TYPE_SELL& M/ t/ o3 K$ C/ V, ~9 D
{, Q3 }. N: z9 c0 H7 y5 ?' d1 r
if (cotacoes[1].low < cotacoes[0].low)
; f0 s3 h% P* e5 x{- m7 S: l3 z, t3 r
return true;
0 F0 Z, [2 S9 ]- m/ C/ F7 E2 v/ ?- q}: g1 n: |- B3 q; I& u5 R( p- R
// there was no position# l" u, W, c2 m! Z6 ^0 h1 ~3 l
return false;
. h8 ~7 O7 L7 l; @}( j2 e- D9 K5 A. v0 h
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。2 `  m* M" w0 X8 d* c, H
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-6 01:19 , Processed in 1.215698 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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