私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA* l' R3 l% w4 W" k9 Z7 i
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。+ a5 r( l+ b: L# V$ U3 t
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
# p& d$ e4 s: Y/ G) F- r9 A/ m以下是制定这些规则的代码。8 Z9 q$ ~6 u/ q0 u5 y- H- m1 g
//--- Indicator ATR(1) with EMA(8) used for the stop level..., y+ }  y; u* F% y( K* E
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
4 H0 k2 j3 B$ }4 Qint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);( `: U2 P) {6 f' a& X$ J0 P) Z- g# F+ X
//--- Define a variable that indicates that we have a deal...' x3 q8 n" ]$ _- v0 w! {
bool tem_tick = false;: [) E0 [2 |& c" `# Z9 t8 ^) P
//--- An auxiliary variable for opening a position
6 O( Z# i% R, @; {. c! h0 B1 g#include<Trade/Trade.mqh>
! P# Y7 e( X( }" i3 v9 }, a  A" N#include<Trade/SymbolInfo.mqh>
) D% E6 f# ~3 i# A" |  C! V& H8 dCTrade negocios;1 o5 @7 m: ~( S% ~: Y  r
CSymbolInfo info;
9 C' P( Q# T& I/ x+ y//--- Define in OnInit() the use of the timer every second
% Y) f4 v: n% B//--- and start CTrade/ b4 Y( S, l( r* ], m5 f
int OnInit()0 j7 R% R& J$ _' K# p
{: Y: C+ d; R8 ~3 x" H+ j
//--- Set the fill type to keep a pending order
& h& p+ t* l& e* h//--- until it is fully filled: E: D9 |+ W0 ^
negocios.SetTypeFilling(ORDER_FILLING_RETURN);  Q5 x: J' ~% I: \. e
//--- Leave the fixed deviation at it is not used on B3 exchange
8 W  Q( s: E! Q6 E( C" @. Inegocios.SetDeviationInPoints(5);0 O( U# ?- E5 u; l4 C; p; h! D
//--- Define the symbol in CSymbolInfo...2 X& F! `3 {' O' Z
info.Name(_Symbol);
: U9 b& s. u+ h! Q9 m- ?9 `- L2 ?//--- Set the timer...% l2 @2 E! ^! n/ r. v" H
EventSetTimer(1);9 I, q  f; D4 x4 X* ^* @+ }
//--- Set the base of the random number to have equal tests...
4 a3 |) C* g$ C$ e8 Q3 u, ~MathSrand(0xDEAD);; v' B/ ~5 c/ j+ S# X1 n6 ]- U- O
return(INIT_SUCCEEDED);) P2 T4 g5 w! f9 l, d
}' A6 I+ }/ N7 {+ \; l
//--- Since we set a timer, we need to destroy it in OnDeInit().
% j: m0 ]/ C- Y. zvoid OnDeinit(const int reason)" G7 e* m3 {' u8 v: s( n+ F
{- ]4 j4 `' P" b, s) R) T
EventKillTimer();
& w. r! N' {; x1 E3 _}& U- `: Y6 ^6 `) l$ l
//--- The OnTick function only informs us that we have a new deal
5 ^" ?# t" G0 Ovoid OnTick()
9 ?8 `; B- A! ]+ j/ p- M( E{1 p' L# Y1 _9 u8 p- O3 ~' z
tem_tick = true;
- l: s0 T5 E0 @' \; A, Q) r: t}
# |$ V, r& D! e6 Y: _% m9 b- s3 ^//+------------------------------------------------------------------+0 @8 u- A' z& L
//| Expert Advisor main function                                     |
" f1 o+ ~6 F+ G. G" y//+------------------------------------------------------------------+
8 X: L; I8 h( v$ i: Z2 M- Rvoid OnTimer()
# D4 a: z1 ?4 i7 a{
. r+ E7 q: ?+ E; |$ H8 _MqlRates cotacao[];
/ z# C* J9 u- H) `return ;. c3 j; J/ {1 H0 b
if (negocios_autorizados == false) // are we outside the trading window?
  u, x: q, R5 t/ j& G, Breturn ;% A0 M; _0 {; \7 R
//--- We are in the trading window, try to open a new position!
+ g$ e0 O0 \, T# q  ]int sorteio = MathRand();
$ X+ o$ w# X- M//--- Entry rule 1.1# ?( s; C# q4 T! ]* g. I* T% f) o
if(sorteio == 0 || sorteio == 32767); G' l" o: D% s/ p
return ;
, |# n1 k& o* g$ o; |if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
& b4 T4 F; E* q' ^& ^! m{
( D; ^3 H3 Q, dnegocios.Buy(info.LotsMin(), _Symbol);
) X% s; ]- o- V' L2 \" c" L; i}
- l6 R8 W: G! C5 j& C1 \1 Selse // Draw rule 1.3 -- odd number - Sell
6 i* A3 W/ U, V8 }6 L2 \{
9 R+ \' U: e6 V9 L+ Knegocios.Sell(info.LotsMin(), _Symbol);
& [7 R" h0 T/ ^0 t}
$ E" T6 e( |, U& t& k0 F}
8 {2 ]" a3 e8 y. Q" c//--- Check if we have a new candlestick...9 j2 ]- q$ v. o4 a  [
bool tem_vela_nova(const MqlRates &rate)
3 y: h+ m; `! T. T{
$ g. o5 M. l4 d1 E{
+ b3 G( r6 k; g" y! a! Tret = true;
0 Y  J$ l" b/ ^1 oclose_positions = false;' ~7 G* |' ^# \4 b
}! j& [; e! a6 J0 T
else" f. v1 p# P2 w+ ?7 j  ^9 Z, @
{4 g$ b1 `, n/ P7 z$ H
if(mdt.hour == 16)* V4 ?/ K1 H: s: K+ I
close_positions = (mdt.min >= 30);2 \/ I* _; d* S  z9 [1 l
}9 B& K. z6 D* H$ z- P
}
; b9 @' Z7 g$ Ureturn ret;. p5 ~* u% ~3 l4 w
}
, y3 z3 O: a! S, L  o: J//---8 J  M" u' m4 g. _$ M0 s
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])& {- Y: z; T/ ?+ S& V+ n
{; i4 N/ S" ~) S0 D  }4 k
if(PositionsTotal()) // Is there a position?
6 r- y! h  u! x8 h+ N{" W) y7 T0 W( T2 T* ]" a
double offset[1] = { 0 };
" m8 P8 v- g6 y: r8 M4 }8 L. Y7 s* c3 {if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
' y5 e8 F' A, ^" L% U&& PositionSelect(_Symbol))  // Select the existing position!9 F  }9 g4 k$ T# Y- D6 U! O7 J: b; x3 a
{
* J8 @* A( r- s$ ^9 I/ \/ Z$ X( P* RENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);9 e9 j4 N8 R; w* s, n7 F
double SL = PositionGetDouble(POSITION_SL);6 v' u1 t. W, H" o! D- b
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
$ X  f' x- n% Z0 g+ M9 a) B5 rif(tipo == POSITION_TYPE_BUY)7 _% \* ]* q7 C0 a5 S9 [. ?
{
7 ^/ W( {/ N9 S! G: m6 Cif (cotacoes[1].high > cotacoes[0].high)( O) _9 Q: N0 p# C' U
{
% d/ m) g$ I$ n$ L$ z0 V& Edouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
8 Y8 C! P" \& ], Tinfo.NormalizePrice(sl);
& H; {# ]1 [7 O3 U7 Xif (sl > SL)
6 x, Y( h! R2 w' T3 }{* ~/ {8 s5 @5 V6 M* v
negocios.PositionModify(_Symbol, sl, TP);) V' m: W  _: U' z2 q: f3 `3 e7 R
}
& o" s# w. I! S3 ]$ N8 I}
3 V* o, O) d* ~}+ t& p1 m  r; M+ Q( ?! a
else // tipo == POSITION_TYPE_SELL
; o9 Z8 t8 s$ o$ O& T4 ?% n{. i& {- s4 r1 Z- ]" v7 g
if (cotacoes[1].low < cotacoes[0].low): p- r! p' X7 V. v
{
# l) d# f+ }1 X0 J5 treturn true;
/ t9 d7 K4 s3 w0 w9 S3 ~}# k4 b$ u/ l# L. T5 m
// there was no position# m1 \; F/ d8 |; M  L6 T7 w8 [7 g6 e
return false;
! f7 k. x; I3 D! M9 d9 g/ C}
) a# x* n0 s9 j: h& {& V1 G6 B我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。, [$ x5 D2 _1 w+ E, g$ G6 K
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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 23:52 , Processed in 1.685638 second(s), 32 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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