私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
" P# G$ S$ ~$ A) J% ]在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
, M5 E/ K0 I& J; T为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。' X, L" A! p$ I6 ?6 d
以下是制定这些规则的代码。& D4 c7 C: `# h9 R# B
//--- Indicator ATR(1) with EMA(8) used for the stop level...' u5 _) j% |% o+ o2 O4 p2 p
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);9 U0 S7 b/ T- Y. _% C9 L
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
% a% s; H; ~. |//--- Define a variable that indicates that we have a deal...
* y  `$ S. f  N$ I$ ?/ pbool tem_tick = false;
# e& k/ A/ H% H8 O% d/ L) e& W  L//--- An auxiliary variable for opening a position
; }8 T9 B! A; ?) J0 f' C3 Y' [#include<Trade/Trade.mqh>
5 `- f6 V  s# E#include<Trade/SymbolInfo.mqh>
$ a$ }$ Y. Q$ C, {CTrade negocios;
# L' r' N* K" X) l$ ^9 qCSymbolInfo info;
% @. B& z$ w5 `+ e//--- Define in OnInit() the use of the timer every second
( e- {2 }3 A1 r5 l; B//--- and start CTrade& M& p* ~1 B3 F2 O/ ~4 O! }
int OnInit()
8 C1 D5 u: X, v. c& r$ s+ l3 c. w{6 i7 u! |, @  i# w8 o
//--- Set the fill type to keep a pending order
; u4 {8 U4 L' a  I: e  R+ N/ h6 ^//--- until it is fully filled
8 G9 m6 z" w, `* |. p6 g/ I$ cnegocios.SetTypeFilling(ORDER_FILLING_RETURN);
" D- v7 F  j7 w1 P//--- Leave the fixed deviation at it is not used on B3 exchange
! X6 `/ A& F$ ^; ~) y& z/ enegocios.SetDeviationInPoints(5);/ ]! j+ c) ^# z
//--- Define the symbol in CSymbolInfo...$ N0 U0 t' }. x, Y" J* F' F
info.Name(_Symbol);6 D' g2 a$ y0 t1 l
//--- Set the timer...
, b9 `6 C3 y5 x" }1 hEventSetTimer(1);5 |9 K0 Z0 }8 @6 I$ p/ Z6 O
//--- Set the base of the random number to have equal tests...
9 L0 W" w2 n% g* I; c) ?MathSrand(0xDEAD);7 ?7 z& z) e: G! B$ l! V, y
return(INIT_SUCCEEDED);0 \& P7 o7 b: R; J; x: n' c
}
  ~9 n" s, x8 R! `3 p8 u7 ?//--- Since we set a timer, we need to destroy it in OnDeInit().
) P% e, ~: O$ A, Q5 Fvoid OnDeinit(const int reason)
( Y: n. g: y1 d& l$ {0 m0 F{3 n. @% E. b/ q, w2 L  M+ c$ X
EventKillTimer();
: g& d  \5 x2 \; N}
" W/ P- T& a3 w" |. I7 ^& O% {5 I//--- The OnTick function only informs us that we have a new deal( |: }& u$ @4 N5 B5 Q' O' e: C
void OnTick()% m4 @) A. ]0 x+ h. n
{
& ^# P  l3 f2 k9 C4 s5 O/ Ztem_tick = true;1 d: I+ g) ^+ U, f
}+ O) _  ]3 x' m4 c- Y
//+------------------------------------------------------------------+3 v8 c5 j7 z4 r  n8 ^
//| Expert Advisor main function                                     |
% O* t1 O0 q( o* f1 d, }//+------------------------------------------------------------------+
8 x; o) k* m5 k/ mvoid OnTimer()
" R) r% w1 s* h2 V" i/ c{
. {; `7 T, A# c& }MqlRates cotacao[];
2 W8 r2 P" }- D3 wreturn ;! ]1 ?5 Q1 f9 F9 g7 f
if (negocios_autorizados == false) // are we outside the trading window?2 g" g: O4 x4 f( p# a% v" s# C$ x9 Z/ u
return ;9 N7 p3 h! G6 a) Q5 u" g$ t( e
//--- We are in the trading window, try to open a new position!! N: D8 h5 s* O& D5 h" L2 ~: B
int sorteio = MathRand();+ l" N9 n# R2 R- a" A$ X+ b
//--- Entry rule 1.15 r7 g) I" T1 e
if(sorteio == 0 || sorteio == 32767)# v; Z% ]2 O4 f, i
return ;! i  _; [) h4 l
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy  ?) O! z: R( M0 |# E7 v$ W
{
" n, d- R- H, h+ ]$ A6 W( Rnegocios.Buy(info.LotsMin(), _Symbol);
! M  k1 m( z8 v8 Q  B7 o}
# t+ Y+ I8 Q) Aelse // Draw rule 1.3 -- odd number - Sell$ [4 R& D" U* K  {% W5 |
{5 f3 @8 V0 D. A- x
negocios.Sell(info.LotsMin(), _Symbol);
: `* q6 Q1 {, o+ @- F}( ^# U' `- R, @4 _, O
}
+ u1 m# u0 w: T1 v//--- Check if we have a new candlestick...) J5 @; w# D3 [# S. b% \: A
bool tem_vela_nova(const MqlRates &rate)9 N* V( ^" G9 ~& Z! P" A
{
& |6 ?: k- i9 M8 v' }: g{  U, W5 p+ D. H
ret = true;# w# b* J3 ]. ^( q: ^
close_positions = false;
) l: D( N4 p1 p1 }7 Y" b}
+ {' w% b1 S- S+ \3 E( x! xelse
; L0 C& ~9 U" \* L4 r{
2 Z" v' }2 x" ^" ~( l: mif(mdt.hour == 16)7 X& M% D. Y1 x- r. a( s
close_positions = (mdt.min >= 30);3 _8 u- G, L) _( h: L
}
* f/ W. j. b1 d3 p7 M* u; z}
% F9 U: {* P' T9 \; F& yreturn ret;
: C$ ^/ e4 ], Q) s$ Z2 w}
0 x: M( x' L. _7 i0 C0 h//---
( W: }+ e; b% j' f/ }bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
  ^. T/ a) H) {- N7 R' R7 `, Q' F{5 B" `* b- `9 [. A: j  _5 \8 C
if(PositionsTotal()) // Is there a position?/ M: l9 f3 I7 n$ ~" L! I
{, u+ f2 f+ f8 C0 [4 b4 H
double offset[1] = { 0 };" C) X" a5 D% l; K  I2 z
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
% ~8 D" C( P1 b  O! ~0 {&& PositionSelect(_Symbol))  // Select the existing position!$ h- {8 F. k8 E7 a) O
{
8 F; b/ W/ a# g( Z9 jENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);8 Q9 s) H+ @  `# f( s- e
double SL = PositionGetDouble(POSITION_SL);8 H6 D2 X" p0 ]# I
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));+ w' ^1 w' |& ^7 `; m$ U2 {9 ?
if(tipo == POSITION_TYPE_BUY)1 d) [' K8 M# _& j- w
{
8 e3 D9 W! T/ U4 `' v) gif (cotacoes[1].high > cotacoes[0].high)! W% d+ W) m: B: J7 m
{1 {& w# S8 f" }# U& h' J' ~
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];  c: }+ E# R7 u+ [# c
info.NormalizePrice(sl);
" q  Z2 A7 M0 G5 vif (sl > SL)
) ?; J9 K9 L; O+ B" `{
- l' g9 e+ a/ m- P4 u' knegocios.PositionModify(_Symbol, sl, TP);, q5 N  i5 _4 V% Z* ^: v! S6 M
}
3 M( Q( U5 G- s3 K; j" j7 d8 Q: r}
- R# d) N$ K9 r8 r3 g. k) _  U}" P  d, g0 [$ O
else // tipo == POSITION_TYPE_SELL3 l0 T7 l, Y! g8 F( K& s, f
{
5 M: \+ s  l+ _# z& xif (cotacoes[1].low < cotacoes[0].low)! J: Z/ z' f) L8 P4 O: x6 O
{
! o# N! C5 h0 f7 Z8 r, vreturn true;8 M/ @8 w% [( Q& `- f( ]3 }1 z  F
}5 N+ i% I3 K6 {! `% U( w" X
// there was no position  w# L5 S/ O& m$ n
return false;/ S7 B) n7 L! o" W2 M4 f
}  z- L; |2 _' j) y/ i" V
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。2 {/ t7 G9 ~" ?+ v: c7 _+ I
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-4-24 21:44 , Processed in 2.076082 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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