私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
0 ~) B0 Q: W7 D8 ^3 g在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。  Y0 [# R7 I) M+ \$ e
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。/ S+ J, s2 J  t" n, C
以下是制定这些规则的代码。
# Z7 e3 f6 r/ |9 y//--- Indicator ATR(1) with EMA(8) used for the stop level...
: J& C* O5 S1 g2 B! v) Hint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);* U% z9 H0 \: o" T
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
) e. y' Z  l: l  f+ J" G//--- Define a variable that indicates that we have a deal...
7 x( t0 \& Q1 F  E3 q# Z/ ?' Ubool tem_tick = false;3 }( c- [3 e7 S$ v
//--- An auxiliary variable for opening a position  F0 |- U6 Q, h- r7 C4 r' R9 i4 Y; G* \
#include<Trade/Trade.mqh>+ a' G0 v6 [# q# |  @8 Y6 e* F
#include<Trade/SymbolInfo.mqh>
) m; f+ U, R- O2 C2 j* v* QCTrade negocios;2 ~7 y$ h2 X  j5 M# G' F  X( |
CSymbolInfo info;+ A* Q) O2 y5 @' ?  ^" Y
//--- Define in OnInit() the use of the timer every second
  O& `" Q  C/ O% I6 v//--- and start CTrade
5 R6 f" c8 U  @6 Aint OnInit()9 t% P  ]' q+ z, B( u
{
, M9 }" b! R+ R# f6 k0 T* Z//--- Set the fill type to keep a pending order- t# W( c5 [6 V  x
//--- until it is fully filled0 A' b8 }* [) ?* r
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
2 @0 _8 ]5 c# e' _//--- Leave the fixed deviation at it is not used on B3 exchange5 a5 I. p4 o1 K% a7 Y% p
negocios.SetDeviationInPoints(5);- d1 a$ I5 I5 n* U6 w
//--- Define the symbol in CSymbolInfo...
1 V" }3 ]9 `$ }info.Name(_Symbol);
; X# q7 E4 L% {- b8 Z8 P//--- Set the timer...3 M3 s/ e/ l1 U) x+ J( T
EventSetTimer(1);
* I; I+ o1 c" J: \2 x//--- Set the base of the random number to have equal tests...6 X$ [; \, k9 t& I& @$ q" r
MathSrand(0xDEAD);' ]- q% K4 |% U0 I! |2 g
return(INIT_SUCCEEDED);
: f/ u4 L" n' ~9 @; d  N}! p8 z+ Z8 P- K. }2 U7 T! W/ J
//--- Since we set a timer, we need to destroy it in OnDeInit().
8 L3 H7 _" U8 v4 b- rvoid OnDeinit(const int reason)  I2 c! }1 j1 z1 o$ W0 @0 Q0 y5 u  {
{
% t/ k/ W' j- V  x! o5 w" Z8 WEventKillTimer();
$ B* f& R! T" P' P0 H3 v9 l3 }3 ^}
# F) `$ M3 Y6 d  {//--- The OnTick function only informs us that we have a new deal
* |7 M" X  S- Dvoid OnTick()+ U& }& R* V* F0 Y
{7 }% E+ C9 W, L/ S5 v
tem_tick = true;
* d4 T0 @& n, O6 q3 H2 V  B}
: z% b2 Y$ p; r* c3 U//+------------------------------------------------------------------+
7 W, w3 Z0 Q! j//| Expert Advisor main function                                     |
! k5 l6 c8 X* ]//+------------------------------------------------------------------+
  z, S; L: l# R- Q# K5 X" \# G# `void OnTimer()
