私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
5 P4 z  x# @# X0 y+ J" V在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。5 ^3 g2 K) L4 u/ q, a5 z
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
0 w2 L' g" b2 c/ n9 E: O$ h* x以下是制定这些规则的代码。1 }( i0 _6 W9 W5 c) b5 r
//--- Indicator ATR(1) with EMA(8) used for the stop level...4 c2 H3 t; N& u
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
! F& n0 X/ S- n1 y/ `int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
2 Z9 _5 x6 b) k//--- Define a variable that indicates that we have a deal...9 P9 _& w' m8 F$ u& W- n8 h+ A
bool tem_tick = false;
7 ]' C1 r( Q9 o& p. D//--- An auxiliary variable for opening a position$ @- ^6 M- h! a+ K1 @' Y
#include<Trade/Trade.mqh>
1 h$ G# ?% ]' E% J6 S' W4 y#include<Trade/SymbolInfo.mqh>/ C% I' g( E9 G/ n: D8 ^
CTrade negocios;
; k& H, [4 L- TCSymbolInfo info;# ]# E- s4 @7 `+ I# F6 W
//--- Define in OnInit() the use of the timer every second$ u1 R) N" d  @
//--- and start CTrade% n+ ~9 r' m% X0 k  j  Z
int OnInit()+ p, \7 r4 s3 f& v
{& m# s% M  M$ C' s5 Z# w) K; H
//--- Set the fill type to keep a pending order# b* B3 |: b  {
//--- until it is fully filled2 ]- y5 F* P, n; \. k
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
* m/ x% v: \" R- ?9 T4 G8 m: V//--- Leave the fixed deviation at it is not used on B3 exchange7 d' |$ q- M$ b3 Z( X; u" a: f* p$ D
negocios.SetDeviationInPoints(5);$ w6 u1 d- p' ~, U4 X6 S
//--- Define the symbol in CSymbolInfo...2 J4 O, Z! w8 V6 v( \0 M
info.Name(_Symbol);
/ D( u( s5 P4 G//--- Set the timer...
/ L4 A6 V/ s8 t  eEventSetTimer(1);
$ \8 U2 j1 s9 z/ F//--- Set the base of the random number to have equal tests...
  w1 x4 k* A. t$ t% {MathSrand(0xDEAD);3 J3 ~# F. \, u/ W" g/ l
return(INIT_SUCCEEDED);
% m# d, w" X6 p0 T3 F}
, x! Y, e& t0 f, q  d2 z/ Y% Z  N//--- Since we set a timer, we need to destroy it in OnDeInit().
4 z' ?+ [7 T1 u4 Y4 o& Mvoid OnDeinit(const int reason)
' Q  H: i  c9 M1 J. f, s6 K. J{& E. j* G2 b1 D/ z
EventKillTimer();
' n3 r1 X0 ]1 i% M0 E}
! L0 J& u; d) q8 l4 [//--- The OnTick function only informs us that we have a new deal! ?( L7 N$ Z: l7 ]
void OnTick(); v9 O! S/ m0 u9 U2 f, X
{
/ k# P/ J2 S4 btem_tick = true;: U8 a! r0 |+ Q! S5 n
}0 T# P: m2 G: `
//+------------------------------------------------------------------+2 H$ `# e- X* B% a, H
//| Expert Advisor main function                                     |
' x: W! M- S/ R+ D& X+ o//+------------------------------------------------------------------+
8 X. m$ ^- t$ i9 Hvoid OnTimer()+ N/ @) l$ t" G& P8 s
{
; d8 b7 A/ t1 g0 P' Q0 bMqlRates cotacao[];/ X; @0 _" u' r0 A: b- J
return ;9 {9 q2 R% J5 j; ~% ]2 f: y
if (negocios_autorizados == false) // are we outside the trading window?
* G+ k8 ?% k% |4 z: {- L8 [2 n+ Sreturn ;
# ^5 }0 e# k9 w2 c# x" ^//--- We are in the trading window, try to open a new position!( m" {& o' T- W- y$ O7 S6 O
int sorteio = MathRand();
5 P! t9 x- E" r# Z5 }. j8 }( J$ P4 q//--- Entry rule 1.12 i: j1 i/ D! |5 K$ q7 ^' L+ A
if(sorteio == 0 || sorteio == 32767)8 P5 d$ A) J% P' |! ~6 l" A
return ;( A! t6 _* j- I: R( N& W# K/ i0 s
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
* B5 r3 J' r$ H! Y) c5 J. [{
* ?! }$ a% ]% F/ }6 Dnegocios.Buy(info.LotsMin(), _Symbol);7 P. U4 b6 @1 F" X8 X3 C
}. M3 B" l/ n3 m9 P" K& }  X
else // Draw rule 1.3 -- odd number - Sell! z1 D' |  r& ~) X2 d
{
7 H. D. s- H% fnegocios.Sell(info.LotsMin(), _Symbol);
) q4 f5 r% z  Q6 K' v}
5 J# `! y. a+ P- u9 R}
/ r- B& j/ {/ Y. z6 X//--- Check if we have a new candlestick...
1 V8 L# Z- F7 U7 p6 Q! H' q  tbool tem_vela_nova(const MqlRates &rate)2 @/ ?/ Q7 }4 L% z  s, u
{
1 s" |# O4 R' S+ i{
/ f$ f! d* u+ E. @* Mret = true;6 j. y1 D$ ?, F9 |5 n! F$ X
close_positions = false;
7 Y+ S. I4 h; w+ [; `}  z0 w. a* p( @- R% s# g7 {
else
  _2 j  `$ h/ |& F% f  \{
$ I2 L6 E  R8 c* i5 bif(mdt.hour == 16)' m7 [; v  Y4 k- x
close_positions = (mdt.min >= 30);; w/ F8 }- h2 G) ^' |/ C
}7 h, W2 S! k& w
}( B. Y, |8 U; }. x2 G/ S. _7 K! I4 t
return ret;
2 _7 W8 E3 V6 a; ^; [8 T}
. @5 X+ [5 P. K8 S4 N$ K2 q. V//---. s" a( P  a& I
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])2 g, h8 Z0 N3 a6 G4 i5 ~' D
{
+ `  D& x  ~  V9 T1 l- M% Cif(PositionsTotal()) // Is there a position?
2 N& ^* i& ^2 O. v{1 G9 u* ?% q  {' V$ x. e
double offset[1] = { 0 };
4 k9 p  W& ~/ m- H' S' r/ Aif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
, m- g- N/ L4 i( h; {3 Y. x&& PositionSelect(_Symbol))  // Select the existing position!+ e7 A6 Z3 z2 I. S
{" n0 U6 ]! B6 F
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);4 t* V% R" T" ~& O
double SL = PositionGetDouble(POSITION_SL);6 q. E% Z' b- a# o
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
7 {. O4 C' `# ]: W8 Q: J  M! Nif(tipo == POSITION_TYPE_BUY)% c+ g, i, k0 M3 I; L
{7 K/ h' }) L' |' d) F' k
if (cotacoes[1].high > cotacoes[0].high)2 {- }: |/ ^+ i2 ^+ u3 b. [' A
{
( {# ^. t2 x5 l2 h2 H4 [  @double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];- E! f8 z' [/ {6 \, v; A
info.NormalizePrice(sl);
" a: d9 R% U9 h( W8 _if (sl > SL)/ F& R3 q: d" C  b  I
{" U# T3 i! G7 h% B0 j9 [! {/ N
negocios.PositionModify(_Symbol, sl, TP);
. u2 j5 k* W& `0 F) ]$ ?}
% L7 e& g7 @7 e2 H- }  X1 A( c( x}- D: C# b% j% l/ n8 a. Y% R
}
+ m& y6 W* {. B2 }: y7 V8 }else // tipo == POSITION_TYPE_SELL2 B( L: H; ^4 D
{( N3 f# i1 u& L0 S5 R
if (cotacoes[1].low < cotacoes[0].low)' ?2 H: N$ W$ L& A
{/ ?- F& G3 [& n  Y* L, t
return true;) I5 k9 u  Q8 y
}
/ y" k' P" v4 r, y9 f6 e" i) f" m- K// there was no position$ D& P9 t% q. \% T9 k/ [
return false;
7 k! d( a- F) [* D7 R}2 S& B3 d* s0 B. T  n' e% Q; S  ~
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。& T) y$ `  }4 P( O' d3 ~
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-27 10:47 , Processed in 2.016060 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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