私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
9 d  @3 x7 \4 \" @8 P- J8 O在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。9 c8 Z  X. D" y3 K% Q2 w
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
6 h. {. B1 t5 q9 Y4 t* c: D以下是制定这些规则的代码。
# @/ @3 e$ ^6 e% j. J5 M4 m//--- Indicator ATR(1) with EMA(8) used for the stop level...
, r$ I1 z9 [" Q: u$ p( m- f7 H$ Bint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);1 z( g. r2 y1 R, d, q
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);5 E" c0 Z) H5 X
//--- Define a variable that indicates that we have a deal...% m$ E# v1 l* R! v. h% T/ O
bool tem_tick = false;
! i  a. y9 \  S& A* i' j2 w1 W//--- An auxiliary variable for opening a position1 Y5 U  t- }  |( L4 r7 q$ w  ]2 P% u8 s
#include<Trade/Trade.mqh>" L9 D) p2 i8 c/ H" [; G. Y
#include<Trade/SymbolInfo.mqh>
4 f- ?$ s$ |# X  V( ^CTrade negocios;( o$ x: v. `# I# f2 I( @* a, J
CSymbolInfo info;0 c- d9 D' g' i  Y0 E
//--- Define in OnInit() the use of the timer every second, S* a. ], ~& `, t' p
//--- and start CTrade
- [+ m/ D8 a; g' O: q' o3 S, L4 ~int OnInit()
) s# P" A0 t' _  [6 Q/ }' k3 y{& M0 ~- D: Q# p0 j3 l2 m9 v2 i9 F
//--- Set the fill type to keep a pending order
  n0 D) B; V% c2 x! W) k3 b//--- until it is fully filled