8 P9 T. s2 W: p* X+ K% P* Y{: B6 Z  \- @: D/ l4 e5 K
MqlRates cotacao[];
7 `( G% J1 W3 ^return ;5 j4 n/ Y- X& t' v
if (negocios_autorizados == false) // are we outside the trading window?
) L& d# o3 U* i/ H6 Preturn ;
5 W2 Z2 Q7 t  H' p6 o0 F% t//--- We are in the trading window, try to open a new position!6 `& M- v# \% \2 L. U
int sorteio = MathRand();
3 c) p# b5 {) w0 k4 O* t+ G3 p//--- Entry rule 1.1/ {# D' F  U& @. v6 k0 h& _
if(sorteio == 0 || sorteio == 32767)
( p/ D/ _/ ~0 W, K+ R* k4 ~0 t' breturn ;
- K9 w  l4 T* M) N: m' g1 \if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy5 j' i2 d1 }: g9 O5 o: G
{9 A$ ^* H( c3 K5 c# d. g
negocios.Buy(info.LotsMin(), _Symbol);
8 V2 a9 s$ h+ @4 e0 B}
! w/ G, R3 s4 eelse // Draw rule 1.3 -- odd number - Sell6 K# a3 N" U% [( f. W- l5 B
{! ~3 V# t3 d' k6 O6 r- b' F0 ~# k
negocios.Sell(info.LotsMin(), _Symbol);
4 e' }6 k, E; R2 W+ ?+ G1 c: O( E4 T}
# Y1 |0 D' ]+ z% H  H0 D2 D9 r}
, [: s8 `' [' S" [& ^+ Z$ s9 h//--- Check if we have a new candlestick...4 V; J3 C- S9 ~/ J$ r
bool tem_vela_nova(const MqlRates &rate)3 t7 z4 d& q  b& a; U: t
{' C( w7 R/ `8 @2 c
{( ^. u) x7 v4 o4 L
ret = true;
. ?- T3 ~; ~( Q# \; R7 y" _close_positions = false;+ o8 ?5 O3 a9 l, {+ I; ]! y# E% G7 R
}' r  }  j! z8 f: Z! v- L: F& w/ y
else! p) f# v$ [+ x8 N- O$ @
{
/ h( ]0 n- `6 P. E/ H5 E  O' `, mif(mdt.hour == 16)
3 B& ^. s: r5 {8 O' ~: Jclose_positions = (mdt.min >= 30);
# T& m. A9 p& {" A- V}$ q& }1 k. D! g' i0 f
}
4 B( f. \3 g4 T7 nreturn ret;
6 y  h* X4 F% k- _7 c}- h0 `9 g# d6 P9 I, m8 R/ q
//---% h4 e/ s7 B" ?' M
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])- g5 ^' f0 L) U3 {  L# }+ B
{
2 v$ z$ r$ Q( K# _# R' t) g0 G0 ~* Qif(PositionsTotal()) // Is there a position?
) J2 ]* h- G" E& b) o  i  F{- T1 S5 h; j  H. g" E+ M
double offset[1] = { 0 };9 L6 k; q2 |! O( k0 i% V7 ^
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
7 x) i/ c" G( ]. S& y&& PositionSelect(_Symbol))  // Select the existing position!6 r; q" _3 D- {5 @2 d% ~
{
, d0 f( M. `' H- G/ v* Z) Z: BENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
3 W' i: D7 R+ G7 ]! udouble SL = PositionGetDouble(POSITION_SL);- S5 d( e5 T' h4 O
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
/ _9 M# W- v  D/ {# K+ nif(tipo == POSITION_TYPE_BUY); V' e: K: B1 ^, e, c% {8 N
{
; x2 @( w1 k  y: v% Nif (cotacoes[1].high > cotacoes[0].high)0 R" @, H+ H( S! x' \
{5 O' \, v: L* ~4 T
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
. O6 v1 j3 q: j5 a6 S$ \info.NormalizePrice(sl);# p$ _: k  T2 c  `* n2 x
if (sl > SL)  b( k: `9 W, i& {' T8 F
{
1 F  a, l* @+ `2 p) `- e% P# gnegocios.PositionModify(_Symbol, sl, TP);  d% s8 G" i$ y% O* v
}
# z6 t& t0 ^) E; P* e" ]& I, f}) _) G1 d7 Z: |9 x8 i, D1 v. K
}8 k+ `7 X: R+ w- C
else // tipo == POSITION_TYPE_SELL6 T+ M- v1 B/ O/ T5 E: z6 ]) m+ x
{, v# V1 `, x# ?8 L. ~
if (cotacoes[1].low < cotacoes[0].low)
& D( @8 H: i# C' e3 q# n3 y{
( U; N6 [; f" k! J% a8 }return true;! M. |" N: I5 t' d# y
}
. \, j- P' |$ e* B) U+ d: z// there was no position
6 V/ ^; U& P- \5 u  Sreturn false;
8 W8 [, S3 T2 e, k9 K/ Z}. q; L8 X1 q7 h- P$ O$ I3 o
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
9 Y" M* b4 z6 ~( v, U到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-27 11:18 , Processed in 0.574369 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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