私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA, s  `* _! @$ L8 |
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
. x. }, N3 {5 C为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
! [7 f9 U; e9 {) K以下是制定这些规则的代码。
2 u7 b7 }& @9 b- {//--- Indicator ATR(1) with EMA(8) used for the stop level...
9 c  ^3 E- s$ s8 uint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);/ K5 |  {# k4 W3 D- }9 Z
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);# w7 i6 Q# }* F- E8 ~7 a! |
//--- Define a variable that indicates that we have a deal...% [2 \% I) r# p  D; d& t/ ^/ e# l
bool tem_tick = false;$ `0 e6 }: f0 f0 u; W$ w  e7 H
//--- An auxiliary variable for opening a position
( T3 w; ^! X" }" p. a. y#include<Trade/Trade.mqh>8 K" e7 K' a  w" ]# A
#include<Trade/SymbolInfo.mqh>
3 ~3 x# F, u: l* R$ k" [; e* s, A- f: |CTrade negocios;
' p; V; g$ c$ `0 b8 a6 d" V4 M& ECSymbolInfo info;
- W6 ~  I* K3 I- g* O: A//--- Define in OnInit() the use of the timer every second7 c1 M$ k% m- Q1 ?
//--- and start CTrade
1 I6 D! x( ^4 iint OnInit()
( d  M. V+ Y5 v$ b{
7 Z; h  l2 A' g' Z- |( X1 |  Y//--- Set the fill type to keep a pending order
+ E- _# v/ u3 S- u! e9 [1 K//--- until it is fully filled
. Y& c+ t  f0 mnegocios.SetTypeFilling(ORDER_FILLING_RETURN);
# T* U* H7 V( J) p8 F, t" v//--- Leave the fixed deviation at it is not used on B3 exchange
" k/ @- P, }2 b% a% M4 s5 Anegocios.SetDeviationInPoints(5);
7 {5 \; d$ f0 m2 b" ?+ ~. ]//--- Define the symbol in CSymbolInfo...5 Y' c" a, h5 m6 R3 A3 m0 ^) u5 @
info.Name(_Symbol);
! j1 F' ?* |0 }% }//--- Set the timer...
. R% M/ e$ T( b3 W: `! s- V' g, XEventSetTimer(1);
4 {* A7 m. l* S' b- T. x% k) v//--- Set the base of the random number to have equal tests...  g/ e! B; c  l4 \+ C
MathSrand(0xDEAD);2 V/ ~1 Z( S# F7 `  `# y) `
return(INIT_SUCCEEDED);. ]7 U2 J: r. {0 A6 J6 O1 g
}5 O2 v* ?' O- Z
//--- Since we set a timer, we need to destroy it in OnDeInit().
& {# P$ v. |! V7 a0 r; bvoid OnDeinit(const int reason)
7 l1 Z# g8 P# K) U; s{
& p/ g  L, v) q, hEventKillTimer();" N9 G/ k8 J& U* l
}
: a: b& _  W9 A& `//--- The OnTick function only informs us that we have a new deal
. @; w' Z$ C! @, `void OnTick()
, A7 X4 b$ Q5 f{
  n8 I9 ^1 C4 @tem_tick = true;
) e# d. o' {1 i  [}0 m) Y' V0 M" X- V3 i* L+ v+ K
//+------------------------------------------------------------------+
' W8 u7 a8 N7 c2 I3 V8 v//| Expert Advisor main function                                     |  ~' V5 P9 A# |5 W
//+------------------------------------------------------------------+/ q5 C, A. f1 c3 v7 ]) h. \( I
void OnTimer()
* ]+ l  ~4 U" K8 A& B) c3 h, {{
" j3 E, E2 c( `& ^1 `% B# @MqlRates cotacao[];
1 Q6 x3 I( ?1 @* t1 |9 ^return ;
& i* n+ w1 q& _* Vif (negocios_autorizados == false) // are we outside the trading window?8 b2 W( `4 L4 I0 h
return ;* g- I) [; D8 Z5 s  R3 `
//--- We are in the trading window, try to open a new position!+ t5 G# F* }' G2 j3 T
int sorteio = MathRand();
4 h+ H6 d3 K, ~4 @2 }//--- Entry rule 1.17 _4 Y: ?# P+ X. [
if(sorteio == 0 || sorteio == 32767)  l  j$ C& o! B' k" D8 b
return ;; U" c3 G0 r2 e
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy& A$ i9 u% \, W7 K
{# H# Z5 r6 y3 }- N/ l/ l
negocios.Buy(info.LotsMin(), _Symbol);# t8 u6 ?! |; ?$ r) c
}5 D, e2 k2 B; G  x: k0 Y
else // Draw rule 1.3 -- odd number - Sell
% ~) U* U1 d( z1 M{+ U! t; H1 w1 [; O0 C8 o1 Y$ d
negocios.Sell(info.LotsMin(), _Symbol);6 k3 J5 d+ Q8 A# N" ]
}% ~! H% T# X+ R
}
3 N9 u. W* J( I9 h//--- Check if we have a new candlestick..." b3 a( c+ J# h2 q
bool tem_vela_nova(const MqlRates &rate): j: e# e6 y- |: W, @/ f  h
{
. d; h9 m. ]: M! T) s{+ R/ F* B, o, x: a6 G
ret = true;+ w8 ?+ a7 s* B$ P+ L  t: N/ f/ L
close_positions = false;
, N& j9 b; `9 }}1 q$ O: J0 O; e- X
else! X4 W8 i/ V% F6 x, Q0 T: H
{/ w1 G( ?( `: ?
if(mdt.hour == 16)7 w6 Q" _5 |/ R+ d! P2 r
close_positions = (mdt.min >= 30);! u0 b- t: `3 u
}
2 r8 @- P$ i  X( q! u}! d2 n; m' i( m, b" r; Y
return ret;! E, l( E4 X' a/ x; O: f  p  t
}
, A5 F# Q& i( d  y( e1 |//---' Q  E% z, y. W  G# C+ W0 {
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])4 y  E; x& R1 f, q3 ~) n
{" C8 y6 F! p: W) x6 y' w% n4 [
if(PositionsTotal()) // Is there a position?
" o+ B) a& [- x# A7 l4 s: i' W{
. p, m# ?; x6 N1 x8 m  F  Ydouble offset[1] = { 0 };
% f. s8 `: x, }( Hif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?; ^* |/ I3 z4 ]8 \. T) b9 @* _
&& PositionSelect(_Symbol))  // Select the existing position!
: g+ v. A' G) {% g" _8 c0 Y{& J7 G. F/ L# D- F% p" y
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);. A3 o; b0 [7 |2 X0 a
double SL = PositionGetDouble(POSITION_SL);  a( h) ]# z! f9 J$ z
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));$ t/ u. \& c* I
if(tipo == POSITION_TYPE_BUY)
6 M7 P+ ~. M8 Y6 j' x{
9 `3 ]. |0 s3 ]% M2 p! Q5 rif (cotacoes[1].high > cotacoes[0].high)( p$ e* H) g  S# p4 t& z
{
# N2 r$ }+ p! T# M0 g* x9 Ddouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];8 k! J/ n- `7 _1 o7 d+ x7 U
info.NormalizePrice(sl);9 w' B$ N. c7 n2 I* f' Y& `  \
if (sl > SL). D- C' o% B9 H) U( j8 A9 d' j( t/ V
{
( K( z& f! H* O7 {$ k6 V- onegocios.PositionModify(_Symbol, sl, TP);; D' b$ C3 U# ?
}
1 Z  Z( O7 N5 H+ Y$ z+ u; S1 n}9 v7 W! ^0 a3 I, }
}
2 c- V' d1 a  T, @/ Kelse // tipo == POSITION_TYPE_SELL
/ f- M; J- g  S, O{
6 U* w) l! v$ a& d/ K! Fif (cotacoes[1].low < cotacoes[0].low)0 y1 `0 e' e' \
{" k! _# h/ R  {4 p
return true;' }, [0 ?# `1 h- {$ F( m
}5 J- L- E3 h. c% {9 Y1 Z$ F
// there was no position
) B0 X' e" ]# z% Jreturn false;: I" b, U5 r! H! J4 d8 M
}: a+ C# w  c" L9 |
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。5 l& @& ?# j6 f; b" s6 [5 B3 R
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-12 06:15 , Processed in 0.433221 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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