私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
  i$ G2 g: I# C1 V2 W* F$ t! l在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
4 S% Q* z9 Q+ n: K为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。7 }' Z8 {9 @! `) o8 b  B8 r: E1 k
以下是制定这些规则的代码。
, F! e! B, ]/ N2 t$ f$ @0 p//--- Indicator ATR(1) with EMA(8) used for the stop level.... m3 s+ m5 M2 Z
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
- P) g/ p% U7 e& H5 tint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
! N4 A7 r9 L9 Y3 z0 e  t7 o//--- Define a variable that indicates that we have a deal...1 a, K, r8 Q+ Q
bool tem_tick = false;
3 G" M4 G6 R( f- P4 t$ A; R8 q  V) Y//--- An auxiliary variable for opening a position" i0 G+ D0 q9 {8 u$ B* f) P
#include<Trade/Trade.mqh>2 c- J; e# N- P5 j6 a9 x) ]- k
#include<Trade/SymbolInfo.mqh>
1 s/ c# X0 B/ x! R; }1 }+ OCTrade negocios;
9 r% Q' U. X3 @$ w3 X7 Y6 J' LCSymbolInfo info;
$ _: ]5 P; ~- l//--- Define in OnInit() the use of the timer every second
4 q4 u4 s1 ?+ j  P7 W//--- and start CTrade
4 }$ _& E( a; r& W$ j7 Dint OnInit()7 q, i, Z8 K# L& h% ~7 c8 D
{: E9 [% o9 `$ S+ [$ t+ w/ l
//--- Set the fill type to keep a pending order
, _: m5 p7 C. v- w//--- until it is fully filled2 V- u6 \9 |6 Y6 j7 l) k! d
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
4 ?- e% Z0 _& T//--- Leave the fixed deviation at it is not used on B3 exchange" j0 P1 E; r+ c: J3 ?) u5 A5 [
negocios.SetDeviationInPoints(5);
! Z: r( X/ S2 S' a$ j6 k( a//--- Define the symbol in CSymbolInfo...
/ E$ F. u! y9 z2 j4 V# |5 D$ Hinfo.Name(_Symbol);7 A3 d6 `6 ~( u5 v" i( L4 g: `2 n
//--- Set the timer.... I2 n* _8 W3 x- \% i
EventSetTimer(1);
0 k- x; i$ n) o7 A1 N. S//--- Set the base of the random number to have equal tests...
/ J# _/ m& ]' e( F$ ?8 S$ C( K: J3 c' JMathSrand(0xDEAD);% D6 N( f4 D! k/ `" a' d
return(INIT_SUCCEEDED);
2 ~' |( R1 g, G8 F$ ?) o}( ^/ h* Q$ s! s; k, \+ ^, u
//--- Since we set a timer, we need to destroy it in OnDeInit().
! {7 [1 w* _1 M9 W9 C% u1 Kvoid OnDeinit(const int reason)
# j6 C; E8 J1 [. ]& P{& V  x4 s  n+ w6 t$ w( X% v7 ?$ Y
EventKillTimer();5 K+ @5 @" A% I' Q2 Z
}
+ V: B, p! s. m) k8 [9 ]8 S  ^//--- The OnTick function only informs us that we have a new deal( z! }  a" q. K
void OnTick()" {* j6 c: B* F# n1 T( s
{
4 ~/ f8 L  U/ S0 ttem_tick = true;
) U# ?& A4 o+ g+ I# U7 n  ^+ c}
& v+ c* m4 F7 X//+------------------------------------------------------------------+/ P. ~: G  B6 p1 o
//| Expert Advisor main function                                     |1 n+ i% s2 s/ g0 r" _  z
//+------------------------------------------------------------------+5 W8 @" S+ T8 w) u/ q% d. M( C
void OnTimer(), C5 i: }5 w6 H$ i
{% g3 s1 X% ^, s! B! X
MqlRates cotacao[];( o% M8 |$ @% e+ ]3 |! e+ z
return ;( W% ]+ a/ V6 N  d
if (negocios_autorizados == false) // are we outside the trading window?1 ~6 z5 {8 l0 W, E& ?  s" w8 U
return ;
* w$ {- W% e4 s6 n9 V5 U//--- We are in the trading window, try to open a new position!
/ |* i5 K- \2 o3 N1 Z5 H$ Qint sorteio = MathRand();
& ]! l4 f! b0 B4 c# h8 Y# J: C& C; L//--- Entry rule 1.1# G3 b1 T2 n- M% q4 r; {9 p! o& z, r
if(sorteio == 0 || sorteio == 32767)
% T; l: }0 I" ?# t$ {/ a' Z! D" treturn ;
7 [! P# J9 |# T+ zif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
# a7 t5 ]9 H( _  f. A" ^" {/ E{5 Y& w1 u0 I/ H! [+ w1 i  b
negocios.Buy(info.LotsMin(), _Symbol);2 {+ j* g8 A* _  M6 D) \0 G
}% j; x) m! \( R( o
else // Draw rule 1.3 -- odd number - Sell
* u$ Z* }# l4 G" A1 h, ?7 p$ v{
. G+ Z4 _0 Y& w6 H- Snegocios.Sell(info.LotsMin(), _Symbol);3 m( ~2 I$ D6 Y
}0 _) Z2 W: r4 R
}
% }9 k( M9 E8 ^! c) q0 ?//--- Check if we have a new candlestick...
: u# W1 a( y3 S" I" W8 I, Sbool tem_vela_nova(const MqlRates &rate)2 _* ?: ?: t% U/ N3 Z  j
{
" U9 g- r" Z: @3 k{; h& {! }# x, `3 b+ y
ret = true;2 q' K8 b8 Z' Z9 ~+ y
close_positions = false;
5 X$ n" m1 I: d& d( c9 g}
5 ]" V2 c" K' q( m$ telse  V) ^. S4 I) B/ T7 |! c  r$ W
{
- M# ~- j+ G# [& ?) E' t; Zif(mdt.hour == 16)- g$ n/ O8 m. w* _, b
close_positions = (mdt.min >= 30);
& _- f% h$ B' y}
0 Z3 L; i/ s' U# L2 c}
' F' `% C0 V1 d. c: n) A5 yreturn ret;
; d0 a. t0 c# D& J$ O}
* V! m. Y2 A( D$ a" y! K0 l$ S//---
1 ^8 s# n5 q  }- Rbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])* {9 M( x0 B/ O" M2 g7 C
{
9 k5 ?0 y* j: @if(PositionsTotal()) // Is there a position?
1 C* ^: D* R2 r* \. j: x{. D, ?/ p/ d+ z# G3 g
double offset[1] = { 0 };
$ H9 ~; ]  m' i2 d+ z- M  Bif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
$ _: E' N. w1 K$ j; X( c8 y  n&& PositionSelect(_Symbol))  // Select the existing position!& K2 i2 ^3 I/ Z- ?6 K. L7 ]
{7 i, a& w/ {6 m. `6 ?7 n( `
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
. l0 F; e+ Y1 ~8 U+ Qdouble SL = PositionGetDouble(POSITION_SL);
5 c+ X+ t0 Z* w* v: S3 S0 w  Cdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));. T1 E1 }) K* i! F$ R( O) T
if(tipo == POSITION_TYPE_BUY)
8 X5 Y2 r6 I1 g/ j0 u$ ^4 u{* K) f4 N0 [& r) V0 O
if (cotacoes[1].high > cotacoes[0].high)8 G: q7 U5 q/ O# k& R
{
8 C% i3 r2 h5 ^, ~* ^- rdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];1 C' G( I( o% G) z# j, z9 x
info.NormalizePrice(sl);8 ?9 U1 \9 h* ]/ D4 Q
if (sl > SL)2 n0 p2 u! f1 c/ d  F7 ~
{
/ m9 c  a- k3 V3 e3 L+ knegocios.PositionModify(_Symbol, sl, TP);
. U! a9 N, S# l1 a}
% I- m3 L: O( h7 e0 w6 V}3 _  U# X0 m" F* w5 i4 h
}
: c$ u, V% O3 s+ A# a2 i: Y' {  }else // tipo == POSITION_TYPE_SELL
  ^' ]7 Q+ y/ P( G{
2 |* K/ E8 k' N2 C3 [* V+ G( cif (cotacoes[1].low < cotacoes[0].low)& B9 z1 e7 G' [. l5 t  V
{) V6 T4 P0 k! g0 v7 b
return true;
7 @% d) \. G2 v  C: e2 }$ \8 y" p}
- ]7 M6 r2 f4 C$ j// there was no position0 R3 a& \7 _  i  w( n1 ~9 K
return false;( L+ o: D6 |. m( I' [1 J6 ?
}& l" n4 D" }& K! D( `* o. d9 l
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。- \$ K  Q* ]3 A  ]
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-20 13:14 , Processed in 0.397432 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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