私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
5 g% D: @; I' \在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。' p/ R5 \; \+ _* L$ p( N  M. z
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。7 P% I( v  c# {
以下是制定这些规则的代码。6 Z# N7 g: b) w5 @4 i/ r6 t4 ?/ H
//--- Indicator ATR(1) with EMA(8) used for the stop level...
2 [) b5 M' R3 ^1 Q+ G2 q% j# Oint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);. O9 w  u3 N/ X0 t- j! i) y" I! i4 e" Y
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
3 j" O: t" G' V" f. E, C: ?- I//--- Define a variable that indicates that we have a deal...6 {1 g- }6 d) z
bool tem_tick = false;( @2 |2 c% n) D! u9 w5 A/ u
//--- An auxiliary variable for opening a position
# O" Y" \9 o& @" s0 S#include<Trade/Trade.mqh>
  ?8 D8 I  J1 `! n. {#include<Trade/SymbolInfo.mqh>! j2 ]5 H  x4 ?, L) Z$ k
CTrade negocios;
; T% i+ S& Y5 s2 D) w2 }6 DCSymbolInfo info;# p) b6 V) d0 v0 u3 g  t
//--- Define in OnInit() the use of the timer every second  B1 X5 D* J. H& u) R
//--- and start CTrade! C$ M/ v- y, _* r7 W) `, \
int OnInit()  I8 Y% \. |/ X
{/ A% {: h# D( `
//--- Set the fill type to keep a pending order4 p% @2 O) Y6 M2 V$ S$ w4 ]7 ]$ d3 c
//--- until it is fully filled2 _6 D1 ]! A( I2 u
negocios.SetTypeFilling(ORDER_FILLING_RETURN);$ G% Y& \2 W. n
//--- Leave the fixed deviation at it is not used on B3 exchange0 G& ~/ g. h2 C: ~) b3 C* T" K4 E
negocios.SetDeviationInPoints(5);( J4 a  i! \; v' k. W! H
//--- Define the symbol in CSymbolInfo...
0 v9 _7 o/ z- [4 kinfo.Name(_Symbol);
( O8 j$ p4 P9 Z0 X9 p//--- Set the timer...1 @( Z; N5 D& C+ h! E5 q5 i/ z
EventSetTimer(1);
" L' V) q" n; T& C  N$ ]# D3 ~9 w//--- Set the base of the random number to have equal tests...9 D  q9 e8 m- @* w
MathSrand(0xDEAD);
; Z# ]7 N' o) Ireturn(INIT_SUCCEEDED);4 U0 y0 c7 I+ j) w
}
5 }! _6 _6 v8 m( l//--- Since we set a timer, we need to destroy it in OnDeInit().! K& E: |6 G0 U0 z
void OnDeinit(const int reason)# J! O' _1 c$ F$ _
{- \/ |! @1 c, z9 K" L9 s9 B
EventKillTimer();
: f% J; r, j( Y! Z0 g- j# g# {}
3 c/ `6 O* [* x  a) i6 i8 I6 K8 u- K//--- The OnTick function only informs us that we have a new deal; k: b2 h% t6 Y6 k) I. \5 d
void OnTick()$ D- ?" L& n  p0 f" ~  B
{$ B! z! h6 B0 u9 K
tem_tick = true;3 c* L: O' L+ C! z4 Y3 z1 d" i
}
+ y5 U' a$ t6 u1 K, R//+------------------------------------------------------------------+, L- K" }# ~! ~( i" R2 s0 A/ }
//| Expert Advisor main function                                     |# I6 s8 G. a4 N; h
//+------------------------------------------------------------------+5 X1 N" M4 k% G7 x3 D7 U
void OnTimer()( }' S: @) W& h  R9 ^$ A- B) x
{
6 ^. I2 N, [1 ]# k0 B. i+ jMqlRates cotacao[];
- t7 n  B. i! y0 r! _2 F, vreturn ;
, O; X" F4 B: y: m! _) |  X, [9 e* sif (negocios_autorizados == false) // are we outside the trading window?
! A6 h- y) f: T5 l+ ~3 Sreturn ;
) _, M% ~$ c5 T$ o' ]& x//--- We are in the trading window, try to open a new position!* q9 u) @8 M6 S! k+ _/ u
int sorteio = MathRand();
3 M- ^% n! F; Q- f//--- Entry rule 1.1) f) ]' v  c& O, R
if(sorteio == 0 || sorteio == 32767)/ y; q; l% ]! D6 R
return ;
, C, Q& k! T) F3 s: |! m6 G3 Sif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
8 j' {& ^. k' ]( T' }' b2 p{
$ B8 K1 f( G1 ]# l* G' y) Knegocios.Buy(info.LotsMin(), _Symbol);
; |5 i- ~5 b# x: }: W2 s}# ]& V( g4 A, W! l
else // Draw rule 1.3 -- odd number - Sell- @; F( K" c9 w9 Z2 X
{. M; n; u8 a6 W
negocios.Sell(info.LotsMin(), _Symbol);: f7 z5 p/ ]0 f) S9 t, ?
}
, @- `' [  A2 K* @9 m; }& U}6 k  @: b5 l3 t( ^9 L$ w1 I( B# y
//--- Check if we have a new candlestick...
/ c/ O2 n2 f( M/ V, kbool tem_vela_nova(const MqlRates &rate)
$ W6 S/ I! R" N  t0 M8 o3 t{1 L* }0 N3 y( L) A. ]
{+ j( F( d4 B; q- V4 g' [. y
ret = true;
0 e, g% ~4 [+ \/ m" S0 F! Q1 F; h# Yclose_positions = false;
$ `7 M' x2 }1 O$ [: E}
% D. A4 x7 q1 ^8 q2 N8 _else) t) A/ @% _3 H0 u
{
# C" K5 x0 e4 w2 i" uif(mdt.hour == 16)* b: i4 V) u3 r9 S3 A
close_positions = (mdt.min >= 30);3 S6 d- j8 a, L5 B& b( y0 x
}
0 C+ y) |+ c* K}
7 I  k1 r/ h. Z# Mreturn ret;
4 R4 k; I* O- {}
- o( k# L6 Z$ j. |; u4 f" |; m! |//---
$ B1 _% e4 V# v3 g1 q5 F/ ?bool arruma_stop_em_posicoes(const MqlRates &cotacoes[]): w" C; I, {7 ?9 E& ^
{
! W3 \7 [9 {( q" t- t  Uif(PositionsTotal()) // Is there a position?; R. k1 h( K; `% d
{
7 i! ]- h& A, ~" C) b: v9 Hdouble offset[1] = { 0 };! I" f& E! {2 J" {- m% k
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
6 n' b, w7 I+ v+ q) A3 k/ V. ?) |& ~&& PositionSelect(_Symbol))  // Select the existing position!, s* k( w9 @  a6 ^
{) j2 k* N0 Y! p" x! |+ E8 c
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
7 L+ U- l9 A* q' Q& pdouble SL = PositionGetDouble(POSITION_SL);
4 S& t5 J# e: G; W4 {) f6 Mdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
! ~, i4 ]5 M0 Q* ]if(tipo == POSITION_TYPE_BUY)
: Q2 Z2 }9 {# u1 V7 k9 W{& O; [4 m- e  ^; {
if (cotacoes[1].high > cotacoes[0].high)
' L7 Y& B* N5 ~9 D7 C6 s{7 L( m0 G* C' i# R- z3 [
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
" u( A( \0 Q" Xinfo.NormalizePrice(sl);
+ B& \7 ^. |; e/ {1 X5 N* P; Z4 T/ \if (sl > SL)
& X. r$ N' {, u! ^0 a{( G; E: O2 k' |8 y9 X4 t: M
negocios.PositionModify(_Symbol, sl, TP);
2 O2 U+ P' y- z0 r2 M0 u: \- U}6 U7 L0 k- h; H- g7 g7 k5 D' Q
}
1 h% f; @4 p3 N) B3 U6 h1 _}
) x0 G: |# ~; N* Q  y1 {else // tipo == POSITION_TYPE_SELL
( \" u" j" h1 P, ~( n& _{7 o' _. }" y8 O! c3 A7 G& w
if (cotacoes[1].low < cotacoes[0].low)
; k  M6 ^# l6 Z" E# o{6 [; P" |4 f6 f& _9 |, G* l
return true;1 v2 v2 s  J5 a& f
}+ Z) f9 t, o7 _& d
// there was no position" O$ `5 z& [2 j6 B( N. T
return false;* W& y0 a, v* I
}
+ q& G" t# e2 V* U我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
  d  m% Z/ D# C  N/ }4 m到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-10 08:42 , Processed in 2.239263 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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