私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
. X: M! S! v  [1 H! ~/ Q5 B1 k3 a! M在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
$ Y( }8 r) e9 V' `( [为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
) M9 k, O) D3 V0 }9 L) x9 |以下是制定这些规则的代码。
6 n' A" z: @6 v+ X8 U//--- Indicator ATR(1) with EMA(8) used for the stop level...- R6 K6 O, k/ x/ N4 V
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
, f; P1 G* C. Sint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);9 \' v: b& ]; G+ ^) n
//--- Define a variable that indicates that we have a deal...6 c3 _/ D1 t9 O7 G+ {8 b2 X
bool tem_tick = false;
  z6 Q3 V2 Q5 g* Z/ z! C//--- An auxiliary variable for opening a position) o! u3 c/ K: D* ^0 J9 B
#include<Trade/Trade.mqh>
0 j: H1 r. r( [( H" k+ C% |#include<Trade/SymbolInfo.mqh>
4 z2 o  O8 ]/ }: D4 m) i8 LCTrade negocios;
% d- J+ e) u! s2 [5 L% l0 JCSymbolInfo info;
' r( h" _! ^4 t3 ]+ _# e/ K//--- Define in OnInit() the use of the timer every second
4 [9 R" v% V" n3 ?//--- and start CTrade1 y9 g9 u' o# Q4 ?
int OnInit()
3 y1 z9 `, B% {; Y{+ e/ _( h- Q7 g/ b% ~" m
//--- Set the fill type to keep a pending order# y$ g1 J- g& x
//--- until it is fully filled- x: s& v: g- n6 @0 U: t
negocios.SetTypeFilling(ORDER_FILLING_RETURN);5 A- r8 h# g; N# ?. L
//--- Leave the fixed deviation at it is not used on B3 exchange5 [1 Q/ U" @( \. S" X) c
negocios.SetDeviationInPoints(5);- z6 X$ ]' {7 O5 G, h
//--- Define the symbol in CSymbolInfo...2 l( K3 c0 m- M5 t
info.Name(_Symbol);
4 @; N: i- h" P- m5 P//--- Set the timer...
" i3 W5 f! n+ w6 `EventSetTimer(1);
! W6 P& }9 `! R//--- Set the base of the random number to have equal tests...
8 \, V) f8 o0 L5 R+ R$ g5 VMathSrand(0xDEAD);; U( [- C& \2 @6 N9 K, s/ ]$ s
return(INIT_SUCCEEDED);
) W' G6 T# y* O! n/ h- N}
# ^6 F  D8 a0 T( G' Y//--- Since we set a timer, we need to destroy it in OnDeInit().
* g, J/ r8 G5 m' q# I1 Tvoid OnDeinit(const int reason)
  o# v' `5 D7 N$ b{( c6 q. |% {. {- y1 t
EventKillTimer();
/ W- o  g1 P" t}7 `' v+ I: T+ K( k6 {
//--- The OnTick function only informs us that we have a new deal9 t& f5 U, E" y! n
void OnTick()  `% B; R  P/ R0 e2 z; A- b, d
{
9 v, u# c, k! O( K3 `tem_tick = true;
$ H/ ~. E7 F3 g; ?}
8 g) x1 i& ]7 |3 d7 P1 n//+------------------------------------------------------------------+7 V: n: _6 o3 W! n8 I8 J# M5 I
//| Expert Advisor main function                                     |
8 f4 g& g' N8 k' ?; p//+------------------------------------------------------------------+) h/ L+ H8 p% Z* I  e* x. ~
void OnTimer()$ z% ~  I9 t& b. f* m- f! ]
{
# U6 f/ ~- y. ?0 {3 OMqlRates cotacao[];* s' A- Y# `' z
return ;0 X# |9 G+ n! S
if (negocios_autorizados == false) // are we outside the trading window?
; f! d, d* Z0 S, V7 ?0 L- M' yreturn ;# g1 U9 `" T6 m& h! H& \. q- i4 E
//--- We are in the trading window, try to open a new position!5 C  n! \) }0 c! C% O# J
int sorteio = MathRand();
$ A. R+ w1 V( d. o//--- Entry rule 1.1
- |% _1 q  o6 z$ |# hif(sorteio == 0 || sorteio == 32767): _  M$ j" K* U8 v) e2 ~
return ;! ^: n; o5 k1 f. I6 c
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
1 B+ Q" y: M$ H/ r: q7 A9 w. ?{
) R: w+ U1 G9 znegocios.Buy(info.LotsMin(), _Symbol);
/ A9 A" I# l4 d9 O9 X8 V2 @}
" e4 G" Z6 Z; pelse // Draw rule 1.3 -- odd number - Sell3 I- Z5 u7 U  [
{
: s5 B. Y! R8 C% n0 J, [' \negocios.Sell(info.LotsMin(), _Symbol);* b: |; F+ I% ]* G0 b9 J' v
}- `9 J( @- Y* W8 n2 v) z( Q+ q# v
}% R& s  j/ j7 d) q: ]$ h* X
//--- Check if we have a new candlestick...
6 N$ G1 J7 N* y& |) }bool tem_vela_nova(const MqlRates &rate)
/ G# g: }$ U, l& H# K1 w! ~{
0 d6 f1 }9 f' I) R% h{
4 E- Q- ^/ n; N, k/ o, eret = true;
7 T. `0 W( W0 m  kclose_positions = false;, `$ d. `* Z) `. f1 H
}4 G$ A) R7 h% Q! n
else" l- m: Y7 v4 l/ ~# }3 J) u  m9 P' c
{
2 H& J, t+ z9 L& J" bif(mdt.hour == 16)
8 D3 v% t+ x/ x. _" cclose_positions = (mdt.min >= 30);
; ^0 q* H- @! D% v  B" `* E}
1 @9 u$ V' a2 ]}
% e( B" q) i+ a- treturn ret;( v" ~7 D$ R. N" f" W
}
# h+ X7 |7 H# v4 G5 M6 f5 m//---
& T" _' ~, H) ^) q; c" m( M5 ^bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])7 S/ M" ~" `$ w; K
{" |5 H; O3 i/ g; [- q5 ?* g
if(PositionsTotal()) // Is there a position?1 q9 b' n+ J$ J3 ^5 g
{
- ^3 K# K* m1 Y5 L7 _  O9 rdouble offset[1] = { 0 };; ~7 ?4 y; _; Y% ^; x
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
5 E) j8 ?9 @- o( V" s4 ], t&& PositionSelect(_Symbol))  // Select the existing position!: z# L  ?* K. w0 K" `4 Y. e
{
( S9 r+ \+ d9 C  g! Q% F* GENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);; m$ L" w& t; V/ d) q
double SL = PositionGetDouble(POSITION_SL);
! Y) P; v$ v3 B+ m6 }) a2 Gdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
  c  n: d1 `( |if(tipo == POSITION_TYPE_BUY)2 J' q. s, Y9 K3 @- [. A2 d
{( i7 _: h) `! y* Q, h( z, l. z8 f* Q
if (cotacoes[1].high > cotacoes[0].high)# m- y) e; O. o" v" h0 v) t# Z( m; v
{
- e' J, C, }  j% P( |0 pdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];# s. D8 J5 k7 \% D6 T8 A. t; Z$ [, y
info.NormalizePrice(sl);
' E: U( Y* b' G8 E' [- o! Jif (sl > SL)3 k  j" h$ Z* Q! N$ w- O8 ^6 X+ P
{8 _$ ^1 T4 k4 s
negocios.PositionModify(_Symbol, sl, TP);$ W# l0 N2 s) k" O( ]
}
6 I  ]- k6 H- w( M3 U}# U; W9 x5 u* C6 ?
}
  D% K2 k7 d% welse // tipo == POSITION_TYPE_SELL6 [$ m2 @6 w0 }: I2 H: h
{' H# U1 m! G% P/ V; c$ k, r) n
if (cotacoes[1].low < cotacoes[0].low)
, \' n% E7 c4 _- t7 z! ?( W3 z{
9 _( w2 I# i2 ?return true;
; j3 ^) l- O$ B7 Y2 z3 \}
. q( b/ w4 I, @5 ~" g: {% r; A' q; t// there was no position
+ H3 D+ Q# G8 W( ]4 _) v) Lreturn false;
% ^' Y$ Q6 C" ]3 f}1 [0 }7 P3 o: [1 l
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
8 f# [% K. L, |到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-5 16:11 , Processed in 0.401802 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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