私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA# h. u7 \4 r- K, X$ w2 H+ {2 _' ]7 k) T6 e
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。8 f4 G/ q& i9 ]! h6 ?. f
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。. |' m8 Y  t8 A% s* t
以下是制定这些规则的代码。
: d/ O5 n0 a6 r& `( S4 _2 n6 L//--- Indicator ATR(1) with EMA(8) used for the stop level...1 S% M; Q. k4 J  n" `) v! A/ F- M
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);! t8 g" y# S; O  H
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);" i/ U; ^  I$ l, @1 J4 b
//--- Define a variable that indicates that we have a deal...
# @0 S$ k" u) Lbool tem_tick = false;4 M: \. j, {9 `; m: R9 |! {. ]
//--- An auxiliary variable for opening a position% O4 I$ P9 c, D3 J
#include<Trade/Trade.mqh>% Y. r) {! w) I4 w" @
#include<Trade/SymbolInfo.mqh>$ q, n! p. m" @+ J
CTrade negocios;
# C; `# Y! c5 ~! HCSymbolInfo info;
0 Q+ v: [# Y' k/ b+ k//--- Define in OnInit() the use of the timer every second) |. C% A8 Z# @, w  K4 h' [
//--- and start CTrade
4 X; m7 L/ B9 Q# J( [int OnInit()
6 C/ E) s8 _. H! c9 }7 [6 t{
, z$ j7 _. C: _1 m3 g0 s+ `9 z& o/ _6 ]//--- Set the fill type to keep a pending order
! b- O& @9 T. F4 [2 H* c/ L//--- until it is fully filled9 E% N: r! y6 \, _* `. T% D( q6 d
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
$ L' x6 J% f4 S' {( v; t% x4 _//--- Leave the fixed deviation at it is not used on B3 exchange- X1 V/ o3 e% M
negocios.SetDeviationInPoints(5);
( I2 h  I' U2 ^0 c: h//--- Define the symbol in CSymbolInfo...
4 E5 k: f/ p* E* Binfo.Name(_Symbol);
# C& l8 a/ a* V. N3 M  L//--- Set the timer...
( R( n+ ?. Z& B/ J: f  FEventSetTimer(1);
7 |0 y  y% z/ e//--- Set the base of the random number to have equal tests...
7 J7 K! M& s: m- z# z, D: }MathSrand(0xDEAD);
2 [- J  ]4 r8 {( jreturn(INIT_SUCCEEDED);+ ]7 J) f" n  S8 t% x; D8 w6 E# |$ c
}+ r$ i  O& a1 i0 g- D4 b
//--- Since we set a timer, we need to destroy it in OnDeInit().$ r; `4 {, X5 _& L2 g- h: J
void OnDeinit(const int reason)" N5 U( l7 r0 y8 a5 {4 j
{
6 E4 B5 i7 t5 ?: b( IEventKillTimer();
. Q1 L4 J( S; g6 s1 ]9 a}
7 h2 }+ }" H' h% t6 ?- r1 l& D//--- The OnTick function only informs us that we have a new deal
4 G5 ]) B. w% \! H$ ^- n. Gvoid OnTick()
6 d* h; Q/ W: o" F. x* L{
9 h$ ]) d8 {4 ?5 Btem_tick = true;
" q2 T6 j8 c: [}4 C, f- }9 u: `7 O2 s
//+------------------------------------------------------------------+" z$ g5 N- I  s9 K
//| Expert Advisor main function                                     |1 @5 P7 I6 E" I/ y7 i& ~
//+------------------------------------------------------------------+
2 F4 H7 Y* D5 N& Mvoid OnTimer()
2 }( Q9 W) \. p2 i1 j! [; @{5 y1 L! \2 w) ^4 N) o
MqlRates cotacao[];* k1 E/ c6 w; n+ J1 L
return ;
2 y/ f9 H  y3 |$ i& ?& Q; ]# Yif (negocios_autorizados == false) // are we outside the trading window?- d; v. r2 Q" \7 k$ `8 X
return ;
4 r4 J, v. T" h0 ?//--- We are in the trading window, try to open a new position!
5 g1 ]6 t- I8 nint sorteio = MathRand();: l$ h" P6 d8 [9 I9 }0 X1 r
//--- Entry rule 1.1
& {9 |* v' i! o9 Pif(sorteio == 0 || sorteio == 32767)3 ~, }  H! Y8 s
return ;
; Y% S) d4 o4 wif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy  w4 O0 `5 S+ r; J. A
{
7 ?& P9 j+ @4 F& ]* v7 G- u5 tnegocios.Buy(info.LotsMin(), _Symbol);
( w  ^! t8 H/ @}, w; Z) [* y* `5 W
else // Draw rule 1.3 -- odd number - Sell
, \9 n* g( \. V{/ l: E1 I% R' g; G% y% U
negocios.Sell(info.LotsMin(), _Symbol);
& R- y% N; {) Z5 Z}6 ~5 Z1 q* K% o/ E" L7 E' G# m
}) R* [0 {6 J6 f4 e% Y
//--- Check if we have a new candlestick...
" `) L3 X3 p1 Wbool tem_vela_nova(const MqlRates &rate)" j$ n9 R. W7 d' |( R0 ~
{
! Y% u8 S" G  y3 h  ^( D0 h{
( U4 E- Y) q% Uret = true;
0 J0 t+ }% Y1 `% W. m' J- xclose_positions = false;. y3 ~( |  I, X
}
) w) s3 f  D& Belse4 C* R. X- u" u( E
{
+ G0 }6 s- c& g* S% Fif(mdt.hour == 16)) s9 ?, f5 u+ v9 q! W# `, J. p
close_positions = (mdt.min >= 30);
7 ~% A6 S# u7 m$ E, U. t0 ~}
2 K; x! i5 G" X1 z: P8 o1 [3 ^( Q2 f1 F}9 `. F6 w/ x! Q  n$ j
return ret;1 [+ R/ g0 k/ ?. J
}
! y2 m, _& d8 {7 p" ]//---
3 p3 r: u7 }0 I5 q* m" Zbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
- _- B. Y& _' L6 j0 q{( W6 x  Y1 f9 O' m# F/ n
if(PositionsTotal()) // Is there a position?
# P" M# u' x/ j0 o3 ^1 W& a{
8 A8 m0 {- f6 r3 k* _1 fdouble offset[1] = { 0 };
1 E/ G: u% F0 Jif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?  h# N) z5 c- ]% u  D' F7 E
&& PositionSelect(_Symbol))  // Select the existing position!
" T7 Q7 w9 j+ S0 k' U3 D2 @{! N! p" m! m8 ^" U0 n
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);/ V. H8 E# t1 l
double SL = PositionGetDouble(POSITION_SL);
  ?# \7 J" ?* S0 }double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));$ z+ \/ |1 a& Z) Z0 h
if(tipo == POSITION_TYPE_BUY)+ X$ C4 ?8 V( K, y
{1 l5 I% J: D- X/ }3 ^) J: W
if (cotacoes[1].high > cotacoes[0].high)
( B  b2 @7 _1 ^{- h6 [4 ?6 J  t( ^1 Y5 A$ o5 b: _3 R
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
' c" L$ Z+ i  y/ k3 \! ~info.NormalizePrice(sl);0 i3 t) Z1 e( w& {3 z6 ~
if (sl > SL)  Z0 \0 |6 W) S. e& o: M2 E
{
+ q) J7 i2 D. i# S# {2 znegocios.PositionModify(_Symbol, sl, TP);
& y) X% l9 U0 B$ `}
" `: }& D6 W- u, [; Q}
9 w3 B$ f: f8 m3 y7 S5 b, Z}
1 z/ ^  w! c5 n7 ]else // tipo == POSITION_TYPE_SELL
; Y0 X4 M9 J/ A  J+ N; A{
3 s7 A) r9 [, D' Yif (cotacoes[1].low < cotacoes[0].low)
/ N: ?* I8 G$ e; G+ M/ ?. F( N{
/ z7 R9 @' {4 D9 l% j1 _return true;
, J. J* Z* H6 m5 c1 c+ Z}, i+ u. d6 i! @1 t. r6 H, B* s
// there was no position2 Z( u$ X; D& q- m' N6 g7 P" g
return false;
: H+ v; h$ h- v& W}
$ @& ~9 }6 `1 X, g; K& P我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
0 Y0 F! |8 @+ B. s: F( L% W+ H* _# E到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-22 04:16 , Processed in 0.400961 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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