私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA+ z5 B# ]! i" ?- Q8 u0 A2 Y
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。) X; y8 P5 T7 M1 C
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
" w- d% u& }' d' s6 H; R: r/ E以下是制定这些规则的代码。, [& J7 Y7 q7 @" t! @+ x" t3 m# ~
//--- Indicator ATR(1) with EMA(8) used for the stop level.... h: x7 l, ~: q; n* ?
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
4 d6 K4 G& B: {) m: G0 R, C  o- K8 t7 Uint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);" g6 ^  @8 f2 h: L. H0 Z# i
//--- Define a variable that indicates that we have a deal...# u+ y! h5 c; I# o' j1 D# W( I& b
bool tem_tick = false;
+ y& G3 K4 D  B* S& v0 h7 E$ k//--- An auxiliary variable for opening a position; j/ T6 I' r% C3 {+ I
#include<Trade/Trade.mqh>1 L0 r1 @2 c- B. _) f/ F* U& H
#include<Trade/SymbolInfo.mqh>
! n3 H4 ^0 I1 h6 iCTrade negocios;& N+ T( O# ~; ^* J. K& O
CSymbolInfo info;; \$ N; u4 D/ ~+ [# ?9 B5 Y
//--- Define in OnInit() the use of the timer every second
) @& @9 l& f) C3 \" v! e. N//--- and start CTrade! \& s3 m3 Z; c
int OnInit()
5 C5 ~$ k/ S3 N8 r{
/ ~# J, m# U' K: u1 @# j6 u//--- Set the fill type to keep a pending order
  A# z3 L# M4 z& B9 N( A4 A//--- until it is fully filled! R& D& q* X7 D) i
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
+ s, z  F6 z( Y/ `; h' R//--- Leave the fixed deviation at it is not used on B3 exchange: c4 l( R5 J2 k
negocios.SetDeviationInPoints(5);  P1 U1 |( h$ j0 S
//--- Define the symbol in CSymbolInfo...
6 P8 V2 D& g8 k  K8 j8 binfo.Name(_Symbol);! m, w' y" e. J% G- N% T$ m& r3 w
//--- Set the timer...
: s0 C. _' _) f% |3 lEventSetTimer(1);
- r# ]/ q6 u& l, d4 C; j' A6 w//--- Set the base of the random number to have equal tests...! I  A' c) i1 E9 k5 _
MathSrand(0xDEAD);
, G/ P+ C& `6 r$ Rreturn(INIT_SUCCEEDED);5 k+ j6 B) n& k
}' B! r5 D9 }' c
//--- Since we set a timer, we need to destroy it in OnDeInit().) ^6 z4 i$ }; g# j; |
void OnDeinit(const int reason)
+ j0 i8 }0 Z" w& J( B& g- R% `{7 ^8 O# |" B; ?/ g% t, R( B2 _
EventKillTimer();( S, C% Z8 @$ D2 |0 V' \% y+ n
}
$ S! C( {8 Y" y' _- _) |+ \3 `3 `+ O: l//--- The OnTick function only informs us that we have a new deal9 R/ t! |& z& E! Q% Q" o
void OnTick()
8 i0 R- @- {$ q. a" x{
7 B$ p' ~; S! U! C+ l' {tem_tick = true;$ \/ y7 m, h$ p/ s
}/ r! Y( {  k7 Z* r* z8 a* [0 B
//+------------------------------------------------------------------+( V! ?: W& L! k) R$ t5 _
//| Expert Advisor main function                                     |8 i6 o, w: K: d0 T( l: l& ^2 m
//+------------------------------------------------------------------+
- S! ]/ O1 h' V' bvoid OnTimer()9 ?- d1 v. v$ L* y
{) t/ ^! N! B- q# ]4 e' B
MqlRates cotacao[];* s! j' }. B" b* q5 e) K. w
return ;
8 p5 `% i5 I3 hif (negocios_autorizados == false) // are we outside the trading window?; c1 j$ M& @$ `% y5 A! I& U; D
return ;( \% y! Z$ b3 ]1 E" d( y- @6 `
//--- We are in the trading window, try to open a new position!/ R9 T& F7 }2 B$ P* ^' G
int sorteio = MathRand();/ x. Y* x4 z" f1 I( u8 D
//--- Entry rule 1.1$ H0 R/ @7 c0 _) z+ l
if(sorteio == 0 || sorteio == 32767)
3 u" g" V" \& Ireturn ;! v- R2 V. I( D8 _0 Z( m  l
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
, f' ^3 `1 o' R5 @! m{
9 x- ~6 `; m9 N# u: [negocios.Buy(info.LotsMin(), _Symbol);
' ]: x! u2 T+ B* T5 s}* C7 d  h  f9 `5 G3 a% C+ B
else // Draw rule 1.3 -- odd number - Sell
8 x- Q7 L# k6 x' e) T# C+ h{; S, k2 {9 R5 |3 ~3 @& z
negocios.Sell(info.LotsMin(), _Symbol);
5 U  w2 @/ h4 i9 i}4 U6 s, C% w! m3 c$ Y3 z3 I
}
% c' D% z" t. e6 M' g" X1 r//--- Check if we have a new candlestick...
* P) F  u; X2 w  y3 H( j, ]2 A$ rbool tem_vela_nova(const MqlRates &rate), F. I4 c! ]; ^& V' W% t! ^
{
" _8 E: _8 y! Z{
) x& m9 t; B, e$ t6 C. Rret = true;9 m6 d5 J( l" v8 Q" [& _6 U
close_positions = false;
4 x9 D" A' c' k( l$ |1 b}
6 {6 @3 V7 g) k4 ?& n3 Y3 U/ j' telse
4 t( {6 J! y0 g2 ~{7 L# E: K. y* L0 W7 d( e& I
if(mdt.hour == 16)
6 i8 f' l; v! |- P) Gclose_positions = (mdt.min >= 30);
- k3 S, G: j% D( f) S& O) N& v/ h  b}
+ ?# d7 |; [, G. j  I& R}0 u# F' c3 `5 ]7 t* k. H, w
return ret;& q+ }" g7 m: q$ M( j4 ~" q# f
}4 i, K% u+ p0 u7 u( g
//---
* B# O$ B0 f, P* g& Jbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
) w( A0 q1 U2 [, R0 }# D5 q{
1 @0 \% y8 R8 P* r$ b5 ]if(PositionsTotal()) // Is there a position?
# f; `. j, Q2 a, m{! A, z' M6 N5 ^% x3 W: t# A/ k5 Z
double offset[1] = { 0 };( |1 i" ^' l7 D7 N. @+ n
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?% ^" Y8 I% B. ]+ |$ k+ `8 j" k- A$ \
&& PositionSelect(_Symbol))  // Select the existing position!
/ c* C) i* a. \% f1 `$ N# G- E$ n- R{
. R1 T5 U% a# P% tENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);& Q$ g0 A& H0 K. p4 T
double SL = PositionGetDouble(POSITION_SL);
. e* w/ I9 D8 Y! z+ Rdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
/ Y2 f( L; a) s3 yif(tipo == POSITION_TYPE_BUY)
3 Y% U% B5 \3 y{2 U) S5 D, t, _0 g; E
if (cotacoes[1].high > cotacoes[0].high)) m0 }2 ]; i0 i* {* T
{: F+ k- u2 [, ]1 j; y4 h
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
! q% q8 K6 t6 r( r! ?; P0 a" Winfo.NormalizePrice(sl);
3 z! c8 A! W2 D- rif (sl > SL)' N# O/ w4 R% Z2 g9 ]: [  R) M
{# O. j7 p3 o4 I. U5 y
negocios.PositionModify(_Symbol, sl, TP);
$ v' b9 z$ t( K, ^' \; p  P3 q}
: {: S6 Z+ H$ H2 G) W}6 R8 g9 o- d' C2 \% g6 Q3 G
}9 ]6 x: J& w- `9 d
else // tipo == POSITION_TYPE_SELL- G1 T2 w4 Z2 |5 h
{( R3 [- T1 X& a$ e6 b
if (cotacoes[1].low < cotacoes[0].low)
) [! \* ?- Y( |" e& v5 X{/ \0 V( F9 S( N- c' r
return true;
8 R+ h( }; }$ K2 S$ F4 c}: U+ f% E) o8 X, z0 X
// there was no position
* o7 R& m6 h  b- P4 t, s2 Rreturn false;2 V: P. j5 F+ W% H! B$ D
}- ?' Q5 E% j$ {. u# k9 ~
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。8 S% a+ I+ A0 R  b
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-4 10:29 , Processed in 0.452095 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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