私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
1 q0 `  Z1 F- X1 v0 q5 d在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。8 A' Y* f7 r1 j. W; ?( Y6 ?
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。0 s2 u. {! |- r' p9 C
以下是制定这些规则的代码。; }, d3 S) a7 Q2 b9 U
//--- Indicator ATR(1) with EMA(8) used for the stop level...
( s0 a  j& e) ^5 k( a- g8 b9 nint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);3 J7 O9 {4 Q  X) i0 A
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);6 h/ [# b! J2 s, t4 o( u9 e- w5 d
//--- Define a variable that indicates that we have a deal...9 {4 o3 o5 P4 ]6 Z1 L5 C9 ?
bool tem_tick = false;) |- G$ X4 |7 y2 k$ y4 i( |0 I1 q. p
//--- An auxiliary variable for opening a position
) i9 `" ?8 ~$ k, N5 m6 {#include<Trade/Trade.mqh>3 I/ w4 V& |0 i; n4 g% r' q/ e
#include<Trade/SymbolInfo.mqh>
! z. r; e. A" ?) LCTrade negocios;1 H+ v6 n; a0 T  X# [
CSymbolInfo info;) d4 g( T3 N8 t& _+ @" A( a8 c  E
//--- Define in OnInit() the use of the timer every second
/ z/ v2 N& g2 Z" T( M! `( d, `//--- and start CTrade
) P& O9 r) f% N) n9 hint OnInit()( a9 V& j8 l9 w1 {! I
{
/ E9 d; H1 n2 G0 S" S7 y//--- Set the fill type to keep a pending order
7 n+ ~: ~. c2 f//--- until it is fully filled
8 S0 k5 w" ?4 }negocios.SetTypeFilling(ORDER_FILLING_RETURN);
" Z4 d( S! U* c' g//--- Leave the fixed deviation at it is not used on B3 exchange
' v: e; I( e$ P4 T0 i' }0 Vnegocios.SetDeviationInPoints(5);3 V+ ^2 m! V7 ?: G% e. i
//--- Define the symbol in CSymbolInfo...
' P1 t# Y; J% n# M' K, D, f+ rinfo.Name(_Symbol);+ ~% Y* u5 z+ |% \
//--- Set the timer...
! e# A& u2 B- c5 @& Z  o6 XEventSetTimer(1);0 _- G: i) n; u% F7 w- j
//--- Set the base of the random number to have equal tests...7 q8 K8 }6 @8 |8 C4 H
MathSrand(0xDEAD);
% X' b3 L6 E6 x5 g0 n# Xreturn(INIT_SUCCEEDED);6 H  K4 i+ w( Q# e/ u7 {3 i
}
: B  u# z0 W' O+ W2 ]3 g//--- Since we set a timer, we need to destroy it in OnDeInit()./ d4 Z# _! }& o. a- |
void OnDeinit(const int reason)
  o8 v) n; t/ ]7 v) l{0 j) k/ I0 u& {' Y1 T
EventKillTimer();
" [0 M8 i4 O! C7 d, g}3 N) Y3 ~. M  X7 m
//--- The OnTick function only informs us that we have a new deal. i6 Z! O( O* g9 X9 s/ p$ D
void OnTick()
5 b5 y2 }/ H% ^, a1 ^3 f4 B3 u{- l$ `6 \4 Z5 D3 P
tem_tick = true;, ~& L" \9 {% y; r  F
}
4 k. u; h! r% E& h* P7 w//+------------------------------------------------------------------+" b# u5 T0 e& ~) I! Y- O
//| Expert Advisor main function                                     |
6 a& I1 }& v8 S3 p8 b//+------------------------------------------------------------------+
2 R# |! b0 X, t4 q; F$ j7 Wvoid OnTimer()& M  l# v" [: O8 s
{9 w1 h. s# p6 h6 `. M0 N
MqlRates cotacao[];
( Y; B" d: o: U# J" {, k% preturn ;. r+ ]) a  ~+ l+ F
if (negocios_autorizados == false) // are we outside the trading window?/ N- U" Q1 b, J# S0 o' Y+ x
return ;
0 x) W5 X0 V, B1 K//--- We are in the trading window, try to open a new position!
' K% C8 M5 Z! q3 [int sorteio = MathRand();: q7 @0 s; V9 a. h
//--- Entry rule 1.1
6 n1 C( v' d; U( x" L9 p' Qif(sorteio == 0 || sorteio == 32767)
- J# K% w8 z2 N0 _5 ]8 zreturn ;: k4 G1 B* H! i1 l0 ~
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy8 V/ |: S; a" p: J
{$ F  z1 i8 k' h1 x/ d* q: |
negocios.Buy(info.LotsMin(), _Symbol);, x( V0 V/ V4 l7 q1 H7 y
}
; n& |" h' p2 ielse // Draw rule 1.3 -- odd number - Sell
2 a# R) R( _: _0 h4 U  L2 A{; F' b( ]% q& [; g" K: w, v! J( G
negocios.Sell(info.LotsMin(), _Symbol);
4 A+ [' ]2 ^* U3 r# @' `}
8 j8 u, ~- G+ Z, P4 p/ _: {# {}8 g1 I& a7 }. ~# j: j4 [$ w
//--- Check if we have a new candlestick...: A3 Z8 x/ C! C8 [& F
bool tem_vela_nova(const MqlRates &rate)
' ^3 ?: c1 \8 c* w! l" \{
7 e# [9 ~3 ~8 S; ~{
8 J9 p8 L- \$ i/ f( Yret = true;0 ]: b# ~) m- f
close_positions = false;9 ]: s* x  G9 Q
}
. g" p5 `; m+ Z  L" J4 f/ ~. G! T; `else
" J! f* ^- |2 S) J: y, n8 U% ^+ ?* M! E8 D{/ F4 l0 z% d! `5 k! P
if(mdt.hour == 16)
% B3 V; w1 {- Dclose_positions = (mdt.min >= 30);/ ?  y& w9 A1 o  ^& e' E6 L
}5 o+ ?) N9 E( [. W4 c
}0 ~9 X  O, P  Y( Q) `9 X: Y' {. G7 E
return ret;
: \9 u  x# X! N; M2 U}
. @: @' y' P8 _5 z; F//---7 S( F! Y2 A% t9 D, H
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
, X% Q: w2 a: V; b+ p2 {& R{' o: c  ~" q5 H9 e! i
if(PositionsTotal()) // Is there a position?
" v7 E+ J4 Y& R3 o{
& y+ M" ]3 [% q: C6 t7 Odouble offset[1] = { 0 };' N% y" R' s8 p: |& X
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
8 }5 i7 l+ d7 g# L&& PositionSelect(_Symbol))  // Select the existing position!- a, I  j5 o3 Z, X- G1 k1 s
{
+ _7 L6 F; e9 r9 c: S9 l% dENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
. _$ C8 Z2 [8 W0 A" E3 Rdouble SL = PositionGetDouble(POSITION_SL);7 l1 l+ P* u/ [% h3 [' Y
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));% s( _6 E; z0 r8 s0 h- I4 Y4 ~( V' v
if(tipo == POSITION_TYPE_BUY)
3 \  d7 d( ~; ]. _1 I$ _{
$ F% r- J- x# g) C+ y$ A7 H) Wif (cotacoes[1].high > cotacoes[0].high)
0 a" X5 c3 Q4 Y. R* l{
  R4 m3 r6 Q7 U: T; x8 \double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
% ~0 z% T7 ?$ d. ~( K) K/ winfo.NormalizePrice(sl);  T; @5 V  y: K! X7 v& ^
if (sl > SL)/ U+ @& q  n' `9 S% V7 p) d
{* n0 |; m6 \% S
negocios.PositionModify(_Symbol, sl, TP);; Z2 K. R9 Y1 o5 d0 L$ `0 U0 f
}, f+ f3 @. w9 D- }4 V9 R
}
% w# \6 ^$ H2 _) [# M$ r  c2 `}7 q) Y3 r4 E! p. H8 C) H" y
else // tipo == POSITION_TYPE_SELL
7 ^( K' x+ }* ~8 O: V: m- g5 J{1 |5 U6 h! i& N8 c4 g
if (cotacoes[1].low < cotacoes[0].low)% E! t" {" k. C/ k' N; I! J
{' S7 r) b+ f& g) X, q- W
return true;. r- J4 o# N9 V5 }2 [4 h7 a
}
" H! F' o, ]& S// there was no position  O/ F( @5 ~7 c
return false;
4 }& K3 P- r$ Z7 n+ O7 D}
  N  B4 `* T2 J$ B0 e我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。1 U1 m1 J" x4 H* p+ l2 X6 [9 a
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-22 07:43 , Processed in 1.363123 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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