私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA- }& v) |& |/ Q% K
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
. s2 i  O) ~: C3 G# p为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
3 g5 ^/ J# |8 U1 G' e% G! G; b# ]以下是制定这些规则的代码。/ f' M6 T$ p: x9 c: G
//--- Indicator ATR(1) with EMA(8) used for the stop level...$ c  e( \; m% H5 l) E
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);) ~, s3 G& v, s3 f7 a
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);+ u) M  w# L$ ]; ]* |/ l0 I5 }6 Y
//--- Define a variable that indicates that we have a deal...& ^% S) B# u. E
bool tem_tick = false;. S$ T2 k" n0 K3 A- k, A& r
//--- An auxiliary variable for opening a position
2 o$ Y; v1 a4 h3 u#include<Trade/Trade.mqh>
( p3 o/ m: V% s$ P( o8 x0 ^& X$ \; Q#include<Trade/SymbolInfo.mqh>) o8 G# F3 S' G' f: H7 S
CTrade negocios;
) }0 ?( x# B/ }, u# XCSymbolInfo info;
6 G6 v& j1 z0 t8 G0 ?7 p//--- Define in OnInit() the use of the timer every second
9 F3 c5 S  c1 o8 M1 u# E//--- and start CTrade1 n; w- l4 y: m
int OnInit()5 q# D+ T2 Q& g9 F! N- O0 ]
{
* J. o. y9 Z* |( o3 ?//--- Set the fill type to keep a pending order1 z7 }% s! q9 s" |  \
//--- until it is fully filled! m# h2 Z, l- x, H  I
negocios.SetTypeFilling(ORDER_FILLING_RETURN);$ d& F) G/ `# T, e
//--- Leave the fixed deviation at it is not used on B3 exchange8 C2 Y! i3 Q6 s# {% d6 m' T, y0 O
negocios.SetDeviationInPoints(5);
; u) {5 D- r7 `' ^! D" F//--- Define the symbol in CSymbolInfo.../ R3 M7 f3 N* L
info.Name(_Symbol);
) z# P4 m2 _1 g# b//--- Set the timer...
9 Y/ Z7 ^4 x9 |! LEventSetTimer(1);% i! m$ T) Q/ J; S0 E/ [, z
//--- Set the base of the random number to have equal tests...
! y7 U/ ~3 D5 r3 e, F* MMathSrand(0xDEAD);! ]6 k' O( d0 A1 c/ J" y
return(INIT_SUCCEEDED);
6 e8 b2 [, J+ i9 d1 ], j. E}- M4 f9 @" k% i2 [8 h& W
//--- Since we set a timer, we need to destroy it in OnDeInit()./ C7 F3 h+ d  H
void OnDeinit(const int reason)& s, U2 o# v: e4 `5 e) V
{. M0 `, |9 v+ \; z4 Y! B3 E4 b
EventKillTimer();
. B" y1 \9 m* |: T. d8 t9 V}5 a! t& A* p0 s% c* L
//--- The OnTick function only informs us that we have a new deal
( W' H4 a' K% Y$ `6 Y" ]void OnTick()
2 B- u& Y, Y, R+ m, L! ^7 ]$ s2 R{
, x1 _. u$ F' M1 C9 V5 M' ~$ F7 {" K1 {tem_tick = true;5 v/ M" M  q* t1 K7 c, P
}' o7 T- b2 x) n  P2 J
//+------------------------------------------------------------------+
% R2 A  P$ g6 }7 l( j//| Expert Advisor main function                                     |- k; |. E( n2 ]4 i" K8 W4 u' P% N; N
//+------------------------------------------------------------------+
# _* c5 ?+ ?( h9 B: W3 Q$ u+ \# K) Vvoid OnTimer()
& _8 d. O; E; U$ m5 P  i; [& U) ~1 M{
; s, b2 {7 v# K; r. A4 _$ g3 qMqlRates cotacao[];
0 ^/ R: T* a+ u! b' L# j3 |return ;. Y6 ~# b1 |  k; X8 y2 ]
if (negocios_autorizados == false) // are we outside the trading window?
! K) ~0 j6 R  S3 Greturn ;
9 {  \- l: l5 N/ S  e) z//--- We are in the trading window, try to open a new position!8 _, \4 b$ ~6 {3 U& r0 }
int sorteio = MathRand();" t# |) w: u8 {: D! {/ d
//--- Entry rule 1.1
& E" }7 s% L. V+ ~( eif(sorteio == 0 || sorteio == 32767)
3 ?  }" e" g9 _. L9 V+ T' h, g' Jreturn ;. v* r" ~& a  }, V$ o  \
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
: T9 S6 H* b- R) A( i{8 P% ]! l  t' Y0 `0 g
negocios.Buy(info.LotsMin(), _Symbol);
% b# V& q: {& `}* c$ G  g; [( R, U5 p% z
else // Draw rule 1.3 -- odd number - Sell# I( r$ n5 w. \- t0 ~
{! K3 }! ~$ E: \3 ^, D2 L& A$ u
negocios.Sell(info.LotsMin(), _Symbol);
* |# j; W/ y" y1 A, Y( y% O- C. ^  M}
, K( Q$ S4 k* k# E* l# \}& B* K" o: G& D- ?7 K4 r
//--- Check if we have a new candlestick...
1 M; j# I9 i, L" o8 `bool tem_vela_nova(const MqlRates &rate)# p8 M9 _. U' s! g
{9 |0 S+ L: I4 U9 v1 E! U3 d' D
{9 E: b9 K8 U) Z
ret = true;
0 o2 u( p0 n, Dclose_positions = false;2 r! r+ c! F4 I3 J# f7 n1 q
}
6 s# L! ~) V) ]* [3 X& Kelse) ^; c6 L3 o# J/ n% |6 p/ y
{: u( z# P  J- z/ S6 @
if(mdt.hour == 16)
% ]; l; e- K. U3 x2 O7 V" @close_positions = (mdt.min >= 30);
* r9 W( y5 D+ W( r; }5 w}
6 H- e* M: t$ P" j. r}
) O6 F; E: R9 F& U. lreturn ret;1 C1 y; r1 F) F
}
! P! H1 }0 I8 F( ?/ f) C* ^" R; ~//---: C5 `: ~2 S* t4 Y; _# z# V9 G
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
; ^6 G0 W4 P2 u9 o# I6 N{
3 Z" ^* P: T7 t- u" iif(PositionsTotal()) // Is there a position?
* A0 f8 K1 M5 j: k. C{" ?4 a. Z1 q5 Y: b$ ?" c0 H
double offset[1] = { 0 };4 p( {  O  W6 @- o
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
- c" L: V3 y1 O5 Y, j&& PositionSelect(_Symbol))  // Select the existing position!/ q$ K! F, s: ~) ?! o) V& s
{, c. J' }- j9 S' k
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
  x  |$ i8 _( {7 i, j3 Xdouble SL = PositionGetDouble(POSITION_SL);
# ?. z4 c2 u- u$ ]/ @7 _6 T. Sdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));, G; g' j( f& g* W9 K4 z% x# `
if(tipo == POSITION_TYPE_BUY); ^* q# K% q" a1 z3 m- K) b
{
4 ?6 H. S  v$ k) R* [" Y5 ?if (cotacoes[1].high > cotacoes[0].high)
5 J$ I/ G  j  @8 U$ \/ Y1 g' i{) [5 S* u$ n. I% M- |( e
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];; A9 b# k. K. ^  `  t8 J0 }' P
info.NormalizePrice(sl);$ d1 V( G% r) u) P7 g$ _
if (sl > SL)' @0 \* ?% Y- b$ U9 ]
{3 p8 B  B+ [! K' H
negocios.PositionModify(_Symbol, sl, TP);3 c5 L& M' k; o# x
}# L$ Y" p* ^! Q0 \
}4 C* s' o+ b8 S3 {
}
9 W6 D! X. G: Q- O: w8 [( g3 telse // tipo == POSITION_TYPE_SELL
! Q6 v- \' F7 r% d+ N{- r( L2 |( X* T' m' Y
if (cotacoes[1].low < cotacoes[0].low)
/ P8 s; q$ ]& W9 P: K{
/ g1 ?4 h! |+ k; n5 {return true;# O* i6 K- O1 h+ f
}- A% ]  K$ I4 f& j
// there was no position
' a) |: u2 P+ o4 e0 Wreturn false;
$ ?$ W( y2 h1 C}2 b2 D7 R4 p5 B) R+ Y
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。9 I. c3 {' g6 |1 B# G0 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-21 13:13 , Processed in 6.423986 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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