私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
& c2 p7 I7 Z5 v! y在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。' X. Z) h+ Y( ~. k$ g, b' e  P
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
8 v% c5 b: J6 r! B6 W, }以下是制定这些规则的代码。
. f% m! F. |( a' I4 p' N. a3 _//--- Indicator ATR(1) with EMA(8) used for the stop level...# U7 w: T& G7 ]
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);2 M  d$ N' n" T2 _0 R/ N; C, s; g
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
: R3 v# B. @" ?) S0 Z//--- Define a variable that indicates that we have a deal...
' n. ^1 M4 Z/ F& g  S) E4 V+ Ebool tem_tick = false;
. O& }  Z4 `5 G# q7 F# m; G8 N//--- An auxiliary variable for opening a position3 S7 e: w" g# j, A4 e% z  N
#include<Trade/Trade.mqh>+ O# y: D$ [3 z7 x
#include<Trade/SymbolInfo.mqh>
) @$ D% x: ^7 b: K+ O4 gCTrade negocios;
9 v, q( g" a) q. M: bCSymbolInfo info;
3 D, d3 h. _) ]4 Q# ~) i  _//--- Define in OnInit() the use of the timer every second3 i/ N5 a: a. s$ O
//--- and start CTrade7 p" W) W! |: h9 q/ M
int OnInit()3 e  x/ Z" M  k6 @1 Y
{
: u0 n8 _0 Z2 m, u//--- Set the fill type to keep a pending order: Y4 @3 P8 r' a5 v1 ^
//--- until it is fully filled
2 i# f4 U% ^/ [negocios.SetTypeFilling(ORDER_FILLING_RETURN);
0 N3 ^$ M8 r1 F& v//--- Leave the fixed deviation at it is not used on B3 exchange
/ Z& v$ E! p, q" o5 b8 E( @- x! ynegocios.SetDeviationInPoints(5);) _9 {# Y- x$ D3 v7 R0 ^5 ?# w9 Z
//--- Define the symbol in CSymbolInfo...% I) R7 s2 t9 M* R3 t
info.Name(_Symbol);
9 S* o% S$ _: X! A3 }//--- Set the timer...1 o9 u/ N1 I1 C5 X- R% V! h6 C
EventSetTimer(1);
" o" _! O- J1 M5 F+ O//--- Set the base of the random number to have equal tests...
7 G3 B7 E2 v' D. W1 v7 ?& |MathSrand(0xDEAD);
/ c3 c2 W8 e; M5 a1 V  g7 r! s9 Q8 F4 Mreturn(INIT_SUCCEEDED);
! m- O, g1 S6 Y  b! {  P  W# L}
% f: u; F) u' D3 e. `+ e' ?3 w//--- Since we set a timer, we need to destroy it in OnDeInit().( H" a! G8 V9 c
void OnDeinit(const int reason)
# k& [; E0 ]% I  @8 j/ \5 o{, p; F$ ~. R! \. D: P
EventKillTimer();
) ~) {; e+ Q  N! k5 b}
/ [$ H& ?2 ?1 W) ^8 q//--- The OnTick function only informs us that we have a new deal
- l+ ~" W4 |! N1 q- m1 s' jvoid OnTick()
9 V/ H% ~& D! O: Q6 h/ d{
+ ~+ S' P* m- ^tem_tick = true;. O( i+ `+ X& K' N& e8 w: K$ I
}
) j. d5 }6 W9 z3 C2 b//+------------------------------------------------------------------+
  N  W$ H& M3 B# X8 n" j" f( e//| Expert Advisor main function                                     |
