私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
! J/ I1 `2 q/ L& ]( O% d在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。3 M) g1 ]! C( c2 w( s9 O4 P
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
# \) i' \" U# ~  H5 }  o, l以下是制定这些规则的代码。& Y1 a/ l+ G! d" n) g+ B: J0 A
//--- Indicator ATR(1) with EMA(8) used for the stop level...
& e/ H: Z: a. Q3 ]# D6 i% yint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);$ y: `% ^+ q7 b2 C' x
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);' z# f7 W) ^- D" [6 o" w
//--- Define a variable that indicates that we have a deal...% V' D' W( j$ E7 o0 k. C: `, _
bool tem_tick = false;2 |  s" Q0 H6 [
//--- An auxiliary variable for opening a position
; _( n- i4 @9 r: y#include<Trade/Trade.mqh>1 I$ M. g$ ^' o1 j+ E, b4 }
#include<Trade/SymbolInfo.mqh>: q2 `8 g8 U5 M2 J& x; P7 W
CTrade negocios;1 [5 v! @0 s# r6 p
CSymbolInfo info;
% e, J- C" m$ ?# p2 e//--- Define in OnInit() the use of the timer every second
; ]+ h. N( {/ g6 H//--- and start CTrade, s  M( o# w- H9 O! Y
int OnInit()
* `, L( B, i8 o7 d; g- p{
8 m' \6 A* E+ Z7 ^  [3 _( l& B- |+ d* J) ?' D//--- Set the fill type to keep a pending order2 X$ h9 _; n/ K0 ^6 F
//--- until it is fully filled3 F! P% D: i3 Q
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
* u5 L: J! {5 e% k8 J//--- Leave the fixed deviation at it is not used on B3 exchange! E* r1 a' l# e- _9 s
negocios.SetDeviationInPoints(5);
& Z# y; m9 m3 o//--- Define the symbol in CSymbolInfo..." O2 ~5 {" V/ @1 k! ?
info.Name(_Symbol);
5 r2 C/ j+ x  o& z: Z/ _7 I$ S$ B* ~. j2 z//--- Set the timer...
! N3 m* X$ t: wEventSetTimer(1);
' z8 v% q- Z( g8 J; i//--- Set the base of the random number to have equal tests...; X8 f0 n( M4 q; m# \$ P* T
MathSrand(0xDEAD);' B& v! y; k4 ~: ^3 P
return(INIT_SUCCEEDED);
6 y! b: V6 Y  s}
7 T7 Z$ m* `+ i; h/ P) Y//--- Since we set a timer, we need to destroy it in OnDeInit().
2 w0 t& ^( |1 L: tvoid OnDeinit(const int reason)- c; p7 G' Z9 d, }" s
{
9 z! q8 }* d, }8 U+ IEventKillTimer();7 W: \  H4 S5 T5 O
}
7 ~& w  b. g. D0 l' N7 U/ z' m//--- The OnTick function only informs us that we have a new deal2 P5 A& ]8 }, ]" w
void OnTick()
) _( ?0 J. Y7 R( J* b! x+ M{; r8 M* D2 O% H
tem_tick = true;, Z& R. Y6 ?- d
}* h2 m% P7 Y# V3 e$ {- h9 g
//+------------------------------------------------------------------+: P% Y# ?, c% t+ B
//| Expert Advisor main function                                     |
0 R1 b: k+ _- I9 s) i1 v! ?0 G) B5 ]//+------------------------------------------------------------------+) A7 E1 f; S( H. Z4 q
void OnTimer()# K/ K  p6 `5 Q; Q9 i" H
{) `7 P. q4 v. W" f
MqlRates cotacao[];
% r8 r6 Y' }0 w, G. v+ Treturn ;! c, n  g0 P8 ^3 i( T3 g, m
if (negocios_autorizados == false) // are we outside the trading window?
; _' i6 V2 `5 c; E) X$ j, ~0 Areturn ;7 K. F2 x: I5 q5 }$ g
//--- We are in the trading window, try to open a new position!
5 y5 K* F6 \, b$ @0 ]7 fint sorteio = MathRand();# s: B( \! z3 [' P1 W; _" k
//--- Entry rule 1.1
6 X; E) V# B/ @if(sorteio == 0 || sorteio == 32767). C, o) g9 y! c
return ;
1 B& v  M3 D* n% I4 a. I, }" Aif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
) }( n% a6 H4 m; k% B{- Q" V# p5 ^9 p, V8 y& ]! e! t
negocios.Buy(info.LotsMin(), _Symbol);  P1 v+ T- I) L- L
}% y/ ~8 g9 F& v/ `
else // Draw rule 1.3 -- odd number - Sell: e" r! P7 Z: F2 e' Z) l( D
{8 _# b3 c: o1 y/ W! R$ B# C1 p- J
negocios.Sell(info.LotsMin(), _Symbol);
' i% o. Y' q% `3 h}
* R8 }5 I7 P* p}
( X& m+ e% N' E% H0 R$ e//--- Check if we have a new candlestick...& K, v* S' ~8 a2 p0 S( Y
bool tem_vela_nova(const MqlRates &rate)
0 J$ n* [) g" a. T6 n% L{
0 h: H/ C5 B- }& c* B- {6 q4 n3 G" P{+ g5 e$ T3 @5 D# Q' V
ret = true;9 `# Z5 s/ H# R7 x! @
close_positions = false;) X: G% D5 m! h) ^2 n! A' P; u
}: l. l' G8 O6 Q! i3 S7 r# h
else
, v% K) Q* e: ~* B{  Y. Q1 |" J+ x& v( Z
if(mdt.hour == 16)& z1 ?8 \, Q; }6 _
close_positions = (mdt.min >= 30);
. C  }" d$ d+ l( o) M+ w5 b}9 D9 ?2 t- h- s/ b
}- F1 P6 i' Y1 e0 @, @$ S
return ret;3 ~4 H, m% i0 A3 B  h
}/ k* p! Z9 H* L& g/ J* ]7 Y
//---# |0 M7 }, _* u6 }: `/ A# C0 K! l, F
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
" D# N! j" z+ Z1 y{/ T7 \0 ^$ E4 l% h1 U! A, i
if(PositionsTotal()) // Is there a position?4 b, H  J7 y% y
{
7 x6 t4 k3 S" d- V* F7 G* S. }+ fdouble offset[1] = { 0 };
9 _6 w9 b, S$ P4 v1 h( C( mif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
+ g: @, R4 [3 r5 m&& PositionSelect(_Symbol))  // Select the existing position!5 [( x) [5 j( ~1 _) M$ z' i$ j
{
. r2 k6 P2 e. g- Y; a/ T. p/ iENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
! H. r; {) _; {. Zdouble SL = PositionGetDouble(POSITION_SL);- N: R( y) b5 n* G& k
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
* h; ]; I: s  r0 d& e( y7 [4 s- Wif(tipo == POSITION_TYPE_BUY)
, k5 z7 r  |" W' @{
6 V& j. N( T+ jif (cotacoes[1].high > cotacoes[0].high)6 E/ O, T$ Y) [/ o, M
{
9 Z- U% _$ `2 B8 y& p# F/ n2 v$ w! Tdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];% u: o6 I# i. q- `+ I9 w; v
info.NormalizePrice(sl);
, m' h4 C5 v1 J/ aif (sl > SL)
: Q) U8 y* z4 g& w: [2 P{0 w2 e0 N' X2 k! D9 b1 l: f
negocios.PositionModify(_Symbol, sl, TP);
. g( A% L# b5 ^4 t* M}
" }6 Z1 c% j3 E* o; ~; u}0 {! s% n# |4 \' x
}! k$ u9 i& V4 M% y; ^
else // tipo == POSITION_TYPE_SELL
8 ~; x" _1 i% G/ U2 {! t  O2 I{6 k, P  y9 q  B4 n! H
if (cotacoes[1].low < cotacoes[0].low)! Y& P) x# K: G! h( Q) J; _% a  ]
{- z8 N6 m1 Z5 @0 Z
return true;) k) M6 U. W* P  q/ ~& N( n
}
5 s7 o$ x& w/ ^; ?// there was no position
7 N) a8 n- [' M4 y5 M) D8 x- ?return false;
+ ~7 W$ r1 K# g% {, v, z$ P1 t4 K}# c# V- O# B& B# F. I
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
6 B5 T/ v- G1 k0 Q( Y& N8 F到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-16 09:20 , Processed in 1.946267 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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