私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA- J, `0 K! j% o. N: Q
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
6 w/ x+ o9 ]) L0 x为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
: Y# C# q: @/ }* a, F, l4 _以下是制定这些规则的代码。
# Q. E0 L9 j) {9 G2 G//--- Indicator ATR(1) with EMA(8) used for the stop level...9 S$ |! J# J7 h" a3 V/ r8 O
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);9 [; W8 h8 Z8 A$ C+ W& m
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);3 I3 j* ?2 |, S
//--- Define a variable that indicates that we have a deal...
! p& m. @& t& i& @bool tem_tick = false;, V: g) i! k( U% |7 \
//--- An auxiliary variable for opening a position
6 ]; ^5 f1 w1 T# t$ q  P#include<Trade/Trade.mqh>" T5 n* J; B3 Z/ v4 @
#include<Trade/SymbolInfo.mqh>( d7 G1 c% P' _) w
CTrade negocios;
. V) K9 ?1 d# ~$ u. w+ @3 hCSymbolInfo info;$ @% f- _% q0 m# k4 e: E* B/ r
//--- Define in OnInit() the use of the timer every second
. c0 z$ O) l7 B: f//--- and start CTrade
$ P" V& ?, r8 N8 r" d0 wint OnInit()
0 B) p1 Q2 S/ {9 B* U6 I2 @{
) E$ z. x  c$ s  U) w/ J/ F) o( R//--- Set the fill type to keep a pending order3 Y3 N4 a9 ~/ k# _$ @( h" z  F
//--- until it is fully filled) d4 c' z- y8 G
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
! v5 h" B& [! @% E# v% v% v. Y//--- Leave the fixed deviation at it is not used on B3 exchange1 U; B! J) J, K; h0 K5 o
negocios.SetDeviationInPoints(5);# N# f- `$ B1 O$ U
//--- Define the symbol in CSymbolInfo...
/ E+ W: [, d. `$ Y$ sinfo.Name(_Symbol);
8 C8 `8 G: b5 M  V* s% Y//--- Set the timer...; s+ ~5 e' g4 w& A
EventSetTimer(1);
+ R0 y' B# h2 k% q//--- Set the base of the random number to have equal tests...
* x3 h$ P6 {* oMathSrand(0xDEAD);. T1 {+ q. h6 f6 @  j8 G$ I
return(INIT_SUCCEEDED);
5 M& _8 i) U# j2 ]* _& Q}3 @0 S* c8 F' Z, I0 p* p3 L
//--- Since we set a timer, we need to destroy it in OnDeInit().4 w8 J9 Z, G$ Y0 o5 B# B  W4 Y! U
void OnDeinit(const int reason)
/ G- P. l7 h5 B3 O& I* M{: o7 a+ W  r2 A5 ~* ~
EventKillTimer();
: c5 j3 [$ P* ?7 z5 B5 ?- H) ]" G}
1 C+ v) o$ Q/ J' ^7 h//--- The OnTick function only informs us that we have a new deal
. T! T8 a2 d( \) svoid OnTick()4 t4 e# L8 i7 X7 {# e
{7 `* @- w  x# f6 v( f) k+ Z, H
tem_tick = true;
9 \; h# M: n, F# f, }$ \}3 x  F: j0 b/ n) I" y* x
//+------------------------------------------------------------------+
6 N( T1 c2 M9 A# Y- P//| Expert Advisor main function                                     |% N; j7 R8 n( K6 p
//+------------------------------------------------------------------+; \/ e: M. D" S. R( z0 l: }
void OnTimer()2 r- Y' f6 ?. B2 o7 L
{
! H/ N( Q5 R6 H) X, W! O* cMqlRates cotacao[];
$ j0 ?( g/ R; q, M" e6 g+ [return ;
/ n9 y# {) W3 a7 E4 G# H# F% b$ C! bif (negocios_autorizados == false) // are we outside the trading window?) I. m/ [; F4 R$ C. t& d: Y% h, y
return ;
3 X. Q# r+ B! N/ O, O: u. \8 M//--- We are in the trading window, try to open a new position!1 I8 X- a# ]. r
int sorteio = MathRand();: p! I) I: K% b4 ?1 x) p* _
//--- Entry rule 1.1( m, J! N3 _; i8 l
if(sorteio == 0 || sorteio == 32767)
2 D* H5 u4 Y( B* n) p. W) Treturn ;
+ q6 `! ?) P9 g1 n+ _if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy0 v  T- W: a0 B0 W: [' @
{) o: r- s/ r' p0 g- F
negocios.Buy(info.LotsMin(), _Symbol);
" N" }4 |/ H* N5 k3 m}
1 j- L* B0 C" `2 Zelse // Draw rule 1.3 -- odd number - Sell
; }* l3 P7 \/ w: T  o2 K{
. d/ c) k7 S. y1 _) d1 {- `$ Lnegocios.Sell(info.LotsMin(), _Symbol);
$ \4 k4 u/ F: C: B0 ^6 ?( Y7 D5 d7 B: }+ s}2 ~- ~7 r- n8 P* `  c# {
}
; e+ ?% v% }5 V" C# k//--- Check if we have a new candlestick...
1 O& ^/ C5 t3 A, jbool tem_vela_nova(const MqlRates &rate)$ E( C* ^& F$ l& T3 i! b8 w
{
8 R( h6 O' u9 a+ L3 ]* p4 z{
# t/ C- D  e) ^% l1 y3 {) Hret = true;3 l1 M! ]3 L) y4 ]" H* ~3 P
close_positions = false;
' f# f2 f' `- e# g7 y. F}
) J# d9 Z; t. e& A' y% O  ^else
& Y( `& E5 `( B3 G. B( N! f{, z! Z: q3 t- K# ~3 }' i5 X' r" r
if(mdt.hour == 16)
; L- I+ p2 Z5 ]: @5 f7 bclose_positions = (mdt.min >= 30);/ a5 k7 O+ [3 H4 J9 E. b, f
}& \9 Q8 y1 x3 F- x
}
/ D/ \  F; [2 ~/ t. J  J# Yreturn ret;; J) ?. g8 y" {) I. G* K/ F+ x
}8 r1 o# l' Q& J, m
//---
  c/ V: q5 a2 B8 e( Q: Ybool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
% S% r$ ~$ d* Y; X. G{
$ f- e. T5 }9 W/ M- s6 ?if(PositionsTotal()) // Is there a position?
! O: a0 x0 @1 ^* [5 s{
, ^2 F4 t' k# {) ^3 ]double offset[1] = { 0 };
) l8 W- z1 j3 M) k, ?/ ]: L" Rif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?1 y* V. p0 C/ N: b) ]4 u4 Y
&& PositionSelect(_Symbol))  // Select the existing position!
! p. l4 f" c. t6 I5 \{
! O8 D! T4 [/ P' pENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
- ^$ a% k% U7 l! X0 q$ Edouble SL = PositionGetDouble(POSITION_SL);+ u, R% ?5 l; y. w8 u' O
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));1 e8 a% z6 H" [; n, [6 g
if(tipo == POSITION_TYPE_BUY)
9 C& V1 _( \4 [) S{
5 b- s) ?; f9 T8 `2 vif (cotacoes[1].high > cotacoes[0].high)
1 r; v6 p" I6 T1 J: S$ h{
) P" v" F' M: ], Tdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
8 v- L) T' c! p- Y" I$ Ninfo.NormalizePrice(sl);2 X; L+ X: V9 f# i' J+ y
if (sl > SL), _' v$ ]/ `9 I
{4 I1 [; N9 \9 {/ n2 O* @- J3 ?
negocios.PositionModify(_Symbol, sl, TP);8 f7 U4 h- L3 k1 `6 }" ?2 w5 Y+ q
}
8 |8 |. z' ?7 a/ `0 |" I}5 n6 t1 i+ y) t1 c3 o5 [9 K0 P; G( m  b
}" S: ?( H% L4 O8 K3 ^- p) Y
else // tipo == POSITION_TYPE_SELL
' g; A# [, F- s{
( l- O4 x2 d! t$ H0 @) K, @9 L& ]if (cotacoes[1].low < cotacoes[0].low)6 X9 h+ S. d* I
{( b9 }& j/ k9 K% Q9 u
return true;6 V' l2 J# |3 ?4 f0 s# x
}, F% [" d# u" N2 l
// there was no position
4 Z1 v) X- R* f1 b6 jreturn false;
9 X8 C7 Q* u% Z1 d/ \! A* D* `9 c! V}1 d! j+ A/ A% y0 o- i& K
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
& V3 Q' d* K/ w) Q* Z: B到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-4-27 00:52 , Processed in 0.705443 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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