私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
( F: K6 m" t( [$ _% x* l在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。3 x% ?$ e, b1 ?
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
, u2 D& F9 E0 L3 C以下是制定这些规则的代码。. W! {5 P1 x2 K8 I1 ~
//--- Indicator ATR(1) with EMA(8) used for the stop level...
8 [2 c6 P2 ?  G: D: v1 Kint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);6 L( A* {$ j* }) e
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);3 c$ C2 L  R; X4 a6 v  e! S
//--- Define a variable that indicates that we have a deal...
) p6 T4 [& O6 ?' {bool tem_tick = false;) _* a4 R9 g6 U' m4 \
//--- An auxiliary variable for opening a position
% c5 l: p8 F# d; P6 O* M6 V4 l#include<Trade/Trade.mqh>
# }0 s1 j3 X- K#include<Trade/SymbolInfo.mqh>
+ D% N8 |, z4 m. H0 A8 u0 h( M- HCTrade negocios;
. P" Q" q: `: E2 O( eCSymbolInfo info;: @7 d" r: z) Z& b/ B8 m( Z
//--- Define in OnInit() the use of the timer every second
$ ^2 W3 ^* g% n//--- and start CTrade& O' w7 j: K5 t. q9 e; S4 i
int OnInit()
, m& L, |$ ^/ B$ Z5 ^. \& I! O# x{# m4 J1 ^. w' w4 c2 F- T% y6 z
//--- Set the fill type to keep a pending order
, y" n9 B& z3 M! H7 i1 ?' |$ E//--- until it is fully filled+ b9 T& F; U/ m9 ^0 X+ s
negocios.SetTypeFilling(ORDER_FILLING_RETURN);* q- x6 L& B- k+ ^9 [5 O' F# p
//--- Leave the fixed deviation at it is not used on B3 exchange6 b* S  J# s" V# h
negocios.SetDeviationInPoints(5);; C/ s. U6 ?2 M: J
//--- Define the symbol in CSymbolInfo...
& y) Q$ {( j6 w2 Q; B' Y) s* v- ?, uinfo.Name(_Symbol);6 ^4 o: H- K9 l7 A1 z4 P
//--- Set the timer...; u1 Z: O0 v- b8 ]
EventSetTimer(1);
6 j- W  x! S* G, K//--- Set the base of the random number to have equal tests...% B! `' ]2 m# z2 k5 Q
MathSrand(0xDEAD);9 c6 \3 E+ {3 K. y# T$ q9 }
return(INIT_SUCCEEDED);
# m3 \  T: w+ n. B6 P1 _4 O8 x, r}
; j# ^' I# |: |' D0 P1 `' P//--- Since we set a timer, we need to destroy it in OnDeInit().% ^  m0 N) T+ u- Y$ V
void OnDeinit(const int reason)) e6 t! z' l' p  H- S! U
{- j( T$ Q. z. E5 O$ w
EventKillTimer();8 D; K- b! |) z: t5 v! x( I; O
}% i( B) Z+ f7 C  q4 K1 A& p$ a0 m
//--- The OnTick function only informs us that we have a new deal
7 ~! I2 B; [+ p2 l  }0 C; Cvoid OnTick(); ]# j3 A4 i* n/ p. e
{" l- Z! m2 h; z6 z, {
tem_tick = true;  f; Y  c' S7 O' g. E
}
* g8 P5 Z# b  ~* q1 T6 n//+------------------------------------------------------------------+
2 q4 J! {- V  I4 Q//| Expert Advisor main function                                     |+ p  d6 m4 g, n. R
//+------------------------------------------------------------------+" P# O( s" c+ D* r) j$ j/ p9 r
void OnTimer()6 L$ w: k0 z+ O5 }  K
{
) w8 B$ l/ s: k' `  RMqlRates cotacao[];0 j& n. i7 P& O% y% R
return ;5 Y$ m0 q) V# [
if (negocios_autorizados == false) // are we outside the trading window?
" W8 ?8 `( ~) M0 [* b/ i! Kreturn ;0 j9 E; ~% D  I  ]0 ~4 L
//--- We are in the trading window, try to open a new position!& t2 ]1 G6 a1 o5 Z! s1 b  b
int sorteio = MathRand();. g( s( y  b) R( N( s4 B
//--- Entry rule 1.1
2 d, j; J  G" N- S$ Pif(sorteio == 0 || sorteio == 32767)4 k1 f* a  M& _$ i* ^
return ;4 n0 g6 l9 |: B. Z; v
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy2 D: z3 L/ x* [6 C" L5 F+ y
{
' h  M- L; G: p& {% `; Z* q9 [negocios.Buy(info.LotsMin(), _Symbol);
$ E9 c6 x4 P$ R. T' p6 ~}( w" W: |. ?  p0 Z1 F. A! Y5 S/ V
else // Draw rule 1.3 -- odd number - Sell
/ s: w" j: [; d4 @. l6 a9 c{
$ _) |# }# A* I) s: ~negocios.Sell(info.LotsMin(), _Symbol);
8 G0 o2 A* v- z& v, q( n( M}) I& d# J4 ?& x3 w* [5 g3 @% }3 l; g
}
7 `' u! i) M* N# s( p  X- q* H//--- Check if we have a new candlestick...6 W# E+ X, t1 N- I0 K  h
bool tem_vela_nova(const MqlRates &rate)
4 Z, Z+ U4 h: I' b1 k7 Z, k. K{" `) K) q- D3 N$ {
{" ]+ `1 y+ X/ r7 u( l
ret = true;- S- Y; G6 G4 Y( W. S# \, n9 i
close_positions = false;2 }% s& k1 h: |% X  i# T
}
+ g5 A( A9 u7 k6 K8 Kelse
" s. e% L! j- R{2 C2 J7 y$ G: i. J4 u
if(mdt.hour == 16)9 V" b# V0 i4 {) |- X5 F7 b! A
close_positions = (mdt.min >= 30);
# V$ i+ M% H) g& T6 n# ^}2 w( c- @+ L- y6 X6 s# J3 H
}1 R* x7 o! V* ^2 s9 f( F
return ret;
! B5 ]6 B2 C8 r- X) |! `}
, L' y' ]# ^) L, M% }# T9 H//---( s% [, h' I9 i# j4 `8 V" M3 |8 {0 z
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
! B4 e& S; g5 ]/ K2 c+ {{7 l5 X" m1 a/ X- @5 ]3 k- z
if(PositionsTotal()) // Is there a position?3 l) n+ g7 N1 w+ L# G% n8 ?
{3 }) ^, v. o9 K: {; U+ Q
double offset[1] = { 0 };
& {' A6 c5 y4 g. X4 [) Qif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?" e6 H3 I; d$ v
&& PositionSelect(_Symbol))  // Select the existing position!& d3 O, H3 R% j6 R  U9 F/ g& d. ^1 y- r
{
; T5 o% O, `) |( d, a, f9 m  XENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);' e; W! K( U; J& t; B/ l: y
double SL = PositionGetDouble(POSITION_SL);' L3 _) }; d% e
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
1 \% d) [9 z; r& ?if(tipo == POSITION_TYPE_BUY)
6 {( W$ i' Q. {5 C{( K) V+ z1 T4 h! e9 _3 H0 U# s8 T& I
if (cotacoes[1].high > cotacoes[0].high)
( m6 c, @$ F1 b* Y1 r{
3 Z2 a# H4 c8 j% Sdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];* b! r% o! L+ E% q. |! l
info.NormalizePrice(sl);
. `0 y( ~& b" qif (sl > SL); S7 g* v) G( i
{
+ [6 I7 e! B7 ^% B( u. B/ nnegocios.PositionModify(_Symbol, sl, TP);
9 N. U4 F" L8 N/ y( {}* v' D0 C/ u' }) O5 \7 |+ V
}
& ]7 t8 a* r- E' G' N}+ b$ E5 J4 g9 C/ t
else // tipo == POSITION_TYPE_SELL" c  L: c+ C# M. K+ r: q; F8 w
{
, L; k4 f" B# X: n; \if (cotacoes[1].low < cotacoes[0].low)9 |" b  L+ |1 N/ h
{7 G; B# p- D+ ^( j3 s) t
return true;
3 C) l$ I& d" e% N- \' c8 Z5 p) R}
" t8 R) [% Q. C// there was no position1 m0 x7 z& a; J
return false;
8 A% O8 N4 M% S  }  ?}5 S* f) W4 t5 w& y- l
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。! s* F; B  f! @4 H
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-23 00:49 , Processed in 4.315154 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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