; j. I( I5 \3 cnegocios.SetTypeFilling(ORDER_FILLING_RETURN);
2 b) r, t6 V/ u  f# z: ~/ y//--- Leave the fixed deviation at it is not used on B3 exchange; O* N1 s* A1 f  ]) w
negocios.SetDeviationInPoints(5);
& X8 [0 V; e( f+ J# f//--- Define the symbol in CSymbolInfo...
, e6 C: r3 t# O# dinfo.Name(_Symbol);
: u* e8 s. T7 E( P* C. k1 ^! s" b//--- Set the timer...$ E( S: \; W1 Q% e
EventSetTimer(1);
; y' a9 x( V/ S//--- Set the base of the random number to have equal tests...4 W6 t, e$ ]8 m0 G6 }
MathSrand(0xDEAD);6 o: r' d' F0 E: L+ f
return(INIT_SUCCEEDED);
: m- V" N5 }. \) @}
* J. k3 s% ^5 K//--- Since we set a timer, we need to destroy it in OnDeInit().6 V. v( G$ m+ S
void OnDeinit(const int reason)4 F& x7 U4 f9 ], i: A) \: G
{# Q+ u7 `( ^7 Y7 j" s
EventKillTimer();1 c8 f3 {# Q, N! r, w
}5 j* i* H* `1 q9 G, G8 I" `+ x
//--- The OnTick function only informs us that we have a new deal
' ]& ?3 ^. {' L3 u% a7 k9 x: Cvoid OnTick()# P, ?( G% V" }  [; c/ [/ O% \6 @* Q
{" b! I& F  A0 B5 K7 y
tem_tick = true;: |8 J. A) _9 f$ c' G5 g
}
9 w% `9 b% \8 [" U  u( g8 l  o//+------------------------------------------------------------------+
5 r& ]4 z- M6 k1 T5 h//| Expert Advisor main function                                     |% N- T' ^' W& G5 t2 d- z" b3 t
//+------------------------------------------------------------------+
* J7 L' L2 F- m1 c' @" }* k9 cvoid OnTimer(). f# n, K% |% c  q) K* T- N
{+ C$ ^2 W1 q# d+ d: y$ {
MqlRates cotacao[];
0 ^" L: y# b3 b1 ureturn ;1 V4 H: c; s' F# c3 Y& E
if (negocios_autorizados == false) // are we outside the trading window?
" ~# Q& t- S7 B: ]% freturn ;
. a. S; A- Q7 k) }8 v' c//--- We are in the trading window, try to open a new position!
" o( t; I8 [. |& {4 Rint sorteio = MathRand();
! E; v! u, _; Q9 f' z' T- {//--- Entry rule 1.1
' n! N6 S7 B3 x: l" \4 mif(sorteio == 0 || sorteio == 32767)% G1 L. |7 @4 j% b
return ;3 v! y6 ]$ Y7 ]# ~$ w) M1 [9 q+ ]
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy/ ?/ a$ S* ?% t! M  a
{  j5 x* K; [0 S  I' V2 @
negocios.Buy(info.LotsMin(), _Symbol);6 _( l+ ~$ z/ I: Y( Z- [
}% y4 i) ?! J' g5 F9 W! c
else // Draw rule 1.3 -- odd number - Sell
& g9 j- @5 F1 h9 K$ s8 y{
2 x4 S. z; d( X8 g; jnegocios.Sell(info.LotsMin(), _Symbol);! \8 j% F  S/ p, X* H! X3 ^
}5 r  f( I- w+ n, F7 x
}+ L* ?7 t4 F# K
//--- Check if we have a new candlestick.../ ^6 ~$ y4 L0 z+ t3 L/ `; z
bool tem_vela_nova(const MqlRates &rate)- ]( {8 F# b  d# {( ~2 W( |' G
{
# ^5 C  h/ V8 b) D" g{
/ n; q# d, ^% M! ~* Rret = true;$ I+ C# _% ~+ x! I6 P( b3 S( T
close_positions = false;
! V7 ]4 R2 @8 a5 Y5 l) c}
7 z( h3 n5 m! I8 W6 ~! zelse
) Z/ g$ v3 ?; t; s* B{8 q, T, A2 W" y4 S& G9 W8 o
if(mdt.hour == 16)
( v! Y) ~; K- r9 x: c/ Uclose_positions = (mdt.min >= 30);
# ~- h: j5 u" K7 q1 h}$ b$ v9 R8 s# J! k# X
}: K# L  A5 A2 o- c) j
return ret;/ V+ G4 S% a6 r3 g* ^
}
5 _" z$ x' C* A. o7 A8 Y: v//---* v* L) n5 u7 U( |0 V
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])9 \, z. _1 Y. |, l+ e
{
3 X/ q0 B+ O& \9 g  B1 Cif(PositionsTotal()) // Is there a position?
; N  w' b, U6 t' r6 z& p& E- k{
/ N! y/ L. y5 z; \, zdouble offset[1] = { 0 };# C! t- @! q& |/ C9 Q
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?9 k; r, z9 X" {
&& PositionSelect(_Symbol))  // Select the existing position!5 M7 y* J& }. u6 Z6 g' |
{" [- x- s0 M) a, C
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);: a& M' I9 h, F. i4 s
double SL = PositionGetDouble(POSITION_SL);
4 B0 W% T9 n+ ]double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
' ?4 x* t  a/ c& ]7 ~if(tipo == POSITION_TYPE_BUY)
, a: Y" @+ z) t5 a# |' f{7 V; u  ^% v2 D, j0 U
if (cotacoes[1].high > cotacoes[0].high)
4 t! W% r' m: ]+ L{
3 z) k4 o6 [5 J" a5 cdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
" X5 q( `  N6 W8 N5 Sinfo.NormalizePrice(sl);- |" G! U8 d" g
if (sl > SL): P) U8 R( q- `$ ?
{7 z: Z* r8 E4 q
negocios.PositionModify(_Symbol, sl, TP);
3 m4 j. Y; N( O2 }}; h% a( Y/ Q% W2 k5 i6 M. g" c0 f
}
! x2 o' Y2 Z. ?* B}0 \5 a0 x+ D$ b! Y
else // tipo == POSITION_TYPE_SELL" J. D( }& V. W2 p- x
{$ }+ g3 X! J3 `  J* e5 Z
if (cotacoes[1].low < cotacoes[0].low)
: A1 u) |1 {, I  i- s9 m- V2 c; n{
8 B2 x4 D7 x9 I2 x9 ureturn true;
/ P& L1 @% q! ?# e}: x' e2 L5 y0 O; Q& q
// there was no position: A8 r# F  I. \3 v' f
return false;
6 P& d. Q: b+ o' |; K4 ~+ s! b8 i}
0 W& C0 O/ M' \5 j1 R5 I  y我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。$ ]/ l( F: u( D" D& K5 \) Q& r
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-17 18:47 , Processed in 0.929335 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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