私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA0 k. p+ _9 |6 W) w& s
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
- f3 G- Z) }. a8 u+ f" t6 V4 c为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
* q/ M1 M0 S8 T$ e' N( T以下是制定这些规则的代码。' E- d3 I1 `, u2 g
//--- Indicator ATR(1) with EMA(8) used for the stop level...+ Y- C0 }7 W/ X+ b
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);' k* m, h. ^$ T- S  t
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
- G/ z$ \+ U2 \3 F6 p# z- w* m//--- Define a variable that indicates that we have a deal...# V( z& ?! O- o$ A* ?# @
bool tem_tick = false;; ^7 J5 l; i2 P9 }  b3 B
//--- An auxiliary variable for opening a position6 s& o$ V8 F) t. _4 [# j
#include<Trade/Trade.mqh>
( z7 [1 Y* {- {0 V- S  p. F! y" l#include<Trade/SymbolInfo.mqh>4 @" ^/ R: P0 q+ f- \( X
CTrade negocios;
- h# ]( k) g# H% e* R% }CSymbolInfo info;2 [! T; D1 k) a1 u" D
//--- Define in OnInit() the use of the timer every second* T4 [$ v) O4 ^! V0 y
//--- and start CTrade
( v: I! v/ \5 T5 Oint OnInit()  U" a1 U) H2 Q4 ^
{
% L6 v; [" C* c* X9 z: O//--- Set the fill type to keep a pending order! L# {- S+ l, K  M% O; T4 o
//--- until it is fully filled
, R. [' J' c4 ?7 g) w& enegocios.SetTypeFilling(ORDER_FILLING_RETURN);% l3 ~" S7 ~+ k$ j5 Y. l* y' d
//--- Leave the fixed deviation at it is not used on B3 exchange
6 v- d- [5 c) `* }' O& T8 ?0 pnegocios.SetDeviationInPoints(5);
% [3 W0 E0 `) w7 I$ [9 W//--- Define the symbol in CSymbolInfo...
) s3 A' d: w* s6 U  X' c+ pinfo.Name(_Symbol);
' c' d1 l) i+ Q+ F3 C$ R7 C//--- Set the timer...5 b+ H- h3 a# X  K, q  n
EventSetTimer(1);" @3 |& z( ?; N$ {. J0 k3 t
//--- Set the base of the random number to have equal tests...) w+ F. _: |; a4 {
MathSrand(0xDEAD);
1 o0 }4 d2 c9 Z  v: T" \return(INIT_SUCCEEDED);
: d2 H% E1 f& b* o. _}6 c' N2 O3 W% |$ U: g1 G
//--- Since we set a timer, we need to destroy it in OnDeInit().% D  a  r2 W5 e, y' ?& k- k
void OnDeinit(const int reason)
- P4 @2 F+ b8 w{
6 F2 ^2 ?7 z8 t# I* B7 S$ Z3 QEventKillTimer();: d$ i& t5 J% x6 ]
}7 e6 z  w  R6 ^9 @- \* P
//--- The OnTick function only informs us that we have a new deal1 B6 Z1 Z$ W  f
void OnTick()4 {, G- t7 f" N- C: A$ q- ]+ ]/ g  f
{* X# m% E2 w: ]9 t* k( X$ A" V1 \
tem_tick = true;) \% g4 E) d7 r4 H2 [
}( @/ ~' h( L3 C- l, \0 @. [
//+------------------------------------------------------------------+* J! A: V  u% x0 }
//| Expert Advisor main function                                     |  L9 ]1 E1 a; E% J2 z- J9 }7 U
//+------------------------------------------------------------------+
8 d  {, ^& c$ I5 V0 wvoid OnTimer()* {$ p% ]8 n6 i% c0 v
{" N+ L8 b' a) {# C. l3 Q
MqlRates cotacao[];" L, z& S, B! D- `- m1 T
return ;
7 o3 w: S  l8 g$ p) C% \if (negocios_autorizados == false) // are we outside the trading window?
+ v; J( M( X& J" a6 Lreturn ;
" A1 _- U$ }4 \//--- We are in the trading window, try to open a new position!- B$ Q4 Q7 E' S. j/ u0 e
int sorteio = MathRand();& N3 E3 v3 L' y" ]' ^  G
//--- Entry rule 1.1
2 X7 B1 L. ~1 ]7 S* a1 K4 b' m1 ~if(sorteio == 0 || sorteio == 32767)
2 j8 R1 k- g, }return ;( V) B4 q: ^+ f7 z
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
% j* f$ E% z8 V" y& r{
; u/ E' X, Y) Q' _( M) K- Wnegocios.Buy(info.LotsMin(), _Symbol);* m! n* Z! J' }" @+ S
}
) o& P- `0 ?; Z0 I8 j) lelse // Draw rule 1.3 -- odd number - Sell
' g6 |4 f& h* e; T6 I7 R3 c6 c3 i{
0 o* j$ o4 Q. r4 j) _6 fnegocios.Sell(info.LotsMin(), _Symbol);5 S7 ?7 v2 W, B$ C4 r
}
" Y' L( f  u0 G- V0 r" B% J}3 @" ^1 t; k: ~2 V, T1 N8 A1 W
//--- Check if we have a new candlestick...
; f+ v- A1 c! X$ L4 _bool tem_vela_nova(const MqlRates &rate)& o9 O4 u  m- Z; y
{4 b/ v  M) q, `! j
{
0 K# t& ^' t$ B; `1 W$ W- Z$ zret = true;7 R; x* M5 Y, N* t6 E% a- h
close_positions = false;+ Y* U- T$ x) \7 w; m2 N
}
9 R6 A+ J  _! Z2 k; {1 eelse
0 G, w2 e6 G4 \3 z# w5 Q' n{
$ n2 X# S' t  a: w3 m6 f+ U7 pif(mdt.hour == 16)
6 D$ g. M0 _% ^0 Z5 Uclose_positions = (mdt.min >= 30);
  _& c2 c% F# v" c5 ?}
1 e: P0 {; f1 M: L}% I3 J6 r* k1 A' k% g+ |& c
return ret;: ]9 Z4 K' j( |- B
}
- W6 G# e- f: s8 V' u& C//---
/ e/ m. v  X  P  h) wbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])3 m7 [. }# j+ w" m1 j5 O
{
8 R' @+ e+ ^  m1 U$ h" zif(PositionsTotal()) // Is there a position?
1 w6 E: M5 q! W1 j' ]* B/ \{- @$ o, S6 l5 ^/ r
double offset[1] = { 0 };7 o  M  C; z, M
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?, N  F' X3 c8 R# p* C; ^2 J' M
&& PositionSelect(_Symbol))  // Select the existing position!
4 O  y. ]$ A% z# W! h% n' Q( T{
# T! ^: i, q& S$ W: [ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
9 D( M! o3 W/ t# u7 ], Edouble SL = PositionGetDouble(POSITION_SL);
$ f7 m; U/ X: |double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));" g9 z/ f$ [+ a
if(tipo == POSITION_TYPE_BUY)
2 L1 _& _/ {' t% R{& @1 G/ m; Y9 S2 C
if (cotacoes[1].high > cotacoes[0].high)( E- @" G- a. |+ U4 p) L# z
{
7 P  G$ b& u, k1 `, N6 n8 j( X. odouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];- j  [) I+ x. R! j* Z: c# Z: l
info.NormalizePrice(sl);
( E2 f  k5 d) J- S0 Aif (sl > SL)2 G# h  K0 M" ?: H& Y8 s
{
3 L/ w  o, I, `$ x+ ?$ B9 t( onegocios.PositionModify(_Symbol, sl, TP);2 N5 [1 t; |8 w. ~5 M& U9 S' [; L( ]
}  A1 Z6 i0 O/ I* I1 S/ `
}6 Q" k; r: L1 @
}
/ L& ^5 o0 d; F; n" @$ \else // tipo == POSITION_TYPE_SELL* s/ I$ F+ B" ^2 K
{8 [8 u; @0 ~! R
if (cotacoes[1].low < cotacoes[0].low)
8 Y: @0 J/ Z9 y/ o8 _& J% _' G{) j- l9 `6 x1 }4 T2 H
return true;
( [8 ?- w9 c% X0 q$ ]- Q( J8 v}: b: K, N7 B! ~; }
// there was no position
3 H' P+ x7 W5 o2 i! Zreturn false;! ]: ~' B7 g- U/ F" W5 y' t* }$ j
}" y, Q% G% v9 ^& W% R
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
% Q$ H) g  C1 K9 c/ R到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-20 22:07 , Processed in 0.468257 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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