私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
9 x: Q! z! ^) Y; S: V5 n% U在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。  V* m/ i: u9 m) }2 h4 O  u
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。& ?0 ^# {5 x; i& m6 h  N
以下是制定这些规则的代码。
1 }; ~, C5 o: j+ G' I' G8 h4 M//--- Indicator ATR(1) with EMA(8) used for the stop level...+ D2 c3 t1 B$ {$ l2 g  p
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
8 u$ q+ ]4 @7 E: @" u1 [0 \int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
7 i4 T3 g. d% S, @//--- Define a variable that indicates that we have a deal...9 k( A' ]9 Y+ t4 R' p/ g( ~
bool tem_tick = false;
8 ]- P, f+ c) P" \" {: ]//--- An auxiliary variable for opening a position) i2 f2 v9 d: s  p4 i- H
#include<Trade/Trade.mqh>
/ J; J$ f, z# j, w' |% _#include<Trade/SymbolInfo.mqh>2 h: M  @8 i, Q' A: c+ g
CTrade negocios;9 ^& M% f0 o% k3 a8 |2 H. z3 l
CSymbolInfo info;
2 s: h8 [% }* {% E9 F6 J//--- Define in OnInit() the use of the timer every second7 d: u+ R' R! P* N& p
//--- and start CTrade
0 j- E" i) c$ h/ F* Pint OnInit()
# b8 Y' l' z3 [- E{3 y/ M: Z  {) l
//--- Set the fill type to keep a pending order8 c2 W0 X0 ~6 O1 Q' O9 E2 t
//--- until it is fully filled, a; ~( J' {+ `, @
negocios.SetTypeFilling(ORDER_FILLING_RETURN);$ A) k, F4 H& c  D) c5 S
//--- Leave the fixed deviation at it is not used on B3 exchange1 o9 ]  B% D# x6 v
negocios.SetDeviationInPoints(5);8 [+ h% ^8 M5 j8 q0 {
//--- Define the symbol in CSymbolInfo...4 V) k  b( s8 Q9 y
info.Name(_Symbol);
& I# @# C2 H- h+ r//--- Set the timer...
3 |( J6 D% n  ~( k- \EventSetTimer(1);) R4 n3 Q7 R( e4 @
//--- Set the base of the random number to have equal tests...9 K& i, `2 e: }" f' J
MathSrand(0xDEAD);
; W3 J. s' J  Y  ~0 a# z: Greturn(INIT_SUCCEEDED);+ t7 n* @( [) g2 |6 f8 x- G! L
}
9 G, |+ k& Z; c2 c) z//--- Since we set a timer, we need to destroy it in OnDeInit().' ?8 z) C$ o" V/ e
void OnDeinit(const int reason); \# p; `* I9 W$ o* V; F  E
{, U  F8 m0 ]& f2 {6 {0 m
EventKillTimer();
: g+ l( J% p. p- E}9 H; m5 @. Q5 O+ E
//--- The OnTick function only informs us that we have a new deal5 B3 l3 k5 P4 F7 H
void OnTick()
' n) Z+ \3 j6 x0 ~' X0 ^+ K0 q, h{' B* y" `9 J. z7 P1 K  t: k, e
tem_tick = true;7 K4 ?* k: i9 H$ x+ g1 v
}% k& p4 N4 p; O& i' Y
//+------------------------------------------------------------------+
' `  c. |- k5 B! W( ]9 e% H//| Expert Advisor main function                                     |
, j' T9 a  T; q5 v6 f( Y/ d( I//+------------------------------------------------------------------+
$ D+ \" u8 e, C# q- i" ?void OnTimer()
% D7 p% u- V9 Q9 p{; [3 U5 q$ a, Z
MqlRates cotacao[];  n5 ?9 T1 `. }4 G# ]/ `
return ;8 z) |0 X. g5 v1 A) v# t
if (negocios_autorizados == false) // are we outside the trading window?
" }( q/ E& U5 ~' `return ;) L# t# D) b0 m) z6 p. t
//--- We are in the trading window, try to open a new position!
7 q. {8 Y8 c4 s% vint sorteio = MathRand();
; S9 u, M$ K2 d; v1 @//--- Entry rule 1.1' ?' Y  A8 ]: Y* e: S7 s# v1 z
if(sorteio == 0 || sorteio == 32767)& m8 l. C# a3 F7 n& I
return ;2 u/ m# ?0 r: I
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy' k  j5 V, N# K. s2 l
{9 H' t) O8 f  c2 y/ \% _; C
negocios.Buy(info.LotsMin(), _Symbol);- V. k: j8 l0 x* {' h: n0 H: L
}
4 P# @/ i: a4 e9 Zelse // Draw rule 1.3 -- odd number - Sell1 m0 G9 _! |3 L. A/ J) ~
{
" d  C  R8 Q  F( |# rnegocios.Sell(info.LotsMin(), _Symbol);* r$ z# r. o5 O) l9 X( J
}' q3 S6 Z, X: a9 N3 M
}
; h" _& C5 N3 S- b* M//--- Check if we have a new candlestick...! r1 |+ @& k  ]  A9 G) p! Y6 t- i
bool tem_vela_nova(const MqlRates &rate)
. o" @; |+ s+ a, b: t" u  D/ ?{3 A/ R2 Y& v1 r: ]( f/ O
{
6 O  W. f  h! V7 u1 g" S6 [, Eret = true;
) l& Y! s- c# x* i: a* M- R0 S6 aclose_positions = false;
6 l$ V* z8 l5 f# n$ c}
" W$ d' G( L$ g7 A! w6 D8 X& Y# j3 [- Gelse
3 e9 }/ m" t/ |: {1 y{; b  Y( C+ P0 d. q2 y
if(mdt.hour == 16); c4 i1 Q% z6 D" U; v$ x
close_positions = (mdt.min >= 30);
. a7 L$ H, C1 y) r}
. I) ]3 N5 t4 O& z3 U}
8 h0 ^# T  ^- B  r2 k4 Freturn ret;! d* G  O# J% T2 C  t
}$ ~. G5 h  d8 a, [$ r9 w3 t0 l
//---
+ Y( {, z- `5 f& O3 Ubool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
9 h# a: [+ N+ g% f( H  N6 U{1 `7 w- \2 X' E$ H" A) c  I
if(PositionsTotal()) // Is there a position?3 E( l/ d8 j8 H; n, ~
{
3 D5 J( z2 W1 }' _double offset[1] = { 0 };" F% g9 l: i6 x4 h4 p6 b
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
, G1 H& Z0 K$ q3 U% n$ R# M3 p1 a&& PositionSelect(_Symbol))  // Select the existing position!9 ?/ @+ c3 f6 N  h# Z# U
{* J* t; A# t$ R+ U3 b, ^& \8 K
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
2 I7 x* M7 G6 \5 ~# {* u1 I+ R; bdouble SL = PositionGetDouble(POSITION_SL);* w* q5 n' `; B8 z8 V8 p
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
& r4 A2 O* z6 L, j8 [* O6 q- z4 Y' iif(tipo == POSITION_TYPE_BUY)
" J+ v- Y& w& ?5 M{
( T9 r, S3 q8 C5 b; @if (cotacoes[1].high > cotacoes[0].high)
/ l- K; u4 D7 L3 i$ U) S{, u: c% _( d" A
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];! F* u5 N* `! \& K% E9 t
info.NormalizePrice(sl);6 f% u' ~2 E" ~! X! P2 y
if (sl > SL); I' x( P$ \% m# P( v0 C4 x
{
% P5 d" J5 `' \$ `negocios.PositionModify(_Symbol, sl, TP);
( ^/ N) O8 I' R! e/ ?}' z; F6 s" `- ^) N
}
0 U" B( S0 P% q& S+ J" r7 O) k}
( w' V/ _, G- }: o# felse // tipo == POSITION_TYPE_SELL! E7 H5 S# R" D. r* O# u8 x
{
' C0 D; J- X  ?4 ]if (cotacoes[1].low < cotacoes[0].low)
, e! K! R$ q: G3 d{
1 B& B3 S0 S) W# N$ s& |return true;
% `8 u: e; G1 B}
' _+ E, o, p1 e// there was no position. r4 ^( s3 b  x  Q: W" y1 h0 q" Z
return false;
  b- ^6 n" F5 B, \" S2 r9 M}6 |& [$ P, ^- ]) V) x
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
* r: v6 x! y: k& h到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-20 18:31 , Processed in 6.605013 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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