私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA, ~2 j( S, U4 J! F7 \
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
6 G- I& n. W  ~7 T' m  ]为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
; g+ E: A1 }( A5 s以下是制定这些规则的代码。3 p2 D! F! t9 [( C) j/ _4 a
//--- Indicator ATR(1) with EMA(8) used for the stop level...3 i) L! G8 a! r
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);/ u/ q! L( S: _4 X4 W+ Q
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
; X) ]! R$ g+ Y/ E& U//--- Define a variable that indicates that we have a deal...
3 f3 E/ R: l. F  wbool tem_tick = false;
5 N8 X4 F" d# q' m//--- An auxiliary variable for opening a position% T" f. P" S1 e: P% N
#include<Trade/Trade.mqh>
# c9 ~2 N$ i# q( J#include<Trade/SymbolInfo.mqh>
, r' `9 c% x5 ~1 A; vCTrade negocios;
4 S" I6 F, T' j' x3 x% eCSymbolInfo info;
; ^5 D' l6 W/ v; Z4 H//--- Define in OnInit() the use of the timer every second
/ T% x( _$ n" D4 _) O//--- and start CTrade4 n" u& m% m! L6 j  q" l
int OnInit()" }* i$ W3 U# k5 B
{
) T7 d; F, q* a3 Q//--- Set the fill type to keep a pending order" _1 ?4 f3 l# n$ \* B
//--- until it is fully filled
4 v1 K) f  z# f; G0 f+ ynegocios.SetTypeFilling(ORDER_FILLING_RETURN);
: C, I- P* u& n/ S7 v8 o6 W//--- Leave the fixed deviation at it is not used on B3 exchange
. v% q0 s$ }5 Z0 S. `0 dnegocios.SetDeviationInPoints(5);
: G/ ^& K) O: ?( k  a7 D//--- Define the symbol in CSymbolInfo...
0 t, b0 X: v3 `( @: x6 a% S$ ]info.Name(_Symbol);
- B0 O1 o6 q. ]! v//--- Set the timer...0 V* b; V$ {) u# K- `  \7 y! {
EventSetTimer(1);
. j' X  W6 |; n  {) f4 K//--- Set the base of the random number to have equal tests...8 s& S% v& K1 O* _
MathSrand(0xDEAD);: p+ M3 M3 G8 c7 ?3 |# ~4 ~2 Q
return(INIT_SUCCEEDED);
- J* R7 Z1 W( ]  u: G% c9 l}2 s0 r7 g3 `2 {4 z" Q8 D
//--- Since we set a timer, we need to destroy it in OnDeInit().0 Y7 {# I/ j6 A+ P0 U
void OnDeinit(const int reason)
! m; g( x* q5 \{2 D& N' j( w3 g( L3 V  P
EventKillTimer();0 D1 F4 k. O+ x& D
}" G" k8 b9 l; L/ u$ C9 g3 N2 S
//--- The OnTick function only informs us that we have a new deal
  m$ X/ O; [, `void OnTick()
