私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
% k' d; |( m+ ^, f在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
" S8 [  w4 w3 G% w6 a6 ?# C% K9 n为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。" x+ O# H9 O# Y7 L3 ~7 n" v
以下是制定这些规则的代码。& ], q( j+ g$ h$ \
//--- Indicator ATR(1) with EMA(8) used for the stop level...
; d9 Y4 G# U# k0 V+ L$ B! d2 F+ sint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
# x. r, n( N0 yint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);7 u1 c* F( J8 Z2 @, I* a. I
//--- Define a variable that indicates that we have a deal...
2 Z7 B( M, h$ A; Z5 m8 R* F% u' l3 Gbool tem_tick = false;, H1 y4 ~/ A% m7 t7 P
//--- An auxiliary variable for opening a position2 V; y6 s! P9 ^* ?
#include<Trade/Trade.mqh>/ {3 P' B1 Y' Y; q* q4 g3 G
#include<Trade/SymbolInfo.mqh>( [* t+ c9 |1 l) B5 Q
CTrade negocios;% o; O& }, F9 e/ U, `4 y
CSymbolInfo info;' h4 v% G7 K3 d2 Y, |
//--- Define in OnInit() the use of the timer every second
) G0 A1 P/ O5 ~0 `3 N+ o% a//--- and start CTrade. A- x. S& M4 y  i2 P
int OnInit()
! ~  h2 O  d7 B1 m{
( u  `5 m8 N+ A; o//--- Set the fill type to keep a pending order
  Z& r# H$ @! @! V9 q% z//--- until it is fully filled% t% Q) g! B! F, R# W% ^/ M& G
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
( J. I, d3 j' J//--- Leave the fixed deviation at it is not used on B3 exchange( N- i+ }5 [4 q8 q
negocios.SetDeviationInPoints(5);3 r; V5 Y3 w' S6 \- J  V
//--- Define the symbol in CSymbolInfo...3 o' W( Q3 e5 d7 h% r9 A0 z; y
info.Name(_Symbol);
! [) A, G1 [4 s) v: T3 D//--- Set the timer...
+ e# R4 w5 z, [3 T( j9 iEventSetTimer(1);+ H/ {: F- i+ E/ ]
//--- Set the base of the random number to have equal tests...
" V; c4 @( b5 P* L# n- iMathSrand(0xDEAD);
- Y/ b3 b8 Q: ?- i: R- Y6 breturn(INIT_SUCCEEDED);
0 e2 u- M8 F- F}
* o6 L# T5 ~3 D* T1 s//--- Since we set a timer, we need to destroy it in OnDeInit().. m; V/ B/ _0 r& q8 g+ }/ h
void OnDeinit(const int reason)
- c4 {  Z; l+ k& ^' t# J{
$ K! \) x" I( }  M% REventKillTimer();
& e/ A0 W; G5 L7 f& B* F" l}
! U& a% s: A! Q5 ?9 U" N//--- The OnTick function only informs us that we have a new deal' |4 w3 `0 m# r4 g* j5 S- |' \
void OnTick()5 A% k6 y: j; k2 N& o2 O
{0 o; ]3 ^+ X! P- f# ]( l. Y
tem_tick = true;
; c1 n5 P# |9 ~3 i" l  M: @}
2 F( T2 l( D; V' C# L% d. a# K; D- T2 m//+------------------------------------------------------------------+
5 ?, W* i: ~8 S! U; ]! N8 h//| Expert Advisor main function                                     |
2 q0 {" F! b- i. @! q3 u//+------------------------------------------------------------------+
. |; K( w8 ~3 p6 W4 nvoid OnTimer()! {) S( O6 {& O! x0 l+ h# ]
{
7 U* m: ^( D# E7 x& W" f  \MqlRates cotacao[];
# ?$ Z6 }2 h9 u7 h4 Lreturn ;
8 z/ u" l3 R, N/ tif (negocios_autorizados == false) // are we outside the trading window?5 b1 i$ |" {1 u: a! q, r& L/ w- A+ u
return ;  u0 B: `5 \) b4 M0 @* e4 e
//--- We are in the trading window, try to open a new position!
9 \( O, M4 W. j8 C; n; f8 s. U2 iint sorteio = MathRand();. x, I+ e/ @- _1 a
//--- Entry rule 1.1
1 J( c5 |0 E/ ]8 y5 [8 f0 ]! tif(sorteio == 0 || sorteio == 32767)
# w( S% T/ p! I. E" _return ;$ r/ ?) S2 j6 k! W( h  H
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
6 F& `& F8 n) {& q: Z+ q# [{0 L7 |4 ~" `# S/ ?( a$ q8 u
negocios.Buy(info.LotsMin(), _Symbol);3 t2 R! l+ H9 L( t# N- s1 j: D1 ~
}
! n5 B; N  o8 q, v7 Delse // Draw rule 1.3 -- odd number - Sell3 n, y% k. N) w; C$ }
{
/ W7 d' b9 |2 {0 p9 a, A2 \negocios.Sell(info.LotsMin(), _Symbol);' t5 ~) |- Q7 k' d
}
  t2 F" w* H* V, d5 K% J- s}% l5 }0 ~7 x& Z0 i1 \- n( G
//--- Check if we have a new candlestick...
& Q1 |$ J( P0 c  W' cbool tem_vela_nova(const MqlRates &rate)' F: A. t4 a  U0 z, q
{6 h/ I# Z1 P8 ]7 i) Z
{
2 u: H+ b$ \( Z4 F$ Mret = true;( V5 U4 [3 i& c
close_positions = false;
% k: f7 X, p; |+ h- V6 {}
5 a* s. [3 [  K0 F8 m! G$ @  d. celse) Z+ l! Y& g) v; \3 Q
{  U: ~' b! I" S) q/ i( |
if(mdt.hour == 16)
9 ^, J# I6 H8 c4 Lclose_positions = (mdt.min >= 30);
! n9 J) }- s2 z/ m- \- \}
9 O# n, F3 c4 l( E7 N}
9 L! _8 S' z, z  E" Z% ]% [return ret;
2 p9 `/ n( h8 i2 b}/ e# Y% A& a3 G* G8 `9 ~; q1 w
//---) F* r. w$ `( u1 o: K7 b4 I
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
, Y' p0 |/ j  _# f{$ j7 v# O* W* }( B4 H5 Q
if(PositionsTotal()) // Is there a position?
6 ?6 p1 U: X3 q+ ^, ^$ q/ k: r{
2 R" E6 N2 `' u3 Vdouble offset[1] = { 0 };
: @' u: u4 H4 n+ i" b0 ^* kif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
6 [# i- {. ^/ g; J: ]* r&& PositionSelect(_Symbol))  // Select the existing position!
7 C, B5 ~' C! i* f/ _+ H{
7 d+ Y" S% O' \" u( H1 WENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
: m! _7 C  B3 O! v7 U" i7 ?double SL = PositionGetDouble(POSITION_SL);8 Y3 [" G0 z9 l4 v5 `, ]# {5 I. ]
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));- M% r) N: v! e
if(tipo == POSITION_TYPE_BUY)8 M9 J" z9 A; M$ j+ T
{. Y7 C) q9 F' C" @; u" F# u
if (cotacoes[1].high > cotacoes[0].high)
- i( {; F! }7 l& J% V{5 |: i4 c* i. H* H
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];: n/ _0 q1 z* I$ R: M
info.NormalizePrice(sl);  v6 K( J. Q- a& E6 u7 ~; t" }
if (sl > SL)# i1 y: U& @/ h7 Z
{
% a/ z4 \7 v! o" S# b7 t/ Hnegocios.PositionModify(_Symbol, sl, TP);$ G! B- H) \( e& {
}7 i+ G0 a2 `- M  g2 c3 T0 J. A
}
" Q. @6 Q+ T4 \" t! x5 p7 ^* H0 s}
( Y' m2 Y  i& Kelse // tipo == POSITION_TYPE_SELL' e# w  w3 i3 v
{
+ f  ~) U1 X. p+ Kif (cotacoes[1].low < cotacoes[0].low)+ M) [  h. E+ _& C
{1 Q7 J/ i3 ~( n: A) ]$ Z8 m+ Y" w
return true;% w  S, ~2 A+ u+ f# z
}* y* C: |  q+ r! l. X3 U
// there was no position
3 g# v9 y, |( m: Y: hreturn false;
; D; U6 P& U  O5 a# ?) }) @6 O6 G}+ s7 O( \3 s. P6 k
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。- V& T  M! {7 }# d4 s7 C
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-4 05:40 , Processed in 1.528300 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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