私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA* n0 C6 m: y) ^2 f
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
3 L' S. I$ ?) B% h为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
& D/ I% w3 |' E# Z4 @* A+ d以下是制定这些规则的代码。
  _3 B6 U3 [# D3 ~/ h, |+ ?  p//--- Indicator ATR(1) with EMA(8) used for the stop level...% ~+ }# N# D, {) z2 A0 }) T
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);7 b, l' A% X/ U7 h
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);8 Q3 K  i" s& v( l
//--- Define a variable that indicates that we have a deal...
8 z, _3 r' _% J5 q5 cbool tem_tick = false;# U+ r! Z2 m1 t
//--- An auxiliary variable for opening a position
, @0 W7 [7 {) \- C# S6 d" `#include<Trade/Trade.mqh>
: Y8 }% n. r5 f* e$ L$ ~* k#include<Trade/SymbolInfo.mqh>, l8 V- d. D  G4 h. J% ?/ v2 O1 e
CTrade negocios;* G7 k2 H4 ^: g" C- W- F
CSymbolInfo info;
" X) S7 m3 y, ]8 w' C; s# N. J//--- Define in OnInit() the use of the timer every second
- C* L; ~4 k+ m# {//--- and start CTrade( E& C& Y; `/ U
int OnInit()& |- F! x5 k7 W
{$ P9 \# r# k+ S/ d: U# P! [4 b: Z
//--- Set the fill type to keep a pending order
! K6 n. l& W) {//--- until it is fully filled
9 g) T( \1 n1 m( o; X' n1 }negocios.SetTypeFilling(ORDER_FILLING_RETURN);* P: U! }5 K) j0 r2 e" C
//--- Leave the fixed deviation at it is not used on B3 exchange
7 @/ I8 {( x$ Z7 gnegocios.SetDeviationInPoints(5);
) J. L6 W  F' _$ x, _' b//--- Define the symbol in CSymbolInfo...5 Z4 R1 j' i# ]& }" _9 c
info.Name(_Symbol);, C& L, T5 X. |9 l
//--- Set the timer...- v/ ^$ H7 o3 T0 q0 S; T0 O3 ~
EventSetTimer(1);2 q' w$ b" v* C7 O: F# G& ~( M0 v
//--- Set the base of the random number to have equal tests...
  s" |5 q6 z, ^  H% XMathSrand(0xDEAD);$ a7 `& r. \  h* S( q+ M
return(INIT_SUCCEEDED);
) W( t" X5 y, ~4 ]8 Y. H}" b) z2 ^* K' i
//--- Since we set a timer, we need to destroy it in OnDeInit().
+ B  G  h7 q# @  u( k7 }9 q  hvoid OnDeinit(const int reason)
6 Z7 d: g9 Q( X5 {: z{; G- h# q8 f2 j
EventKillTimer();
! j" h( B- ^) E3 e9 I) G/ G}$ V' D7 E6 K6 N: v
//--- The OnTick function only informs us that we have a new deal5 R6 n, v1 P+ ]$ e8 h
void OnTick()4 M. m' n: e' g* j1 W+ {
{' E" |+ N$ I5 n7 j
tem_tick = true;" b. N. I2 W4 `
}
" |5 r: A/ l9 _  _//+------------------------------------------------------------------+2 B+ n& F* I7 i: m& y& N( J
//| Expert Advisor main function                                     |- x; D* e2 N+ w* R* y! r
//+------------------------------------------------------------------+# \  E$ i1 L  O7 p
void OnTimer()5 p3 s# k; h  R, L% ?
{
4 K- A" U  t6 c. z* EMqlRates cotacao[];# K' }$ P2 \- Q: ^
return ;
* ~! i8 b* Z6 P+ tif (negocios_autorizados == false) // are we outside the trading window?
5 r& ^& N* s, areturn ;
; c- l+ ^/ d- P* u//--- We are in the trading window, try to open a new position!
2 Y$ ^+ S/ [7 [5 Z% H+ t/ i& sint sorteio = MathRand();
+ V0 p' g) [" P. W//--- Entry rule 1.13 z* U! M& V2 f( \; ~
if(sorteio == 0 || sorteio == 32767)
; ]$ r3 O- j: N# g/ l* V; B2 Z- dreturn ;
, z" q% r. A  \if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy( B8 t& q  M( C( f
{
' E6 Q2 L. l- p8 F' bnegocios.Buy(info.LotsMin(), _Symbol);, H. i( D5 i+ K! a, w" ^# `
}: p( T, I2 w- i1 }
else // Draw rule 1.3 -- odd number - Sell3 v) j" D* _0 `4 l4 u' S
{
/ Z! C* W% h- c% {/ H- xnegocios.Sell(info.LotsMin(), _Symbol);2 B) S8 M) S) N- Q0 y/ r- W& s
}
( R# N) `+ `0 y3 z) {9 I! J- _$ _}  x+ I; D: n* ]% r
//--- Check if we have a new candlestick...* s: N$ V7 j3 w9 W; P
bool tem_vela_nova(const MqlRates &rate)
' G( O4 v: i' l4 ^' t{
" e0 s) C2 p; r) x, e8 E{7 n  l6 {, k% P0 e; u1 C/ h# s* y
ret = true;
9 P6 O9 c. g" a7 ~; P- F, y1 n3 }close_positions = false;/ k0 F4 ^2 i) f5 S
}
3 J/ W2 u) Q. {1 |- b$ {2 p+ ~4 aelse
1 ?. z: v& z* O! F6 R/ ~7 Y{
& V2 I  q3 z2 l' [7 Y. n  \if(mdt.hour == 16)& v( u4 f. ~; R, @: _
close_positions = (mdt.min >= 30);
! \5 k  W0 G" s0 e) g}/ p! h% a3 Q# w# I# n' q# H5 d# f7 h
}+ O% w# y; W" E6 Y0 J0 b5 B0 Q, }
return ret;
' i0 M2 J) b  ]  N}
4 B1 Y" R9 u% P6 o/ Q6 V! b//---
( _3 f  O9 y3 d/ ^1 ubool arruma_stop_em_posicoes(const MqlRates &cotacoes[])% [' F# u6 _1 K9 K+ Y( z# ]
{+ X5 c* n: B9 x! a! C( V+ k
if(PositionsTotal()) // Is there a position?9 V* B, T6 B* A! Q9 V6 z1 N
{7 l! n( F% Y2 ?, _
double offset[1] = { 0 };, L# _3 P8 j6 R+ h2 n$ c
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
/ `0 Y% g7 N- c" C9 h/ [&& PositionSelect(_Symbol))  // Select the existing position!
7 V. j( y) i2 B6 k( l6 i{2 o& b: Q, f7 {" y/ \
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);" k# ^. X+ P+ `; ~$ w. C9 u/ a
double SL = PositionGetDouble(POSITION_SL);. R* p' r8 K4 {' j9 w# s
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
7 p2 a, t' h& w4 W# ~if(tipo == POSITION_TYPE_BUY)* y* m! `4 r* {' p( g7 u
{9 W, n/ u' p+ o8 q* p1 b
if (cotacoes[1].high > cotacoes[0].high)
* u+ [1 y9 ]( ?# f' Y, c! e{
3 j# h9 Z5 o5 C+ E8 wdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];/ ^! H( j% V" j6 ~, ?5 t
info.NormalizePrice(sl);
; N1 J: E( d8 L# Sif (sl > SL)
+ [. ]' W- [1 `7 b7 r5 C- i5 m; a8 P{( D" {8 k& J. U5 s1 }
negocios.PositionModify(_Symbol, sl, TP);
$ T2 ?- ^; f  C  n) o% R5 i/ f}
2 D  Q  j5 J& t7 H$ z}
/ a+ c. ~8 L8 y, K! p6 n}/ x" i$ U$ Q& z4 v6 j; e' d" n
else // tipo == POSITION_TYPE_SELL
- d2 c/ Y9 O6 d) G. a# D( K/ s' c2 a! l{
0 Y1 s- |+ s; G5 o  M% gif (cotacoes[1].low < cotacoes[0].low)& \! Y" G5 N4 A9 X
{
3 y" H: a$ `5 y( Y( K) J1 l+ F) Zreturn true;
/ g, g7 w, ^! D* {0 V% `" t4 \}# n! R+ `2 I4 R: @- u3 P" i
// there was no position/ K0 u3 p" e: o& r! ^, R
return false;
( n% k, d& E7 W8 H}( U2 W6 O/ Z* [3 D2 W
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
5 S. G5 d) y* [  J$ X到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-20 10:05 , Processed in 0.682558 second(s), 32 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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