私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
& a4 C  L+ _+ R) j: T在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
7 H: b0 W9 o' I$ ?7 o0 L# C为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。! C( q  W9 v( p+ H3 \- R
以下是制定这些规则的代码。
2 O. ^* a* T9 ]. F* e3 W/ w7 u//--- Indicator ATR(1) with EMA(8) used for the stop level...
4 D+ y% I/ W! A$ C, |int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);5 @- W( `' ?9 n" Q) S7 G. d
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
2 I  \! j1 q( k  y/ E//--- Define a variable that indicates that we have a deal...0 G# k/ j2 t4 `. R3 \: e' v
bool tem_tick = false;
: z$ O" [- i1 T//--- An auxiliary variable for opening a position
4 b( p6 C/ d6 O6 K# ~#include<Trade/Trade.mqh>
( l* o$ i  B6 |! C  N6 q#include<Trade/SymbolInfo.mqh>
. g, b& v6 s  M4 \% i/ Q& PCTrade negocios;
8 Z* @6 c0 @0 z5 ]. bCSymbolInfo info;
2 Q2 R6 C: [6 r8 t//--- Define in OnInit() the use of the timer every second! j+ N( z" K" O7 q* ]
//--- and start CTrade
/ r3 m/ l/ j& z$ Jint OnInit()
' e5 [/ W) E7 i5 L; Q8 H9 U2 {# e{
! L& n* z; R7 E0 f//--- Set the fill type to keep a pending order
/ J( X  K; K, E  V1 H- x//--- until it is fully filled7 O% T% ^  }5 {7 |+ S) V8 [( K  M; h
negocios.SetTypeFilling(ORDER_FILLING_RETURN);2 p' C" d- b1 h( T& {
//--- Leave the fixed deviation at it is not used on B3 exchange% K% W# x  T2 ~7 o
negocios.SetDeviationInPoints(5);
0 T, ?9 c) _" N) v3 b3 U//--- Define the symbol in CSymbolInfo...
. _& E; p" v6 S0 z1 G. S7 _info.Name(_Symbol);' [! o, w! z% a3 J/ p
//--- Set the timer...
! l* @9 l- q/ S" n, r5 e( }EventSetTimer(1);- t" {( L6 a! g- @0 g
//--- Set the base of the random number to have equal tests...
% q/ n- O5 w* c* J% X! KMathSrand(0xDEAD);& p5 A# h( z- O) U
return(INIT_SUCCEEDED);7 i2 j. r( p0 d( l4 w  o3 s
}
. V0 d' {3 K; _- p4 m* K//--- Since we set a timer, we need to destroy it in OnDeInit()." d% Z, y+ D* t+ R2 V! ~5 j5 p
void OnDeinit(const int reason)
- P% b9 e% A" Q% a{; v0 S3 o7 A' ]$ D; `" h' C. r
EventKillTimer();) Q8 Z( m* G7 y. R9 n8 |
}
8 n& ^% N. J# L, R- n//--- The OnTick function only informs us that we have a new deal: c9 i, l' G( k5 s- R- x
void OnTick()3 b  s9 r6 p9 ^9 C6 k7 m
{# c6 O7 }& |3 ?1 H& Q& J
tem_tick = true;
7 o+ W) {8 w; @; p; I: F- Q$ b# y}% O4 n& u# g' h$ l  _! k
//+------------------------------------------------------------------+
9 m3 ?6 B/ a5 h( Q7 Z//| Expert Advisor main function                                     |
* B- u6 Y/ ^3 |7 T1 n9 T2 U: X//+------------------------------------------------------------------+
8 i( c/ J5 A2 a- J1 l3 vvoid OnTimer()
4 N; h" U' c! p  e1 K/ w{
5 n' c1 e9 F5 d, }- V% XMqlRates cotacao[];8 D" Z! u! q  W' E/ g! m
return ;
4 Y: k0 \1 }6 j2 Hif (negocios_autorizados == false) // are we outside the trading window?6 Y( ^; P9 S* {  ]  l4 X/ z
return ;
1 G0 a0 E: c% ?//--- We are in the trading window, try to open a new position!% b+ H4 |* P5 G( @7 H2 c
int sorteio = MathRand();0 A1 N7 g6 k. e0 t# {) T
//--- Entry rule 1.1
8 |. s: E0 ?  N) y: qif(sorteio == 0 || sorteio == 32767)$ h: G4 e$ ~& m/ e4 k, A6 `# _
return ;7 r- i' K2 L3 K* d- _6 O9 D2 q- L
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy, }* b! X0 L3 v, n5 B
{
3 f0 Q8 _* V4 x6 j& tnegocios.Buy(info.LotsMin(), _Symbol);' k0 ~0 [6 s6 f" X
}; _" [  Q- s1 E+ {7 F3 ]
else // Draw rule 1.3 -- odd number - Sell
# O4 n+ s# U8 N& t8 W{/ E9 N' J- H, L% \0 J% J
negocios.Sell(info.LotsMin(), _Symbol);
% P$ c" Y+ v0 \}
! }* ]$ W6 ~# C* j9 a' |}/ n" S; k$ u5 O2 h( f
//--- Check if we have a new candlestick...3 a6 `3 ]3 C+ f3 p$ W7 X
bool tem_vela_nova(const MqlRates &rate)' P5 m& F3 X6 t+ ]
{
! @( ]: L$ E" G! U! u; t{4 |; k8 K. j5 j* o
ret = true;2 F+ O3 t# @" J% ?
close_positions = false;
9 _; N2 v& v2 _/ P, V: w8 G! U- Q}
# p% Q# E* D( g" g" D2 telse- X3 ~& B" J5 S# Z
{' I6 F: K' W; K% l. z
if(mdt.hour == 16)
! \9 `' x" s5 {/ wclose_positions = (mdt.min >= 30);
1 G, u  ^) e0 Y. F1 J}
& X$ f: c4 w$ z  J9 f}1 B5 R$ N  k/ A5 Z8 i
return ret;2 D; y2 H) }# }1 p% N
}
6 \3 g% e, v# D3 m8 v, w! ]! S5 D//---
. w! }2 r; a6 [& q" O- ?bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])( K& l5 ^) c5 a9 `! X- B# B$ N! @
{* q5 p, g4 I! Y; h1 I) W
if(PositionsTotal()) // Is there a position?
* h, J0 ?# t7 g5 k{
9 x, Z! D$ C, q0 D& Fdouble offset[1] = { 0 };' T6 s% _& v- O/ \
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
) X9 `7 |* N' m, }6 Z* i&& PositionSelect(_Symbol))  // Select the existing position!0 c9 ^8 M- ?7 Y1 }# c
{
/ g7 M, N0 n- W) ?ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);. b% ^/ _2 d) u
double SL = PositionGetDouble(POSITION_SL);- h. l* s7 L: C+ J2 r+ g8 T2 T
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
, S( M: ]0 g1 x; x# Zif(tipo == POSITION_TYPE_BUY)+ p# y/ v0 C1 u) o
{
0 z/ y5 F* b1 z2 tif (cotacoes[1].high > cotacoes[0].high)' S' p( C. R3 b6 s; V3 ?2 J
{
  `1 L; \8 r: c: ~. f1 Ndouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
1 r9 P6 z3 H- @info.NormalizePrice(sl);
; |4 n7 O" s+ _  W' y7 w( hif (sl > SL)7 T5 p) j+ a- }8 K
{/ a1 G+ e+ J7 Z# z# x% ?9 N& {
negocios.PositionModify(_Symbol, sl, TP);4 h9 B' Q% P3 [! ]; ?
}
2 Y/ v1 o3 B8 v7 N# T}+ Q8 @! U8 s$ {
}2 a+ d7 U: P( H& T1 v- Z& ~
else // tipo == POSITION_TYPE_SELL, j% Y1 [) {$ R* t
{& j% B( S/ d# h
if (cotacoes[1].low < cotacoes[0].low)
1 {5 @  R* T5 t% Z7 M/ E  P& d{: r1 _9 V7 W% Q! m% c. T# U5 }
return true;
) o, R5 |' z, C6 P# Z( r( F7 n}" s7 x: n0 a+ ~- F3 B2 ?
// there was no position
  ~& s3 a4 z4 Freturn false;: t0 k7 W* i5 |* d
}, I& b5 q' O; k$ M6 C
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
9 k# Q! O4 c9 [* L+ V; t8 O到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-25 15:16 , Processed in 2.211886 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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