私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
8 h6 Z$ s7 C# E9 o  u5 a& a在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
' s6 D+ w* ^( k8 M7 a为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。, C, k' ^( N) ]( ~# x) \# j
以下是制定这些规则的代码。
2 @/ u4 E* Y7 T3 f//--- Indicator ATR(1) with EMA(8) used for the stop level...! X9 ?/ |0 \9 B& J" g1 |
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);- _- j, I0 V) l3 d9 i
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
1 s+ A: Y' X/ u1 {* a% @& M//--- Define a variable that indicates that we have a deal...
2 N$ }, j0 g$ Tbool tem_tick = false;
2 K/ ^9 K5 c7 h3 ?! o5 Y8 k//--- An auxiliary variable for opening a position
& x5 Y1 T9 |7 B; i: r#include<Trade/Trade.mqh>
9 i' m% X: a7 a8 }#include<Trade/SymbolInfo.mqh>
* _# s7 V$ u, j( G3 h7 y5 b' `6 G; NCTrade negocios;# \8 e5 x! G/ C+ e# |
CSymbolInfo info;
$ t; Y) G/ ?0 E5 \) n//--- Define in OnInit() the use of the timer every second. Y4 R6 Y+ y. O0 B0 L
//--- and start CTrade
( J' q$ m; A$ l2 j( l( X8 _. vint OnInit()) A1 s, j1 M: b: U0 V
{& M6 N: |+ n) k9 I9 x" C
//--- Set the fill type to keep a pending order
, a# c+ G1 W) S5 u! w/ A$ w& |//--- until it is fully filled# ], Q' N2 S% V1 N
negocios.SetTypeFilling(ORDER_FILLING_RETURN);! Q& {* Y+ }/ K$ G0 J
//--- Leave the fixed deviation at it is not used on B3 exchange
3 F9 S; W' z2 p+ ^+ m' w( wnegocios.SetDeviationInPoints(5);
$ Q9 x" f& [& X$ }1 ^//--- Define the symbol in CSymbolInfo...( E5 m" K0 S/ D' C. t' x1 ~
info.Name(_Symbol);
4 p+ F; L2 i/ W; d! p//--- Set the timer...
* B" ^6 r, t: h" EEventSetTimer(1);; u3 t" U7 R' q9 Z9 c) m
//--- Set the base of the random number to have equal tests...
' {  F# m/ S0 t8 S' e' b8 ?MathSrand(0xDEAD);
: ]: b8 U3 p& W) e1 v3 q  V( b' Nreturn(INIT_SUCCEEDED);5 v/ h$ ]8 T- W1 @+ }
}
, l* X! p$ w7 _' Q9 J. U& }2 y//--- Since we set a timer, we need to destroy it in OnDeInit().
7 B* Q- `1 c3 {+ Kvoid OnDeinit(const int reason)
+ F9 k8 V) L) I3 V! e{. Q8 U7 C; I& |
EventKillTimer();% x5 C( v+ X. _: L+ F9 T
}
# @2 r$ G7 y8 a2 f3 X5 w: I//--- The OnTick function only informs us that we have a new deal" G0 ]7 U' s. }  ?. S% R8 q
void OnTick()
9 ~' n* x: O' E2 U3 a1 P& _{" F& m$ V6 X* |( L$ k; |% G0 w1 E
tem_tick = true;. T7 a* E  I$ D' D: e- j: j0 d
}
5 m, u2 T& \; M3 W  |//+------------------------------------------------------------------+, I6 x& L4 z3 F8 P0 c
//| Expert Advisor main function                                     |
' S. z* o3 d2 j3 w* J: z//+------------------------------------------------------------------+
( _' X8 o' T+ y( c: e) n( ~" `void OnTimer(): K; i3 X8 b6 q% \
{1 W1 p% v) Q; q, Z3 b' ~9 S) ~
MqlRates cotacao[];
0 k  b8 S: r: a4 w$ H; H3 F8 V. Yreturn ;3 w' y( b- X' F8 f, n2 e
if (negocios_autorizados == false) // are we outside the trading window?2 w* I+ u* Q. |' v! @/ J$ j
return ;
" j4 u% }. c5 O- c2 Y2 j//--- We are in the trading window, try to open a new position!
3 d1 v( H) q, ]int sorteio = MathRand();( k: d( {9 n- y6 ?* w. J
//--- Entry rule 1.1
2 F) T- A( B4 h( K* l$ h8 aif(sorteio == 0 || sorteio == 32767)
( j2 ?' T; r$ Sreturn ;& G( J" r% `, W" Q$ R+ U( Q' z& G
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
7 m, `9 n5 g& a; U+ j' W) V{( n# T! }8 t) W& y( M
negocios.Buy(info.LotsMin(), _Symbol);
9 v5 [( d( }, \- J1 J# i' ^}
( Q2 e2 }) m! j0 M0 u* {- m7 p! belse // Draw rule 1.3 -- odd number - Sell: C! _8 u% C7 q
{5 `7 }2 a7 e& q# H$ k
negocios.Sell(info.LotsMin(), _Symbol);
* k* f+ `$ y" i' _( w}2 i$ s2 F* ^7 @. e6 f3 r9 [1 U. _
}
* Y' ?, r8 j# ?; m//--- Check if we have a new candlestick...7 Y1 h4 J% d. z3 e1 O! e
bool tem_vela_nova(const MqlRates &rate)
3 v7 J, [% l! U& H9 R; C5 d& c# a4 F{, ^  ~5 A1 N/ T3 a
{
. j! R( R& C# a9 dret = true;+ Y6 a3 u' o6 x7 u1 {! |8 _  t9 @
close_positions = false;3 |3 z" l# K, B( ]: R5 Y% J! f
}
* Z8 w6 ^/ {9 J, P8 q- [+ _else5 x- E" Z9 o. x9 [; X0 v, K( [+ r
{  N% R7 ]$ F0 f  A! s+ H
if(mdt.hour == 16)! [3 F; O5 v) b' d! y. n' g
close_positions = (mdt.min >= 30);. q) u1 w0 U7 D& i( Y0 ~! }  h- {" u
}
  L4 t* v1 w! w! ^/ N0 D}
: U; Z6 w$ y% D5 e( [: J+ hreturn ret;
: m, I% O6 B  v2 z- Z! o  z- J}
5 \4 P# R3 e4 m- v/ q* e# O2 T//---
( Q( |" R0 \8 B9 ^/ i. ^bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
/ F  q2 w' k5 N{& ^' ^- j4 M) i
if(PositionsTotal()) // Is there a position?
6 F  o' s, N6 W* @{) Q* \; Q6 c" E2 g' h* E5 ?
double offset[1] = { 0 };
+ d& F# \3 q3 E; P( l. b/ N, nif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
. r! C, G6 y1 ~4 |&& PositionSelect(_Symbol))  // Select the existing position!2 h7 E  t6 T- _
{  \' |) W& I2 _) A8 L
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);0 k. S; {1 s% K, c* Y' L
double SL = PositionGetDouble(POSITION_SL);; t% y7 y+ R( T( u! h/ L& C
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
8 ~, `7 r$ R: S& w, u/ ?6 H: Uif(tipo == POSITION_TYPE_BUY)5 g+ D1 d! d1 H/ E1 b: q
{) u2 @# F8 S6 P6 I8 M' p
if (cotacoes[1].high > cotacoes[0].high)& Q% U0 b0 D% Z" V( k5 P. i
{
' N* D6 J+ b0 [$ t/ ddouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
/ A* F9 Y& d1 ?% X7 vinfo.NormalizePrice(sl);
& n) }9 u; l3 O2 E$ Y# aif (sl > SL)
, z, i2 Y) n5 p6 K{
! s# }$ ^0 ?: n! y8 w. [9 J8 mnegocios.PositionModify(_Symbol, sl, TP);' t( Q) q6 y! V+ K& t
}4 q; Q' l/ u. n" i8 K+ Z
}2 o8 f3 |) D1 v" \
}5 k! \! p1 R; O9 L+ o
else // tipo == POSITION_TYPE_SELL, s$ P# d3 X# j* a* [& c
{
+ C9 b6 B& q, ^. Q7 a8 [' j: Cif (cotacoes[1].low < cotacoes[0].low)
1 I$ R% z- I- h{; z# M2 d) M9 D. H8 |1 C9 ~/ R
return true;
& D* \9 {$ u4 b5 J}
3 G( I: N/ V) Q4 P& D, J, ?; @// there was no position
1 S9 p0 b0 Z$ h1 K$ g6 ~% f' M3 Xreturn false;9 C( v5 s2 I& I# y$ m* Y" A
}
! }% a+ M% p& R, F4 ?2 F我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。4 o. i% |- t/ f
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-2 10:23 , Processed in 0.424406 second(s), 32 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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