私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA9 i* l/ g6 f0 Q7 a) n
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
% P* n; {8 u9 o1 }为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
7 G+ Y5 a+ `' D" v1 t. C0 v9 e以下是制定这些规则的代码。
) b' O7 v( r! f//--- Indicator ATR(1) with EMA(8) used for the stop level...
, S2 P/ R& `+ F- a0 k, Nint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
2 k, D, R  ^* H, h2 `' }$ H* Oint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);$ J  Z6 n& u; \  ]0 B$ W
//--- Define a variable that indicates that we have a deal...7 a  T) }" d; ~0 U! n& X, _
bool tem_tick = false;  c, T, v( g- d4 B" }
//--- An auxiliary variable for opening a position
! G$ E% v0 k  S0 w; j9 W8 \" g% Y#include<Trade/Trade.mqh>
2 }5 Q8 E# t2 W$ i" J) Y# s' G. c4 q#include<Trade/SymbolInfo.mqh>
2 P3 i/ D$ [- s' T- R) e# G0 GCTrade negocios;. ~2 f6 H8 g0 y2 J* h( I
CSymbolInfo info;/ e2 D5 n( F/ x4 Z
//--- Define in OnInit() the use of the timer every second
0 X  q2 J8 \- _; k* g//--- and start CTrade
9 G4 Y3 t+ {7 X8 k; m( e7 [int OnInit()
! J$ A- |( X4 j  v5 \! l{, z% `+ G  w2 N
//--- Set the fill type to keep a pending order
5 T9 u; p4 B, R+ W/ f1 J$ I//--- until it is fully filled6 B. Y: `" M( q" M5 d8 u: y) [, L& p3 y
negocios.SetTypeFilling(ORDER_FILLING_RETURN);7 l+ s1 z  {- t) i$ B0 ~
//--- Leave the fixed deviation at it is not used on B3 exchange
: z& N. {0 z; Y! X$ |! x9 ?) V6 Snegocios.SetDeviationInPoints(5);' \+ K; [) @3 ]6 Y
//--- Define the symbol in CSymbolInfo...
9 n9 x/ v  I1 t6 E4 c1 ainfo.Name(_Symbol);4 \4 i- `8 C  b
//--- Set the timer...* H3 q: q+ F6 D2 c( Z. F6 }
EventSetTimer(1);2 p/ R6 S5 A4 T4 i' a" }" o
//--- Set the base of the random number to have equal tests.... E# l0 J0 ], k) r% E7 |1 T& \$ w
MathSrand(0xDEAD);
+ U! O& r8 j2 S! Breturn(INIT_SUCCEEDED);* ]1 h3 ^, S: a0 M0 f9 i6 h  h
}, X! O; I6 C( _4 U6 r  [
//--- Since we set a timer, we need to destroy it in OnDeInit().9 {3 k* T' F  |; f/ M
void OnDeinit(const int reason)) g$ U) t7 a  c. y
{
: k/ D8 J5 g2 i& k6 IEventKillTimer();$ J  q4 {& Y% y
}
3 G: I$ }9 P& x//--- The OnTick function only informs us that we have a new deal
1 E5 v4 x7 f* G, A" Tvoid OnTick()0 N- D1 k( f- X  y
{& s6 h/ N) w% K; y
tem_tick = true;% ~0 m4 i; R# Y
}6 j2 m) s: D7 |
//+------------------------------------------------------------------+1 r  _6 _# y# z4 j$ l5 D7 p
//| Expert Advisor main function                                     |' k& X  o3 D, [1 c
//+------------------------------------------------------------------+7 p+ j$ s; u/ K8 k
void OnTimer()
  d( `- J1 h6 G( s+ z{
0 _$ J  G- k6 bMqlRates cotacao[];/ y1 ?7 M) v# T2 [: F
return ;
# u8 Z/ V8 m( t3 Z/ j$ Q& Bif (negocios_autorizados == false) // are we outside the trading window?9 a& Z2 n/ X  S1 Y- Y1 R4 H
return ;/ f* e- ~+ c1 x; r5 M7 t2 o
//--- We are in the trading window, try to open a new position!
- D6 u& P: Y& X+ Uint sorteio = MathRand();* a9 k8 X' _0 b8 v# }9 S4 q4 S
//--- Entry rule 1.1
: e) x  q: c) z2 C6 |if(sorteio == 0 || sorteio == 32767)
+ k8 Z- v  ?9 o; O; x; freturn ;" j+ [" m0 u* \8 N! u
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
0 p2 o& c2 D2 I8 e1 G0 J0 K& M{; I: v/ g5 C3 h. s9 k, |
negocios.Buy(info.LotsMin(), _Symbol);
+ l. P% p/ o* e0 Y}
5 M5 a! k: @0 F1 n+ q1 d; Qelse // Draw rule 1.3 -- odd number - Sell( t$ U3 s5 H* n' a3 T# L
{
1 L: X- f7 X! c9 Gnegocios.Sell(info.LotsMin(), _Symbol);5 _- @5 C0 J" m
}  _% A! `, ^# M7 t3 N1 X8 {
}7 |+ @# L6 U: T0 L' U
//--- Check if we have a new candlestick.../ p5 t5 N. N' M! z+ h# R. l# P, k
bool tem_vela_nova(const MqlRates &rate)
" c* d6 |! u1 Z% ]" l( p! e" W{4 ~) L) R$ w% V/ |
{. f0 L) H5 L  H2 o6 u+ p6 Y. R9 [
ret = true;& R: M! ]6 S+ @
close_positions = false;
9 K; X; S- b) \7 [% S}- i' y! }! J! H* Y; K! R) G) M
else
2 N2 M) a" [" e& l{
# K& D1 ?& s5 h5 L/ c: f& eif(mdt.hour == 16)
; ?; u; v- b& e$ uclose_positions = (mdt.min >= 30);7 Z- v! r$ q" q
}# a; d$ E! Y# f$ e- O8 r
}
% }7 S0 o4 {4 m% M+ hreturn ret;
! G2 R) Z+ f; Z! E5 B}
* I' f3 X  q5 M$ N% M//---
* a9 w& {' G3 s7 u; C9 f2 A: nbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
: G0 i* N2 d% W: L! w5 ^) a{( J# {9 q" Y; L) Q& k
if(PositionsTotal()) // Is there a position?
" l! O! {9 _1 M  w) v{- _$ }) s! T" X% W* @! k
double offset[1] = { 0 };
- \( |; x+ O4 X4 p4 Hif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?- Y- {- C! v/ |+ W( ]  Q
&& PositionSelect(_Symbol))  // Select the existing position!
" _6 Y8 u0 Z- D) I+ A& ]{
& u. g) T! G4 p+ sENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);1 H; [1 l9 Q/ h1 [
double SL = PositionGetDouble(POSITION_SL);
4 \4 w/ U: k; n% ^( @double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));$ f/ [4 g( D8 N( [* S
if(tipo == POSITION_TYPE_BUY)
( F" x0 N7 E0 y) p{
& ]  r+ m+ H: M4 M" B; @if (cotacoes[1].high > cotacoes[0].high)3 }- p5 a3 Z6 i! X( F# G$ N
{
0 I5 i" n2 a( D( `# {  pdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
$ x; _; F8 M" k: Z1 iinfo.NormalizePrice(sl);* p9 I* m; z+ _8 O3 Y9 _
if (sl > SL)
  q$ l5 H) N6 N, X+ V- C{# `8 M0 `. T8 ^" ?
negocios.PositionModify(_Symbol, sl, TP);: j( G% t( D# [* y, Y, p$ T3 h
}
1 g7 b! ]" J2 G0 W8 {5 G% Q( T" H}9 E4 p/ I' M$ f( Q
}
, M$ U. [' I, {0 o9 e5 [1 ^else // tipo == POSITION_TYPE_SELL% q4 y% \; ~, Y. `! t( o/ d
{
. w' `! Z! G1 m6 @5 Y% mif (cotacoes[1].low < cotacoes[0].low)2 l1 X; a: |, y/ X* O9 H
{
  f( X: ]4 X+ U* Q; ?return true;
, m1 R( L+ B( k4 j  n  l) j% e}
3 i$ w! P! l6 A// there was no position/ f) G0 @8 K, {; a8 I
return false;
; L/ t8 d) G+ u( L, n; E4 c}0 @# U! q5 ^# W1 a
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。  L: q( G* e% t, J. U
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-22 17:02 , Processed in 0.647235 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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