私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
0 d# n6 w5 H& d在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
) z! u& h6 W) m# w为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
2 [! e  }; t3 I) v* J( R$ L以下是制定这些规则的代码。
! g4 m4 l9 L- E: O9 V. R//--- Indicator ATR(1) with EMA(8) used for the stop level...
7 f6 y% ?7 r% x8 G: D. b; J7 jint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);6 T8 [: o( Y6 k+ Q2 Y9 u- n. G$ e
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
  {: T7 l7 V2 V, b//--- Define a variable that indicates that we have a deal...
1 s) ]5 B6 S! D0 v2 U6 ^! s, Wbool tem_tick = false;
; U' G% R- W" N* U//--- An auxiliary variable for opening a position: M, [+ B1 @6 K- m  ~
#include<Trade/Trade.mqh>
" w( K4 L3 S/ S( b/ h3 n& |#include<Trade/SymbolInfo.mqh>$ n9 h( [* w6 a
CTrade negocios;
& g: T+ w, s& s7 xCSymbolInfo info;* P& t, ?$ c: r. l
//--- Define in OnInit() the use of the timer every second8 B' \5 v' F- K5 k6 O
//--- and start CTrade
' o3 p6 a# Q- s9 ~  v# w1 {int OnInit()
1 q) {; |. V1 y{
* I- I2 z6 L  A! e' w//--- Set the fill type to keep a pending order
( q( `5 H0 ^$ _//--- until it is fully filled2 u& d' t* ]! n0 C( M* C0 `
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
; P/ i7 ^9 i% d* @3 |/ ^//--- Leave the fixed deviation at it is not used on B3 exchange( G. c4 o2 L5 ~" P
negocios.SetDeviationInPoints(5);
) r7 g( C( c# j* X1 @) {0 B//--- Define the symbol in CSymbolInfo...
2 Z6 s  S/ H# o& vinfo.Name(_Symbol);
& S- F/ L" w& b6 V7 p//--- Set the timer...
  x4 }' g/ W; j$ jEventSetTimer(1);
% g" w4 B& `* R+ g4 i//--- Set the base of the random number to have equal tests...
6 N; g, \/ c4 ?MathSrand(0xDEAD);
) `( o- b7 m% Rreturn(INIT_SUCCEEDED);7 g& d% N: w; A  x' o, A9 w5 G) e
}2 ?0 Y; I6 Y4 v  Z. b7 G
//--- Since we set a timer, we need to destroy it in OnDeInit().) o3 ^% f: y4 T
void OnDeinit(const int reason)
# f. X0 d; Y- E# _/ y{
0 S* T) v8 T- J) V/ zEventKillTimer();% @; B" L6 H. ]) B7 `. L* E
}- ~" v: }* S0 O( r
//--- The OnTick function only informs us that we have a new deal
. w- K3 d, v* T8 S3 M. Xvoid OnTick()
3 F$ O& V. L" L2 r{
& e8 z6 I% I: ttem_tick = true;: |2 Y9 a; b7 o3 z1 e3 m
}
% I3 R  f6 ^2 j8 Y, a( }//+------------------------------------------------------------------+& N: x$ O; \( e$ B! x' r
//| Expert Advisor main function                                     |
  T" T9 _% n1 I5 A  y1 [7 R//+------------------------------------------------------------------+$ q  N. `$ @$ D1 G9 J# ?1 N
void OnTimer()
# m. \' m( u9 A- \: B{
  p2 \& G" C6 A5 ]  n! W2 |MqlRates cotacao[];
; t0 Q# r" k& B$ b  j  W; U! p$ breturn ;
* G1 _0 t) E( N2 z7 m3 |if (negocios_autorizados == false) // are we outside the trading window?3 P# y: O& v; M2 E! ~
return ;: t; Z" O: E* }( y5 C- a
//--- We are in the trading window, try to open a new position!
0 X6 e/ }3 }1 {# hint sorteio = MathRand();
' M/ M7 ?" P5 R6 e# d; ?: P//--- Entry rule 1.1
6 l1 y: H4 L8 G/ r$ @if(sorteio == 0 || sorteio == 32767)3 b0 b+ @& E1 M5 }3 b
return ;( ]' ]# p3 P2 B5 ~2 r4 W1 `
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
: \. C! k) C) `  i( q3 B{
! |2 ]# D  J- g7 u% Unegocios.Buy(info.LotsMin(), _Symbol);& q, D  g' j8 V* q* _' F% {/ l5 G+ m
}" [( R8 k+ Z' t1 G
else // Draw rule 1.3 -- odd number - Sell
! l0 i4 i" J  y- p2 K{
: C5 [9 f3 O) b" t) rnegocios.Sell(info.LotsMin(), _Symbol);" c5 ~1 `4 a+ j) ?
}% @2 z9 M# w- G  d
}
) e& ^0 x, A6 Z7 Q' d5 [1 Q( p//--- Check if we have a new candlestick...& M, n0 |' k0 g! P- s6 J
bool tem_vela_nova(const MqlRates &rate). O. W: C  C) x. @5 \2 k! ^2 Z
{
% k/ ?! c* t, X! a4 G+ _{& {; e9 ]8 K! ]$ D
ret = true;
  R+ |' e0 x6 v; Yclose_positions = false;3 j* Z% \9 }- @- e
}2 m- E+ j$ z* B0 f2 [; A
else: K; o, d8 ^4 T! N$ w, F
{
; G, `! J( r% U5 v6 A2 m9 Yif(mdt.hour == 16); e& |, @) c+ I5 v( c$ {
close_positions = (mdt.min >= 30);5 t/ X8 z6 y5 y6 F* @
}
5 X: h& I; n7 I5 y/ G+ I# ]}0 b" v0 x5 ]% Y3 S8 O
return ret;
3 _2 s! T8 \. y+ m3 T5 K( Q}
1 Q0 @& ]( j) ~1 s. b; d  W//---
: ?/ @% s4 O+ b. Pbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])/ l: ?0 t: k4 d! n( \% c
{
: \$ g; N3 Z0 v% @if(PositionsTotal()) // Is there a position?
2 V. t0 [5 e' J+ {$ O/ w{/ }- y& W3 N1 }6 x6 x" D4 J
double offset[1] = { 0 };
5 o- M# r! E/ l9 p7 f7 [4 ]: i7 Cif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
8 c  N( E4 w7 N9 S% l/ N; S&& PositionSelect(_Symbol))  // Select the existing position!% e; q- q8 m; c( r, g
{
% r6 u2 m" v4 _1 G6 _ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);& f, Y& i# }$ j4 e/ x" g, A( n1 e
double SL = PositionGetDouble(POSITION_SL);9 a9 d) F7 s" O3 m8 I. E
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));7 p$ x- u+ g! L2 F1 G5 d7 M# [+ J
if(tipo == POSITION_TYPE_BUY)9 P4 {& Z! m# u; ~
{
) b6 ~3 s8 x3 ?! V* V8 C) h, Uif (cotacoes[1].high > cotacoes[0].high)
5 F1 Q% U0 D9 ]: \( _& \! i) t{
& G1 |5 H! N+ R2 t$ }* B# S% Cdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
* o8 G( O' Y5 z+ T: Yinfo.NormalizePrice(sl);
3 V- a, ~/ x& r1 D9 N$ C2 N" Pif (sl > SL); E6 n8 u& H* a
{
& f5 R# }7 C' @* B% _- X/ onegocios.PositionModify(_Symbol, sl, TP);
3 |) h5 P! B- N$ X! [9 C# t}
/ q9 ~/ e% v) R$ ~' A}1 E, V- B8 t  e" r
}2 p/ Z7 @$ e+ P% }, f, w* A
else // tipo == POSITION_TYPE_SELL
- U3 u! T4 z% E) z5 ^{  j1 }% C3 t, d) p) o2 ]
if (cotacoes[1].low < cotacoes[0].low)
6 D: V9 g& B5 \. U$ v8 }{
5 Y) a! N, J! x/ Z" E  D/ V8 ~return true;; s$ W, e$ B; p+ p7 d+ T
}$ H8 P, l; U7 k8 I
// there was no position% l- r) V. K$ e; S
return false;& j9 O7 J0 |! {$ P, m. ~, }
}0 ~; R1 ]/ g+ f' A& f% _% C" M
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。+ W  t6 C" ^% ~
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-6 19:03 , Processed in 0.694748 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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