私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA( f2 u0 j& v% s; V: u8 l  Y
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。1 N7 Z' P: d, U& M  P+ u8 a
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
2 I& Y% e  Y$ H以下是制定这些规则的代码。
5 E- s) c' z7 |+ f7 H//--- Indicator ATR(1) with EMA(8) used for the stop level...
2 d$ t- e4 q" T' T4 Aint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
5 |( f! C& R8 E+ N! Q# c5 @4 Lint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);& ~  S* D6 ~9 k' v7 ~3 _7 d6 ?
//--- Define a variable that indicates that we have a deal...
3 C2 D9 ~, L. c+ h, n& z" s; h9 sbool tem_tick = false;, s/ e$ ^( ]; O1 i
//--- An auxiliary variable for opening a position; c  r. u0 h- t2 ~
#include<Trade/Trade.mqh>
+ y" l. F1 R9 f2 }/ c#include<Trade/SymbolInfo.mqh>$ G) R8 n# E% @
CTrade negocios;0 [6 l/ b' c% i5 h0 O
CSymbolInfo info;
, J# i8 _6 W6 |: h9 x, m; Q- i//--- Define in OnInit() the use of the timer every second
4 s, C* }7 R* K/ y! |  Q* d//--- and start CTrade
5 n3 H. j+ }; u: T( Aint OnInit()
- T$ Y2 I% a4 D  j2 i7 |  k- z{" B) t+ Z# }7 s
//--- Set the fill type to keep a pending order3 m% v4 x0 Q- M' @& V
//--- until it is fully filled8 A. S' X4 N) x
negocios.SetTypeFilling(ORDER_FILLING_RETURN);' @& y; _  T: x( R: y4 e
//--- Leave the fixed deviation at it is not used on B3 exchange: l% a, f* H% H  l. @- V! ]
negocios.SetDeviationInPoints(5);
8 B: y# f+ X/ ?) v# K//--- Define the symbol in CSymbolInfo...
6 T% P+ u1 A8 V* zinfo.Name(_Symbol);  J" p  S& p. a! f$ u. L6 j2 l( y3 S
//--- Set the timer...
. F* G* a- T  @; h3 }EventSetTimer(1);9 U$ y6 v& d6 J/ l- H7 V8 R
//--- Set the base of the random number to have equal tests...2 z$ }3 K! a& v" P
MathSrand(0xDEAD);
6 O9 n4 X! N1 V" g) [  Yreturn(INIT_SUCCEEDED);
" a1 u; E- P. W% m' ?, b}
# |& W1 C0 f0 B8 D//--- Since we set a timer, we need to destroy it in OnDeInit().
4 Z" m; ]9 I* e* Z# z" jvoid OnDeinit(const int reason)! p3 d7 }5 J% o7 r
{% f- m. A; o' t+ f* G$ A' y$ ?
EventKillTimer();, a) l3 ]* M- l6 P% [
}! a, {% v' e$ [' }7 ~
//--- The OnTick function only informs us that we have a new deal( G+ k% U0 B4 L8 i
void OnTick()
+ J4 s/ b- {9 T& x{
) y" M8 Y' P/ mtem_tick = true;! _8 R1 m! r: a8 i+ _
}* O& a1 ?: B4 u2 G1 }, d: O
//+------------------------------------------------------------------+: e5 N8 l7 n3 a
//| Expert Advisor main function                                     |% t' _8 z; w; n- D; a" u
//+------------------------------------------------------------------+
8 l; r5 q2 p$ _/ Mvoid OnTimer()
& R7 N6 [4 v( B# a) W{( f1 X' K% {" w( G7 }7 y* @
MqlRates cotacao[];
. [! T  J9 k% @/ {3 X6 [return ;3 W$ c; v- k% N' V! o9 W# y
if (negocios_autorizados == false) // are we outside the trading window?+ A. Q  V3 c) _+ {* C6 b
return ;
! @' d6 w* p4 R, S# c//--- We are in the trading window, try to open a new position!
+ _' H, R$ F( h  R) {7 @' A7 i% Zint sorteio = MathRand();3 _" R1 o# U" e% W! L8 |3 ~
//--- Entry rule 1.15 `/ {, G' S: M. g7 _
if(sorteio == 0 || sorteio == 32767)+ `; {7 U, W& x8 y8 R
return ;
- s7 v+ {: R( ]/ H$ {5 ~if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
$ u, T' Q. p. V* ~# D& h: K; F& m{$ o8 v7 K- P9 G+ o$ E: c
negocios.Buy(info.LotsMin(), _Symbol);; p& l& T6 P. n. L) Z- u
}" K; r' f, }6 d8 y' b/ f, G6 x. T
else // Draw rule 1.3 -- odd number - Sell
* M5 ]+ x$ e, d4 n' H{7 ?. E/ K( Z+ T- o3 @4 F
negocios.Sell(info.LotsMin(), _Symbol);1 {% R8 r9 n1 R* V8 M" Y
}8 d  p7 Z9 C6 c# H- r; e4 {: Q
}% ^. ^8 q$ g* K5 E# T1 O
//--- Check if we have a new candlestick..." x  R. R6 U! I+ h8 A% S
bool tem_vela_nova(const MqlRates &rate); _" _! B+ `4 [
{5 b$ O6 T/ I- C( Y* @% K$ P
{
6 ~+ w9 K+ S9 Q6 Y  J2 X4 n2 [" Rret = true;0 W& i. }" D9 \/ k+ V. n
close_positions = false;$ M8 f$ m5 T+ }1 J4 Q+ ]) N) o
}
4 d* b# B5 O- f1 X' ?! Velse
+ F+ k# s; h* L; z: a& q! }# t{
8 S. l) U! z/ N1 b5 z8 g0 h) Pif(mdt.hour == 16)
1 T! T- @' L, L. {) M8 V; \" Tclose_positions = (mdt.min >= 30);5 d6 M9 P. U$ ?% w( X7 a
}, Y/ t. o* X5 l( b" {( @4 C
}
9 y/ r' I, s" {, ?/ B& O+ oreturn ret;
9 I- H+ y, y) ^- r6 r8 t}, s% j$ o1 p  e/ s' L
//---; v4 @) g# B. e5 R' d% `
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
) L' n- V" `5 Z{+ s  u7 O4 {  F% C7 a+ B5 P
if(PositionsTotal()) // Is there a position?0 y- t- Z& H5 v" ^5 k) I
{
; w, v2 z* T# P- z- odouble offset[1] = { 0 };
- N6 C0 G" m. V# o/ O0 X& n! Sif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?1 _& ~* V6 k8 Y6 K
&& PositionSelect(_Symbol))  // Select the existing position!
- I& u" V& V& K! F8 L: W0 ]{/ m9 k) z# T; x3 o/ h
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);" c8 q- |  c! t# @! l
double SL = PositionGetDouble(POSITION_SL);
2 f' H; j$ j. K# v9 J& ]( E' hdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));' S' `/ \) h" r0 r# \; z$ ^, A( G
if(tipo == POSITION_TYPE_BUY)
$ L3 R) w3 `* l# U. ~: o{
9 I& R* V/ c: |& b4 n9 p, R3 q) v+ Xif (cotacoes[1].high > cotacoes[0].high)/ ?: i7 }. u9 q7 [: l6 d* l
{9 I, y0 I/ p8 E
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];; `, e, [- \( n! s7 f2 r% G
info.NormalizePrice(sl);' y0 _# h1 ~$ `) h: R
if (sl > SL)
- t6 C' m6 S! o5 ?7 q{
# F! @$ x8 s- r: R2 _negocios.PositionModify(_Symbol, sl, TP);  [" w$ d/ P& k5 w- t6 M& u; l4 m, s
}' z4 G6 X; H" f+ j5 E& Z. Q" W
}
: X" k! ?$ G9 A% ~" b* r; @" V}
2 H+ i8 T$ U4 W1 E5 Uelse // tipo == POSITION_TYPE_SELL4 a8 a8 e3 \$ A% D
{
5 R1 w- |* _" H# h9 \; nif (cotacoes[1].low < cotacoes[0].low)
  u) ~7 p% d5 m{* p; G7 m5 n& q% e; I+ }
return true;
7 P: r) J3 Q& @: R, D}( ^9 l4 X# a1 Q& e' G
// there was no position
$ T  h2 h2 U, V: Zreturn false;# v+ F' x& @$ ~1 W0 i8 Y
}
: c( o5 X# k9 {" M  I我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
$ L/ ~7 q1 h* L- a# w: y, x: q; P到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-24 15:54 , Processed in 1.972467 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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