私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
* O1 @# r+ l$ f( U$ O+ `在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
! I; B, i2 v( R7 B为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
1 ?  A8 N2 r% X以下是制定这些规则的代码。" a+ t, i3 ~, ^  S" n* T+ _0 B
//--- Indicator ATR(1) with EMA(8) used for the stop level...
; ~4 \/ T, E) c" Mint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
' H1 v* D0 r4 e; e7 @( bint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);, Z# H2 Y% _; ^! B$ Z( H
//--- Define a variable that indicates that we have a deal...
) S; u# i' X# Y9 Y6 a% Lbool tem_tick = false;1 G+ m$ c& }6 }4 ?2 s- M& v, c
//--- An auxiliary variable for opening a position3 P9 I, S6 ^9 k
#include<Trade/Trade.mqh>
: l, s# O2 L8 j/ a, p5 b$ w9 ]#include<Trade/SymbolInfo.mqh>7 |8 V. w9 L3 Q1 o& O
CTrade negocios;( {) e* j$ a' ]9 |8 Q. n
CSymbolInfo info;
; b0 i7 O  \! s; {) @//--- Define in OnInit() the use of the timer every second
' b0 O  M- z3 j; B; r//--- and start CTrade- l, f; v9 s- O" X1 P5 _
int OnInit()
3 z* h& `3 {, o: T- U' x8 ]2 m: I5 p/ B{! u. M( M7 A; a. x2 D# i9 e+ @
//--- Set the fill type to keep a pending order
- \7 X2 P' B. i- G$ x5 o//--- until it is fully filled
1 v/ z' r+ Z" B7 F: {/ |negocios.SetTypeFilling(ORDER_FILLING_RETURN);7 U& R6 v1 J8 B7 d  j
//--- Leave the fixed deviation at it is not used on B3 exchange
8 W$ [7 [: B' P% X+ inegocios.SetDeviationInPoints(5);  q& k8 l! }8 n7 k$ j, {7 Y
//--- Define the symbol in CSymbolInfo.... W3 n9 E- X0 U. f) X3 P
info.Name(_Symbol);
- M! p, {% R" L& X, G$ S  D//--- Set the timer...
, G; K- v1 g. @EventSetTimer(1);
% K) A' o" a) r  M# v//--- Set the base of the random number to have equal tests...7 F4 @* `( b: h. j
MathSrand(0xDEAD);
; f) A" Y' \2 @! ~return(INIT_SUCCEEDED);
% F" d' i( v4 m( t* m& R}
  o* A) r5 F* _' z//--- Since we set a timer, we need to destroy it in OnDeInit().: [0 b8 z- o0 I1 ~1 \6 X
