私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA, v( K* f% n; [. l' d3 E
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
- s" r" a3 f6 X为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。9 `$ C8 s5 y8 Y1 W0 k
以下是制定这些规则的代码。0 |0 J5 h" s# D, g1 Y
//--- Indicator ATR(1) with EMA(8) used for the stop level...4 M! V4 R  c# l0 C1 L0 ]
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
3 f' m! A0 R0 e' l1 @) y* i, @int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
8 X% G% X8 }) g; f//--- Define a variable that indicates that we have a deal...
8 e1 B  V6 r) p( [  G& `2 k" Rbool tem_tick = false;7 O; p4 x/ u. @/ I) @' j7 X
//--- An auxiliary variable for opening a position
- F; T: \5 D, I# ~#include<Trade/Trade.mqh>
, @! ]6 m! n: L1 \/ D' X#include<Trade/SymbolInfo.mqh>. d8 k+ R  I" G' n, [
CTrade negocios;/ I7 N8 Y; V) `- r  }
CSymbolInfo info;
' J3 u# u/ K# v0 Y5 a" L1 @8 x8 c//--- Define in OnInit() the use of the timer every second
( N& K' p0 e8 g//--- and start CTrade
7 m5 a4 E5 k$ K7 eint OnInit()
5 x' m/ L( O* C5 z; g{( p# i  V4 X7 j' g
//--- Set the fill type to keep a pending order2 ^4 q3 B8 K- c6 \  l$ E0 ~- J+ S" B
//--- until it is fully filled; o! X* l3 \7 V
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
. O$ w3 X( d3 _0 ^8 D' P//--- Leave the fixed deviation at it is not used on B3 exchange, W) n9 O0 l8 J
negocios.SetDeviationInPoints(5);
$ c7 ~7 a. |" G7 L1 r//--- Define the symbol in CSymbolInfo...
# V; s+ G1 P! Z3 ~3 d8 l* }8 minfo.Name(_Symbol);2 G, ]6 L& n$ C1 ?8 }9 b! h
//--- Set the timer...
% o5 v  }) t2 x8 \% E! b8 t" ^2 s8 T. NEventSetTimer(1);
% ~1 I1 a! O3 F# H; D//--- Set the base of the random number to have equal tests...4 T) ?8 W! r2 G  w
MathSrand(0xDEAD);% Z; I$ }4 ]/ H* k2 e% f  r) D+ C% [2 d4 ?
return(INIT_SUCCEEDED);' u* i, G5 U6 [8 q2 ~
}
' |2 J+ }- f6 r9 `* I& E5 o; _8 t//--- Since we set a timer, we need to destroy it in OnDeInit().
4 k- Y6 a' G7 E' F3 |$ A( Zvoid OnDeinit(const int reason)
" R1 x3 m; u3 v) I{
& x1 W4 d* `4 BEventKillTimer();
# d9 x3 M+ k0 R( q}* G* n2 d3 S8 e% E
//--- The OnTick function only informs us that we have a new deal
7 l' ~, v+ b6 z2 d' v8 kvoid OnTick()
3 o! t; _# B4 t& e% z, o{9 t) w8 K1 F! S4 r3 i, I7 F
tem_tick = true;
8 H3 m) |* J1 y0 M, v}/ ^: z2 |6 o$ K: B2 k$ g
//+------------------------------------------------------------------+
" q2 w) e$ B+ b//| Expert Advisor main function                                     |0 v9 p2 b0 y2 t* _$ g4 c; Z& l2 t
//+------------------------------------------------------------------+
* k5 x; t: M+ p, K1 }7 _. ?void OnTimer()
$ X* P3 n+ q2 C* F{. J, x6 X; r7 c
MqlRates cotacao[];6 q2 I3 \( j4 @+ u: p4 `' u
return ;" N& w% r3 K% O7 {# J
if (negocios_autorizados == false) // are we outside the trading window?9 d0 E% y+ j" L! b# R' L8 _4 v  O
return ;
/ a6 k  G' G7 U7 _% y% V//--- We are in the trading window, try to open a new position!
( f" |0 }8 r+ i6 E! t' bint sorteio = MathRand();
! M( O" p- @9 T( Z//--- Entry rule 1.1& _3 ?/ D& W2 R* p+ e( n
if(sorteio == 0 || sorteio == 32767)1 n" v: V% }5 f: Q$ S0 z9 {
return ;
  W0 @/ x# K# I1 F; K2 R/ Y* X* A3 bif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
' Y2 j5 t, S! N8 ?# m{
1 `5 r( l- D, a2 \$ wnegocios.Buy(info.LotsMin(), _Symbol);
8 m4 A) H) _) l* o9 u$ c}
7 u" y) Z/ _; k+ j4 zelse // Draw rule 1.3 -- odd number - Sell- e5 m! c& }0 H0 t
{* y/ A0 `7 B1 `$ c! t
negocios.Sell(info.LotsMin(), _Symbol);9 _$ h. O: {$ H8 \: k
}
) K. d* @  B7 s3 P}7 C! e0 i5 p" k% i& T
//--- Check if we have a new candlestick...% g" `/ s$ ]& |3 T0 k
bool tem_vela_nova(const MqlRates &rate): |+ i, h( x6 y
{% L- r: k3 V6 ~& P' ?( i, ?
{8 S3 F5 ]8 Z2 p  \. g, G
ret = true;& {' p" X# d, h0 G0 e
close_positions = false;& B" B% \( z9 a+ b; E* d
}8 w  \- K; \  K1 ?- f
else
" c) r/ Z( l2 l! H{
4 S& `! j$ z1 ]: }; nif(mdt.hour == 16)
1 a5 R3 ~; z9 Q( V9 nclose_positions = (mdt.min >= 30);
' n2 @" m9 O2 H}8 b/ q- Y* N9 ?0 ^. j$ N& F) {1 S
}( `0 W7 ?; c9 B9 B
return ret;1 z* ~; T+ Q$ q% |0 j4 S: V! f& L
}' A$ X6 ^, e) S
//---" q/ r3 V: q: Y8 M2 y
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
* D7 ?" x5 ~0 k7 Y{
- y" y9 F+ P2 \, a1 K6 z/ bif(PositionsTotal()) // Is there a position?+ c$ d) @# d$ U; y
{$ \# [" A- }' r* t0 s& [
double offset[1] = { 0 };; f" Y7 Y, {) Q! C
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?1 B8 w% E% M8 v
&& PositionSelect(_Symbol))  // Select the existing position!& D( u4 D+ z' E9 ?+ L. r2 Y+ v, k. }6 Z
{
8 y) `; i! L! B0 \+ C) VENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);$ ?9 V# b# b  d' T; n
double SL = PositionGetDouble(POSITION_SL);/ T8 W: u3 \# U* o0 ?2 Q! Q! q- O
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
+ X# B7 I) @& ^& I1 bif(tipo == POSITION_TYPE_BUY)3 V  Q5 b' |9 z. D6 |& r' N
{; t0 W9 h, W' b) e% j1 p/ |
if (cotacoes[1].high > cotacoes[0].high)* @) M$ F# R$ a, ~( z2 A
{
4 a  s* U7 K( \double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];7 W7 m# P. J* I2 G7 ~
info.NormalizePrice(sl);
. H; T" r1 p+ v- y3 i. L! l! j$ rif (sl > SL). A) {3 g4 v: G" J1 }" ]
{2 ^$ J% K4 V4 }% f
negocios.PositionModify(_Symbol, sl, TP);8 q4 I6 p6 X# X) a3 |) }1 q+ \
}
. V/ H9 d1 U, M% p* f; k}* T* f+ P# _' j* b3 z3 h
}
) N  q; W9 S& e( X# U& Relse // tipo == POSITION_TYPE_SELL* p0 T  T6 V' p$ P: M. }
{
2 E- [* ]! N% h' Aif (cotacoes[1].low < cotacoes[0].low)
  D4 l/ t) E: d# P, V3 m{4 g' A2 s7 L% Y" }
return true;; x5 Y6 z, A) z0 G
}4 @5 W  d) I& _, {* V5 j- n
// there was no position
: E  d9 P9 x  j1 K# Zreturn false;" w. U' w3 p7 |7 R, e
}' X6 \, }5 V4 s- A; k
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。2 }+ V" X$ i, ~/ M  }3 P/ f
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-14 13:30 , Processed in 0.589550 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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