私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
1 v: I9 @7 X$ u8 Z7 x. {+ Y在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。- Q( o  ?* L8 c1 x
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。) O: p% Y# R2 ~# X0 ?
以下是制定这些规则的代码。* X. G! w$ {5 }# O
//--- Indicator ATR(1) with EMA(8) used for the stop level...
, @% t8 b5 N' m; g8 W6 ^/ `int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);$ a2 |  G" V5 J/ D  g7 L. o7 Q) J
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);5 I4 m- g" o/ h8 z
//--- Define a variable that indicates that we have a deal...$ y/ D1 ^! M/ r- {, l0 s. p  @6 ~
bool tem_tick = false;4 d2 i* x* h. l: R& {+ v) T0 A' ?
//--- An auxiliary variable for opening a position, J4 C9 [5 `4 R( @. F
#include<Trade/Trade.mqh>9 ]. M) ?" t* i7 e
#include<Trade/SymbolInfo.mqh>
5 C2 m1 w4 {  z; SCTrade negocios;
- ~& l2 [! m+ m1 K$ t. |CSymbolInfo info;
5 Q- t1 B1 j1 F6 }( @//--- Define in OnInit() the use of the timer every second) G4 |. g$ G1 l/ f: x9 N+ `7 g
//--- and start CTrade: V5 J; z  t7 @0 |; p
int OnInit()3 c' j; |/ E$ l# F; t2 Z
{
* ~5 H  e8 `1 M1 G+ L: I9 W//--- Set the fill type to keep a pending order
2 N8 V9 o7 V/ c; \7 E//--- until it is fully filled
7 A1 k# k6 t# j' ]$ Jnegocios.SetTypeFilling(ORDER_FILLING_RETURN);. {& h3 \6 x- R8 N+ |! ?- B
//--- Leave the fixed deviation at it is not used on B3 exchange
2 s- `. K& a  F& c& w& Dnegocios.SetDeviationInPoints(5);
' J  N* e4 D7 ]: S//--- Define the symbol in CSymbolInfo...
7 g3 f3 B2 f4 i9 k0 `info.Name(_Symbol);$ ~6 I) _8 e! s4 v) Z
//--- Set the timer...
2 H3 N8 `9 u1 ^# d# U* x) H- y, VEventSetTimer(1);
9 @3 s2 f: ?# r7 A4 e. U//--- Set the base of the random number to have equal tests...
) A6 T/ `( B% h7 _MathSrand(0xDEAD);7 ^: T5 O+ ]* }3 s& g0 o
return(INIT_SUCCEEDED);" A  P8 w  R* k' J0 r. w. N
}
: [- i0 l" K% h1 a//--- Since we set a timer, we need to destroy it in OnDeInit().
, F" M& @0 V& b. F7 j6 H- R* vvoid OnDeinit(const int reason)
$ A1 }1 Z! V+ H{3 H' M0 b6 k  {, B. D
EventKillTimer();
2 Z& V, J0 N# _2 _( G2 n}1 d, f. V9 M% _
//--- The OnTick function only informs us that we have a new deal
* V) V% y6 ~/ r1 }3 Z! S$ xvoid OnTick()
/ o) {" j4 a) V4 x+ q; O, U{
. i9 \2 r8 G' ^+ Z# J' E. item_tick = true;* x! y! c8 g5 [- Y9 ?9 w+ [
}
4 N/ ~9 g4 F" y" }7 C' v# n* k& {" R( [//+------------------------------------------------------------------+' f9 R- g9 _1 E5 w. ~( g
//| Expert Advisor main function                                     |0 d: k2 J  a6 w8 N3 Q0 E7 |
//+------------------------------------------------------------------+3 S5 O1 s3 N: b
void OnTimer()
" W3 T' i" ~. f& o{; J! o! u7 N9 p7 J, W5 {
MqlRates cotacao[];
* b/ ^0 A  C. N. X2 q/ A8 B; Kreturn ;
7 g8 @& M6 P; h' X1 kif (negocios_autorizados == false) // are we outside the trading window?( v- e5 Z  p+ r
return ;
. j! S$ A6 \3 ]$ [" c: `7 a% W& L//--- We are in the trading window, try to open a new position!6 g/ T) Z  }% A8 _2 T1 W
int sorteio = MathRand();" [3 w: c/ ~2 V1 b- O
//--- Entry rule 1.1
" B7 v6 u9 L- R- l1 Uif(sorteio == 0 || sorteio == 32767)3 p0 ^/ U) T- U
return ;7 X" G+ q1 R3 v! j" i
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy' E0 b& [6 N8 Q& U0 n+ P: M
{$ i, P) m1 Z# a
negocios.Buy(info.LotsMin(), _Symbol);
) t+ ~, I$ w) }2 A+ R5 E}
! O, b. W  ]2 O1 v. \else // Draw rule 1.3 -- odd number - Sell" a, A% n. C: K' A4 d, ]% C( I
{
+ h2 ~% U7 ^- z6 F  o( Z! Xnegocios.Sell(info.LotsMin(), _Symbol);
( Z  v5 x, r4 r8 ?}! J, o- {- s$ K5 w1 z
}
" j( P8 b) p+ _//--- Check if we have a new candlestick.... M& z! H! f7 k" R
bool tem_vela_nova(const MqlRates &rate)
3 D9 f" B8 z1 r' f" M* \4 |{
. g9 l2 S% h# {6 S( T0 T7 y{1 c' {" C6 \$ ^+ `' E
ret = true;
" n, v  y5 q/ B7 i9 J8 j3 Mclose_positions = false;
. v' z% G& T, B& x1 t}" Z) |" p: M' O7 P" r- a1 x# X
else
' E' |( x% P0 F. U5 V7 \{( D: W' a# w- r9 B) l# Y7 u5 n
if(mdt.hour == 16)
3 w/ D  s; z3 ^  N! R5 Wclose_positions = (mdt.min >= 30);  a6 Z! ?- P( A  b
}9 A0 K8 ^; p0 Z1 _" y/ H
}
$ P1 `% N8 W  K+ Y' areturn ret;2 q9 e7 _! y' r, x$ |* @+ V* H
}' j+ e: }: E( O+ D8 U
//---, c  R/ s' o6 ?, M8 `  s
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
2 u8 o. t# Z1 ?{
* ^* g: \7 O4 z, m; A, kif(PositionsTotal()) // Is there a position?3 ^# N. g- x1 ?0 k3 |
{' B8 h% ]6 e+ P9 a% C( ^9 @
double offset[1] = { 0 };8 S8 U  H$ Y6 r9 E+ k, e! f: v* w
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?7 M! c0 r: ^5 s/ o2 ]
&& PositionSelect(_Symbol))  // Select the existing position!8 K/ `' e3 s. a/ u' w: T, ~7 e* I/ d7 _
{
9 c* G& a" G* n% fENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);% w# d% j# P; I/ a, ]9 ^3 k
double SL = PositionGetDouble(POSITION_SL);
3 ]" [& M3 S. b% I1 r8 f/ Ldouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));6 J4 l  T* F9 f, }& S1 t1 g
if(tipo == POSITION_TYPE_BUY)
- k$ `" J: \; v{4 z3 t% [5 W1 o/ P0 i& p; J' K
if (cotacoes[1].high > cotacoes[0].high)6 d6 F6 N. K. w7 E2 g
{# X- @" u* P  R; l3 ~6 |
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
$ K0 u1 R: i6 M3 Qinfo.NormalizePrice(sl);
; [: ~  r4 a4 b' h7 mif (sl > SL)
7 W+ z7 d5 |0 e  b  k$ r0 Y$ v{9 v' N) g+ E2 Z" a0 N5 m- q) d8 @
negocios.PositionModify(_Symbol, sl, TP);
4 D* Z8 W4 Z( B6 }  A}
3 C6 b8 Q: b3 P. `$ j! U. ~- X}7 u, E! m; ]% G5 v# t8 j) e! X
}
9 l% Q$ V4 ^; Q2 L* a5 }8 b  uelse // tipo == POSITION_TYPE_SELL- e2 ]6 i9 ^. N7 s
{8 u0 u9 Y& ?, K2 t6 e4 X
if (cotacoes[1].low < cotacoes[0].low)
- i8 Q) B. u+ h) Y* n5 g+ S{
1 N# i+ B+ ^  N/ y) }( n& u6 z. sreturn true;
" z' S* i! F- N; W; c}
/ D( `$ A8 ]; D// there was no position
6 @3 i" |/ Q9 ]: T3 @7 freturn false;
4 M% N# n4 L, [. j7 I6 y}
8 J$ G0 b4 B& H  x我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
9 B/ Q. G+ P/ m1 o. d' m7 ?到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-26 23:01 , Processed in 0.394567 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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