私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA* {* @) s+ B0 K5 R/ h* x4 \
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
4 g4 {+ {/ ~2 ^$ M* O- M2 j1 n为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。( w  v; }; g' R; f; |5 |& k
以下是制定这些规则的代码。6 H/ a7 j7 q8 |8 T" e! U4 B
//--- Indicator ATR(1) with EMA(8) used for the stop level...
3 F$ V' Z1 r' u, S- c. N& Hint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);+ H& ^0 J. \# @! h
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
& f! A: T8 i/ k- C$ h//--- Define a variable that indicates that we have a deal...
  x+ Q+ s( P' S" b5 P% \9 \1 Ybool tem_tick = false;
# b! D7 @- O8 r4 x//--- An auxiliary variable for opening a position4 g2 w! @1 Q2 u( q$ N. j$ G$ i
#include<Trade/Trade.mqh>  ?7 V4 q" F% [7 z1 _
#include<Trade/SymbolInfo.mqh>
. D$ X0 B4 d7 r" K$ a- K8 wCTrade negocios;$ V* @2 ^" B2 E; ?& v& j& \
CSymbolInfo info;
& U5 N& @3 Y1 l  F//--- Define in OnInit() the use of the timer every second( A* ]" J8 X: R/ e' I  g* D2 ]% e! D
//--- and start CTrade
# H5 C0 l& z! U2 E0 U( g4 S* Xint OnInit()
  P+ k: L7 b0 O- @{- {5 `# |5 T, P2 Z- e, s
//--- Set the fill type to keep a pending order- ^/ A3 x- z+ E# l4 {& Q
//--- until it is fully filled+ i5 h) u, P3 i& t8 y
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
4 P5 w. V! k6 c, G4 r//--- Leave the fixed deviation at it is not used on B3 exchange
& U, ^# a+ {" E, w9 B  T) k% pnegocios.SetDeviationInPoints(5);. K# L1 i3 m+ ]& G& {6 O
//--- Define the symbol in CSymbolInfo...$ C' l" i9 w9 L$ c
info.Name(_Symbol);
2 H  U' i* ^3 n! O& q; r. J( B//--- Set the timer...; ]9 t" y1 y& y6 r
EventSetTimer(1);$ S& b3 g7 t3 F9 Z, G% z! u
//--- Set the base of the random number to have equal tests...- i  D0 ~; Q& B
MathSrand(0xDEAD);% A: k, H. g: }& `* b# w
return(INIT_SUCCEEDED);
. t& [0 W8 J) o# A# R$ F0 R}
2 ~9 ?) M# h+ M//--- Since we set a timer, we need to destroy it in OnDeInit().9 A  m( V  z$ L' E/ w
void OnDeinit(const int reason)
: a# C+ G% f! }/ V{. j" ?* B) z$ c  z2 D4 X2 G
EventKillTimer();
. v4 i- E9 S9 B0 I! {7 q}
; n9 _& K( }0 b( R$ ~& f//--- The OnTick function only informs us that we have a new deal
1 \3 W& t) p0 H7 o) Ivoid OnTick()
( Q* [) Y! x/ c# T8 z- N1 r! X2 `{
) J% Q/ Y# c5 S; d: E9 Ptem_tick = true;
; ~% C. n6 Q' y% x  U# I$ H}
( J6 z) T+ B2 x" A( ^//+------------------------------------------------------------------+2 F4 g( s' c1 O9 ]
//| Expert Advisor main function                                     |
0 \4 q' ]2 t' N//+------------------------------------------------------------------+0 Q/ ]/ O, Y. n6 F* G
void OnTimer()
6 z8 d3 N* z' ?  a0 F5 f{/ F  p& d+ m2 q
MqlRates cotacao[];, z& m* _+ S! p2 r3 O; i6 z
return ;
  s6 |' r% s% F# Z, Eif (negocios_autorizados == false) // are we outside the trading window?3 a' O5 G  F7 }1 T$ L1 \
return ;
" t. ~& |& z  Z! F/ {+ T; j0 X//--- We are in the trading window, try to open a new position!3 R) g$ j4 Y$ W; f
int sorteio = MathRand();' P+ U/ x1 b; i3 |1 K
//--- Entry rule 1.1
! Y" Y: {" `4 `- K, jif(sorteio == 0 || sorteio == 32767)- |/ m3 {1 w' m& C
return ;% d, v' |: ^/ g5 U  s
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
7 Y' O  Y: X; T* g! w. d{
) m. R5 T. t* H3 Onegocios.Buy(info.LotsMin(), _Symbol);9 q9 V! {9 y- |; E7 E
}
% }4 j3 q, J: v9 V  velse // Draw rule 1.3 -- odd number - Sell
( _) Y0 Z9 J" E/ V{  b: H6 t, f. z6 O6 d0 O1 o' B+ S" o0 g
negocios.Sell(info.LotsMin(), _Symbol);2 r$ Z$ j; b: M% D! x. o- C
}1 r  ]) B! ?$ n  ^! ]
}; q+ Y1 M9 M! K' v4 t$ e+ d
//--- Check if we have a new candlestick...* b" P1 U2 X0 U& B2 X3 D
bool tem_vela_nova(const MqlRates &rate); B  X7 [9 ?8 Y* R- j2 `8 A/ }
{
) V% y  N4 l5 p' X2 }4 g{- M% o2 Q% n1 j; i5 }
ret = true;- S( Y& y' g/ m: t' w$ y( O( I
close_positions = false;. H; Y6 z* H# H3 E( v5 |0 B8 j
}
( ^) \" B5 j/ |( i" J- Qelse! D, v4 X& g6 O6 {
{9 r' }2 q9 `/ m" x+ j
if(mdt.hour == 16)8 Y5 j6 G& x( f, X' k2 ]& S% u
close_positions = (mdt.min >= 30);7 n9 Q4 K' I, @6 `  T
}
% b& R9 S& f4 H- ~}9 H6 ~, n3 C/ c. }& a0 m; o% q
return ret;" Y$ T& G2 D& Y, [0 e/ c$ ^
}
( Z! f" F6 N/ o1 D$ t  g$ o, o//---
, s0 H! [' l% r: ]1 P, e* Obool arruma_stop_em_posicoes(const MqlRates &cotacoes[])+ P% e! N" j9 ~/ J
{
0 i9 k! ~+ D' n  R, U6 rif(PositionsTotal()) // Is there a position?3 b, A3 T5 H" F* }
{8 ~" ^4 b! |2 _# k7 M
double offset[1] = { 0 };9 e; O4 i5 r* [  Z- a
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
1 S3 ]  \5 T  @" L% v; l: Z&& PositionSelect(_Symbol))  // Select the existing position!
" A. x- I2 e9 T% X{
/ z: d6 i6 O. i% nENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);3 j3 W/ R9 j; X2 h; J/ e" ]5 c
double SL = PositionGetDouble(POSITION_SL);
# e. Z& g' d2 k: A: j! b: G. b' a' wdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));& e' {  Q9 C  d6 t" U2 J
if(tipo == POSITION_TYPE_BUY)! Q: r, Z0 x4 c: q6 n8 y! H; e5 _
{& m8 Q& n2 Q3 I
if (cotacoes[1].high > cotacoes[0].high); L: f, T" E: O, J+ k0 m
{$ J: b4 g/ a$ K4 C
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];& k8 _4 Z+ @3 x
info.NormalizePrice(sl);
/ d" R6 A  Z6 _) n( H& qif (sl > SL)
  Z1 U/ l1 |% f{( J' S4 U% K9 O/ j8 h0 m( X
negocios.PositionModify(_Symbol, sl, TP);
/ l* I! Y0 k3 i! P}
+ }/ A/ A& E6 O}  d- ?& B; g/ d  D
}# Y) T6 H/ x5 g. V% b0 p
else // tipo == POSITION_TYPE_SELL) e% B  R6 o' I0 Y, n1 S
{% I7 s! T" M- i, q. K
if (cotacoes[1].low < cotacoes[0].low)
/ M6 L$ p1 M1 S) G$ w/ \6 l{, W$ @( m, d" f3 ]& [3 A
return true;
; N3 ^; x7 S; c}# {5 w" [) N* O  B9 W% h
// there was no position/ I9 H3 z- q- [5 W, o" ?2 n- {
return false;0 \7 C6 V1 d+ V( A# ^
}8 |, {' N1 l+ ]% h
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
6 o9 `' e" n) J  Y7 }) N到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|Archiver| ( 桂ICP备12001440号-3 )|网站地图

GMT+8, 2025-2-17 17:45 , Processed in 0.682096 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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