私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA- m$ W( c7 s1 i( h. O  D4 [& X9 Q' q0 |
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。; X: N8 O/ R! f5 {2 S/ |
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
$ E0 d: G: \& `& m4 w6 ?以下是制定这些规则的代码。! w1 u  s- a$ _5 W# @5 n
//--- Indicator ATR(1) with EMA(8) used for the stop level...  U8 ?) c. d. F1 e; ]/ O# x
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
- `1 M: g# }" D' \int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);4 y( a6 i9 A$ m. F
//--- Define a variable that indicates that we have a deal...
! e1 b1 x* D% J" Nbool tem_tick = false;; Y. F9 `' C7 k8 O3 f1 E' [
//--- An auxiliary variable for opening a position! [9 d' Y1 `2 V3 j$ G& v  V
#include<Trade/Trade.mqh># N$ n4 k7 X) Y; M- t4 x
#include<Trade/SymbolInfo.mqh>
+ f6 U+ z' \  o' g! nCTrade negocios;9 }+ j2 v( {5 R* D
CSymbolInfo info;/ ?- T# }0 K& E# _+ P5 K& h. _
//--- Define in OnInit() the use of the timer every second9 o5 f" t; C; Z4 J. P+ N0 A
//--- and start CTrade
& T( K# \9 {2 A3 t) \int OnInit(). C3 U. c' ]. [2 |$ X: u
{5 n( d' O& l3 F2 C. G
//--- Set the fill type to keep a pending order
" D) v' S# l1 o) E: M' v% s//--- until it is fully filled8 X8 ~* I1 A" ?% b( g) y4 A
negocios.SetTypeFilling(ORDER_FILLING_RETURN);' @) `7 ?6 i! y  X0 d# B: A: t
//--- Leave the fixed deviation at it is not used on B3 exchange
5 |. ]+ x/ v; w( Y& lnegocios.SetDeviationInPoints(5);. P5 E! q7 J8 H7 `
//--- Define the symbol in CSymbolInfo...6 h# g4 J9 j( F9 U! V: _, w
info.Name(_Symbol);
5 V* z+ y6 u5 T* Z4 [//--- Set the timer...
  q/ x0 {6 A3 HEventSetTimer(1);
0 \3 `9 V1 q7 {5 }0 q) h//--- Set the base of the random number to have equal tests...
/ ]0 b+ j4 d4 g; ^MathSrand(0xDEAD);1 ^8 O3 |5 R3 F6 t
return(INIT_SUCCEEDED);
5 y/ j$ m+ A- q6 Q4 w- \) I}/ Q- C0 J& y* q( D' c9 N1 a
//--- Since we set a timer, we need to destroy it in OnDeInit().8 H  X3 U" P7 q, b6 j
void OnDeinit(const int reason)( l7 _8 W" o% a% s
{
: L! v9 G! K. P  n; iEventKillTimer();
: n/ z$ B% z. w- @) r}  |: d8 G$ ~' M* n0 K( d
//--- The OnTick function only informs us that we have a new deal8 d& U; w! Y6 E7 n; H/ g5 {
void OnTick()
  _7 x' g* u% l9 M: W+ {{& L, m9 A! @' b- x: K
tem_tick = true;* I; |( N2 {6 D2 a; J3 l
}
! T6 M( i' i" S6 b//+------------------------------------------------------------------+7 t2 S" k, \) |7 R, K
//| Expert Advisor main function                                     |
) K; X" P, e) S. Y//+------------------------------------------------------------------+/ W& f7 c2 t* `8 p+ k0 n3 t
void OnTimer()
6 p) l8 z4 T" G9 k7 U4 }* K{
+ _4 a5 V; j' W. eMqlRates cotacao[];
/ v( J+ T/ ^+ j8 O4 t7 C: Areturn ;. H* n  x$ O# n  S% ~& d& [
if (negocios_autorizados == false) // are we outside the trading window?5 Q3 R" Y) L  p$ ?% G) @6 {0 ^
return ;
# m0 y) a# K6 `- s% s! O8 C//--- We are in the trading window, try to open a new position!
9 q' g! w+ Y, C: Sint sorteio = MathRand();- y% [3 G" ^. L" ?5 ?, q4 d* L
//--- Entry rule 1.1$ U2 h5 A- T/ U
if(sorteio == 0 || sorteio == 32767)7 h3 p7 F& r( B2 T, b+ D
return ;. r+ N- b8 Y6 x% m
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
: b7 G: j% {- z* o{
3 A& X& o& q' nnegocios.Buy(info.LotsMin(), _Symbol);  h7 Z! K. a3 [1 V& K9 z
}
; |1 Z, E$ W+ K  K3 Delse // Draw rule 1.3 -- odd number - Sell
+ m4 b5 s+ ?( U{$ Q% E1 K7 e. @- W! ]
negocios.Sell(info.LotsMin(), _Symbol);
( c% l( s6 u; e( b/ ~4 V5 ?}
. j; t/ l, N1 T}
, b4 i: u  {& ?//--- Check if we have a new candlestick...
' ~4 J5 S  x  I& t! x5 Abool tem_vela_nova(const MqlRates &rate)
" C6 b- A% I: Z! o+ [  v{7 N  W, f2 j- ^2 s+ I
{
" L) Z3 P0 U# i: cret = true;
1 S+ G0 X: @! b" m& A, T5 Rclose_positions = false;
, J4 P3 a. }, d4 l, O}: N2 |, ?1 d: M+ G( N
else
( n; ?' f! W6 r# j6 ^! }: M& S) S{
+ C+ Z" G4 n6 Lif(mdt.hour == 16)6 L7 }/ U8 x1 M9 I
close_positions = (mdt.min >= 30);
, ~. C, ~3 T1 V5 o" s8 z# s. c! D4 y+ X}
( i2 M  I; `! r$ H* w9 p" v4 A}
- a& ]. v2 J; V. b( vreturn ret;0 b4 n# D0 y+ P
}: @" X2 m8 Z7 I! k9 j, U
//---+ N1 l) j( i" G4 A
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[]). w8 M2 r  \. t- {
{
3 R3 [5 c0 t! Uif(PositionsTotal()) // Is there a position?
. W* t4 I% {( w/ \: _- W, g9 s{3 U/ @7 Z) w/ L% L2 b2 t' U% b
double offset[1] = { 0 };/ X5 T; m8 _+ H9 R
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?4 ]* Q9 o9 ?4 l0 a1 B
&& PositionSelect(_Symbol))  // Select the existing position!! G/ X2 F* b# g
{2 j9 @$ {4 Z$ I$ y: d! j9 j6 r
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);4 K' ^* C1 L$ W$ _1 W9 v0 U
double SL = PositionGetDouble(POSITION_SL);4 f8 g' q" i; E
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));' q1 U6 ^* u$ D4 K" }
if(tipo == POSITION_TYPE_BUY)  a: H1 h5 V9 {+ z9 G, x) a) x
{
( i. n" V+ ~0 y/ J$ j& y) b3 Eif (cotacoes[1].high > cotacoes[0].high)
8 P* b: X# B+ V$ A{& M  [4 R9 g1 |% v
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];& m# h9 U6 O; s6 j) C. o3 h( ?, |1 T
info.NormalizePrice(sl);# I4 b7 z3 @  n, w' s  a
if (sl > SL)/ G' g. }! k  O
{3 j1 l: ?, `9 o% B  U7 ^
negocios.PositionModify(_Symbol, sl, TP);
8 [* M! [4 z4 k" r" I}
; x( N( J8 Y  I}
! i: S# c; S4 o0 q1 V' M/ d( @! p}% i; k- B4 a1 v* D
else // tipo == POSITION_TYPE_SELL
# D/ ^! t: L! j: s. o8 e{. S0 V, N: m- q+ b1 u4 u5 ?
if (cotacoes[1].low < cotacoes[0].low)
, O2 K) ~+ ~% M" ?! {) U. G- }/ Z, `{  Z. D6 {7 v2 c7 q3 D! _% U0 I% Q
return true;9 E1 M& Y% N% f; m( B0 w6 G
}
+ A1 v% g6 d# E: j6 ~// there was no position! B1 j: ]/ [, Q0 m! r
return false;
1 w& O! U# _* `( u/ h1 F( d}
# ?! ]% [; H9 e' h8 ]! F' {我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
5 D5 B/ j# H) ], `: 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-11 13:27 , Processed in 0.430827 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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