私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
( Z/ A! G& U1 t8 {6 F在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
7 e8 j$ @, {$ q. c4 A7 V  L为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。5 Q* G) X9 F+ ]
以下是制定这些规则的代码。
; N: l& {' ]: M: q# g& }. X) a//--- Indicator ATR(1) with EMA(8) used for the stop level...; \1 H* u/ L4 i  N
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
5 D5 H4 v/ D) z+ u2 }int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
5 O; |' r& Q1 a. {! X( ?//--- Define a variable that indicates that we have a deal...( @$ n/ K$ z- u; J
bool tem_tick = false;
$ V& M5 W% g2 ~" H+ r//--- An auxiliary variable for opening a position; J+ H6 {" Q3 B5 }
#include<Trade/Trade.mqh>. a! [- X) g, ~8 ?( a$ b- i
#include<Trade/SymbolInfo.mqh>
( M& Q% [" j* P" h' }7 X' ICTrade negocios;0 e* z, `4 e. j* f* T, d, @
CSymbolInfo info;
; Q$ y5 {# y- S' ^& d2 U/ Y//--- Define in OnInit() the use of the timer every second
+ o' }' J1 R$ q//--- and start CTrade
  ]4 Z; T3 w7 _5 gint OnInit()
4 l: @# P  C) a* `; G. Q{9 x0 z$ s+ n! e5 T; q
//--- Set the fill type to keep a pending order
/ `' |: Z' F) D; Q% K//--- until it is fully filled
$ u% Q, }/ Y, n) R9 W, E# T3 bnegocios.SetTypeFilling(ORDER_FILLING_RETURN);% d- @2 U" b8 |
//--- Leave the fixed deviation at it is not used on B3 exchange
/ I- o6 e! O* x( Snegocios.SetDeviationInPoints(5);1 Y) s/ S! |5 R8 g
//--- Define the symbol in CSymbolInfo...
4 E8 G1 y' [/ Y; `' ]) D' [info.Name(_Symbol);7 T, \( I$ k( D$ d, {
//--- Set the timer.... V9 E$ y5 Y7 @! m
EventSetTimer(1);
* V$ b9 f7 Y3 u+ X' Y9 M//--- Set the base of the random number to have equal tests...
( x9 A+ |' N: U0 T1 G. aMathSrand(0xDEAD);
  y/ U; p. J) P, `& dreturn(INIT_SUCCEEDED);
6 L) i2 J( `  J: j. M0 r}
5 m$ t; \+ n. U! Y' L1 r//--- Since we set a timer, we need to destroy it in OnDeInit().
5 h, j, `( a, [4 Yvoid OnDeinit(const int reason)! A4 \2 H0 Y* d8 `! h
{
, Y$ ^1 e  R* N8 q# TEventKillTimer();
2 {* ?* k. C* Z3 Z4 r( o}
: d$ I+ Q$ @' M! y% f2 R//--- The OnTick function only informs us that we have a new deal. ~0 b5 J. K7 F2 J4 }9 C# t
void OnTick()* B5 d; O/ p( j, \. f
{! m4 E. N/ ~/ O3 O; m1 ^! e
tem_tick = true;; y, N& O  Y9 q; L7 Y
}
  j: ^4 n. c' p) B//+------------------------------------------------------------------+
- f; v4 `! a$ z: l9 k8 b5 d4 ?//| Expert Advisor main function                                     |4 K: o% d7 K) t7 j
//+------------------------------------------------------------------+. A& @  W! r$ x6 F
void OnTimer()+ F. u. Q4 g( P% t/ h2 u
{3 M: s! J& j* P1 `4 X) V( ^
MqlRates cotacao[];
$ p3 G* i$ H2 Lreturn ;
/ ]' d. J' n3 zif (negocios_autorizados == false) // are we outside the trading window?
  {8 u: Y- b) {# b& F& D. y+ Qreturn ;# k& b7 a/ H, N7 q% j
//--- We are in the trading window, try to open a new position!. q' H+ S" C  R& L5 t
int sorteio = MathRand();+ r8 k8 V, J. l+ A/ a
//--- Entry rule 1.1
; H+ B' p& F. p7 W+ T/ f# b$ Eif(sorteio == 0 || sorteio == 32767)  K" y( o$ _( x: w
return ;9 d1 ]  {% c' X
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
* _4 C4 i$ s! I$ w{' \; C- s3 v! _6 Q* E
negocios.Buy(info.LotsMin(), _Symbol);
8 k; o) R" U) b2 Y* x; x}2 `& F& U2 Z2 Y% C. o( T
else // Draw rule 1.3 -- odd number - Sell# K7 a; R' h2 m( `
{
' y; P; ~# s: b* A' e7 P+ Q! ]negocios.Sell(info.LotsMin(), _Symbol);- _: u; `% g' b) Q, x
}
" p3 ^- g7 [. i9 j3 ]: i}' ~% E  W( m; T! S: Q- q/ d
//--- Check if we have a new candlestick...
6 y2 x* Q7 c+ U0 a, r$ b  x. R% ybool tem_vela_nova(const MqlRates &rate)
6 l' M" F8 l7 r2 T" P{1 l: @8 F& G" R9 }" z$ r9 ^
{
/ \( |1 A0 U& uret = true;$ \0 Z" Q* a, l; k# g
close_positions = false;
3 e) v+ O. E0 w) V2 q: f4 @1 ^8 s( S}
8 q: r7 e9 \& N3 h7 B  P) oelse3 E0 G  }7 E9 b0 @  u
{
% K& s5 Q8 g% l+ ?! X. xif(mdt.hour == 16)
' L) {/ x6 X/ E0 y1 X4 }, u$ _close_positions = (mdt.min >= 30);
  T1 B" S+ X- N" F  s  F7 E2 E}
" _+ m8 m2 }; Q( m( r}
2 D" N* s. Y+ I% j6 ~/ f# Nreturn ret;
* l0 G! |5 U( r- s  ^* R}/ e4 k' u2 e8 g
//---
- @# Q. T0 R4 A* @( B! y. obool arruma_stop_em_posicoes(const MqlRates &cotacoes[])4 t! `) O+ \. d/ u0 p; d* K
{
0 w5 L9 z$ o: L/ ~if(PositionsTotal()) // Is there a position?( {8 Y8 z) N1 o6 G4 Y7 N1 e4 J
{1 f" K9 _9 F9 g
double offset[1] = { 0 };/ t5 ^' y8 `9 i" ~6 f4 J! b
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?8 I  M4 k2 o  |5 Y+ j
&& PositionSelect(_Symbol))  // Select the existing position!. `$ j; i1 u! _: A6 y) b, c5 i
{
+ Z; _5 X/ g6 U( X& H' f; q6 r' SENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
, X  O3 P! t! [( e$ t7 Qdouble SL = PositionGetDouble(POSITION_SL);" I$ h# L; k; W5 k0 |7 d( a6 F
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));! M/ [$ \5 P9 x5 a9 Z" k0 a, a" i
if(tipo == POSITION_TYPE_BUY)
- L+ D6 p( g3 ]( X3 l{
) e: a9 K9 B8 I8 i0 J( Qif (cotacoes[1].high > cotacoes[0].high)$ g' F9 _% ~! S4 N
{/ L9 a, ^2 Z; d/ S0 o) d
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];4 D! r$ y% C$ |  t2 s
info.NormalizePrice(sl);) x, q! G" O$ j" a( h' L% E5 Q
if (sl > SL)$ z" J% P& @3 a+ M/ B0 K+ f/ z
{
* {8 H& w% M) Q: Y: Y( m) Ynegocios.PositionModify(_Symbol, sl, TP);
# N4 H$ S. \3 r9 X& e}
1 C* Y0 j; G( r* r( |1 S& x}3 ^) K6 v# P  o9 p' {( O
}
% }$ I7 ?# D1 [2 {5 L  J, melse // tipo == POSITION_TYPE_SELL: d4 X/ T% @0 B; R4 h+ X1 ?( U7 t
{
8 E9 C% ]$ F: Qif (cotacoes[1].low < cotacoes[0].low)
, s9 u* m5 _$ W+ t5 H{
0 z0 z4 A- H* G8 Xreturn true;& _9 {" z- @7 z5 X( W5 [
}. ~$ k4 j: w& Q. N' E
// there was no position
' m' q5 x/ t5 e# h. a6 N2 Sreturn false;
: x# q+ u+ B" x}
% R: H7 f8 S! ^我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。1 b, J/ j6 K7 u1 v: C
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-19 21:41 , Processed in 0.551320 second(s), 32 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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