私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
) I( `- V- }' n+ j+ Z在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。! ?5 l  ?" p2 [. T; X" n
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。# K, U. w/ o- b: A: a; e! b
以下是制定这些规则的代码。
, A" Y- @5 ^5 s( j//--- Indicator ATR(1) with EMA(8) used for the stop level...
- X" ]' W3 f$ c8 Z* v* ^int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
/ k2 i) {: |' Pint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);( [; m% @  h& t8 `$ g# `+ ~
//--- Define a variable that indicates that we have a deal...7 t, Q$ \% C. \# o, _+ ?- _
bool tem_tick = false;' B1 Z" x+ s1 c. y
//--- An auxiliary variable for opening a position. q4 \2 f) n0 ^! ]' i) G, E
#include<Trade/Trade.mqh>
0 P2 y5 ?# _- h( `  A#include<Trade/SymbolInfo.mqh>
! A- l( Q8 `6 y; |. vCTrade negocios;
7 o3 Y8 ?9 E! ]7 @  r0 rCSymbolInfo info;
1 u% p2 _1 G( d  m" [//--- Define in OnInit() the use of the timer every second
9 W# C9 t  s4 ]3 Y2 `//--- and start CTrade% d% j$ u; v' U' O+ C6 J  E- {+ r  n# u4 b
int OnInit()
7 A* _& g) ^6 c9 W2 |# G{6 @$ [! Z+ ]# h; t$ s$ C  m
//--- Set the fill type to keep a pending order! ^" u4 f+ B" J3 H3 T: m
//--- until it is fully filled+ h. U( w( {0 C3 ]) d! z+ @: S
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
$ c; d( f9 p$ ?& ~+ n0 ^//--- Leave the fixed deviation at it is not used on B3 exchange
; R$ y! e: u( F* i, Q. h1 H: }negocios.SetDeviationInPoints(5);
: r: H' t  p- J7 i0 v//--- Define the symbol in CSymbolInfo...* t# A* Z2 h+ K6 V; {( ?
info.Name(_Symbol);3 D2 ?1 d; h0 l( Y. R4 Q
//--- Set the timer...
) B. l: ]: ]' {: d, w4 s& kEventSetTimer(1);6 t9 p9 m1 T# A3 E. S
//--- Set the base of the random number to have equal tests...* U) R/ L. b; r5 a
MathSrand(0xDEAD);/ }- I  _# F7 Q, B
return(INIT_SUCCEEDED);
. m1 {3 b& R, U" p* K1 j, S; g}) s2 ^. T5 R, A
//--- Since we set a timer, we need to destroy it in OnDeInit().
6 k: i" f" m" y- R! R) Fvoid OnDeinit(const int reason)' L& ?  g9 L3 Y1 D! T, k$ \  h
{
$ U2 {# o& i2 o- PEventKillTimer();( R" Z' b4 q! p; `3 ?6 y
}
2 Y% C' }% i) v: U$ d5 X//--- The OnTick function only informs us that we have a new deal  \2 u. ]! x8 ^9 x- l& q* f
void OnTick(); z) H+ ^7 E' j( @- J0 X2 d) ~
{
  s  m; V# z# h5 z: x$ w- ptem_tick = true;' e5 K# U* M- _
}
3 Q' F( _. C/ _$ L  Z  e//+------------------------------------------------------------------+! d% j' d1 z3 l  [) b# ~
//| Expert Advisor main function                                     |
9 n+ a7 G7 C0 a//+------------------------------------------------------------------+6 L& o% i: r  O. c( a
void OnTimer()1 k; U3 n- p; J1 O
{
; S6 j9 [# g/ A! FMqlRates cotacao[];
0 {. ?6 I. q& s; I- m' F' creturn ;
* Y8 w/ L4 @, s+ Q2 X: {3 P$ p  [if (negocios_autorizados == false) // are we outside the trading window?
4 w6 B3 A% T4 a1 r% n; `return ;& M2 [8 k: P4 l
//--- We are in the trading window, try to open a new position!& k- t9 ~4 m+ ]8 K
int sorteio = MathRand();
7 M8 x& g0 T2 q- J9 z0 V- J0 ?//--- Entry rule 1.1( m2 W; _! G7 k- u& Z, {& R0 T; H! {
if(sorteio == 0 || sorteio == 32767)% |& T: A% ^$ o
return ;
6 P! f( I& o( x& a" zif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy1 \! `2 \  n: N3 }- w! r
{. {, V: n/ m$ e1 ]7 H" n
negocios.Buy(info.LotsMin(), _Symbol);* X% L  ?  O" v" `, F
}7 \+ b2 N$ m9 S
else // Draw rule 1.3 -- odd number - Sell3 v) q7 W" f- l% w" |" [7 @+ `% \
{
& w- T2 v8 p; o6 x1 K  J& L- \  i# I) dnegocios.Sell(info.LotsMin(), _Symbol);
6 x) ~. x# S/ |  M2 c3 J1 H( r}5 ?8 r+ p1 [8 H  ~& U
}$ H' `  M% x) w' W! y
//--- Check if we have a new candlestick...* _3 ?9 |& p& R3 K- z
bool tem_vela_nova(const MqlRates &rate)+ u; {1 }0 i) X  G* V4 G8 D0 j1 d
{7 `% B9 Q% N1 w) ~. h% W
{
' V- E/ {, }9 M- d1 Q4 a4 Qret = true;: k' A' K  D8 l9 W& g7 d
close_positions = false;- E' l3 @7 Q% Q* W* S
}4 v1 \1 i- I+ K
else, |1 l% w4 v# L4 E. C
{
$ x4 H9 t* z. B: X! Oif(mdt.hour == 16)& J. Q- }9 Y' |: a' \0 y8 l. i
close_positions = (mdt.min >= 30);7 p- S; T4 b, P0 m3 F, }
}1 K& e0 g! a/ ~. d/ \% r- F8 d
}' Y' O% z  T% b; C# Q
return ret;
% l8 W1 m; [' ^6 E}
' I; @3 n7 c) E! m/ D//---
  Z2 g" v" b' N1 W& ebool arruma_stop_em_posicoes(const MqlRates &cotacoes[])$ {, G, l4 z, J) y* X% Q
{7 ~1 _5 H1 `% s# _( j2 ]( h
if(PositionsTotal()) // Is there a position?
2 W/ j) T  d0 H/ K" a+ A, b" ^7 T{! t) R+ b# G4 q0 q0 O' [- c( Y
double offset[1] = { 0 };
4 E% l7 W1 x+ `# h! Xif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
# e  z* h4 v0 y2 O& ]0 p/ S&& PositionSelect(_Symbol))  // Select the existing position!
9 }4 ?* S! {7 ^% L0 v- b0 B{
9 K  |; o' K9 D1 @ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);1 t; h$ K6 h( z# ]+ w' G7 V4 B- }
double SL = PositionGetDouble(POSITION_SL);
# Z/ V4 Y0 E, C0 U' f! E$ g6 Ndouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
* H( B; i* w% V6 oif(tipo == POSITION_TYPE_BUY)9 N3 k  K4 @. d$ E8 t3 X* v8 M  c
{
* p( B: P1 k" o: z& ?5 Nif (cotacoes[1].high > cotacoes[0].high)$ ?+ J  ^, [. @3 x: l, f$ l' T
{& v9 ~, a' f5 l* @% u8 f! r7 m
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
  U4 J2 b" d4 w- g$ G" Y# Winfo.NormalizePrice(sl);
+ W, z, k, e0 p5 }6 g- C+ z7 Hif (sl > SL)8 E1 i# ?1 Y- X" c2 Z" }2 @; Q
{
! F; D, [0 ]) Y- E/ jnegocios.PositionModify(_Symbol, sl, TP);
+ V" _' B5 p; t0 y! W9 x& S, Z}
/ A, O2 t( K/ q  v1 K}5 {7 E0 x/ J# q0 ]
}4 |5 Z# F5 K: v7 {
else // tipo == POSITION_TYPE_SELL
9 e6 S5 T4 i; U  K/ x{
" i* i5 g# b( |. M# bif (cotacoes[1].low < cotacoes[0].low)% |& t6 K# k" S, V
{
5 U4 T) k# |5 y0 b5 |return true;
, h( Q/ I$ F* D" z/ c}8 k4 |+ p' V) W
// there was no position2 j+ W! v% d3 {4 l7 n4 K
return false;
/ f6 O4 m3 u/ c; y# Z2 _; g}
/ P9 b3 W3 h' Y/ L) y我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
, t3 e" v3 I6 g  X+ z到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-18 08:51 , Processed in 0.754926 second(s), 32 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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