私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
5 k6 }, X8 ^9 d2 o在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
/ [8 T# \  J  ]) N为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。( r% u, s" c1 \0 I# S1 S
以下是制定这些规则的代码。
5 Z: m  r- m: [( ~* |//--- Indicator ATR(1) with EMA(8) used for the stop level...
/ J) D0 p1 C0 q, z* H* `1 U- rint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
' {, I0 e; f2 i1 \  N! S' {3 Jint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
3 P; r2 _: v# x4 P//--- Define a variable that indicates that we have a deal...
& x/ o! Y1 Y2 f& g5 t: A% ibool tem_tick = false;
: m5 ]  F# p% V6 D$ f2 B//--- An auxiliary variable for opening a position
) c4 d0 d% D# ^7 {' N+ U( C#include<Trade/Trade.mqh>
2 r' ?1 g# L2 ^! e* U$ I4 N#include<Trade/SymbolInfo.mqh>
# @( O% C$ O7 L! y; {) }: g5 jCTrade negocios;. ]$ r9 f& e1 M$ d& A
CSymbolInfo info;4 t4 ^$ S8 j7 w# \
//--- Define in OnInit() the use of the timer every second
/ X3 n% ]' Q* e/ o( s9 _  V7 ?//--- and start CTrade
* w3 `6 H# E/ x8 J' _int OnInit()
: t4 O' D; r' v/ X1 [; O. M{
. U% Q; L2 G9 }; K//--- Set the fill type to keep a pending order
2 W# @1 S: k: C) m, A4 s& V//--- until it is fully filled
- \& s" l1 G' U( R" x+ `negocios.SetTypeFilling(ORDER_FILLING_RETURN);
0 k* ]6 q/ l1 t+ h//--- Leave the fixed deviation at it is not used on B3 exchange
% g& `7 A$ d/ ~0 ynegocios.SetDeviationInPoints(5);
5 C1 r$ L$ q7 l# _//--- Define the symbol in CSymbolInfo...
- t% e& R% ?2 V- z! finfo.Name(_Symbol);
/ v/ E6 n# }7 o' f9 }) ?# R9 T//--- Set the timer...$ O! `$ @  s) i7 a
EventSetTimer(1);
+ `. A+ Q4 Q3 ~: @+ h+ Z" |( i3 `2 ^//--- Set the base of the random number to have equal tests...
9 P- l9 F/ l( G+ ?: m- ~! t1 HMathSrand(0xDEAD);
% Y6 e/ F. b# R& L; r( sreturn(INIT_SUCCEEDED);8 N' R$ c% A$ r6 ]6 \4 \/ ~5 J% z
}
( j: C- r2 Z4 X# @$ O$ k//--- Since we set a timer, we need to destroy it in OnDeInit().; b+ [/ A3 w4 ^7 {# Y4 }8 D! i
void OnDeinit(const int reason), b! H) W! a! D3 R& |3 P( D
{! E. w  N8 W, T" P6 S
EventKillTimer();, M2 E& ~7 e% m
}/ \8 g2 W3 `& a9 x5 e
//--- The OnTick function only informs us that we have a new deal
# `4 g- i8 Y* |+ D7 \void OnTick()
- K8 B" h1 x; c7 S{  ?- L3 J2 {$ [- D$ @# f
tem_tick = true;
. r$ l' I- v# A# ~}6 v7 Q8 I/ E1 D! E
//+------------------------------------------------------------------+
* X4 {) }; ~4 |: y//| Expert Advisor main function                                     |6 O3 _, K; [9 L5 F/ Y
//+------------------------------------------------------------------+& e+ a" B! l; ]6 M" W, z/ a
void OnTimer()
4 n6 s5 i' K$ {' H4 n/ F1 f{
9 z! V5 _" o, l+ zMqlRates cotacao[];0 L4 ~# K+ c; o# v% A, H6 `
return ;3 Y7 M" ]3 J$ K, _1 D1 p
if (negocios_autorizados == false) // are we outside the trading window?0 U1 \4 {( ~1 B% ?  `6 K/ `# }
return ;+ |, I* t' q; b3 }9 a
//--- We are in the trading window, try to open a new position!( g: W& P" n3 C4 z, \- `
int sorteio = MathRand();
* o9 t: J+ [  [8 Q' I7 V$ S- `, y) I//--- Entry rule 1.1/ g  c8 S- G8 w7 r8 s
if(sorteio == 0 || sorteio == 32767)
) r4 ]% C, U( D' C3 G: Nreturn ;
% E! b0 m2 h8 y/ |( e/ l! E# h2 b" Sif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy, D' f* u% f% g1 W- D
{
4 U  n$ S; x4 ~0 S$ A9 c; ~( dnegocios.Buy(info.LotsMin(), _Symbol);' W  i( K7 v& F' {# X  ^
}; U' Q' I# Y! A7 Q7 t3 Q9 u, c4 V
else // Draw rule 1.3 -- odd number - Sell& g% V% [; M. m0 k2 f' I: E
{) J4 M* P) |! H5 y) B) b6 ?
negocios.Sell(info.LotsMin(), _Symbol);
: c) p  a5 v/ |9 u9 M8 @7 m- O; ~}; w, J/ c5 w. n% l) @. B, ?: z, O, v
}' S) l% L( J' j! c; w, K+ Q8 ?+ C
//--- Check if we have a new candlestick...
6 v0 j9 u; e" e, v8 f' x1 U# lbool tem_vela_nova(const MqlRates &rate)
. z8 e2 `7 a# L# r" Z{" I1 v2 f, h4 X
{  M8 z4 k# t$ ^7 D+ B0 a
ret = true;
: S' [0 Z5 p: j# H- v0 o# d8 ]1 N% Qclose_positions = false;
/ R5 Y( G, p+ ^. }3 w" b}
1 t% r0 o/ @7 E0 t' Pelse; F" v$ {! I1 R4 D. k, b$ W7 P4 ?
{4 E* V- x1 W6 k3 L* ~
if(mdt.hour == 16)3 e# d1 G4 Y$ n& i, Q$ _6 J. E
close_positions = (mdt.min >= 30);
; e( q' \, W/ t9 Q, M& o) {5 R# L5 y}7 s2 l6 i7 \) O! _7 u; E( p1 g
}2 j# ~9 c" b5 H8 t
return ret;
4 b, E0 b8 s9 d2 ]6 U2 {& o}: Q2 [! W8 o. o. j
//---
( Z3 H0 x6 ?& F2 j, abool arruma_stop_em_posicoes(const MqlRates &cotacoes[])+ v/ \# {; H  Z3 W3 z- z+ L& |1 Y! F
{5 K. C' ^# G& D
if(PositionsTotal()) // Is there a position?9 w2 ?3 S7 w2 I7 `0 q
{
8 C. W* P$ G) T6 Ldouble offset[1] = { 0 };
5 ^, {/ F0 h) L( _0 uif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
+ z# n6 a  v  E7 Q2 g' J9 S5 {1 r&& PositionSelect(_Symbol))  // Select the existing position!
1 D- q- Z$ w" g: i/ G/ J* v{
& c; H' l0 ?% {% W3 ?ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);) `; x- h6 l' S3 W* r! I0 w  h
double SL = PositionGetDouble(POSITION_SL);2 h# z, l8 J% F0 l1 [( L: {) `
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
& n, q- l4 |* n+ u2 a9 h9 vif(tipo == POSITION_TYPE_BUY)
2 }' \7 E- l( ~3 g{
- Y7 m& A! ^- Lif (cotacoes[1].high > cotacoes[0].high)3 Q  K9 }9 s# ~4 g; Z+ s; g4 R
{
- ^6 O1 V5 Y, zdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
8 {: [  x1 e3 u7 minfo.NormalizePrice(sl);9 o0 ~& W7 s2 z& @
if (sl > SL)" M1 d8 y4 r5 o* w
{9 I# p0 ?! F* w: N7 Y
negocios.PositionModify(_Symbol, sl, TP);9 L; n* E5 }0 g1 K
}' M; y/ U* `$ @4 c, c6 I
}/ x% O4 O/ ]8 {; Y! e0 s
}; m8 |8 p" ~) y# U$ e0 }% N" X
else // tipo == POSITION_TYPE_SELL! y- {- `* C% ]+ D  \2 s
{' f: L) H' _' q+ Y4 m. _
if (cotacoes[1].low < cotacoes[0].low)
- ^. s  `0 a  l5 e; o{
# f2 M' _1 I( ~. j# ureturn true;
) x* I0 S! s! H7 I5 }}
) S  X: f9 Y+ g4 |// there was no position
; \- q; T1 v! U/ S5 |return false;
& m3 Y( \8 C5 X3 ~% X}
2 b# q: N( q1 W我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。) A7 p4 W: K0 P9 J$ f( G! q
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-17 00:30 , Processed in 0.441465 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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