私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
3 r0 N1 c5 @3 t3 w在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。* ?: |  O; t5 m
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
* \6 S2 B/ `. U' H' X, L6 Q以下是制定这些规则的代码。: ^0 |+ B3 W; l0 s$ q4 E
//--- Indicator ATR(1) with EMA(8) used for the stop level...
8 c% N8 O5 J- O) x& ]; X8 [  Jint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
( q4 B/ I* u/ k7 g" L' m8 D: ]1 j* ~int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
: O& ]- d0 E* c$ }//--- Define a variable that indicates that we have a deal...
& C% N% i& G. m& G" N: Q/ s7 {bool tem_tick = false;4 O5 v+ o5 d; p# @, O& b+ G4 x
//--- An auxiliary variable for opening a position
& w8 U( r! ^) o) l' C#include<Trade/Trade.mqh>( D  d. n1 c2 f4 q+ k
#include<Trade/SymbolInfo.mqh>
2 ?0 u" c: ]4 w. F% I* V9 xCTrade negocios;* q0 A/ C, i2 @0 s% n- r
CSymbolInfo info;
+ v( O* I& v% O& [9 G! k//--- Define in OnInit() the use of the timer every second
: m$ B1 @, a! K8 j' o1 d/ R, ^//--- and start CTrade
# K3 x, Q) Y8 h+ Y( I, P$ J9 Oint OnInit()
0 H: h% u  j8 J- n  j: z$ j. I( F8 ]{
2 x) r2 q) {& [/ l//--- Set the fill type to keep a pending order* F4 H+ [; M! N# }
//--- until it is fully filled, r2 f1 U# j* E, c* Y
negocios.SetTypeFilling(ORDER_FILLING_RETURN);/ X! H* k0 h$ i% O  d# c, o4 Y
//--- Leave the fixed deviation at it is not used on B3 exchange
2 K9 C& L% c6 \/ L2 p9 unegocios.SetDeviationInPoints(5);4 r$ d, G' K% N4 X
//--- Define the symbol in CSymbolInfo...
3 e* ]+ _: c$ ^info.Name(_Symbol);
- Y5 Z0 v) |, G3 t5 i& P1 ?) o* N//--- Set the timer.... f# D! }( D6 e4 s
EventSetTimer(1);
. D, ^: e8 w7 O4 T& G" A//--- Set the base of the random number to have equal tests...! @, ]  f9 w' @2 K8 W( R# U+ e
MathSrand(0xDEAD);7 M% |, t2 u  J9 P
return(INIT_SUCCEEDED);
% ^3 u- M: ~% g! L& {* f4 d}
* E& B3 c% ^7 ^# V/ O//--- Since we set a timer, we need to destroy it in OnDeInit().
' S1 P+ N5 t; b' ]# rvoid OnDeinit(const int reason)
! n) v, O5 v$ s' m+ g) w{/ h4 F% S' x# H0 M, |* D& o1 x
EventKillTimer();
( p2 h' F: O! w( g/ R}
2 z! d- E) G6 M2 }# w' m  {; j1 O2 i//--- The OnTick function only informs us that we have a new deal* H7 v2 P: _2 V, W) }9 s' x2 F
void OnTick()
- P( p! r7 p# s+ E{- [  O) w# w+ o, X( V# U' c
tem_tick = true;1 R! g( H9 f7 g  ~9 Q( Z2 a
}# n$ }7 T- |3 w2 {
//+------------------------------------------------------------------+
" z+ i3 f! h7 n  w* G' a# a3 w  o//| Expert Advisor main function                                     |
2 x! C6 u# K2 _+ A//+------------------------------------------------------------------+
$ V2 I" `4 s% Y- n3 P: r4 f% Mvoid OnTimer()
- O$ b! P1 ]$ j{
3 S5 K# [8 V# j; OMqlRates cotacao[];1 f( G& v. s! f* L! B
return ;2 g, q) g9 c' d2 ]
if (negocios_autorizados == false) // are we outside the trading window?
+ N& _1 E  ^6 h+ v4 \; Xreturn ;+ C6 ^8 ?/ @( L; M5 u/ ~
//--- We are in the trading window, try to open a new position!' g- E( E/ z, a  t# K" v3 V
int sorteio = MathRand();
$ t- ~; M: p+ a6 K' f& g/ r//--- Entry rule 1.1
+ }: K% X( V: d( q' j8 qif(sorteio == 0 || sorteio == 32767)6 ~8 U9 g$ j7 n% D( l
return ;$ t$ P. ^: s/ o2 Z, I
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
6 V6 Q) ]4 V6 |: c' m{
8 E, y, z  [" F& O! ^4 c% o6 p/ y5 rnegocios.Buy(info.LotsMin(), _Symbol);
$ P' n( u& l/ U+ ~7 S1 k}7 s) c/ M' d# c. ]- x# _& H
else // Draw rule 1.3 -- odd number - Sell; E4 _3 N3 f/ U/ x3 E* f+ |/ l
{8 P  T" b3 t/ p9 \4 i+ Y
negocios.Sell(info.LotsMin(), _Symbol);
5 }' I  s% H' \0 ^/ ]}) }( w+ N3 B7 f2 y6 i& s
}
% x$ [5 F; W7 A" y: t- r( T# d//--- Check if we have a new candlestick...
" ~; Y. g% Z: {! e: C0 ~bool tem_vela_nova(const MqlRates &rate)
( u8 R5 h% _! |" G0 T{
) r. S2 I- ~* d' y; \{, F5 _8 G8 z7 k1 o$ L% Y8 e
ret = true;
% e. c4 ]" o( N- n( Bclose_positions = false;
2 m% Y/ x3 A; ^5 N, \}
# m& e' A* V" a0 }4 l' O& E: q7 Nelse
4 S+ h7 C7 G- K( G0 i5 q/ {- w{
& L, `$ F, k6 t; ]: dif(mdt.hour == 16)
& d( P2 Y( @) h% N9 y9 ]8 G' lclose_positions = (mdt.min >= 30);: |" V6 E  Q8 ?3 A# }
}
9 e* ]9 {' i7 I8 A* G2 D}6 w$ g  u/ ?! }% M" M/ x7 x) [8 d
return ret;
7 y7 D5 _/ V! J2 N# X- c}
& u/ s0 j' @' L+ \6 T. ]; G3 S+ X  U//---
6 t% j0 H" l8 E% `  ebool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
" s7 `; [3 A- o0 ]{. Y! [* ?0 T3 q7 ?" e: @8 H+ F0 M
if(PositionsTotal()) // Is there a position?
1 Y5 @$ [7 l( w- B* N{
1 K1 D# Z( d  I0 ^double offset[1] = { 0 };
5 w8 y0 j0 T$ k; s3 Sif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
& v+ G8 ^! N/ f&& PositionSelect(_Symbol))  // Select the existing position!( O+ @5 `# e# z+ V0 b6 n3 b
{
) E5 A2 }( f+ M" h5 b  ?7 FENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
5 F5 e! R: m9 e: {6 P) `double SL = PositionGetDouble(POSITION_SL);
! S4 I. V  m" m( c  ], [3 n& gdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));, E4 d5 _9 v9 t
if(tipo == POSITION_TYPE_BUY)
, x/ x, j+ v1 W% r& N{/ ~( {! }4 h3 D6 k9 G3 p
if (cotacoes[1].high > cotacoes[0].high)6 `4 [, S6 a1 A8 f* I0 W3 |0 e
{+ i+ F" x* x; v; n+ |  p9 Y
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
  \1 ?% z3 r( |+ j& iinfo.NormalizePrice(sl);
- S3 j9 M: o8 @; i6 C, }if (sl > SL)8 W7 E8 B- }) t+ E# n
{2 [1 R; b- X+ ]( A  X. c
negocios.PositionModify(_Symbol, sl, TP);7 D) g8 b5 l- ]8 |" ~: N; S( J
}
0 x! f" f& \- t& \4 O}+ {5 Z0 H9 n* k
}
; M5 B) Z7 U6 ~) f* |4 A1 delse // tipo == POSITION_TYPE_SELL
! p; e4 V9 s: ?/ n{. S0 W! W9 L, P* F) i! _. H3 \
if (cotacoes[1].low < cotacoes[0].low)  H2 w" ~: f9 G* i6 ?
{& H( H4 Q5 X2 E; t" G0 @
return true;
/ C9 h4 q4 P' U5 D$ r1 p}
- _. B* M! \* D: {: W2 j// there was no position/ _7 }; K1 n% Q! ^- O+ a
return false;
+ L5 \$ N% L) Y! Q6 c$ v: |}$ {1 O3 X8 ]+ T
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
3 w9 L" R  G5 r3 ^1 g) N( ?5 |到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-9 12:07 , Processed in 0.430056 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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