私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA9 h  B) ^! C5 @! i* q
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
* C+ U3 C$ f2 d为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
0 E- ^( L. n/ }" F! T3 b" g以下是制定这些规则的代码。
' P; i7 T3 z" u! |- a1 X//--- Indicator ATR(1) with EMA(8) used for the stop level...; |( V* G2 y: M1 H# F9 y7 z
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
) A4 J9 E% ?* U& hint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);: g  Z! Q5 o% ]' `0 v4 z
//--- Define a variable that indicates that we have a deal...* `4 _+ [1 [( M( c+ Y2 z' A' g- S
bool tem_tick = false;, g. s. J/ l' I" ?. Y
//--- An auxiliary variable for opening a position. t9 B$ E$ e2 f. p; ^
#include<Trade/Trade.mqh>" C) ^6 E, u& S- D! p' g$ c  M/ T
#include<Trade/SymbolInfo.mqh>8 F5 j3 y& B# p4 O4 _1 E
CTrade negocios;0 M: R: ^1 \% u
CSymbolInfo info;
: K# r, J: n3 H! t1 z" y6 _  ~//--- Define in OnInit() the use of the timer every second# x& \/ m. `. K3 u  X  G
//--- and start CTrade
2 K7 x3 u: @. U) w/ c. h  Qint OnInit()
$ s" J, s& U5 P" i5 ]8 z( j' w{
6 _) R& z# d4 W//--- Set the fill type to keep a pending order
- X- Z% z+ I/ q6 S4 F6 O4 U" U//--- until it is fully filled
0 I: p; h  ^" }negocios.SetTypeFilling(ORDER_FILLING_RETURN);
# `- }; r/ `2 N8 y: X//--- Leave the fixed deviation at it is not used on B3 exchange
4 q$ O  x* D4 v& f  bnegocios.SetDeviationInPoints(5);
# |  O8 L( G; A8 q//--- Define the symbol in CSymbolInfo...
/ X; n8 ?4 u( \1 |7 finfo.Name(_Symbol);
% J8 o0 v9 T% p/ T9 V//--- Set the timer...
8 \0 \2 C- c7 E2 ]+ j# yEventSetTimer(1);6 {5 p$ J9 r9 x6 U
//--- Set the base of the random number to have equal tests...! x8 ^, t! [2 p$ S
MathSrand(0xDEAD);
' A# H7 P4 X5 Nreturn(INIT_SUCCEEDED);
. \. v4 \8 ^3 D" w}
& H2 Y3 v# D9 a//--- Since we set a timer, we need to destroy it in OnDeInit().. I0 H9 o' h+ v/ M' y
void OnDeinit(const int reason)
9 s# ]5 @2 e1 T; J. `, f{
* n7 }+ W. E$ JEventKillTimer();
( `$ O) ?! E. y' J; F5 T2 i}6 [) n$ ?% b$ i8 @* ~5 ^$ n
//--- The OnTick function only informs us that we have a new deal
$ n# X* }- ^2 X- d6 L. Wvoid OnTick()
, z7 B! V0 k5 a' K{
4 O4 a/ l( ?" o$ j# i* mtem_tick = true;  ], P7 E3 L" w  w  |) q
}: H3 B; L* {  t; ?: b
//+------------------------------------------------------------------+- K+ S2 {& u$ C% G0 N# g* H
//| Expert Advisor main function                                     |
2 z: a2 y+ P! c1 w# _1 ?: r2 O8 L//+------------------------------------------------------------------+0 f1 U1 J7 G1 g9 L
void OnTimer()/ |: [- ~# I, Y- S5 U: a  W7 h, e( A
{6 y0 j8 c& e5 m% e+ d1 I
MqlRates cotacao[];
7 G9 l: M6 C; @9 vreturn ;
5 R  s0 z2 R& ^2 m- }! v# yif (negocios_autorizados == false) // are we outside the trading window?
5 y$ R6 ^! Y# d% |return ;$ T  A7 Y6 o. E3 \, a9 x
//--- We are in the trading window, try to open a new position!
3 [* r6 y2 d* u& aint sorteio = MathRand();. W& F; V1 h* I3 C% d
//--- Entry rule 1.1
8 s1 b+ q/ F! M2 Uif(sorteio == 0 || sorteio == 32767)
# }' H2 Q  E% t, }return ;
, F* @$ z+ X5 f: u" r! bif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
  ]; A. D* ?- p4 D$ y6 w{  }: I/ I. C- b, m& F3 d$ B
negocios.Buy(info.LotsMin(), _Symbol);0 `# m" K$ y& q6 v, O
}4 X! c, f# m3 W
else // Draw rule 1.3 -- odd number - Sell, F: O* Y, }2 p/ c; s
{
$ N" O6 Y/ B3 lnegocios.Sell(info.LotsMin(), _Symbol);5 L. B! C  a8 ]  @! ~
}, u# Q5 B3 ~3 _: y: L+ q
}
2 i# l+ C% p* i5 V/ Q1 B/ S//--- Check if we have a new candlestick...
2 o; R* p% j, A4 [7 tbool tem_vela_nova(const MqlRates &rate)$ ?) d/ c, x( C
{
6 E8 p9 Y5 E& }  m* z$ _3 Q{) |+ s2 C' d1 J3 i
ret = true;: ^6 F1 m7 I1 I& W
close_positions = false;
, ?& `, F5 ~# {. i}
, s1 V, M3 R' C! A$ ^# ]: Q* ielse* i7 N) F; A% a) J( X
{; u4 W# g& q) _& e* d
if(mdt.hour == 16)5 @4 U# ?# m' x! d
close_positions = (mdt.min >= 30);
1 }, a3 s& e5 B/ Z* T}
& Z( U0 b% `8 f+ Z}8 |1 }5 Q4 d- `' l4 Y8 r0 R
return ret;
7 n; ?! x+ |. C}- D' S8 J, x) m, ~) w( _! Z, r
//---0 R4 V; z) S/ l0 J
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])% [  h9 U2 l& h- ?3 d- U
{
+ B+ I; V2 s0 M/ b. fif(PositionsTotal()) // Is there a position?9 L) \' k$ q0 {" z& j4 b- |
{
' ]8 k. Z! W: z% M0 ]. \% sdouble offset[1] = { 0 };' M/ @5 ~6 y8 I0 }, F) B, }
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?' |  k* t1 C* B! ^4 p/ A, L: k4 d' w
&& PositionSelect(_Symbol))  // Select the existing position!
7 B- ?& C4 S- u+ c{
  ~$ J. _, g# E; B) r4 h+ i; pENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);5 k% s: F. N( K' U7 Y# Z
double SL = PositionGetDouble(POSITION_SL);
9 R: ]; w8 A1 G  r' Ndouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
7 g/ N: e  M, g  M7 I5 Cif(tipo == POSITION_TYPE_BUY)
  `% \  b0 p9 M: @, Y0 ?{! ?  X2 N1 e5 z; t
if (cotacoes[1].high > cotacoes[0].high)
8 v5 c* J+ f( y; M{
* _4 {- _( _% x6 F* O3 A3 odouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];" u: n' L( L8 I7 n+ c2 o. F
info.NormalizePrice(sl);6 s$ I, B& s* U: j& Z3 ]2 k
if (sl > SL)4 ]& @3 t' i+ I8 U# ~
{
) K9 u1 X- X) H% Snegocios.PositionModify(_Symbol, sl, TP);& o' J1 {! u. l: _( x6 c& ^, ]
}; o4 ^3 V  B1 e2 c& T4 \$ C
}
. u0 N0 B: P4 a}+ z) w: B4 T6 j# ?
else // tipo == POSITION_TYPE_SELL; _! n9 W: e: `$ z/ Q9 l: R- n8 j  j
{
& Q! F5 x8 Y# k/ e+ hif (cotacoes[1].low < cotacoes[0].low)! r9 P8 k" ?) ~, t8 f# ]
{- L' h- }" O+ S3 r
return true;- q3 x# G5 r3 ~7 R/ L( ^/ F
}
4 k* D& k' G7 @" d6 T  u: A, T// there was no position% y: s8 r; ~8 _
return false;
, c, A$ p* j, G. n( I  ~}/ V$ j% [: R/ K4 \8 l
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。0 H: ?1 l% |2 g( y3 F4 y" X
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-25 15:06 , Processed in 0.749289 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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