私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA8 ^0 X$ R/ ^& X# Y8 d
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。* p# M9 s4 x, W) x* m- x7 o" U& `. Z
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。  w0 N2 t7 e) A7 c3 E1 F, `: g+ U( e& v/ k
以下是制定这些规则的代码。
1 Z( C+ b3 b% v) I; |9 j! ~3 ?//--- Indicator ATR(1) with EMA(8) used for the stop level...
+ \2 w# x, P# V5 C" `7 g, o1 n$ d" ^int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
, B7 f$ t4 s& c1 Qint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);+ R. k5 S; K; m4 A2 e( L8 y
//--- Define a variable that indicates that we have a deal...( y  k; Q+ R  x2 D
bool tem_tick = false;. D9 _! K# q- q' t3 m1 z
//--- An auxiliary variable for opening a position
2 p; M$ {! o1 l#include<Trade/Trade.mqh>
2 c3 G% p/ s& j: x5 Z#include<Trade/SymbolInfo.mqh>! P7 l0 a' o9 f) z  h, J
CTrade negocios;9 L/ X& s9 N) V# \% Q6 j) ]# E5 Z* i& c$ [
CSymbolInfo info;
+ \- V+ m) V- ~) ~//--- Define in OnInit() the use of the timer every second& P( Z% q. F4 U  \/ Q% g) ?, l
//--- and start CTrade
$ h  h2 f$ d! r9 I' }8 eint OnInit()9 F- I4 N' i# N* \5 [- b$ y7 |
{
6 z/ T8 R& {. K//--- Set the fill type to keep a pending order" @% m; i8 n4 ~! Y
//--- until it is fully filled0 U2 x- h! o! c7 C
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
! n8 [1 ]: {) a0 ?7 _//--- Leave the fixed deviation at it is not used on B3 exchange( m* t6 {2 \( M
negocios.SetDeviationInPoints(5);
9 F$ d" A' q: y: B7 @8 I( U# V5 D//--- Define the symbol in CSymbolInfo...' g! L! K4 D5 Y
info.Name(_Symbol);
  m& m3 x- h: h0 O+ P! p8 ]; y//--- Set the timer...0 s. _0 f6 ^  f. r+ A9 h9 m  M
EventSetTimer(1);
( p) ^' a: k7 a! w//--- Set the base of the random number to have equal tests...8 V( F5 y/ G1 ^. K! Q9 {
MathSrand(0xDEAD);, i! {' N, T0 B1 P0 F# M! ^) f
return(INIT_SUCCEEDED);1 X4 I  Y& X3 l
}
6 t$ G7 w9 l# V7 }! H# y//--- Since we set a timer, we need to destroy it in OnDeInit().
0 \8 U. y& I1 G( w- A* Nvoid OnDeinit(const int reason)/ Z* b4 w& M1 x# U, Y( `
{
, T5 U8 k# B' H; M4 V: o; xEventKillTimer();
7 t1 O2 {& s" u5 j}- v+ y0 h0 t/ v/ c
//--- The OnTick function only informs us that we have a new deal: y) D% t& K* q* X' |
void OnTick()
9 c7 O" p; O: v4 Q; q{
$ k1 Q5 H7 M* |( ?tem_tick = true;6 s8 g8 L- ?" @; V
}
! m- h, P  _6 x//+------------------------------------------------------------------+
2 S' y1 F9 Y/ Y. R//| Expert Advisor main function                                     |
# Z+ r% V7 O$ _+ E5 Y* j/ l9 N//+------------------------------------------------------------------+
! m8 z4 \( a- {4 n+ ?void OnTimer()$ o& R" \0 b) V* W$ z; _
{6 \1 |, ?6 Q7 q; U, ?& i9 `
MqlRates cotacao[];
; |, D+ d, L' Q$ H2 I: W: J$ {return ;1 X5 p8 q. c' Z, {! R
if (negocios_autorizados == false) // are we outside the trading window?
$ U" T3 b- d% Z( d# Vreturn ;* v  B; A" D1 l& Y7 s/ }, J
//--- We are in the trading window, try to open a new position!, I9 N1 i) [9 a
int sorteio = MathRand();/ D& B3 Q, A/ ]# J7 v
//--- Entry rule 1.1
, G! d% j6 O% t( J5 q- ?% @if(sorteio == 0 || sorteio == 32767)# I8 }$ Q+ [; a* w( j" D# j$ N
return ;
4 i, V5 w* y; c, ]; E' K1 k! Pif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
9 `2 C' `& h& ?1 G$ l* {% N9 P{
6 a+ l( b& V+ G9 Snegocios.Buy(info.LotsMin(), _Symbol);2 n. ?2 d- Y$ i% {, p2 j& R3 @$ s% U
}
1 t0 o! e/ q/ e# N9 Z+ ]else // Draw rule 1.3 -- odd number - Sell
# }( Z* }$ O0 }3 e{% T, Q/ c) W8 m
negocios.Sell(info.LotsMin(), _Symbol);
4 v' U; O9 H: r! ]& S5 Q}# G! \. C4 }, h
}* S5 u* Y: r0 F5 H4 B( E
//--- Check if we have a new candlestick...  x" H" A# B+ ^$ J6 k; \& D; T
bool tem_vela_nova(const MqlRates &rate)$ R2 c) b0 L$ e/ ]- E7 [
{, \3 `' o- V3 F; y4 |7 H3 z3 c# u2 n
{
% j1 d0 S, I& _- E5 B3 m& Zret = true;
! T" i! ~7 ]2 P6 @5 Kclose_positions = false;
8 f+ g9 a/ Y% s& S}# m0 [6 G2 i& q! n! Y! d; g
else
' L; Q, \* \8 H" s! L5 a$ H4 k! I& J% b{
% s  J9 x# Q0 h; _if(mdt.hour == 16)7 e2 w9 h. o2 u6 o6 h' A
close_positions = (mdt.min >= 30);
( r( {7 A! }* a8 b}- U) I  r7 _  \6 i& S
}
( Y) p1 Z7 \2 i; _* Ireturn ret;% w/ k- S: |5 R$ H- I, K
}
/ T+ ]9 D' j* D5 X6 \) i9 l//---( c% k  ?" W! X  q; B1 a3 N4 B
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])* ~, |) `: V9 g& e9 A
{
9 a/ Z3 S; A- ^, W1 Jif(PositionsTotal()) // Is there a position?
3 o1 }' q! k! p7 T5 B- w{; i3 s% e# S% s/ ]
double offset[1] = { 0 };" f* Z# S% \( e: f
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?' G9 l  l0 m/ q) i+ s' g2 G8 g, r
&& PositionSelect(_Symbol))  // Select the existing position!
+ f, o" m6 {6 b( C8 L# F6 ]{
1 p' J. |/ q: i: ^ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
! u0 @8 O" K( }# U, r3 Jdouble SL = PositionGetDouble(POSITION_SL);
7 ^) R3 }0 [7 d. m- s3 ]5 Idouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
+ U8 J$ p5 k, Oif(tipo == POSITION_TYPE_BUY)0 l+ D! s* S* N. [
{( J# b5 {9 T% c
if (cotacoes[1].high > cotacoes[0].high)
4 X8 J8 h2 D" c- Z5 S{
& L& Q) H0 P, C( I/ r1 idouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
5 w% o9 N! k6 a1 Ainfo.NormalizePrice(sl);
; h5 \8 ], g  s# J; s8 K- ]% ^1 iif (sl > SL); r# Y* k1 z. C
{
0 r1 |7 M+ ]1 Snegocios.PositionModify(_Symbol, sl, TP);
) G* W; g% T6 A5 j9 T0 R}7 b; H* `0 x- w
}
; z# ?; k! ]: Z2 q) ]& f% \}1 [3 I/ F* h/ v+ T" y% s& v
else // tipo == POSITION_TYPE_SELL
0 y% X# T& s7 n7 o1 V2 k{+ d& F, e- U7 m8 `
if (cotacoes[1].low < cotacoes[0].low)% C0 Z  o5 B( f/ v1 J0 |2 l5 ?
{
+ L/ I5 B, y" Q; [5 E, kreturn true;
8 t4 O( i# a4 l1 }}7 G  X% _) D* ]; u/ ^4 ]* P' j
// there was no position
% b/ _, d9 h' C- q$ Qreturn false;( _& b' s+ y4 d% y
}
. K+ ?5 {9 V$ S/ i2 m* n, o& @* Z5 x我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
+ d9 u0 h" u) X: a( m9 V/ U3 U* {到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-10 08:54 , Processed in 0.422473 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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