私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA! U$ k+ c, {3 |
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
5 T( n" Q7 v" h3 X为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
7 d) ?2 }: Q; x5 P% ?% T以下是制定这些规则的代码。( o( S3 H. [! D  D7 D' A
//--- Indicator ATR(1) with EMA(8) used for the stop level...' d" q6 L% E( n! v% g
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);! U  a! ?3 b3 z3 u
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);1 A& ]+ D: `8 {# J5 k
//--- Define a variable that indicates that we have a deal...- r7 D3 |- B7 Z; L8 C# v" s7 t
bool tem_tick = false;" d  i1 |3 j* K8 q
//--- An auxiliary variable for opening a position1 m4 P0 m: x0 Z
#include<Trade/Trade.mqh>
- c0 ]5 C# T1 V#include<Trade/SymbolInfo.mqh>" |3 _" }' [' a3 b' k
CTrade negocios;
. c$ _8 L* M+ CCSymbolInfo info;
! h# s$ v# \+ I. q3 Y* ]3 N//--- Define in OnInit() the use of the timer every second% k- z) m/ B3 ]4 T1 t
//--- and start CTrade
8 i( B8 P& j4 p% ]+ Uint OnInit()
1 v- {+ O, H( q1 Z; n0 Y1 V{9 y% J% @! {. p& |; R
//--- Set the fill type to keep a pending order
5 f' y5 d  |3 [  H//--- until it is fully filled
8 i* x+ I9 A) u0 knegocios.SetTypeFilling(ORDER_FILLING_RETURN);
% \/ D, U! y" q6 ]" k  m//--- Leave the fixed deviation at it is not used on B3 exchange
9 c) K+ a, p1 O/ s0 x" inegocios.SetDeviationInPoints(5);: \+ _" B) j" h
//--- Define the symbol in CSymbolInfo...
/ |6 ~% o0 x8 f( X3 l% T- \- [/ oinfo.Name(_Symbol);0 r  q2 w3 {# X! t
//--- Set the timer...# }7 Q9 C$ k, J% v$ o! U/ _$ C) T+ ?
EventSetTimer(1);
: N2 ~; K0 ~3 M2 l+ R//--- Set the base of the random number to have equal tests...- n2 O! O% X1 u6 R( X# M
MathSrand(0xDEAD);
( V1 T# m4 ]& e& F0 X: vreturn(INIT_SUCCEEDED);
) w& h2 |! w# V& E  f  x) `7 Z}
# |& m% D3 Z7 r//--- Since we set a timer, we need to destroy it in OnDeInit().# v. `) i% A% z! p- f
void OnDeinit(const int reason)4 x7 K& W* l( d. a
{) j& t: W' w0 i, a% [% p
EventKillTimer();  W9 R1 C3 k$ @" R2 I. y# L
}3 ^9 A, j; [, y5 w" s
//--- The OnTick function only informs us that we have a new deal  z! O( g. F: g
void OnTick()9 T4 G: y- x% n, u
{
. y) c' q0 w4 \8 l# [& e& otem_tick = true;. c. N0 C2 b9 V6 P2 [9 u$ _# I7 ]
}0 n' _& ]4 _% p) P$ f: E
//+------------------------------------------------------------------+
0 n; p, ~" @' h6 Y; v* e//| Expert Advisor main function                                     |
( }8 s% g6 i$ S8 e$ a0 ~" f8 L//+------------------------------------------------------------------+
3 Z5 G" o+ W  A4 \void OnTimer()
) m- U2 W3 k/ m0 P# }& C/ s{0 i! U8 \3 H8 c. q( o9 I
MqlRates cotacao[];6 [, Z: I6 s8 [$ `
return ;
' R9 g2 j( f8 cif (negocios_autorizados == false) // are we outside the trading window?! Z/ ?7 ~3 a2 _* [7 u
return ;
% b6 i. t; ]3 |1 }' f//--- We are in the trading window, try to open a new position!5 g8 z" `7 e: o" Z7 B
int sorteio = MathRand();; x/ |! q/ y9 y/ W6 {: W" W' w& S
//--- Entry rule 1.1
7 d2 s8 @& p& F- Nif(sorteio == 0 || sorteio == 32767)' R) w2 C0 x" F5 n, w
return ;0 L" i/ b1 W' H) N, p" O/ y
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy. z8 P2 a, b) S  R2 H  v
{
0 Z* e  A% G5 z8 xnegocios.Buy(info.LotsMin(), _Symbol);6 m; f& ]2 F& ~
}
$ B( W( A- N0 ~/ relse // Draw rule 1.3 -- odd number - Sell2 p; O  l! e/ k$ G. a
{
  [5 G; p( o; M0 ]2 x  K/ a# }7 x. @negocios.Sell(info.LotsMin(), _Symbol);3 B3 U7 B3 V* t4 J
}
+ r+ O" G& O  D}3 T5 E+ `. {( z. D6 `7 F. y
//--- Check if we have a new candlestick...
0 X$ V; M6 Z, D( E( [0 pbool tem_vela_nova(const MqlRates &rate)
( G# Y9 k: O* K{& b* ^6 a6 k  F8 \! i+ i* h
{
, }5 u" A0 X$ g( ^) h* Tret = true;
$ i) ]3 e# Z- `, w: Oclose_positions = false;; O6 H* Q# L) w' T# t7 i; v* I
}& W* I% @$ U3 H9 i; Z
else
( d$ y$ y( X( {) T& `3 M/ \3 t% J4 d{
, p# g4 T& y7 T7 L+ cif(mdt.hour == 16)
8 z0 {9 K3 F& k3 E* p) tclose_positions = (mdt.min >= 30);* |9 e7 t2 C% c$ M7 @8 t6 \
}- ~% Z2 s4 b/ ?2 q# d' D6 y" P0 r
}
4 E) v9 x5 b3 x/ b' ureturn ret;3 X# N/ c" o; c9 l: X% O
}# E$ L4 n1 h% v2 Q  @" e+ R
//---/ l6 n7 n" B( l- M, C8 d4 i6 }
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])# U) d3 z. a2 c
{
; ~/ o2 y! B' F1 k# Kif(PositionsTotal()) // Is there a position?
$ ^/ N1 R! y0 V6 C5 I( R( d{% G4 H$ h/ |; n+ M0 }
double offset[1] = { 0 };
7 I5 M# _9 j! S: ?5 }/ Vif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
) [4 B8 r7 r/ @+ w9 ]0 w- g6 s&& PositionSelect(_Symbol))  // Select the existing position!; j: [1 P$ J1 H; y$ S' {/ y1 B0 v
{2 z$ r4 M% j+ S2 l& s+ w. ~+ ]8 K5 S
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
+ C/ F2 @+ W5 Y1 R! U. j6 T( Udouble SL = PositionGetDouble(POSITION_SL);
. t" q. p( ^6 x. O! odouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
' Y  h" G% c* {if(tipo == POSITION_TYPE_BUY)
' O) l  S2 y) J0 i{
% [4 D, b1 w0 \8 v1 u' uif (cotacoes[1].high > cotacoes[0].high)3 D$ T; J3 C* B% B/ e
{( W2 ^! _& Y  K$ v# T
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
3 c  b2 f. W# i! pinfo.NormalizePrice(sl);
6 l0 N7 w$ t" k5 E, d1 Iif (sl > SL)9 Z, n) M7 L) o" N- f' u. Z, l
{
' Y, H# O0 H/ c( y2 ^' gnegocios.PositionModify(_Symbol, sl, TP);
+ \& O7 |- c  ~; N! A}6 W/ h/ `2 p9 }4 }: o1 E" l
}
7 H% y5 q$ }4 _. z2 K" Z, p2 \& G( {}. q: K) E. n9 ?# i
else // tipo == POSITION_TYPE_SELL
: Y$ U3 e8 n1 @3 l{
# y; ^5 \- y/ f1 \2 u5 {if (cotacoes[1].low < cotacoes[0].low)" R( z/ {/ ?8 j: O- q
{4 v( }; @; G4 O  L5 q9 Q) y
return true;8 Z3 h: M7 _' K
}$ g2 E. O" _+ ?9 V/ p7 Y
// there was no position
8 h% v0 F2 N- a; c& k6 yreturn false;/ ~+ K$ _( r9 ?) I5 N
}. A1 _4 K; Z& Z
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。. _& t( H( @2 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-12-23 23:49 , Processed in 0.447377 second(s), 32 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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