私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA4 Q* R- u1 ~5 |. s& c# Z7 v6 o
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。' Q1 C7 f* e# ?' l4 o: d
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。7 s$ R* v6 A* @& r  {. Y
以下是制定这些规则的代码。
% D1 [# H+ b8 w" Y+ R+ o; u! }//--- Indicator ATR(1) with EMA(8) used for the stop level...
7 D. D  u1 V; b( B& ~int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);9 r- {$ \# K0 ~
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
7 @- a( O' M* @& O. g6 s//--- Define a variable that indicates that we have a deal...
4 p  n' a" I0 v/ S# L3 T- j  k* f# lbool tem_tick = false;& S7 @& A/ w0 T) M" K3 O
//--- An auxiliary variable for opening a position+ w: x% G6 x# M
#include<Trade/Trade.mqh>0 y: F. q. s  }5 A) G+ y8 m
#include<Trade/SymbolInfo.mqh>1 V9 u* D( C$ X* p2 O0 D* a- y; E
CTrade negocios;
6 v3 X9 z1 g# O0 }CSymbolInfo info;* J) k' l; D2 [5 ~
//--- Define in OnInit() the use of the timer every second
( \) v7 X& l# x) i; e4 i//--- and start CTrade
( q0 X  W3 v4 o0 G% k# Yint OnInit()- A0 R  x' A  X% @  N! Q
{
$ K6 a2 J. j: O. @//--- Set the fill type to keep a pending order* u  t- ?) X1 c# N6 O: F
//--- until it is fully filled
( G4 X. H5 G% y# f/ Gnegocios.SetTypeFilling(ORDER_FILLING_RETURN);
% O4 M8 e- L% \4 Q1 @//--- Leave the fixed deviation at it is not used on B3 exchange
# e) x6 I) C% v6 Knegocios.SetDeviationInPoints(5);
( R; Q& ~' E) ^" P+ J//--- Define the symbol in CSymbolInfo...
6 A  H3 E/ ~' Q5 H. ^. y" iinfo.Name(_Symbol);
7 {' `2 Y) i( g. L8 L' n& j- B//--- Set the timer...* P+ E) \0 F; K2 J' z/ _
EventSetTimer(1);5 R) ~$ j1 k5 @; i: c1 T  H% i
//--- Set the base of the random number to have equal tests.... @4 f# _5 Y" j1 ^/ k: b7 _
MathSrand(0xDEAD);
/ m6 l" D7 J+ f  ~return(INIT_SUCCEEDED);
' U+ [- G& a3 B. `. B7 f}) h7 Y! J. K$ n
//--- Since we set a timer, we need to destroy it in OnDeInit().
3 W  B6 B5 |% n! W! f+ Ivoid OnDeinit(const int reason)* B0 I3 d5 T- P/ T6 f, w
{, [) ]& `3 ]& R3 n( r+ E. ]
EventKillTimer();
) s  n2 A2 ?% K8 m4 Y}
& d. |$ g% U$ M% Q//--- The OnTick function only informs us that we have a new deal
' f7 m- j; e8 F+ ~void OnTick()
+ V7 ]# T4 ^. _9 }% Y6 m: ~{
5 n8 t- {" d* U0 ?0 z& x, H+ Ptem_tick = true;
$ ]5 [6 u8 I- Z+ I5 \4 K, m* T5 ]8 C}7 u, `& x4 d- J* F
//+------------------------------------------------------------------+/ z' g: h  n9 l; s4 |0 g
//| Expert Advisor main function                                     |% E, s# N' R! v/ E# X2 n
//+------------------------------------------------------------------+; }; h; D/ F8 N7 p7 Y: \
void OnTimer()
( _; r6 I, G4 L5 t. j{
& R' x' a) z( r( v) k; VMqlRates cotacao[];' c7 m2 p" |; W* B8 v
return ;
1 x( X/ h7 ^' M7 O$ H- @! Uif (negocios_autorizados == false) // are we outside the trading window?3 C, F* ?( H- G% w+ Q& s0 Q
return ;* A" a+ h2 u" K
//--- We are in the trading window, try to open a new position!
3 V0 s8 U4 `* h5 R1 Z8 G9 vint sorteio = MathRand();0 n. a1 B/ \+ S* d
//--- Entry rule 1.18 m+ ^& M) a# ?) X/ e
if(sorteio == 0 || sorteio == 32767)
+ Q2 O! N7 L0 l/ E) greturn ;
* S- O% @" P9 Q/ Nif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
, e0 w1 w* N" z2 |+ d8 |" {{
9 U% O- c4 i6 q& I6 j, a  knegocios.Buy(info.LotsMin(), _Symbol);
" P2 ?! d9 Z& ^/ T  @}2 O6 W7 V% X4 X1 |& o- ^+ \
else // Draw rule 1.3 -- odd number - Sell3 O0 w; ~7 b( w+ T* l
{  `* U2 V0 C% N! \  \7 u3 ~
negocios.Sell(info.LotsMin(), _Symbol);
/ o' Z# U$ V+ ^}  x/ w  p% }7 g' k* _! T
}
# H+ P# e1 H1 z" Z//--- Check if we have a new candlestick...
5 e, ?% c" s6 [' |: Kbool tem_vela_nova(const MqlRates &rate)
* X) o, q# z0 N{
4 ]7 p# ~- m9 o1 ^# T7 l4 @7 e{) \. J! z: t6 w
ret = true;
$ z; E3 \3 }6 pclose_positions = false;! E/ j8 u8 @# {
}
2 V2 `6 K7 g' S- _else
; t; l& T( e: ]1 S: [0 [3 o{0 L4 y* }( R; o0 J% N! T2 |
if(mdt.hour == 16). K( j/ k1 I$ y. D$ U0 Z8 }
close_positions = (mdt.min >= 30);4 N3 r9 [. P- v1 D0 z
}
; I% O% J8 P  }}; v) m: m0 T2 t  o% C
return ret;
6 ^" I! A; F7 b}
6 N7 G. j2 {8 w. p. I. `//---
& l9 r) ]$ L& ~& P" J2 B: qbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])  k0 |3 O8 c% \+ M8 w+ A$ N4 i7 D
{
# n' y7 I' {" Q; c, X0 Kif(PositionsTotal()) // Is there a position?8 z0 S2 l6 P# g9 T3 D$ R
{& Q+ \% R- Y8 E4 Z/ l) N
double offset[1] = { 0 };" ~# C2 L* }7 X8 J
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
; u" Q2 c7 ~8 W% M&& PositionSelect(_Symbol))  // Select the existing position!
! f0 k7 \1 c  f3 w/ `0 Q* _; p0 c{. N7 }" D% k/ _
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);$ W1 N% N" F9 S4 y, a
double SL = PositionGetDouble(POSITION_SL);
( t8 X2 t/ E' S, G! p6 Qdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));9 A/ Y5 g. s1 @- l- J: Q0 w
if(tipo == POSITION_TYPE_BUY)
. B2 N9 W/ u2 k, `{
  [; o/ C# N+ P4 B; [8 n  N5 eif (cotacoes[1].high > cotacoes[0].high)4 |* ~( U4 f3 G: j8 L
{
  ~' X/ G$ H, C+ m3 {5 ldouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];: k  T4 V6 j! ?. Q2 @, b
info.NormalizePrice(sl);
4 `* c4 U* u( E3 b: |( \, Pif (sl > SL)
7 E3 c2 g( D* |# _{5 b; Y$ l0 l; y2 Q
negocios.PositionModify(_Symbol, sl, TP);
" y) ~$ I# o0 ]* U1 U1 A}
# W6 t% H# `# g4 F5 I% |5 W7 c' L}
( k1 r' u9 s: m% ^( v& w3 b! d- e}
5 n9 @- a* |/ @" ~# A8 c' E: A" u6 U0 uelse // tipo == POSITION_TYPE_SELL
2 n0 `- C$ l3 @) h9 s+ E$ {; Q{
/ M2 Y  i3 F9 T1 Z5 _if (cotacoes[1].low < cotacoes[0].low)
9 A+ t# v6 H# ^  o- B6 X. F{
9 V3 B3 U" Q1 F2 vreturn true;
, q0 ?, k4 F  v6 J8 o" G}' M: T# s7 @0 Q4 _3 c. |) |8 v
// there was no position7 G6 A# j; v  R/ l: U
return false;% l7 O3 c; k- g# ?+ i
}
& V5 G/ A8 y7 ?* I& {- }我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。& w3 |+ P, Q/ r5 h
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-21 01:54 , Processed in 0.493094 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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