私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA% \+ h7 \$ u! l6 C# A: \
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。# c9 J4 k0 ]% D+ ?; K+ C7 b" a# a
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
1 {! R1 ?* y3 C0 P0 O' x' z9 Q- e  z以下是制定这些规则的代码。
, c5 p  s% x1 r( d* F( T5 F1 ?//--- Indicator ATR(1) with EMA(8) used for the stop level...* H- \7 `# J; {, y6 J+ I; p1 _
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
+ q8 g8 v7 f5 `  s. zint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);$ @& V4 k  b: c  {0 G
//--- Define a variable that indicates that we have a deal...: F+ X! S& y; I2 T
bool tem_tick = false;
8 [! C. W+ d5 J5 e& \$ }  v//--- An auxiliary variable for opening a position; G1 b" k/ [4 _
#include<Trade/Trade.mqh>0 i" V" K; [+ B, \
#include<Trade/SymbolInfo.mqh>. \+ K  s0 f0 j) T& P
CTrade negocios;$ R( Z4 c% K, ~! Y' R5 Z
CSymbolInfo info;6 c' W2 e: w1 G2 e
//--- Define in OnInit() the use of the timer every second% z5 x* ]8 V/ _% W6 L* I
//--- and start CTrade9 o4 o5 e' q( K8 L# b/ N
int OnInit()
* u4 W4 X. f- Z1 Z! [' n{
% q. y& X0 \. }//--- Set the fill type to keep a pending order
+ ^: N+ F# r& S//--- until it is fully filled
0 s3 t  r8 r0 m1 g7 s3 `negocios.SetTypeFilling(ORDER_FILLING_RETURN);
% ]  S! ]9 M( k' d7 K+ Y//--- Leave the fixed deviation at it is not used on B3 exchange. M" g# l  D: M% h
negocios.SetDeviationInPoints(5);% A6 k$ [: C+ {3 m
//--- Define the symbol in CSymbolInfo...
+ V. y6 |  x4 ?! }# o/ sinfo.Name(_Symbol);
0 b% U7 W6 N3 ^, z: R4 ?//--- Set the timer...1 a  }  l2 D( E$ m! ?
EventSetTimer(1);
- y) }. r; u' o/ k8 ^0 }) C//--- Set the base of the random number to have equal tests...
7 P8 R2 D. k0 O, XMathSrand(0xDEAD);& B  j2 e5 I7 q' W) I7 r
return(INIT_SUCCEEDED);, I3 H: |! \$ Q( ^5 l$ {1 q% E
}/ p5 |( r5 s) [
//--- Since we set a timer, we need to destroy it in OnDeInit().
8 m% o) o* Z+ L0 n% Ivoid OnDeinit(const int reason)
' n$ U" X: h, U* B{$ S0 k# w& Z$ H9 ]6 b
EventKillTimer();
7 h- s3 C/ e* Z- B7 l( Z}# O- |6 S" L' p( J* G: K
//--- The OnTick function only informs us that we have a new deal
( s) ^9 e+ I9 b& h' Q1 wvoid OnTick()
2 F. h6 I/ `# V  T; j! Z{
& }: E6 [- i8 E; ctem_tick = true;
. X3 j0 F  ]: H9 M4 d; P2 O( e2 H+ S}* r2 M" ^( j1 I5 x* [* E8 |
//+------------------------------------------------------------------+$ u! |7 y$ a  X! F
//| Expert Advisor main function                                     |
. I- _. d) k4 x: [& L//+------------------------------------------------------------------+
, C( {3 F: ^/ x' q) s" X- q  lvoid OnTimer()
/ e" A1 F, y# [  g: K* {: }{/ w. P, C& ~' B6 g+ w" J' N! K' y8 M: \
MqlRates cotacao[];
8 h) r* n; T0 C; i! jreturn ;, Y! o9 d" @8 X4 H
if (negocios_autorizados == false) // are we outside the trading window?
* U8 e0 l! ?0 l0 Y, {8 D, n. nreturn ;4 }" l% L9 {5 H! b& N& c
//--- We are in the trading window, try to open a new position!
) H0 y6 ?/ U! K) Kint sorteio = MathRand();9 L4 `2 u: t+ n$ @
//--- Entry rule 1.11 V4 i/ s4 G% `- P* m
if(sorteio == 0 || sorteio == 32767)) {! f- I9 r: K8 d3 d  s
return ;
9 Z6 N' {( J( ]$ N* s) tif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
) M  \" N; a" O  H# }$ j% E{
5 r+ e- Q' a" }$ ^negocios.Buy(info.LotsMin(), _Symbol);- X8 ]2 v4 g5 p( |1 K; a* f
}. Y+ N2 d+ F2 {" @: ]; M8 C( E
else // Draw rule 1.3 -- odd number - Sell
1 j/ ^# M/ I) U{9 w$ g- G' C/ {' Q+ I
negocios.Sell(info.LotsMin(), _Symbol);: h0 x" L0 u6 R8 y; m
}! i& N  R6 J) h4 z. V
}
/ D$ Q) S, h! {; V9 h$ R  Z//--- Check if we have a new candlestick...# L8 P  x' T% A) }, O
bool tem_vela_nova(const MqlRates &rate)/ ^/ T7 G2 u) a
{
; y; }; a: {# p{
9 G$ |* J$ B& |( Pret = true;; ~5 X4 K! ?' k' r  s/ ?* {8 P
close_positions = false;  {, I! t" K! Y" ^
}- A' j, g# P. D5 m/ t; {: [
else) n& g& p3 [4 g" y! ^: D
{
0 f3 a  S9 z1 [0 P3 Z0 V4 uif(mdt.hour == 16)- I6 b" J) h2 T
close_positions = (mdt.min >= 30);
& M9 y: y% f* J, ^( V: j}
8 j5 P: h* g) E; `  ~}6 A. e9 U: D- T3 a
return ret;
1 U$ N2 L% ?4 v; a/ B}
. u, Y+ {* e  P4 g% D; ]$ n//---
. ]2 P3 ^) s) C9 \( B+ dbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])! H) t- P2 |/ j7 a1 q3 L2 Z
{
% R1 p, o& n# y4 uif(PositionsTotal()) // Is there a position?
1 P( [% A% L- {9 g{
! i" T4 {* r' o, T8 sdouble offset[1] = { 0 };
( k3 L  ]9 l' X% H1 f: Dif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
# P; _) M; I. q# ^# _3 [3 k0 f! ^&& PositionSelect(_Symbol))  // Select the existing position!# Z, P6 u! l7 ?5 V  f/ `" C7 o
{3 i& O  t  j9 D+ _5 U/ V( }- k% S
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);9 y# e' f/ l+ \* Q2 h
double SL = PositionGetDouble(POSITION_SL);+ e# g! E1 c) ^* ?" a. X8 D
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));, k5 @' V) O0 \4 ?; x
if(tipo == POSITION_TYPE_BUY)
1 `2 |1 _3 `+ D* y! t+ ?' |{# [  @% x+ g" v% E" n
if (cotacoes[1].high > cotacoes[0].high)
9 c9 D( B2 }) H! Z( ^* M{: {5 K8 a. b: \& \" d. l
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];  ?' m- G4 Y- P& D
info.NormalizePrice(sl);
$ n9 x: Y! Y. l6 N; q- k1 E% S  ~$ dif (sl > SL)8 P1 m( V, S' a
{) i7 `# j" d- p+ w/ _
negocios.PositionModify(_Symbol, sl, TP);( c. q$ p/ U$ o% m; x
}8 p: `) G8 b4 i. v. |
}
* c9 {. _, S3 U4 [}, z; g: ^7 p. W8 E& o# J
else // tipo == POSITION_TYPE_SELL
# ]' {: C! _1 y, I# n{4 D3 A" }+ r$ V* Y2 d3 H6 D
if (cotacoes[1].low < cotacoes[0].low)
/ u9 S, G' i/ J, X{
; R+ k  D2 A7 I: C, U0 rreturn true;
" l" c/ M) R( U' K7 J}
# t0 f, q7 R5 s' B& m" v// there was no position6 {3 A" g! m" @% P
return false;
  {! _+ P' O0 Z, H  \}
8 {' W! z6 o( q& e+ d我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。3 i) T$ F  z/ i  {% L
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-4 12:00 , Processed in 0.392438 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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