私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
/ X& }4 T' T5 B) S; A在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。2 U- [4 ^4 g: N8 D% ~
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
) y0 W# f/ ]( u. @0 ?以下是制定这些规则的代码。
& K8 Y/ g( j3 d5 a$ ^//--- Indicator ATR(1) with EMA(8) used for the stop level...+ ^" M! t# @" ~+ q( f8 `: z
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
: Z$ J: `/ |- \int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
7 v' x( k' l$ v//--- Define a variable that indicates that we have a deal...
" K8 Y# P4 _- {- x0 `/ F* e+ U# Tbool tem_tick = false;
1 w1 `0 |: z; v' l4 l2 U4 Z//--- An auxiliary variable for opening a position
) m$ U# b9 S5 E#include<Trade/Trade.mqh>1 Z* z+ ]( G7 g
#include<Trade/SymbolInfo.mqh>
( r( o# R" f6 s7 F+ h" j  CCTrade negocios;
" o7 Z! b& o1 o) v+ MCSymbolInfo info;7 Z/ q- V/ ]5 `  p
//--- Define in OnInit() the use of the timer every second, l6 m* E0 C. O$ N( Y
//--- and start CTrade
' W, |% T8 X  hint OnInit()  o% N: p; ]/ @) k- I4 Q9 }3 k
{
- d. \* k1 N8 B: B2 q% _3 \) D//--- Set the fill type to keep a pending order
) \; ]( L( w% w3 o2 W7 f. H//--- until it is fully filled
" r. H5 F/ Z2 B6 l/ X: R8 [- U  gnegocios.SetTypeFilling(ORDER_FILLING_RETURN);
! D  a, Q2 t0 ~//--- Leave the fixed deviation at it is not used on B3 exchange# V$ Y" c2 K* O' L4 Z/ A8 y& h
negocios.SetDeviationInPoints(5);
3 L2 t6 L' z# a9 r/ D//--- Define the symbol in CSymbolInfo...
3 B! U* R8 Y  d% Zinfo.Name(_Symbol);
: ~2 ]& T: ?3 {1 G//--- Set the timer.../ k+ r* y# @6 W8 I2 t/ @
EventSetTimer(1);
! D% R3 s7 N4 r% N//--- Set the base of the random number to have equal tests...) F6 @, e/ f$ v) s! e$ A
MathSrand(0xDEAD);
- U, A7 S5 _1 @$ ?. l1 r4 d. Areturn(INIT_SUCCEEDED);
  ~1 f# h) `8 O' o/ O}
6 j6 U8 D, L+ Z( k- g//--- Since we set a timer, we need to destroy it in OnDeInit().
: n: U( y6 B& p, ~, ~void OnDeinit(const int reason)
, g2 S7 H5 J! X2 N4 C& e$ v6 [{
7 v; U, c3 c$ d$ r, z$ a, j7 sEventKillTimer();
( r5 c, d8 U- g5 E, Q}
0 Z" t+ I) H+ d  N//--- The OnTick function only informs us that we have a new deal; @' y. A# p+ b
void OnTick()
% o! e, L9 z$ |: ]/ {8 ^5 k{7 V* y; ^$ j# E
tem_tick = true;
0 I* M. \2 H& E' ~: C}
7 S9 A  _& k4 E, s//+------------------------------------------------------------------+! m% |# ?, i# m% L
//| Expert Advisor main function                                     |" x" C6 R. p& Q8 X# m
//+------------------------------------------------------------------+" ~8 v  A$ n; K# f! v+ i
void OnTimer()# W0 h' N$ b/ _
{0 _5 W6 S7 R$ k+ u
MqlRates cotacao[];
/ F8 H' E$ y3 B. D8 ~0 ireturn ;1 I) f7 a, e' A6 f1 m
if (negocios_autorizados == false) // are we outside the trading window?
9 ?) F! y8 o! ]  M! Greturn ;2 l" m& R4 t6 V) x4 P8 l* n0 o
//--- We are in the trading window, try to open a new position!
$ N+ k  x( i! _7 W* [int sorteio = MathRand();# G$ d' W4 l" ?4 [
//--- Entry rule 1.1
/ G" r- N: a, ^if(sorteio == 0 || sorteio == 32767)
8 G& Z; k- b7 I  Dreturn ;
4 {" K6 d. r$ I, ^1 F9 aif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
  Z' c% s8 i1 R. y{9 s8 m; _+ V4 C9 X+ n" w8 I
negocios.Buy(info.LotsMin(), _Symbol);3 W! S; T% N- B( z2 S( K' \& P
}2 `+ ^6 X# r3 W% m
else // Draw rule 1.3 -- odd number - Sell+ p4 Z- E1 v7 D' d5 P
{# p4 c- K/ ^6 F/ q3 N  ^3 e, S; n
negocios.Sell(info.LotsMin(), _Symbol);$ U* C" m- X5 |1 P9 X! B5 m1 D
}
0 {  F: ^4 _5 w}* s& n, P  e* i3 g4 b5 L
//--- Check if we have a new candlestick...
2 O) N6 ]9 [+ b" ?6 o5 U- Kbool tem_vela_nova(const MqlRates &rate), G# K5 h1 B6 M! S
{  Q( }1 p, T0 t5 S0 g9 J0 S
{1 F7 p% k& m  L) O8 r" ~+ M
ret = true;
; v' D: r) l! T( l7 e$ z( rclose_positions = false;# R; o* w( T8 V( q
}
- M  t3 N+ t* X- Ielse
& I+ K! a: c, g' m" t3 v, W{% ]. n, y" R! x( K3 W1 t8 ^
if(mdt.hour == 16)
! I( ^* h) H, Y- W. x; U- Nclose_positions = (mdt.min >= 30);
: T( k: a8 h/ r/ H}
! {" M/ @& F2 t! f. _2 x( O8 \% G}
/ r. u- _2 n! G8 z) u+ Qreturn ret;
* `- B, `7 i3 e, S; Y( a}
6 X* f$ }8 m) s/ Z//---: l7 {6 M* ]; {5 F6 W( t, e# R2 h
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
, _& s2 N0 O9 n, b% v{
6 r4 _& e0 S4 }4 X4 J' c2 mif(PositionsTotal()) // Is there a position?
  n7 A8 n2 v$ k( c+ v3 }{( g; H! ?5 J/ R6 ]" W
double offset[1] = { 0 };/ P1 s: \& F/ C6 w( x: a  [4 g* r
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
7 M* W; X+ F* C- G&& PositionSelect(_Symbol))  // Select the existing position!
# e/ n: `" r; u" f9 s{  K" f4 P  @! y4 q$ \
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
" ]; {5 I7 B& ^$ }double SL = PositionGetDouble(POSITION_SL);
6 M" X" Z" q3 X& W; k2 ^8 l, i) rdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
+ H* N" _+ V8 w/ x4 C# i$ s* iif(tipo == POSITION_TYPE_BUY)! ^0 G$ k6 Z+ M0 T
{
, s9 `. }/ S2 N: Qif (cotacoes[1].high > cotacoes[0].high)
3 t) [! M4 k9 Q& z# y/ ^{
. a- }. O- l1 e+ ~- F1 O1 w2 R/ ddouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];* o7 L  y9 s1 Z% K
info.NormalizePrice(sl);
, d& X; d7 O9 L- w" S+ Wif (sl > SL)
$ j( j! B6 m0 s# u! D% K; R; j{
# b* T( K# F6 X/ Wnegocios.PositionModify(_Symbol, sl, TP);
. n2 y! ]& I8 F6 ~}$ k4 B% u$ k/ ?2 |- F% s& j
}4 Q- s$ E; L; J
}" h" [" r# D7 V4 d: H
else // tipo == POSITION_TYPE_SELL- G6 |8 t! @: r2 \2 r& }0 U/ n/ A
{6 s% |! q" J$ n+ ]4 Q; k6 k( Q# n
if (cotacoes[1].low < cotacoes[0].low)
$ C( C/ m' g. s0 a3 e3 O{
7 Q9 {1 E* C+ W% D7 s+ W: Hreturn true;
0 M; s6 J) U8 }3 A2 S}
& K% Z9 U% Q; ?0 e0 A// there was no position
& C/ t* ?/ n, H2 z# r. preturn false;5 s2 K' g: T9 Y2 m9 |
}
1 L1 {. O( ]/ t% H- l我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
# R- r9 T! b2 H2 ]5 d  X到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-21 00:37 , Processed in 0.406223 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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