私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA, o: w( O5 S7 `# s/ a
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。9 V: T" P* W1 h5 Z. i% t! P+ \
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
: d4 H# [, e, X& g: n$ m5 m% o以下是制定这些规则的代码。
: {; S  P+ _3 o6 L" u* M+ }/ U1 U7 C//--- Indicator ATR(1) with EMA(8) used for the stop level...
, E: A& S; L0 }- uint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);2 G  a  J9 z5 Z
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
# @) f' R. @/ M( s8 y" B, U//--- Define a variable that indicates that we have a deal...
( a: ]# M. a5 f1 T" h0 M1 Abool tem_tick = false;
  q$ w* O* r1 z/ e//--- An auxiliary variable for opening a position
9 m* g7 H' ]  e2 _0 W+ k* _0 ?; y#include<Trade/Trade.mqh>5 y5 O# \* O4 Q) L# T
#include<Trade/SymbolInfo.mqh>. Y( v( m1 p9 x- x% {) h
CTrade negocios;
- I+ s4 K2 x8 T' k" ICSymbolInfo info;1 d3 ?9 |7 q3 q
//--- Define in OnInit() the use of the timer every second
, p4 ?/ L9 R; W! P' [//--- and start CTrade
7 E+ ~. N8 w& x" }+ x) |4 ^% yint OnInit()
# _0 l0 u, }: e' X- C; e{
- z' m! v- C8 A  p; F# `//--- Set the fill type to keep a pending order
4 J6 L7 R/ O) n- c+ V% y//--- until it is fully filled; u' U* Q4 p0 O' t- S/ `  T* X
negocios.SetTypeFilling(ORDER_FILLING_RETURN);8 i. d) g; @# X# N' }
//--- Leave the fixed deviation at it is not used on B3 exchange$ ^6 [! R+ V" Q
negocios.SetDeviationInPoints(5);
3 J9 q0 K' A6 \, d//--- Define the symbol in CSymbolInfo...
0 j' B. b5 i3 u1 s3 minfo.Name(_Symbol);
, c, U4 f! D: ?/ Q4 i# C//--- Set the timer...
6 x2 O3 J" e( {! T" `' E- oEventSetTimer(1);
% Q) h* Y% u6 ~//--- Set the base of the random number to have equal tests...% {* C4 p  {2 |' U( {6 C$ ^
MathSrand(0xDEAD);
3 l2 h+ W, a# _: W$ R6 d0 p# r/ B% Sreturn(INIT_SUCCEEDED);
/ A5 }' k# n6 h* o) R- q6 m4 l}
  H2 E" C! ]( P0 E& v7 }//--- Since we set a timer, we need to destroy it in OnDeInit().
2 m9 ~  t* ?# v5 \$ Y* gvoid OnDeinit(const int reason)
; m0 l! Y+ f5 \& s{4 G, y3 r) `4 I$ _* a: ^, \4 B0 w
EventKillTimer();
4 O1 r, s- t1 F$ L6 M  B}
% E: v" N' V( k7 M- m9 K//--- The OnTick function only informs us that we have a new deal) L9 Z( q" b" C' S- M. ], T% s
void OnTick()
9 ^* W* n% W: G# ~& F6 P' C" z{" l6 G' l1 X, f5 q% w
tem_tick = true;
5 I$ i' y# U0 D$ Q' k}
0 {; f1 C' n% ~2 o) i+ A//+------------------------------------------------------------------+- k  m# o9 Z+ M5 }& S
//| Expert Advisor main function                                     |
  |& j8 k2 W6 p' u" y//+------------------------------------------------------------------+
: ^4 ~1 j, O' h2 M$ zvoid OnTimer(): o8 ~5 h  r+ b4 N
{& r2 [4 W1 p( G% m9 n
MqlRates cotacao[];
$ y2 `5 B" U1 ]' T4 O" Kreturn ;# L4 L- J0 ^9 i6 |" i  m
if (negocios_autorizados == false) // are we outside the trading window?' ~+ B/ T' O( @2 M
return ;: E" B) ~1 u8 N
//--- We are in the trading window, try to open a new position!
6 g/ n3 s  g5 J5 O, O/ [) m  T% \, jint sorteio = MathRand();$ p; {/ E4 f8 b2 U
//--- Entry rule 1.1
- D) g& B/ S  u, D( gif(sorteio == 0 || sorteio == 32767)
$ }1 S. U1 a( f) G$ m5 Hreturn ;
+ P8 R0 K6 P! p4 k8 D" c6 |if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy# p; C# [) q" I1 F$ V4 M
{3 P$ T/ ]7 f5 t+ @) J, O/ `0 X5 p, n
negocios.Buy(info.LotsMin(), _Symbol);
  U! ^# M/ n  c/ n% C}- _/ a- [8 y5 d  L5 P: j
else // Draw rule 1.3 -- odd number - Sell
) _9 z' b) @. ~" `) w{! O9 U8 F  {0 w: |
negocios.Sell(info.LotsMin(), _Symbol);  \, s- y6 u# A, a
}' I6 w, ^1 C% t
}
: X; n( I' e: h4 p  b2 c9 n//--- Check if we have a new candlestick...5 D. Y! {! X/ V9 ^% y
bool tem_vela_nova(const MqlRates &rate)
  q# N2 U& z8 J5 n{3 v- H9 [& C- U4 Z8 t
{
1 j* n( a% B: Q* bret = true;# U" D, [0 g) d& }- M
close_positions = false;$ A" E+ F- k8 o: d( B' ]: x
}
  U$ Q! S. ?' selse: N7 @; V. Q/ c
{( Y/ A( N: K8 w) M
if(mdt.hour == 16)# C3 u  X  G/ M5 I3 l3 D3 N
close_positions = (mdt.min >= 30);6 l5 |4 n3 K9 Q  Z& c
}! a- t' i8 `+ X# S, ~
}, k9 @, ]0 M* o  V! L5 |
return ret;
4 @& g) |* O1 a% S3 P* u}
" b4 Z) D% c8 ~- D( t//---
  ?- ^* {8 j. F% t6 ^1 vbool arruma_stop_em_posicoes(const MqlRates &cotacoes[]); E; S! k0 F) M3 [6 V: ^
{) X7 S* f/ `7 l  E  t
if(PositionsTotal()) // Is there a position?
2 s9 g$ f$ j5 y7 q1 f" u& \- Y{; u6 r) p7 ^- h2 w5 m5 [
double offset[1] = { 0 };! x+ ?" x- F6 m  R( |8 C
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
" b, h$ R8 V3 ~&& PositionSelect(_Symbol))  // Select the existing position!
. L  h. j+ q+ j{" F& n# ^. u$ `; s
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
  r/ H( v' X7 udouble SL = PositionGetDouble(POSITION_SL);
" `  _3 g( J( v5 Udouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
2 f, g2 t( |" Y& t$ j; k& kif(tipo == POSITION_TYPE_BUY)3 B( V  C3 h3 X1 c$ S6 D
{
! p. u1 \; {' Uif (cotacoes[1].high > cotacoes[0].high)
1 ]& F( q' [3 J' U8 V) m& N1 U{
- d* W  \' D- Bdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
2 u. h* }9 i) R8 N, a! N6 n  winfo.NormalizePrice(sl);
# \, h, Z1 X" uif (sl > SL)
" G: x$ B+ N: e{
; v7 ?& X7 b- m3 jnegocios.PositionModify(_Symbol, sl, TP);* W, y4 Y( R" w1 I3 j4 ~
}! `% Y/ H, y. W5 I- k. w# ^9 Z
}: m% K1 C$ ^/ a5 D' U
}" S! _. v  ?# z) W( o
else // tipo == POSITION_TYPE_SELL8 A% I0 w( q( t$ Y7 l1 Z5 A
{
: \  w- V2 S$ s  j  z- }if (cotacoes[1].low < cotacoes[0].low)
5 X' W+ z8 g* {2 ^% a7 O8 g{& l* j# f4 b8 i3 Q0 R. O
return true;: P! j5 i& F+ Y3 |' S4 w
}% b, m6 L  z0 D. ]! u
// there was no position
. d3 }+ j1 M5 K: freturn false;
' e- A  V5 `' j0 R* a/ j+ a}+ `& e. t/ G9 r" l
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。- F& R$ l' J4 X
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-10 02:11 , Processed in 0.582033 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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