void OnDeinit(const int reason)9 b) _& }. A' z8 F5 ^1 C# M& y5 x
{/ |; r5 o7 A& f' u, O/ k8 l
EventKillTimer();# Y0 Z  `. c( i" n
}$ ^& E7 O. B; d5 t7 d9 I. R
//--- The OnTick function only informs us that we have a new deal4 t# `5 F, ^" Z! r2 E+ @
void OnTick(), @0 X5 H" ~0 n( p0 q: f  {
{9 A8 Z& D  u, S9 Y
tem_tick = true;
6 a8 v4 r$ I3 U1 n" f& ?}
7 I) n# U+ D6 |//+------------------------------------------------------------------+
+ i/ q' V: Q8 W9 C//| Expert Advisor main function                                     |
, _& }" w# u. O//+------------------------------------------------------------------+4 |% C6 Z& h, g5 t- A' D8 @
void OnTimer()
* G) b7 s& a) `9 I{. h2 f% e9 }) Y3 ?( @
MqlRates cotacao[];, q1 u$ H/ }3 S9 t- ~
return ;
2 c, s, @. m9 f( E8 c7 U* j2 _if (negocios_autorizados == false) // are we outside the trading window?
. n4 {1 h. p3 D" t& xreturn ;
5 D, _% u/ F% x//--- We are in the trading window, try to open a new position!
+ t6 D5 T  c1 S9 i( fint sorteio = MathRand();5 r- t7 s, A' A1 [5 c* a5 Y
//--- Entry rule 1.1) G# s0 J4 t5 c. E* h3 l8 I) ]5 w0 G  ^
if(sorteio == 0 || sorteio == 32767)* S! \  B$ @5 ]& q! S
return ;
' I. p0 i. O3 w+ @if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
( d2 Z! u* H3 q( F! ^{
# u, p' Y% T5 L( K  \: \! anegocios.Buy(info.LotsMin(), _Symbol);
- p+ e4 \8 r' |( x9 m9 l}
+ m- I9 W/ A) B9 X  x! Oelse // Draw rule 1.3 -- odd number - Sell6 u5 S, r  O) M! l$ j4 y
{0 Q' l  u' M1 G  a) R* }
negocios.Sell(info.LotsMin(), _Symbol);
( ?* `9 |( B, e% I! A" S$ z}
$ D% m& e/ Z8 R$ |6 G9 K4 o}
1 z& P( {0 K! ]( u* x% g8 L9 M//--- Check if we have a new candlestick...
4 C. x8 ^* t7 O0 E) Rbool tem_vela_nova(const MqlRates &rate)( b4 O( X6 z5 K; a. o# q
{* Y& |* v) u' L0 b3 |- Y0 o& W
{
0 Z/ {9 b% w% Y) Kret = true;* y' c0 t# `9 O7 S) q$ p
close_positions = false;
  u" A& o3 M) H$ R}) _; \; Z% _" w' b7 R' p2 l
else# D2 G8 A3 i% G. @; Q
{
+ s; i3 T- M) \- m: Bif(mdt.hour == 16)2 y1 `/ s6 W! P" t$ n. M' {6 |* @: j
close_positions = (mdt.min >= 30);
) D/ m' |3 j) t}
" f4 i  d( B" r( [/ y" R( Y: v}: X( J2 M7 D) U) {. A( Y) }- L
return ret;
# Y* f: ?. C, _2 J}" E% z; p/ N6 P% V' V
//---
& x: w8 x7 @: t8 C9 B- x' L6 D' Pbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])  v& @% E8 F: W! O" A
{0 S( [8 ]/ D  r
if(PositionsTotal()) // Is there a position?) [5 d# K: _/ _4 Q2 j6 ?& o
{
9 H" q2 i5 O" F6 V* Bdouble offset[1] = { 0 };
) B5 }4 d" M+ dif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?  K2 F/ D! @: ^: ^: P- h" e$ v
&& PositionSelect(_Symbol))  // Select the existing position!) _' L- V. t" ?- q
{
* K, h& d; @2 r" T$ S/ }2 nENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);& c* F0 H: j  _( \" q
double SL = PositionGetDouble(POSITION_SL);
2 e% L! q3 p1 O& {. l- \$ [double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
# n& P) R+ E, `8 {2 d; W, G- a7 tif(tipo == POSITION_TYPE_BUY)8 ?5 g0 M. k9 _" _; G: t, S: M- Y
{
) N0 {& y5 M4 c* l) ~" h8 |* yif (cotacoes[1].high > cotacoes[0].high)/ M: t$ s7 V% O
{7 k" w$ y# l- W; P3 U2 J
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
8 ?* L' B. H3 p* G4 L  ~7 e3 jinfo.NormalizePrice(sl);
% U) M, n" W7 T" Iif (sl > SL)
* W" b6 T( p, s' D! k6 ~2 W{3 i: K; }6 K4 \( s3 r# e  \
negocios.PositionModify(_Symbol, sl, TP);8 w  ]( d7 r' u
}  S/ U( u* Y$ S& v/ h
}" A9 W  @% B% ?6 }- \% @# q
}' k. N# u# H4 v+ Z' i
else // tipo == POSITION_TYPE_SELL& K; T9 a$ S3 n
{
6 w, N  D- i7 h& Q; U2 |+ v, ^if (cotacoes[1].low < cotacoes[0].low)
4 o8 o0 `8 v7 r, @& I/ m9 V{
9 n2 t* t& s, F2 o+ Z& Breturn true;
0 B1 L% ?- v% C; E- I}' K- ?5 A! F8 `7 x$ a) z; q
// there was no position% m0 \* [9 }3 I
return false;( t% N' R4 H3 X( d6 D* E
}5 _* e5 g* c; r2 Q1 {7 H& D- g
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。. W2 X: A. r0 T& G# r9 A
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-7 12:48 , Processed in 0.565968 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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