私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
4 y- B, E' e2 R9 O4 i! C6 A1 e3 M在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。$ U3 |6 L( r& ^; I0 A
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
: y8 D- t, G! v  b: t# f, ]以下是制定这些规则的代码。
0 j( j5 s3 @- D5 K5 k- S  l) L) \7 n//--- Indicator ATR(1) with EMA(8) used for the stop level...0 M2 l5 Y/ X  d; P) a0 s
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);9 r) E8 e: }1 \" y1 y8 U
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);6 @& u& s8 c2 Y9 r5 h
//--- Define a variable that indicates that we have a deal...
3 G) B' J! I! N/ b+ rbool tem_tick = false;
6 Y6 N2 C7 }. |$ g& e//--- An auxiliary variable for opening a position
6 _  Z- q! P) _, Q9 J#include<Trade/Trade.mqh>
/ H5 P% |9 b3 p  O#include<Trade/SymbolInfo.mqh>
6 W- [# H# n6 E# y) yCTrade negocios;
( s/ l8 f- c* ?* iCSymbolInfo info;# c4 t& P" O" F" e- U7 w* C
//--- Define in OnInit() the use of the timer every second: w% i( P7 |4 L9 w2 E+ R/ H
//--- and start CTrade
& Q+ I/ p; U0 z) Aint OnInit()$ N2 ~* v4 W7 y+ u; r
{
% M& [* ?4 H& C; S- v. d//--- Set the fill type to keep a pending order8 ]) T; z5 z: l# Z" L: q; Y
//--- until it is fully filled& s1 w- z; `/ J$ c4 p( T# Z
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
/ Y% B5 H  Z$ G5 q! m5 M2 b5 y$ ]//--- Leave the fixed deviation at it is not used on B3 exchange4 z/ C: d7 I( ~
negocios.SetDeviationInPoints(5);
2 I; j+ `/ ~4 s5 I$ ^+ @//--- Define the symbol in CSymbolInfo...
7 }% }7 I1 w% F9 `info.Name(_Symbol);
5 Q+ b1 T6 t2 R0 l/ p: s//--- Set the timer...
# K2 M# T8 k" l3 Z' [EventSetTimer(1);
6 P. G& j& H0 t3 M- ^//--- Set the base of the random number to have equal tests...
+ z0 T9 b+ q: b4 uMathSrand(0xDEAD);
7 U( z. \! G: e/ l% r+ o* Ereturn(INIT_SUCCEEDED);
3 ]$ n* R8 u2 c}
/ J! r" c1 ^, W- {9 h/ B4 q//--- Since we set a timer, we need to destroy it in OnDeInit().
. V( y6 B9 D, c+ M; N3 ]2 bvoid OnDeinit(const int reason)
9 o( X" T* K3 o; b/ Y5 h{
3 U! M( p* R8 ?# C) u5 h" T! ~EventKillTimer();+ s& K: O6 F! b9 G4 H) e
}
4 A3 \  X8 K$ {  C4 L- K//--- The OnTick function only informs us that we have a new deal
! t* _% S- g; O8 q# X. n. j* t* Rvoid OnTick()
* `! O7 {3 a. M- A9 R7 h7 b{
/ F! `& a: D+ T) x/ W/ q8 }6 `, Ytem_tick = true;, O4 q5 `' i, J7 f& e
}8 K4 s9 t0 N9 W& Y) l
//+------------------------------------------------------------------+
% o% m( f' |; y$ K" S0 |8 u9 K//| Expert Advisor main function                                     |
4 h: _0 B. p: z//+------------------------------------------------------------------+( _4 v" B9 g$ Q
void OnTimer()
, L; C! x, [. n- X; ]& U{% n: \  a: s) u
MqlRates cotacao[];  k9 O, M8 ^. E- P2 b2 N
return ;
# B) k' S3 {" D( s7 F0 h( J& X  t  ?; Cif (negocios_autorizados == false) // are we outside the trading window?
8 A  F" O2 V+ }1 jreturn ;
, Z5 g! \, b; e6 ~//--- We are in the trading window, try to open a new position!8 y" k# K  c* F, k. P
int sorteio = MathRand();( v/ I& J& y) k; x+ b: l8 w
//--- Entry rule 1.1
, i6 N, ~8 K! z9 S) J8 Nif(sorteio == 0 || sorteio == 32767)
/ H# ]: n  C! `8 \4 Freturn ;
! N5 N: S7 F8 s8 Q& @. Gif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy9 a4 u! n+ F* C( W4 D$ l
{5 G6 a. j; C0 T0 T% o) }; }& i
negocios.Buy(info.LotsMin(), _Symbol);4 O4 i% M3 K5 K4 ]) G  [
}, V( e8 L: B+ J8 V% _3 t4 m
else // Draw rule 1.3 -- odd number - Sell
1 x* U" ~3 N6 a% w/ p{' j% x6 a2 ~+ h  m' \5 i
negocios.Sell(info.LotsMin(), _Symbol);
2 v; ~6 m3 G/ {8 Y  w  Z. g( C}0 M9 |6 c2 F& N2 ^7 D7 [* w
}- X4 R" f( n8 E/ x! `4 H' Z) m! e+ X
//--- Check if we have a new candlestick...  P) C; T% }1 _' p
bool tem_vela_nova(const MqlRates &rate)
! {6 O3 k+ D- ?/ `3 x{
% B5 g6 t0 y# p9 \{
8 x5 k2 z1 g! Z" {' S/ ]ret = true;
* ~: }' o; ?1 G/ D) S) qclose_positions = false;
; R7 d" U: w- d}
/ o" t" J: Y# welse6 z/ w# Y/ A, F
{( Y" A% ^+ s7 {7 F& R
if(mdt.hour == 16)
, A/ @. P# G% ]6 u! l" \close_positions = (mdt.min >= 30);
" r/ G" q# ^9 g4 K% ~}
) j5 Q/ h" G1 ~$ z}1 ?/ x* L2 U# t9 m  m" A4 ~
return ret;
5 P& q& X3 b3 W2 f, p}, x# o# D7 [! {: M, D: x( m5 B0 g
//---( [- _  Z0 N7 m7 v5 O( W
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])+ n! c% U) x$ m( `0 U
{9 S% s/ e- ?/ F' W# I
if(PositionsTotal()) // Is there a position?
% R+ @' V0 Z+ Q' E! m{
" R) W+ u% Z. P9 H# j- P6 D: ldouble offset[1] = { 0 };
! O8 F2 T1 s: y0 _  Z" Cif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
1 N# Y- ^6 N' e( Q' o* z&& PositionSelect(_Symbol))  // Select the existing position!
! Y7 R8 T* A$ Q- f5 D8 K{& e- B2 d& m8 _
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);' U3 e8 u0 ]+ U' d  ^( |% B% u
double SL = PositionGetDouble(POSITION_SL);
+ n' ^  o  X' z7 B" D( Qdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));! X, t  `9 b  R  h) C( \- k
if(tipo == POSITION_TYPE_BUY)/ f$ s- x9 k& ?# b0 w4 z1 ?0 @5 a$ R' q
{4 n0 o6 Z3 k0 w9 {' D
if (cotacoes[1].high > cotacoes[0].high)
) h5 H6 A+ `4 v& W  G5 c{! e$ b; ^9 q9 n' z0 \; j) R
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];0 W9 L: j; \% |
info.NormalizePrice(sl);, E8 ^6 f* ?1 t1 O# Q) A5 v
if (sl > SL)" i( p; [, V) R1 x5 Z& B+ L
{
$ P- [* l" j: H, W4 S5 z: v& Snegocios.PositionModify(_Symbol, sl, TP);# z( J2 F5 p/ v" \* r2 `
}! p1 @* K# t- q2 W( [6 w* U# I
}
* q4 M7 [1 T# R( S# ^6 F2 g}9 x; U( }7 U3 v3 ?9 v  {9 j
else // tipo == POSITION_TYPE_SELL8 D  R; f1 U: p& U) d' w& |. Z' L
{8 ?% o% ~$ V$ `$ |" g
if (cotacoes[1].low < cotacoes[0].low)8 |0 @1 I5 z5 B& y2 O' s5 t* _
{
1 m1 b( C" @$ U/ \/ Mreturn true;
' Z( M% I! ?1 w4 k}) A6 d- l' I( z
// there was no position
2 A4 J& ]5 r7 w' L6 Jreturn false;
& P7 i# |# {" P' }8 P# f4 O9 S}& Q2 L: i* ~# W
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
+ v2 E+ S( S" U- M到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-17 18:00 , Processed in 0.652910 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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