私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA9 o( ]/ [( `0 R
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
1 B* P0 |8 B# g0 e5 w为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。3 d! O9 `0 [8 u6 e' x0 n
以下是制定这些规则的代码。8 s, }  I2 ^3 Q6 d+ V2 I
//--- Indicator ATR(1) with EMA(8) used for the stop level...# }. @/ d5 c7 ^# f+ k
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
; Z8 J6 I8 w; @( o5 G) Bint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);" F9 N& K9 Z! T  p
//--- Define a variable that indicates that we have a deal...' b" o' x8 b0 {4 M
bool tem_tick = false;2 F# a/ {% Y/ b& R
//--- An auxiliary variable for opening a position! s) V  f! X! }1 V& ]. ^
#include<Trade/Trade.mqh>0 [; M0 Q0 S0 J+ w  j/ D9 V! a: ]7 j
#include<Trade/SymbolInfo.mqh>
: M+ z9 S1 V3 eCTrade negocios;1 }3 h& A- z3 k  r( Y8 Q  c
CSymbolInfo info;! |) F1 g- J- b# o, r+ k3 J
//--- Define in OnInit() the use of the timer every second- }* v: @7 r2 E6 v4 ~
//--- and start CTrade! q' h  k+ j$ W3 K- E. x! h
int OnInit()
' T+ {$ w( P/ E6 H& N{5 a& r0 |, Y) y! ]4 X( q
//--- Set the fill type to keep a pending order: v6 o9 a7 U8 I- R# [- [
//--- until it is fully filled, Z- ^/ p, p, s' j" y% v1 N$ o
negocios.SetTypeFilling(ORDER_FILLING_RETURN);4 c5 U; ?* b$ O" u  ]: ^1 K
//--- Leave the fixed deviation at it is not used on B3 exchange
5 o. i8 F2 |1 a( \0 J  M3 K1 X; }negocios.SetDeviationInPoints(5);0 Z- T+ ^* l0 K# l) }2 [& ~! s
//--- Define the symbol in CSymbolInfo...
& X+ ]+ N: T1 L) Minfo.Name(_Symbol);! S5 H6 M2 U1 v2 S
//--- Set the timer...
- S4 W1 D% K3 Y" A; v( @EventSetTimer(1);
) S4 i) w1 u* S' [7 {0 Q: [( |//--- Set the base of the random number to have equal tests...: w- d/ ?! L2 J) {3 c7 h7 L
MathSrand(0xDEAD);& q" ~1 D4 i% R% E
return(INIT_SUCCEEDED);
6 S* {  Q; p/ O; s9 |0 H}" C$ d5 m( b7 }" q  |* l
//--- Since we set a timer, we need to destroy it in OnDeInit().0 @4 p) v. X0 L5 A! `
void OnDeinit(const int reason)) p8 p: N: d  R2 ^
{
# j2 A  R9 L2 t! I$ `( s) _5 ~EventKillTimer();
# q) J9 u. o6 X- i8 z0 _! q}, o1 [9 n$ K7 R. g1 d+ ?
//--- The OnTick function only informs us that we have a new deal% A1 Y% l- ?8 G( L, t% ?* |& ]
void OnTick()& `) {% t  E% R$ H& i
{, _  L' C( ~8 F1 c; N# ^
tem_tick = true;/ S3 N0 A9 C9 A/ I6 L' W1 c8 m: R, _
}
/ {( H* l2 U9 @  D; I//+------------------------------------------------------------------++ i3 p# v$ a8 @) |+ s+ _" E7 e
//| Expert Advisor main function                                     |5 m3 l+ [" n- ^! _
//+------------------------------------------------------------------+
4 p7 p7 ~0 a6 u0 I5 |! [" j7 Uvoid OnTimer()
$ u+ u5 o4 [& U* d{, y( x. x0 c0 i# T" r4 F
MqlRates cotacao[];
* t6 W* H% G2 B% N. V1 mreturn ;
6 W: @- c' ^, Q1 |* P# {! eif (negocios_autorizados == false) // are we outside the trading window?
. I) f5 l6 r7 K1 ]' a4 V+ L! s# [return ;* Y5 R* |: d- U5 @
//--- We are in the trading window, try to open a new position!
0 N) m) O" Z' nint sorteio = MathRand();
) r8 f: Z) |( W) L9 h% o//--- Entry rule 1.12 r; m# u: W9 D6 E, y+ D# O& o" t
if(sorteio == 0 || sorteio == 32767)
* k5 \. U) Z$ H2 R5 ^6 Qreturn ;+ b. t" e5 F6 z) K
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy% n) u2 p5 P) D/ k+ J9 W5 D
{
% o' u3 G  [7 L7 Z0 Q) hnegocios.Buy(info.LotsMin(), _Symbol);5 p- @4 ?/ w6 z: v- K( T  U6 i
}
  A% @; O' o) Y: ~else // Draw rule 1.3 -- odd number - Sell
0 S) i1 k4 K& ?' ]" V{
# V7 J% P1 |$ l' @negocios.Sell(info.LotsMin(), _Symbol);
. h2 y; p6 I$ Z: _4 S. o% q, i}' m) T; X5 }2 h
}
: v8 T. _, Q* r9 v//--- Check if we have a new candlestick...6 q* ~- B2 h) C  _
bool tem_vela_nova(const MqlRates &rate)4 B0 k( N4 u+ J( ^2 ~* D
{
+ Y: D3 o/ a! |+ e3 x& ?* V{
& o* w' D  ^1 J5 S7 m0 ^ret = true;
' O8 A' U* z: V: |3 G  [. W: ~close_positions = false;
+ \3 @( V" v" D) _9 z}
8 [+ a3 Q! B1 S$ B! Belse6 a& D9 d+ C9 z! n( O- e
{
+ ?; u' J) t) K4 Kif(mdt.hour == 16)
, U. q7 `) v; x) bclose_positions = (mdt.min >= 30);6 D7 {! X7 E5 u# i
}
2 L) U' q! a; \8 |}
: q  E/ f+ H5 Q# ~; treturn ret;
/ f: G3 x+ o5 T+ j( m3 r1 ?, i2 M}
$ [& j4 {7 O, A0 J4 n: [//---
+ u$ A+ Y& q5 g; Y$ ^, Vbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
2 h( V/ Q1 _7 m% A3 E/ ]' h: o{+ O# s0 ]! u2 p( ^
if(PositionsTotal()) // Is there a position?9 {" K- H  ^( F2 m! j
{/ X4 L. A7 |3 s! }$ R- M
double offset[1] = { 0 };
( c% Y* L' e1 G7 g- @& Pif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?8 x8 e8 _4 m( [/ ?4 C. E7 C+ t; R1 Q
&& PositionSelect(_Symbol))  // Select the existing position!* b4 L- Z% C+ f$ |- o2 u* A- z
{0 H  i1 e, c' C3 l( ~; }5 r( H
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
; s% F  p: o' ^! B2 Fdouble SL = PositionGetDouble(POSITION_SL);0 W2 a. f4 j; S1 a, g  J
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
/ n! b8 q5 ?! p: |if(tipo == POSITION_TYPE_BUY)
  q+ ?8 @' L& W: P$ U1 o{
- @+ P4 o; ?" L9 Eif (cotacoes[1].high > cotacoes[0].high)2 Y( |* L+ I* t) y# _1 n7 z4 f
{& [/ R( H: t/ F% v* {% i
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
% S# m5 A2 W6 P) y" V5 sinfo.NormalizePrice(sl);2 |. Y0 F" ]  g# P
if (sl > SL)
% N) g2 A, Y* V1 h% v& i& N5 u{0 H, Z* K4 k/ n  N% o
negocios.PositionModify(_Symbol, sl, TP);' K4 d) G- E, g
}
; r1 J) b# J: c6 Z. R) W}
3 o$ N, v7 i: Y( C) j# N! |3 t8 R% \9 M}
4 W. q2 W& k; f, B" gelse // tipo == POSITION_TYPE_SELL
+ V3 K! a5 o8 W{
0 U  |' Q7 S5 \if (cotacoes[1].low < cotacoes[0].low)3 @1 x) y+ P; X& c. m5 y
{0 B3 q8 G( F5 ^& @* E7 q, W
return true;; b% B7 ^) K) |% d$ _% i+ f* ~% [
}
4 B4 S& R* L' |* [4 C/ B// there was no position' D, S! G4 V$ j- B! w
return false;2 m" w6 Y" |/ i3 Y+ {: X0 p
}8 Z4 b- k" g/ e& `4 f8 L8 p
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
" e2 ?3 L2 r8 k2 l( z8 w到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-15 23:43 , Processed in 1.051288 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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