私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
, d# y3 B, v9 j在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。9 j. B/ q5 Y% R2 ?2 F7 {
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。* e- w0 H$ i; ^# X
以下是制定这些规则的代码。* d) ~! B  P/ Y# i8 T) S* P9 S
//--- Indicator ATR(1) with EMA(8) used for the stop level...9 o2 e6 d3 j6 {# P
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
) x4 v6 k' i2 v% V. d/ d/ tint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);7 z5 B  L3 b3 h" {" N$ v' k. p' V8 s
//--- Define a variable that indicates that we have a deal...6 A; k( a) x3 Y4 F# p& z0 s) o
bool tem_tick = false;/ J$ P. g2 Y8 J* u, {' x
//--- An auxiliary variable for opening a position. W& e6 g; U! `  l, y2 u( k
#include<Trade/Trade.mqh>
& R6 j9 ^# R- J: G: c! X#include<Trade/SymbolInfo.mqh>
2 E4 A5 K) v2 T7 d6 H) J, Q3 ?7 X2 _CTrade negocios;
6 l' b1 @' e: Y  x1 \( x( D8 iCSymbolInfo info;
( T+ n& A' W. A  v0 v//--- Define in OnInit() the use of the timer every second, E5 A6 L9 E2 I' t6 p8 P1 g
//--- and start CTrade
! U" L! n* I& P) X6 f" Gint OnInit()
& k. G/ [+ z8 P4 b& t{
8 S, Q( \* D3 L# M( M( Q//--- Set the fill type to keep a pending order
- [. @2 c% s4 T' z7 ~" d6 x$ ]//--- until it is fully filled$ R# Y- n; Q1 f+ [% L
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
6 p9 m2 G: H. R8 M; i//--- Leave the fixed deviation at it is not used on B3 exchange
  q3 s, \# c! u3 P- \) e0 mnegocios.SetDeviationInPoints(5);
% U- R/ e! v# P0 A$ k//--- Define the symbol in CSymbolInfo...; v0 L: P3 O; r1 {; @2 W. m. }
info.Name(_Symbol);2 T9 M& s' z# l+ r2 R
//--- Set the timer...
! V2 L$ C( `; H8 A9 U5 TEventSetTimer(1);
9 {! r' s2 b8 C( J! ]# P//--- Set the base of the random number to have equal tests...1 h  L/ l& J+ B+ T; A
MathSrand(0xDEAD);7 P1 X. I% S5 w2 I& U
return(INIT_SUCCEEDED);
" |& Q2 B( k# m/ j( t" G, o}: }" l# K1 Z% B4 F! j% O+ u
//--- Since we set a timer, we need to destroy it in OnDeInit().  f4 h) A) p4 d, w
void OnDeinit(const int reason)
$ ^) N* S" L" K{) O6 `9 `. V% n/ I0 `$ z* F
EventKillTimer();. C* D, |5 F, }7 a8 `# H' r
}
+ [+ C# ]* B) z& p) N//--- The OnTick function only informs us that we have a new deal
- c% x0 |7 H$ W/ Uvoid OnTick(); {/ G! h2 t* t) G- _/ d
{$ O/ c9 `) O: G) e4 z! P
tem_tick = true;
6 B+ {9 A4 V) m}/ Y4 N/ b) h" u' L5 [/ J) D
//+------------------------------------------------------------------+1 S6 I" L8 `$ V* x/ w
//| Expert Advisor main function                                     |1 A  Z( b" |3 k9 y9 R
//+------------------------------------------------------------------+( n8 I# @$ h% I# g
void OnTimer()0 T7 y0 F( V  x% l
{
( g# J% y8 r" P* gMqlRates cotacao[];
7 A+ E3 d7 m5 |7 Q7 ^" I6 ^return ;* f- o. F( Z# Q) z1 V# q! L
if (negocios_autorizados == false) // are we outside the trading window?
4 N2 A$ P" n: D+ g6 Lreturn ;+ E3 x6 Y9 d& [
//--- We are in the trading window, try to open a new position!
. \  j5 Q( z3 }+ @! oint sorteio = MathRand();
0 o5 A5 q; p; v; M  |" x//--- Entry rule 1.11 p; Y/ ^5 P) {7 N6 p) t
if(sorteio == 0 || sorteio == 32767)- S/ v8 j. z; P4 C% J
return ;8 I) f! [8 c* ^+ y
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy& S$ p) F% p5 ]9 Y7 F3 d* ]
{- o0 n: E: n7 ^8 O, R6 B
negocios.Buy(info.LotsMin(), _Symbol);! p3 B; P; W% f
}& t- \5 V/ B7 O/ J1 L5 F
else // Draw rule 1.3 -- odd number - Sell1 `5 B: Q" S% N; F7 U; ~
{3 a* W& a+ v0 u  d
negocios.Sell(info.LotsMin(), _Symbol);7 b8 w& h6 ^! g5 U3 C6 q
}
3 W$ g9 g/ S! W}
2 x# i1 o! c9 P; Y//--- Check if we have a new candlestick...  h; B& |4 i& ?! o% f
bool tem_vela_nova(const MqlRates &rate), l5 e; h4 S3 d% j4 n% i
{  Y! n4 P' T8 m6 S" F
{5 V! d$ J, W' @5 U( v& e
ret = true;7 }1 v/ ~* ?( H) A# C
close_positions = false;) R( k0 l1 J* I) r( s
}1 Z  _) q/ Z2 M1 |, U; O
else
/ b" b) Y. @, ?9 t/ c% a& _9 c! s{
: k4 [% ?+ L1 Yif(mdt.hour == 16)
) A( m+ x* o0 U; h# n( rclose_positions = (mdt.min >= 30);
( w2 _3 b. I7 @- r}( K" E9 s5 S) N# V( l3 l* H2 q
}
4 O0 a; h4 G  e' v0 Q, f/ k2 }return ret;' s# T# L) B) O8 O1 k& ?
}8 }; U: Y! b# B5 Y
//---1 C7 N% j  h/ s% n% Y  j! |
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
1 ?- i6 P/ ~6 s+ t$ s: c& Q/ Y{
; h6 s6 V) r2 v. u% F3 {if(PositionsTotal()) // Is there a position?  P% L: W3 c' E/ V9 V6 }
{3 J6 o2 m! t% m8 x: h
double offset[1] = { 0 };& h8 K/ x$ E& B" `3 B* B- a+ O- J& c
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
4 I/ y& t/ C. n, B9 Q; x: J&& PositionSelect(_Symbol))  // Select the existing position!
$ I5 `+ e/ W* w' f3 h( Z{
& ]* h. M& _; _  }: KENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);$ d" e/ p/ A7 }! Y. B
double SL = PositionGetDouble(POSITION_SL);* G- [, \1 T8 H6 Y6 k
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
0 {, l, M4 L$ T6 E0 wif(tipo == POSITION_TYPE_BUY)6 U8 x8 l/ G) d3 M7 y) {& r& x; V
{" W  A' D9 a: I$ l/ |
if (cotacoes[1].high > cotacoes[0].high)
) N7 A5 _0 b0 Q5 n% o+ z" y( B{
1 I6 G. h! C$ a, _( A+ ydouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
; A& P3 I9 `5 N5 sinfo.NormalizePrice(sl);  A: `- |7 n1 R8 Q* z" R  K. a
if (sl > SL)! x2 {# T0 I( }+ K& J% `; C
{) x0 n8 N( x7 u/ u# y7 L* @: q
negocios.PositionModify(_Symbol, sl, TP);
8 E+ K  h8 c5 P& r5 a}
. L; ?8 f- l+ R1 p4 S}! R, o& M9 l- g8 d1 }- u
}6 C, e8 P' y& I9 e# t5 s) F
else // tipo == POSITION_TYPE_SELL
5 B8 n) l2 X) W5 [8 V6 S{
1 h2 x  L5 n' yif (cotacoes[1].low < cotacoes[0].low)8 }$ v+ P6 D3 z& r" C8 p
{! \$ A$ T4 u  v+ J% J: _
return true;
) S7 W. s7 E  M. o, F3 n# V}! @. V8 P0 E" d1 B- G( M3 Z
// there was no position
* f" ^6 M1 Y6 F4 V( @, G4 ^return false;
, s# {7 q" |. O0 ?: W. ~. a}- t4 `# c  e6 h
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
1 s/ c/ w2 I5 a2 I到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-22 23:07 , Processed in 0.889728 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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