私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA( @+ r# `3 `$ ?! a0 x2 k
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
* d6 C3 H$ i- N4 x为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
( U7 [& b) ^  H" z- n以下是制定这些规则的代码。
% G0 x; L; q  I0 t  H+ O) v//--- Indicator ATR(1) with EMA(8) used for the stop level...
  _$ r& b9 y3 \$ yint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
9 T! c: r0 `& l) k0 ?int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
) F+ t: D1 {6 k4 e6 z3 w//--- Define a variable that indicates that we have a deal...* a% G9 m1 e0 I4 _- }
bool tem_tick = false;
  ?, N4 [5 `4 w! e5 e/ i//--- An auxiliary variable for opening a position) T! ^, N9 E* J& W6 I
#include<Trade/Trade.mqh>
9 `1 S; `; c+ F* Z! N#include<Trade/SymbolInfo.mqh>/ H1 P6 m( ^- t
CTrade negocios;! B2 M3 I% p0 P7 `$ e3 L
CSymbolInfo info;5 z4 a+ g3 |" f' {0 b# f
//--- Define in OnInit() the use of the timer every second
% }% j9 I9 W6 n- o2 Y: f1 F//--- and start CTrade# g6 |$ i1 b# q% B. f' J4 A  L
int OnInit()
. e7 ~& T& q5 P{" C" n: K9 V0 N+ o6 i% G
//--- Set the fill type to keep a pending order1 ?" e; Y7 w& f6 w
//--- until it is fully filled( Q& R/ H% c$ l7 X; `
negocios.SetTypeFilling(ORDER_FILLING_RETURN);1 m9 i9 F9 u  A3 x
//--- Leave the fixed deviation at it is not used on B3 exchange
/ P( i& U7 y" N1 znegocios.SetDeviationInPoints(5);" C" I3 {' \2 u( H: O
//--- Define the symbol in CSymbolInfo...
- J/ J) O7 l9 I$ u- G) ^info.Name(_Symbol);. l6 z" f) N8 Y# z% C
//--- Set the timer...  [9 F+ b2 y- Z  S
EventSetTimer(1);& E# H2 P* r& Q9 [" b
//--- Set the base of the random number to have equal tests...- L! e! l" ^; r( s. k
MathSrand(0xDEAD);: o1 x" C3 \; N: C9 q1 h
return(INIT_SUCCEEDED);0 J. ^* r1 d$ x* M) h
}/ U( k2 F0 B! ~% E1 h
//--- Since we set a timer, we need to destroy it in OnDeInit().
( E/ c2 _$ q! M7 Y0 q+ [2 @void OnDeinit(const int reason)
# V/ _& E, Y6 J3 z1 Y- ]' U; c{# m+ ^. j# S2 F6 ?- ?* [
EventKillTimer();
# Z" {8 U! i# m6 V% f" z: R}  S) |& e" |% i$ P' Y0 K8 u* {
//--- The OnTick function only informs us that we have a new deal
6 C  L7 S; p3 M1 L& W/ Evoid OnTick()
8 \; r7 k. O, P8 R{
$ u* I, r6 f; v7 |8 S6 xtem_tick = true;' ?4 ^& F0 Z" R8 j3 o  e* c; m
}4 p& D7 I, W1 M  x" K
//+------------------------------------------------------------------+. i, V$ D5 O' E' g5 h( T) V
//| Expert Advisor main function                                     |7 i7 k% g+ _4 H5 V. w
//+------------------------------------------------------------------+
, R2 b& j6 d$ _# j3 k) Svoid OnTimer()
! k! ^6 l% U& W% t{
/ Q1 ^; Z7 F9 p4 k5 m6 a: {MqlRates cotacao[];
: j7 K- S7 S2 freturn ;
& b, Q, A7 Z% ~6 @. hif (negocios_autorizados == false) // are we outside the trading window?/ T% x  Y# x0 S; G
return ;
+ G4 s# C; O* J% J- Z//--- We are in the trading window, try to open a new position!
/ @( h$ A3 u  ^+ K/ n. }int sorteio = MathRand();0 n% n+ N' v- ]+ t/ m  l
//--- Entry rule 1.1
7 ?$ A  `7 F5 Tif(sorteio == 0 || sorteio == 32767)4 d# ~0 z2 _1 x
return ;6 w. H3 C3 z' S. Z( }
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
; Q& E  X& f. Q# `{
! n( V  J5 T5 I# ~9 Unegocios.Buy(info.LotsMin(), _Symbol);
; N; e: p( H1 ]. t/ c) ]/ T+ Q# E}
# g" {" f/ n# L4 g. Aelse // Draw rule 1.3 -- odd number - Sell- l; \' y0 |+ O7 D( l" d1 W6 T
{
- D1 ?1 W. H) w, e2 r% p% Snegocios.Sell(info.LotsMin(), _Symbol);
7 r% H2 C- b, P; d4 K+ ~/ f+ d! I$ r}. v5 `% i0 b# k5 l) r
}
( C6 R$ b) y" M" s5 ], h( B//--- Check if we have a new candlestick...$ K" W/ V) f9 k2 @0 k! b- y1 C0 A
bool tem_vela_nova(const MqlRates &rate)
: O9 s1 ~" q" k{- b. G( ?; \2 b. W; w
{
8 Z) H8 R" @- M( H9 X/ r1 T4 d# jret = true;$ W' p; N& O0 @' S, t5 a
close_positions = false;# z* T. A( e' X) M' }- V. u8 @
}
2 x1 l  [/ \, ]$ w2 a9 s; D; W2 melse) Y$ [8 ^% G7 r% ]
{9 J  B! E' j: y& j( I& t: D
if(mdt.hour == 16)
, |" ~8 p. P$ U: c+ a" @- cclose_positions = (mdt.min >= 30);) ~* [. N4 \& A) S, p( o
}
4 q, G% M9 I' x0 C}
; m: F/ u" {( y* treturn ret;
" I% e* ^* e& I9 Y9 L: S}
  \2 Q- ~7 [3 a2 p3 o//---) u. o) q6 G6 X. |2 \5 w6 H; E
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])3 ?5 e! J+ ?# o; A, ~
{
; G. U% n+ @4 Q8 W  }if(PositionsTotal()) // Is there a position?
) z" K" g5 C7 R9 [  z3 t) C3 v{
& G4 X7 U4 r# _. edouble offset[1] = { 0 };+ j4 i; H$ Q, r4 i. j
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?1 T! F' e* ?6 b0 ?
&& PositionSelect(_Symbol))  // Select the existing position!
0 `# k! I! Z# f1 C{
. D; q7 ^( B" c) [4 NENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);( R5 L% K2 k# B& h" i4 x+ ?' d# \" {
double SL = PositionGetDouble(POSITION_SL);
" |' }" _, }; Z. z" A+ Zdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
: `7 M! I7 u3 x0 T. k% oif(tipo == POSITION_TYPE_BUY)
: G$ s2 m3 }6 e; U! W9 ^" J{) s8 f. m' W& z
if (cotacoes[1].high > cotacoes[0].high)
: Y& y! a. Q; w+ Y  [4 E{" R8 Z2 g4 y) B. `1 k% L8 |6 S
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];% R1 X0 g/ ]& D& e
info.NormalizePrice(sl);8 e0 b# N! U1 w  H4 w/ E
if (sl > SL)1 w0 I* _! `0 B  G
{
& H: o" u4 |; C) f  s  q/ T" T) ?: Unegocios.PositionModify(_Symbol, sl, TP);
) _( V3 J( v- K% [$ `5 M/ |}9 q5 R0 D  v' h
}
4 T; `! f+ i; ?& ^6 ^% T; P3 Z}
0 j! B6 w3 ?: I) b5 L* J. v" X0 nelse // tipo == POSITION_TYPE_SELL
# X8 t: B. j- M# j5 ]$ |) S{# l7 l& C* C; `9 @& d
if (cotacoes[1].low < cotacoes[0].low)
& V' T3 j& c$ N. n) f{
; u; ?. p, P1 U0 E3 i% ]! ]return true;( U6 A+ C# y5 o' g. ^
}
) v$ _1 t& |# O1 i) [8 C// there was no position
; u% X4 [7 r! v! Y3 b9 jreturn false;
/ V/ j* ?& Z7 G6 r9 p' J% w}8 |( W# D% K: @( K  D& a* {
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
- \$ T9 }# S- B到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-7 02:02 , Processed in 0.949947 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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