私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
! Q/ u8 `) v% x0 p- K+ |5 A- I在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。( d- e8 {* a7 L6 L" P" k$ Q1 o, c
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
3 {- e+ x0 `( G* C6 n, n  A以下是制定这些规则的代码。
! h2 a+ d" v! c1 [- Y9 e+ D//--- Indicator ATR(1) with EMA(8) used for the stop level...  b+ h2 S2 w' ?7 J* ]) ^9 U# G2 P4 s
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
" O5 z5 c7 M: zint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);. O* l, G- C4 f0 ]4 k
//--- Define a variable that indicates that we have a deal...7 g# X4 {7 p) o( T7 W8 f
bool tem_tick = false;
. D) {  P& {. x3 z//--- An auxiliary variable for opening a position& y+ D( a# U4 z( S8 Q( q3 B) e, o6 H
#include<Trade/Trade.mqh>
; n1 I! B+ S7 r% }3 I- L#include<Trade/SymbolInfo.mqh>
) R2 k- T1 X$ t4 U( r4 c. eCTrade negocios;
( P( d. |( D2 G# nCSymbolInfo info;6 W" F% L2 P, L( i9 Z* W
//--- Define in OnInit() the use of the timer every second8 S  t- g+ u1 V
//--- and start CTrade- K  m9 A5 h+ Z! u8 D
int OnInit()
! Z  t" l! ~" y% ~0 m( Z- C{
+ {  S, @( s- }//--- Set the fill type to keep a pending order
, k% s, x6 K) Z) i% P: m) J//--- until it is fully filled
# n) a9 ^; B* ?" R+ c+ Hnegocios.SetTypeFilling(ORDER_FILLING_RETURN);
1 z' M1 R2 J4 B( G/ c& l//--- Leave the fixed deviation at it is not used on B3 exchange/ W# l' B9 V5 d+ A' E
negocios.SetDeviationInPoints(5);8 F3 g) I- a, [/ |0 Z5 H, x
//--- Define the symbol in CSymbolInfo...3 [! g2 y3 h2 v" D7 Z/ k
info.Name(_Symbol);4 T, O6 d; M/ b: I9 E7 O0 E. L" p/ m
//--- Set the timer...* t4 f* Y2 u4 ~
EventSetTimer(1);; |# N8 u* H2 O/ v- F/ r, E
//--- Set the base of the random number to have equal tests...* r' _$ k" p' i; d# H
MathSrand(0xDEAD);( `6 F1 y( E1 u$ T% R' u( B
return(INIT_SUCCEEDED);
0 h+ B' Y; B0 u2 _2 E}
& x6 g1 J$ }6 t//--- Since we set a timer, we need to destroy it in OnDeInit().
- y3 |# Y* F9 S! n* }  L- Yvoid OnDeinit(const int reason). S4 S2 T5 g# q8 r+ t3 P: ]2 y
{, R" `3 i. A' q: L3 |# z) {" W4 I
EventKillTimer();( y# {! u* B( Z9 U: p& a
}$ u* _% A$ b  k3 O; X. a4 U
//--- The OnTick function only informs us that we have a new deal
& o1 B  p* S: Evoid OnTick()
  b! ]$ X! A8 |/ C6 f( p{
$ K6 r$ j- d# [8 A1 m8 Ctem_tick = true;
. [9 k! g& `: X$ N- x}! }  C- d6 L% n- E! r! q& N
//+------------------------------------------------------------------+) t- \; N6 k$ X$ C2 s  ]& q
//| Expert Advisor main function                                     |
% w! {0 r! q4 ?( F" G//+------------------------------------------------------------------+& D. M' a8 S6 i
void OnTimer()
8 f/ F3 L2 q- M2 `{
" J  I# u7 p- H: k! UMqlRates cotacao[];
+ n' D& o9 o' @; oreturn ;, u7 F# F+ [' ?0 N% p2 }
if (negocios_autorizados == false) // are we outside the trading window?8 O% N9 Q5 s: |# z2 C5 j
return ;
0 U6 c2 Y! }4 P6 |1 D//--- We are in the trading window, try to open a new position!
& {# ^0 X; |: Z5 _int sorteio = MathRand();
+ g( v9 D6 p2 L3 g9 ?//--- Entry rule 1.1
/ M4 Z5 r- Q+ X0 T0 s+ V; Wif(sorteio == 0 || sorteio == 32767)# a! v" B: s6 v& L# a& @
return ;
3 W5 {- ?. c7 l7 c* o, gif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy$ q9 N8 i8 ^  F+ n  d+ a6 e+ C
{
3 L  v" M7 H( D1 J. G9 X2 [negocios.Buy(info.LotsMin(), _Symbol);, i" I8 _2 [% f8 R9 m' A7 y. Q( W
}2 |% i: X1 ^1 e; s: R: T" a
else // Draw rule 1.3 -- odd number - Sell
- \' U2 L  }5 L{
( q0 Z) _& M6 y4 z; Inegocios.Sell(info.LotsMin(), _Symbol);
5 g& d/ ~  r* N8 c5 A! q}# x$ j# q, A$ U6 W1 x/ O0 B, f
}. @7 ?1 o& f! W# K5 V7 x' U
//--- Check if we have a new candlestick...
4 p3 e0 ^2 V1 N" s2 m, {. |bool tem_vela_nova(const MqlRates &rate)! u+ Q4 ~1 d5 U+ i2 x
{, C! O$ w! L& F4 T" a
{
3 K# `4 O! G: o# E% u5 fret = true;7 t- Q* W0 y6 X: Z' A
close_positions = false;
; [6 y) b# u8 |" Q2 I/ G( e$ J}
. R! v2 g6 c, X" Lelse) O1 |, o+ ]% P5 y2 D  h, A
{
2 g( t2 H2 _' M' lif(mdt.hour == 16)8 K8 z* n1 F. P1 \1 H
close_positions = (mdt.min >= 30);# @  ~+ U3 j. _' {
}( P7 ~( g5 k! N0 J. g8 @
}% N# [6 E1 ^) I; p/ [% C
return ret;
6 x  a" t$ e  z7 `, @/ g- D}6 ^) j! V6 ?' `! a
//---' |1 m3 R7 c4 A3 `1 u- o5 x# V
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
) _' n7 ]6 E$ m4 G{$ C" W$ l1 G& Y7 u' S+ O! W  J
if(PositionsTotal()) // Is there a position?
. h' G7 @) _5 g1 g% ~{
0 E' B9 s& T7 m3 Rdouble offset[1] = { 0 };" U2 r- p& g. W' ?  O( d
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?. Q7 A9 B+ G# c" q
&& PositionSelect(_Symbol))  // Select the existing position!
0 o  X3 I+ y/ L; ?) P( t{
! j, n" _) a0 p/ ^4 b) F  j' PENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);: e# Z5 z! }3 }4 t7 i0 a+ O$ x- m
double SL = PositionGetDouble(POSITION_SL);+ p, ]8 X7 b+ Q7 ]8 P2 R
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));) ^  o5 P7 W: e4 X& [
if(tipo == POSITION_TYPE_BUY)
( r/ I9 p( u# }/ a% w{% l" b: k$ y% l5 e# X8 R2 t9 [
if (cotacoes[1].high > cotacoes[0].high)3 H0 M: W7 y& \  L. I
{
4 S0 ]3 b; \+ zdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
, R7 f4 T/ f' W9 u- Qinfo.NormalizePrice(sl);
" c, b- D& D( @  |" tif (sl > SL)/ f/ b* M. B7 ^% g+ S- Q
{4 A0 }+ O5 c1 ?1 d, S
negocios.PositionModify(_Symbol, sl, TP);
( F9 ~# D$ u* Q  [2 T& q7 G}) l8 O3 p% N6 \3 T* @2 J
}1 g1 B, z1 e7 `: ^$ N/ J" Y* A
}* j" o, V- p2 l6 X: S1 Z' d) R
else // tipo == POSITION_TYPE_SELL8 f7 a; }, M* ]& D5 w3 A+ _3 V( r
{
# B4 J) n/ d, t& @# w+ {if (cotacoes[1].low < cotacoes[0].low)
, y2 o& M8 `4 H& Y8 s  X{% x+ u, o# T6 f/ T4 W0 S( ]! [  A
return true;
' B$ }0 |! B. ^# @- h  t}
5 ]5 \/ W4 M7 |; I. t) \% s+ R( d: [// there was no position
5 g( A: P1 W* o- b- Y/ Yreturn false;
$ {4 `2 I7 C3 R/ x1 e- g}
& e3 d* ]+ v' U( c3 M& q4 b我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。) U9 y3 F: Q% X: }7 I/ o1 B
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-1 14:51 , Processed in 0.950792 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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