私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA8 H' `4 H0 V7 S; H* \. X" _
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
8 C" a3 u$ b% w+ i9 b为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
: f0 B& k+ G" K& Y# F% j以下是制定这些规则的代码。0 Z4 ~* h7 X' N5 L/ y
//--- Indicator ATR(1) with EMA(8) used for the stop level...1 C9 o! a  P1 G2 y8 f8 J2 S( i
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);, N" d1 T- `+ s6 p; A
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);. A* w; u; |( `# |
//--- Define a variable that indicates that we have a deal...1 x- I9 J: ]  T: E6 k1 d
bool tem_tick = false;
; L2 u4 {: V2 Y+ ?/ T1 w//--- An auxiliary variable for opening a position
" N) I( Z6 W' y0 A#include<Trade/Trade.mqh>
8 G4 I- {8 z; W6 B#include<Trade/SymbolInfo.mqh>. `% m# r, n( m& [7 R, T
CTrade negocios;- h; J( r4 m0 V7 ?; b% Z3 r$ e# p, E4 g
CSymbolInfo info;9 ^0 o1 Y! D1 z5 X
//--- Define in OnInit() the use of the timer every second
( H/ ~% v( C1 X4 |% r3 |//--- and start CTrade! }# i' u0 `# x, w# t1 h: p2 _
int OnInit()
. D# ^( Y$ j- G' E1 b, F{
) n' [' i. O+ E, _0 I//--- Set the fill type to keep a pending order) y6 ~5 l5 F  w! Y% S+ I! b3 ?# V
//--- until it is fully filled% J8 o  N8 M' _$ c. L
negocios.SetTypeFilling(ORDER_FILLING_RETURN);3 W: \$ T1 y" o$ K* F2 u
//--- Leave the fixed deviation at it is not used on B3 exchange6 L2 P( W( ]- W! O' [. j
negocios.SetDeviationInPoints(5);: \  P9 d$ ~6 s, |5 B
//--- Define the symbol in CSymbolInfo...
' a# p6 A# i: X" P' U1 ^info.Name(_Symbol);
: R0 F& `) Y) z. g& Q% i/ I% w//--- Set the timer...- \/ C3 r; s( B% ~% o) x: s, d
EventSetTimer(1);
1 g+ l  I3 F; R& T" E//--- Set the base of the random number to have equal tests...
( a# ]0 E3 O9 q' t) i: l+ q; h( q5 zMathSrand(0xDEAD);
0 n/ a) {, U+ c- \& J& K2 W4 i4 Preturn(INIT_SUCCEEDED);
! G4 b8 ]/ d: `4 }3 o6 z) Q: q}: y* o7 K/ X! Q9 K! ?
//--- Since we set a timer, we need to destroy it in OnDeInit().; v, X% F. M# w; i$ N. Y) Q& s
void OnDeinit(const int reason)
4 ^0 N& G) F2 j' K{5 ^5 q2 k/ H7 D# \  n
EventKillTimer();
+ u) s, a+ R" U}$ ?8 m3 Z6 r" o2 x' [+ |
//--- The OnTick function only informs us that we have a new deal
' ^. |8 P0 b6 Y& {3 ^void OnTick()
' |3 g' k, k1 Y2 i0 n  P& Z  {, P  ~{$ U8 `% V$ |+ k; v, e% y
tem_tick = true;
3 _( j9 n  l# |* d}. ?" M5 u( `8 ]1 @# s2 X3 d
//+------------------------------------------------------------------+
" j9 `7 h9 n% M//| Expert Advisor main function                                     |' }5 H& }, ~0 J8 o* s1 B' r2 R" g1 O2 v
//+------------------------------------------------------------------+  V7 y0 V) ?- s& g# N7 |! d, h
void OnTimer()1 _" W; C" `2 u2 c1 p, P
{- x7 F6 P1 m: C9 W: R( E6 R0 V
MqlRates cotacao[];
$ f0 T0 z7 L* c( k6 ^& ?return ;" I0 K- W, g, t. i( R9 e" b
if (negocios_autorizados == false) // are we outside the trading window?
; n+ o$ \- R9 _3 }! S8 Breturn ;
8 S& A8 W. }- ]/ Q) K/ D. c//--- We are in the trading window, try to open a new position!% D8 j( c& z4 @5 E
int sorteio = MathRand();
* O; F- v+ b2 m//--- Entry rule 1.1- z3 j3 L5 \4 I/ g# q$ _6 m( f3 Y) Y
if(sorteio == 0 || sorteio == 32767)% T1 l2 R8 Y3 F
return ;
; d5 Y& A2 z3 J1 H  I( xif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy. M& y! z* i" ^3 @# v
{
7 B* C! U* P6 E  G; W- z! G1 mnegocios.Buy(info.LotsMin(), _Symbol);& d9 ]3 G0 X! B
}
3 S6 D! W: X6 Z( \4 Selse // Draw rule 1.3 -- odd number - Sell
! i8 O, ?$ W6 Y! y2 T8 ^{: [* _3 B' t4 p8 g4 I( w; Y/ a
negocios.Sell(info.LotsMin(), _Symbol);: i* m" g* S6 J/ @
}, ]9 C; p5 ]" @9 S; s% X
}
: k% ?5 F7 f8 Y/ n  [& y' g( Y//--- Check if we have a new candlestick...
  ~& h7 }; p7 t9 _- M2 Y  K6 Vbool tem_vela_nova(const MqlRates &rate)
. B# N; G7 J; O/ r; D{, r9 y* [' e( x  B8 c- U, u
{
- H( k4 B0 ^8 k0 N' d2 Cret = true;. O2 _( Q  H* o; A  M* C, M
close_positions = false;& e7 u1 @& B( Z: {/ x- ~3 T
}$ c' f8 G: o/ V8 }5 Z; D
else2 T  _& w! R5 `5 ^
{1 M+ m& C1 S# J  Z( F
if(mdt.hour == 16)
* p1 g: O( {4 F( h. ~% Uclose_positions = (mdt.min >= 30);& U$ k5 F; j4 X8 ]3 p4 O; A
}
4 N* G/ ?) D& T5 _7 E$ E}
1 h& M2 r' Q- ?2 {& z1 zreturn ret;4 a; m8 Q% ^5 J" \  D  j. F
}# E; L, Q1 }3 D. c; q( e
//---
- r3 d7 T5 \: G; P' d, `0 bbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])9 ?, e9 I$ [2 }, p
{$ w  i' e: d1 n5 n# V
if(PositionsTotal()) // Is there a position?. e- K9 \) X+ }- {5 z4 ^4 Z' Y
{
- ]1 L+ M) z% Fdouble offset[1] = { 0 };
- \% K6 @3 |! X7 O: r9 p, Oif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
; Z' r3 B/ K- N/ C  w&& PositionSelect(_Symbol))  // Select the existing position!
; ]( ?1 Y  @5 R+ u1 r{1 I) `. n" m( ?4 f; u% i
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
" Y9 `3 u- A2 W( v4 j$ y( Odouble SL = PositionGetDouble(POSITION_SL);
% w1 D6 `" O0 L! E) p7 tdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
' N9 K$ d# {' @: D% T/ vif(tipo == POSITION_TYPE_BUY)
" N! G& D% e6 {/ H{
% u6 c8 ~7 }6 S# c  G, e2 ]if (cotacoes[1].high > cotacoes[0].high)
, G& d% h0 n$ K, x& }1 ~  o4 P1 k2 g{
, {' a* K! J' m5 @# R% sdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];+ r, y8 B( x, v
info.NormalizePrice(sl);: j4 O/ D) [2 t2 b' c/ L
if (sl > SL), s8 f: D  Y9 @# h$ E5 d( j4 M6 r- y1 S2 B" o
{; O7 V% X0 [, ~$ s# u7 v
negocios.PositionModify(_Symbol, sl, TP);/ \9 ^+ |' A" A' B- X
}
: \. I+ T3 g) a: [}
1 H/ E( a& K1 g9 O}
# u: n5 t: A( ~5 v+ N" ielse // tipo == POSITION_TYPE_SELL: q- F4 u- P) A0 ?
{
  V2 p! m# N+ b, H# R1 D( _- }if (cotacoes[1].low < cotacoes[0].low)7 l' D, W6 v2 `/ U
{
3 ^& j: T# E( e/ Q% g0 F% I& p! ureturn true;
$ ^1 k* Z: Z' ~- H; g. K}9 O. I# Z5 r+ C# I. c: y1 E
// there was no position
( W- V6 J( |( c# G8 m) g4 r2 H& {return false;6 x4 Q) o6 P9 O2 ~
}& x  S  t' P. N( f3 k
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。; j7 p" _% T9 t/ b
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-20 16:43 , Processed in 0.675169 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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