, ~0 }) l% G) ^" c% V{
; F% u1 M& M8 U' _/ Q+ W$ ~! D% d$ Xtem_tick = true;
2 D% \& I) @+ g$ `}( R/ d, D" z$ N5 p
//+------------------------------------------------------------------+
, e* d, Z4 B4 s% p- V//| Expert Advisor main function                                     |( X/ ~1 G1 l; U
//+------------------------------------------------------------------+
, `6 z; p0 X( a- Lvoid OnTimer()& A: T. ?3 y4 x3 U# d' t) n' i
{+ T& M  |) M3 |5 Z* M1 R* W6 s
MqlRates cotacao[];
% A3 o, C3 L1 ]4 \/ Dreturn ;
6 O3 P/ o8 h1 Z/ c6 U  W* t; X4 zif (negocios_autorizados == false) // are we outside the trading window?
: ^1 k0 ]* x6 hreturn ;
+ Q: x( @6 Q  h3 ?" E. z+ _//--- We are in the trading window, try to open a new position!
- \; ^9 V5 v$ t, y& Z3 v2 P# Qint sorteio = MathRand();' v# {$ E4 T. B1 `
//--- Entry rule 1.11 x0 m  n; Z# H  r  K, l8 F
if(sorteio == 0 || sorteio == 32767)
, c+ L5 @- ^  ~# K  @7 v5 hreturn ;/ m6 ]8 ?' \* b# g, w, V' i5 g7 ?
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
  H9 T9 s& H) Z{
2 h) L; b3 v( d  i9 |3 \negocios.Buy(info.LotsMin(), _Symbol);- `$ U3 t% U5 m7 A. i0 Y
}
  {/ d7 X. s7 ^8 _+ \else // Draw rule 1.3 -- odd number - Sell) z# w8 o  V; E3 n+ ], B
{4 {; I3 W& w8 \, W
negocios.Sell(info.LotsMin(), _Symbol);( h. X& C) i( h! R: ~
}
; G1 I4 [0 M: ^}6 h2 }! z6 ~# ~3 f! M
//--- Check if we have a new candlestick...5 s" d9 N7 J& U$ @# v' B! v( g: x8 @
bool tem_vela_nova(const MqlRates &rate)
& H  C% C5 b7 H4 @6 F* c" y: i{0 l. I* K' |+ t% N+ @' s6 l# e
{2 {/ l: T  U  }- y/ L
ret = true;4 U4 y3 A8 R$ k* [3 o6 V% h3 B2 c
close_positions = false;, O* E3 ~6 D! m( `/ N
}6 |+ ]/ t) y3 H
else
3 }1 {2 U' E1 @( P4 M{  ~; K! c& g- w* i1 f: c1 h
if(mdt.hour == 16). W; Y* Q. M& v
close_positions = (mdt.min >= 30);; C( q- E9 M5 U+ t0 h$ a- o4 b8 C# S
}# S" w  S* y! b8 E( [" _. ~
}
: W# f( \: ^  I& greturn ret;
& g" a3 X6 _% b% l}- b$ \/ R3 n: i" e1 K* C$ j
//---: `. T1 |- o/ U0 M: G# B
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])9 S3 v  r8 r) q: X4 Z
{
" d) V2 ]1 \. U( P  ^* ^0 O( }2 zif(PositionsTotal()) // Is there a position?
  E& h2 J0 f, _* @6 X, [6 G) X{9 I  r+ e8 L1 v' E/ d' J
double offset[1] = { 0 };
8 [1 I: O, K/ S  e! Y: Lif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
! x( x5 o4 Z; q" @$ h9 \, D6 v- @! \&& PositionSelect(_Symbol))  // Select the existing position!9 v) d3 Q, X: F. }( P" o2 v: {' _
{
+ V* s% }8 ]  {  ?& uENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);0 T( s1 n/ P- M3 P( R
double SL = PositionGetDouble(POSITION_SL);
' s2 F( C2 m8 {7 W/ z" R1 o: k# Sdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
' K6 v; c0 K  P! i+ T5 vif(tipo == POSITION_TYPE_BUY)
' E- _0 g/ o: e) r& Q; Y: T7 P; D- U{) [& Z: O- H* S9 ?2 d
if (cotacoes[1].high > cotacoes[0].high)) C' G8 q* v* {. E
{1 x  U' p9 C; F0 ]4 U; O
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
. N: H3 n7 S3 `* linfo.NormalizePrice(sl);* K* b. p6 Z8 F& ~# Q. b% R* a; J, L
if (sl > SL)
$ y$ `3 _" {" L- B! [# X{% @. x+ \' o5 F1 A' q1 z9 d- K: ?
negocios.PositionModify(_Symbol, sl, TP);# V# E+ m) o$ k
}+ g; M. A( b$ c, W! O1 A: P5 L
}
3 Z7 k# k) V+ w6 w. |9 I9 ~}$ ?) V2 f5 Y- f
else // tipo == POSITION_TYPE_SELL
4 l# X: M' \+ q5 a' d. k& q{; @/ ?' E4 _: J5 [# S! ~. d
if (cotacoes[1].low < cotacoes[0].low)
  L/ Z5 w; N( r- w{$ U, e; u! G/ L/ i0 G" w4 F
return true;) f# ~9 u  C8 ^1 ]) h1 F( n& J/ e
}
+ o  |; _7 b& P: v# A// there was no position
7 g' f8 i$ |/ Yreturn false;
2 \% t7 C7 m/ e  p% N}; v! b2 u! X" l- y
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
4 E1 ~% [8 S" X, R: K( A9 `0 S到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-19 03:25 , Processed in 0.558751 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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