- m9 {0 }$ e3 e, U, Q% u3 Q//+------------------------------------------------------------------+, J& H$ T$ b. m" A. F2 v. N0 e/ ?
void OnTimer()3 ^7 {: t' X: ]2 o: f* w! a
{: [8 z# W% P) O  m( f( U0 B) e
MqlRates cotacao[];. M' x' m3 N# ~8 c9 Y
return ;4 z- e8 H+ [( O, O1 k3 m
if (negocios_autorizados == false) // are we outside the trading window?  L4 ?6 Q' W% x# X; p: |' k, S% K
return ;
* {0 I6 J& d* L# v9 \% ^0 y//--- We are in the trading window, try to open a new position!
+ I- `9 ^$ G1 T7 t3 D, ]+ iint sorteio = MathRand();% l$ t$ t9 w' }0 \2 l9 M
//--- Entry rule 1.1
! z' M) l5 }! b4 Q7 v9 d; `if(sorteio == 0 || sorteio == 32767)
4 x0 K7 X: c. t3 B. Z: Z' r9 \# U- nreturn ;" Q+ V! O, y4 v$ ?- z. j
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy: \$ f1 ^& ~( d3 n$ H; E9 t1 l
{
, J1 G% H, |4 Z$ u5 fnegocios.Buy(info.LotsMin(), _Symbol);
! C! e8 E& N3 S- O  R}
. f' {& w) P. S( w% F3 ]else // Draw rule 1.3 -- odd number - Sell
6 R  O; S5 p- I{! x, G* E' X1 i3 N' R
negocios.Sell(info.LotsMin(), _Symbol);
0 f. b8 E5 I5 g5 S6 Q}  Z" L: Z* S! n- G& a# v$ ]" {
}% E; R6 R6 l! I( c
//--- Check if we have a new candlestick...
# T7 r+ V% y# \# ^8 ibool tem_vela_nova(const MqlRates &rate)5 y/ m( r1 c- ~3 ]& J, Z. @
{
2 A, u) E5 d, d  P6 B+ n* N{
- K3 z; c9 n, N! R& d0 Iret = true;; M- t0 X& @9 ~' X8 N( M6 O
close_positions = false;0 E  x4 D% D5 D
}. @  t$ k4 \1 E
else
. Y) n0 b, A. b) p: i+ c{% @- g3 }8 X# W
if(mdt.hour == 16)* a8 `9 a6 }0 N( |5 T
close_positions = (mdt.min >= 30);
' U7 n) ]) _2 m% s, X+ T3 m3 J1 Z- T}, p1 [9 u) l: x0 u" T2 C
}
& N" J% T1 k' |/ h$ ^3 ^- H7 Creturn ret;' D3 K7 w* z+ ^) }2 Z) P. J9 _
}
. |) @* S% }- M* `2 j3 X$ {0 K: |//---
9 }" ?. u1 @, V/ B5 P) dbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
/ _/ r. S5 ~6 R- B/ a* ?{
+ i7 [5 z0 Y; i3 F, q9 Jif(PositionsTotal()) // Is there a position?& P/ H3 T7 a; @; _3 n1 o9 W" ~
{
- @3 T0 a( s8 R1 F+ r/ Ldouble offset[1] = { 0 };
" ?6 w5 [- G8 b; c5 F" G7 q# ?if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?# g4 r, I; s& d6 s( H( B
&& PositionSelect(_Symbol))  // Select the existing position!
0 U8 @5 t( q" @- R$ H# J1 ?{' M  ^; }' X/ K* q2 x) O' d; \
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
0 B: o; O% F, |; ddouble SL = PositionGetDouble(POSITION_SL);
0 t( }3 s1 F3 xdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
+ `$ Y; `6 p% b* _if(tipo == POSITION_TYPE_BUY)/ L2 i5 g5 y" `6 H
{) Q! ^& A0 n" F$ R: Q+ e* d! h. X; o
if (cotacoes[1].high > cotacoes[0].high)
& L- L5 ]: S. _{% l% w  @/ N* P. [1 n6 w
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
4 P9 Q$ |; K- h) S: Vinfo.NormalizePrice(sl);" m9 y, H3 L4 O) g& b  o4 [0 I9 ~
if (sl > SL)
' ]8 j  M2 w) H$ Q3 V/ s2 h{( F  g3 ~5 d; U9 S* q
negocios.PositionModify(_Symbol, sl, TP);" F! b. W, A$ C; j8 X( V  j
}
, W3 O& U8 I+ `' `# V}1 p/ w7 }# V. M+ q8 L& |/ z$ P% M
}
9 x1 X$ J$ |; C# [( Kelse // tipo == POSITION_TYPE_SELL
: a: w5 H7 f! X: w* n9 T9 z{
% E6 P3 A5 Z0 F/ cif (cotacoes[1].low < cotacoes[0].low)
' _0 o7 c" Y. S{
; `3 y0 H" L6 [0 n$ A0 K+ X. Lreturn true;
& _# [) l- V+ }7 N. c}
9 `' d- ~6 o- R" G& Y+ k// there was no position
7 s4 L: L, m6 B$ C" Rreturn false;6 V; Y  w: P; H" D2 k
}
6 f! Y) J, A9 f2 u% G我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
; y' C2 e( g; O到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-9 06:42 , Processed in 0.662164 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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