私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
; n' y, o7 z# i9 o在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。' ~, g2 g8 R6 ~. n
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。. f- T) {% V% [2 q- y
以下是制定这些规则的代码。# Q& {. G+ m( Q2 n0 g) y/ O  b
//--- Indicator ATR(1) with EMA(8) used for the stop level...& g0 s0 R* |* K1 N
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);1 C8 L. I: Y' g
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
6 M3 M' G2 j0 S3 G0 m/ Y//--- Define a variable that indicates that we have a deal...
: T$ d( A: P0 Y9 F" ~8 b+ x8 f) _bool tem_tick = false;
1 P: u/ r, s% J  A1 _//--- An auxiliary variable for opening a position
/ y$ B3 Z$ H. |9 l  ^% ~#include<Trade/Trade.mqh>
; g2 V2 z7 r- P* A" f9 _2 ?#include<Trade/SymbolInfo.mqh>  V( V5 `& Y$ ]: Z4 Q5 t% S
CTrade negocios;
8 U0 J; f$ h$ @& v1 ECSymbolInfo info;  ~( `8 }7 x! x
//--- Define in OnInit() the use of the timer every second7 N5 J/ X$ L7 }8 G* W2 I) P
//--- and start CTrade
* C+ S+ a  p  J% gint OnInit()
; Z8 C+ ~" ~0 ]: q( J9 e{
0 r  s/ y! c' t  K' o+ v% h( ]//--- Set the fill type to keep a pending order+ t/ f. _9 Y+ r1 d5 O  A
//--- until it is fully filled- T; {8 X2 w# l5 Z4 e( _- M9 J$ r( }
negocios.SetTypeFilling(ORDER_FILLING_RETURN);! ~6 b' v8 `) ^, P  a
//--- Leave the fixed deviation at it is not used on B3 exchange
$ O$ S7 t9 J3 o: [negocios.SetDeviationInPoints(5);
9 n! L& M" k. l1 i6 O# _  G0 L" N//--- Define the symbol in CSymbolInfo...
, [& r6 O; B& I2 h* [info.Name(_Symbol);
, Y9 R$ Y& H9 ?% f- m% |//--- Set the timer...
9 i8 b/ Q4 j2 h1 D' b! REventSetTimer(1);9 b. |6 B4 {  [  N# ~0 Q& W  Z; u  p8 [
//--- Set the base of the random number to have equal tests...
# ^6 P9 x7 d& l) l, w6 EMathSrand(0xDEAD);2 Y9 h  g8 H9 n0 a+ X3 A
return(INIT_SUCCEEDED);
0 K! v$ s  w8 Q( @& o}2 B0 L3 E, A" w) t  f6 P- s
//--- Since we set a timer, we need to destroy it in OnDeInit().
' f3 v; N5 U9 y$ L' j8 ]# V9 xvoid OnDeinit(const int reason)
0 \/ p- ~* H2 J/ V{
" j8 ]1 T& m" _' a1 zEventKillTimer();
4 ]; k( k4 ?' ~; S( Y& ?$ c}
9 c  ^  V& q& C* o) f//--- The OnTick function only informs us that we have a new deal" f5 Y0 x: J. J: H8 A
void OnTick()
: J; ?; n' b. x/ K( c2 @{$ |! L. B5 p1 D+ r
tem_tick = true;
6 P- X" W/ U+ E- [3 F. T. h}- N' a" M4 q$ k  p6 n
//+------------------------------------------------------------------+6 p) @1 ]3 Q# B! g7 H& D
//| Expert Advisor main function                                     |; o! ^7 j0 `( j8 F! X/ q
//+------------------------------------------------------------------+
. [3 Y4 b8 X: S; X* zvoid OnTimer()
2 d& y. A: s- H; m& h( \. i2 z1 S{
1 T6 R2 K8 I, _9 L+ v% H: ^" RMqlRates cotacao[];: |0 a% b1 F+ G  I. B* X7 o/ u
return ;
0 C; N. T1 n& C$ Bif (negocios_autorizados == false) // are we outside the trading window?, x" {# m0 |) `* k+ V
return ;
8 Q6 ]9 E* _& O$ I- r+ l$ e//--- We are in the trading window, try to open a new position!3 u, h  P2 F: p! S, i
int sorteio = MathRand();
/ I1 A: q, V: @. R6 _- I4 `: n//--- Entry rule 1.1; D; L' y$ ]3 U: B
if(sorteio == 0 || sorteio == 32767)
* I0 d% O; P0 Z4 {) ~$ e9 preturn ;
# t* a* m3 Q( f" v* q; |3 Yif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
% q' D, s! K" i5 J{$ y  ~& U, R( M; Q  p2 k
negocios.Buy(info.LotsMin(), _Symbol);
- c9 Q% B3 R+ ?' A' X/ b* J. U}
' c5 E8 f+ X9 f7 qelse // Draw rule 1.3 -- odd number - Sell2 m; m: X2 [. p/ p) U
{
7 L: ~5 a) W1 n9 z) N( qnegocios.Sell(info.LotsMin(), _Symbol);: j* h0 S9 c5 V$ P2 g. G
}7 ^! m0 ?# Y/ L; O  Q  j. v
}8 n& S( z( X  V" B! d% B' W
//--- Check if we have a new candlestick...
7 |! @. c) \8 y. C- cbool tem_vela_nova(const MqlRates &rate)
: b1 r9 s+ t) S{! T3 y( Y& G& f: u
{
9 ~5 d' _! T) R+ [8 L  Eret = true;
  I5 {; B9 T! h2 [; ^) L8 p# {close_positions = false;
& G1 h& `3 r/ k% W4 E' k1 d# l4 y  [}' _: `& h, u! L0 ^% N- M* x
else7 B3 E7 S5 U. x
{2 g3 Y3 q+ O6 r) B, }
if(mdt.hour == 16)5 h5 h" t. h( Y) t# J0 z% Q2 L
close_positions = (mdt.min >= 30);
9 f0 F( i8 W1 ^" P2 N}' X) h8 Y2 u9 Q5 a4 h% U
}
& S8 U7 l3 X/ z; S9 n# f2 creturn ret;; v4 x8 Z9 ]6 R- J! A. r
}8 n# L: v* ~5 w3 F' l
//---
1 n3 R; [) B5 `( S# N. ?bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])0 S) x3 {' N1 _) |6 K" x$ r. K1 |& j4 |
{
: U; r+ N: F* Q+ W* d( f. hif(PositionsTotal()) // Is there a position?
  N( B8 W) g: s7 K{
5 d# U2 Q8 _2 I: wdouble offset[1] = { 0 };+ b2 g; e$ q; W
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
  }1 @- @+ f9 o$ H4 s" E8 w/ @&& PositionSelect(_Symbol))  // Select the existing position!4 @- ^/ M3 a' U! e
{( X- }6 g4 F5 b7 S% N* O+ o3 U
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
6 f5 l4 ]6 K* zdouble SL = PositionGetDouble(POSITION_SL);
; `+ `/ G- o: O4 t6 c) Wdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));8 @. u$ R  G6 y, Q
if(tipo == POSITION_TYPE_BUY)1 L8 ?$ c6 i$ [" u/ J
{
, Q; R! p- w8 p1 l. b" }if (cotacoes[1].high > cotacoes[0].high)
8 z$ }# V, q  K{8 X$ P, Y* ~2 A; i
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];- N: j/ ~9 h% a6 S9 T( C
info.NormalizePrice(sl);
% n) q0 [6 Q) G1 qif (sl > SL)& p  I0 A  z; `+ ^+ f% T
{. D0 v2 ?2 y+ |' L" n+ F7 |: T
negocios.PositionModify(_Symbol, sl, TP);
  U, J# q* L/ {4 E+ ?! R5 q}
& [  c, C* R1 Y7 Z. f}# z! r- Q/ B1 u. I, z9 f
}: I9 Y- e& x4 a, ^, o. w$ C; `
else // tipo == POSITION_TYPE_SELL, g- g" F* z/ p( p) J
{
3 q9 l9 H; `3 l+ o: H. f8 P2 Mif (cotacoes[1].low < cotacoes[0].low)5 k) t/ H3 d0 ~( ]  U) O
{' L# W: u: l( J( m5 J
return true;
3 s& X) W- w. w, S/ m: i}
4 `3 a& Q: L4 r! O// there was no position
  B  |3 A6 ?  V8 y" qreturn false;
: `) z, Y1 @  K2 D}8 y+ ]! D2 t7 m) x0 ~# B5 a+ A! S, I
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
1 H4 V9 @' B- s到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-29 04:02 , Processed in 0.582159 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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