私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA! d6 b* ?/ m& Q  z
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。+ }  ?2 r: O0 ^5 y  n) u
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。2 m# [, k5 }$ M, `! e+ W. g# _
以下是制定这些规则的代码。
" i- a6 v1 N+ O* b: Z! s1 h! P0 w% O//--- Indicator ATR(1) with EMA(8) used for the stop level...5 ^- t+ ]* r, P  ?. e
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
0 Y! q) o  V1 {6 U6 ~3 ]int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);1 p/ Q* ]" o: j
//--- Define a variable that indicates that we have a deal...0 U" h, y9 l! B* d" u& y& M4 H) j! C6 N
bool tem_tick = false;
% P2 q- F( w+ ^/ D2 i; Z//--- An auxiliary variable for opening a position
( U! [. D3 F8 l( C$ P#include<Trade/Trade.mqh>
& K$ T0 P' V) n9 H. n1 q6 m#include<Trade/SymbolInfo.mqh># P" l% V6 ]) a3 p2 w
CTrade negocios;3 B+ [# ^7 S6 E4 M
CSymbolInfo info;7 p8 R7 K5 I2 h1 E- @. x
//--- Define in OnInit() the use of the timer every second
( q; o; I/ f; C& m! |//--- and start CTrade$ Q; X: f5 b5 O& r
int OnInit()$ i1 L' I' S: _* C
{/ t9 ?( [/ i4 d+ U) T" ^5 U
//--- Set the fill type to keep a pending order( z8 q7 T; {7 s
//--- until it is fully filled
0 f$ ?+ p$ c9 x) n" ~( Znegocios.SetTypeFilling(ORDER_FILLING_RETURN);7 U3 m5 W; W/ ]' a+ Z3 v
//--- Leave the fixed deviation at it is not used on B3 exchange
( M# ^- o) E. e  c( t. anegocios.SetDeviationInPoints(5);
2 o4 M- s* R! e: ?) p: d3 `//--- Define the symbol in CSymbolInfo...2 K7 P) u* u, K5 B9 A
info.Name(_Symbol);
8 b  i  C3 }. n: S5 s% R//--- Set the timer...+ T$ i* j/ r# B) {; b8 Y+ f
EventSetTimer(1);- q' d5 o. l; q# F3 \, |% R6 B
//--- Set the base of the random number to have equal tests...
3 ]! t; }8 N* t+ `0 VMathSrand(0xDEAD);1 a3 [* J# j1 w+ ]+ S  a1 {$ f% \
return(INIT_SUCCEEDED);
, O/ @2 D; x  p& M}
8 R' U& E5 F% l9 W, F//--- Since we set a timer, we need to destroy it in OnDeInit().
  L) o1 T3 o9 q+ x7 L/ d& l9 |5 [void OnDeinit(const int reason)% X/ |" j7 K1 ]# \( P
{0 f8 N" ^/ A; e+ ]& `
EventKillTimer();" v  K- V3 ]( h
}& h+ t* ~/ A8 a6 S! r: m
//--- The OnTick function only informs us that we have a new deal
( A8 s; R7 U2 g) x, l% V  ^4 fvoid OnTick()
, R( h7 @! J; C, U0 R: O) c{
7 ]2 J/ g/ U7 u& X/ ~tem_tick = true;
& y/ A4 J, U" h4 F) ~# b$ f}% m; t- s* Y3 K4 L: N6 H4 p
//+------------------------------------------------------------------+
: c$ ]! y" B# g5 i. ~//| Expert Advisor main function                                     |
7 }2 L4 w% P/ Y7 o" W$ w//+------------------------------------------------------------------+
5 R+ \, n4 U* Y" j. d6 c, q6 vvoid OnTimer()
( K8 J7 E& a0 R; v: ^* W{
1 n; h% D6 a3 @1 A- {; `MqlRates cotacao[];+ A% `& h: n3 d( x. a+ N3 T( d3 i4 L
return ;% v$ v4 q- P& K: z; u9 {& K1 @" G
if (negocios_autorizados == false) // are we outside the trading window?
( q+ a  ?) \* f$ V2 o# t' Yreturn ;5 y; m5 L" {9 m5 w2 |; `
//--- We are in the trading window, try to open a new position!
: q1 o9 d7 r0 N; {int sorteio = MathRand();4 x, u) r! P1 o
//--- Entry rule 1.1
: e* m0 |& V  u6 ]. {  cif(sorteio == 0 || sorteio == 32767)
  c3 v! z, ?  F9 Wreturn ;
8 Y) M; m) x6 _  `5 N/ H- Xif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy+ l7 _1 q1 a! |8 A8 O
{0 a9 y! Q: M1 D/ U1 K! M4 m0 X
negocios.Buy(info.LotsMin(), _Symbol);# a- Y7 }# j+ F- {# D' q
}  r5 g3 `9 g1 a- H  n
else // Draw rule 1.3 -- odd number - Sell
& o% K1 E0 ~7 G$ }1 w" ?{
1 {; R; K6 U; p7 i* _7 ynegocios.Sell(info.LotsMin(), _Symbol);6 f, g' M/ F) U5 j7 l$ k1 D
}5 [8 u8 ]8 ]) V
}- [5 p1 N$ V) @) i. \* g2 \
//--- Check if we have a new candlestick.../ a5 B' J( j; E. s$ N
bool tem_vela_nova(const MqlRates &rate)8 u7 b: T3 z: u" e7 I/ [
{$ r  i8 G) G2 z' c+ Z
{
, S$ @' v. U% K1 W7 i0 O0 m/ lret = true;
, t& [, j. n6 G. ^close_positions = false;, G5 }+ }" x. k9 @) O5 Z- M) [! U
}3 o% h' J# g4 Y/ Y8 b( Y+ J+ H; Y' V
else, I8 g: n/ f: w1 U  m4 N' J
{
; }2 X, g  `% J. rif(mdt.hour == 16)
' y) c4 M* q- M: s1 \close_positions = (mdt.min >= 30);7 C" f$ T* l# e, w  \4 n2 g% L
}
- \; Z8 ?" V4 ^2 [  c- S' t}
' U+ n. H2 [9 }6 J7 v( |$ rreturn ret;
5 ^* G: k! x- p0 W: \}
7 ~* U3 l% g6 v+ H4 l( T( W0 _//---2 o- g( |* z4 b+ K' p) A, V% ?
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
/ s' c, m5 @4 f{
+ j9 _$ N7 ?& t. }# A  Cif(PositionsTotal()) // Is there a position?
( u3 X. ?2 q4 L4 X8 b3 T+ j{
  G) @8 v, _7 b' \3 V$ Ddouble offset[1] = { 0 };
2 l8 p+ h8 j0 R# ^6 rif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?4 ~' [% N6 C, @6 y
&& PositionSelect(_Symbol))  // Select the existing position!  u& f/ v% z4 V0 d" v, M3 b
{
* x: R7 a4 `( d: j3 k# _, P, kENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);$ }9 s$ r  j6 b1 {
double SL = PositionGetDouble(POSITION_SL);2 a( W9 o5 X7 [0 T& J$ Y( h
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
" T& v% w# c# tif(tipo == POSITION_TYPE_BUY); I1 y2 M& T* A- V. ~
{- g/ q+ H2 x3 y
if (cotacoes[1].high > cotacoes[0].high)8 n( {3 t. c2 a/ f
{
7 ?4 `$ G* O; ]/ A4 gdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];* c: [2 k( R2 }5 M- S) }, S
info.NormalizePrice(sl);
/ e; Y& d: U7 U4 kif (sl > SL)
1 j; I; ~! G6 @# e8 x" r* q{
" G  N& _% V8 Hnegocios.PositionModify(_Symbol, sl, TP);. }8 d. f! O5 ^0 }* C; X
}% H( \9 @7 C. G$ ~8 V* |
}( B) E: |3 A7 [; V$ Y7 f
}' W7 M: U# t4 q& d
else // tipo == POSITION_TYPE_SELL
( ~: ~) o8 [3 h) u& U0 F: l, b{3 }; `! D; v- m) b
if (cotacoes[1].low < cotacoes[0].low)
; h2 I1 s7 B. ]3 a4 G* u{6 M  W3 x$ p" _
return true;
# ]) h* V' q) u) D}8 \4 g/ ~4 G. @6 {
// there was no position
2 |; k# B! r: Q# B  ~% I7 Hreturn false;
3 `; r4 u9 ]8 C; [% m, h0 ~! s/ K}
5 m' G9 n# `, Q& \* m1 x我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。# _  C1 I5 @4 _: \& Z2 L$ f
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-23 03:10 , Processed in 2.462750 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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