私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA. z- B/ ]) H& i9 Y1 N
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。3 s; B' \. U( C
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。, H6 j) J2 n( D
以下是制定这些规则的代码。
( q& M. l' l6 e: k2 U3 m) w# y//--- Indicator ATR(1) with EMA(8) used for the stop level...
8 x2 o$ a# A* I* b; K7 @, Yint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
% W2 O' d( g; J4 A$ P- j1 O# Hint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);3 k0 F5 V8 _) m- Y
//--- Define a variable that indicates that we have a deal...( u* D/ H! s: v9 B6 G+ v2 ^3 U
bool tem_tick = false;  c" e( ^. @3 R# S, w* Y) x
//--- An auxiliary variable for opening a position
: `) _  b2 n. `% t7 [#include<Trade/Trade.mqh>, J& |, y: o9 Z# m( X% M
#include<Trade/SymbolInfo.mqh>
9 i- T5 L7 \* D8 N4 UCTrade negocios;' L" Z( J. _) d' i& k, k- d( M( j
CSymbolInfo info;/ H% ~8 b: g0 L! X  X; v& v3 j% l
//--- Define in OnInit() the use of the timer every second0 f/ j% \3 P9 j0 b$ S9 ^
//--- and start CTrade
8 Y$ Z/ q1 |: T0 ~& Yint OnInit()
( C- W. H( f7 A' l$ r1 f{+ A4 `- u" K0 H
//--- Set the fill type to keep a pending order
' A3 P( ?) F6 [2 u8 w) @( a//--- until it is fully filled* N: ~0 [# N8 S6 p- Y$ z
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
# v7 Q$ g; @, O' p4 r) [" v9 g' ^//--- Leave the fixed deviation at it is not used on B3 exchange
# n  x% a+ N% Z- p. Q2 \  s8 Fnegocios.SetDeviationInPoints(5);
' @& ~2 T4 Z+ K5 z% A' A//--- Define the symbol in CSymbolInfo...9 N: |# f! H# X" B7 l5 z( J' Z' F
info.Name(_Symbol);' F/ n& ^1 n- a7 M$ {- m# f4 r
//--- Set the timer...
& T- m: _2 d1 i: L  mEventSetTimer(1);
- O3 D$ p. R0 c, `  o& D' z3 T//--- Set the base of the random number to have equal tests...9 ~/ S9 [2 f9 ?" n  o* j+ m- ]
MathSrand(0xDEAD);8 C/ l3 ~, U: ^
return(INIT_SUCCEEDED);0 I5 l6 N: F3 {
}3 B- h: y: v$ P. e4 S
//--- Since we set a timer, we need to destroy it in OnDeInit()." {- U# E- z+ G" l
void OnDeinit(const int reason)
0 V# b2 L, c3 V/ O{
" n" w, e- D( j) GEventKillTimer();) N0 x5 L! O3 ?: M9 J9 o, R* ~
}
9 V0 ^; R% g5 m* s//--- The OnTick function only informs us that we have a new deal
* f1 V' e8 \" ivoid OnTick()
8 f, e% Y1 p/ Q{! x8 y, P9 G. Q; s3 L
tem_tick = true;
/ t% _4 G, e9 I  Y}4 j' a& p9 c% W- f2 A  I
//+------------------------------------------------------------------+
& X4 R% z6 Y1 s  o; T" y//| Expert Advisor main function                                     |
. P- y6 K5 K- E) n//+------------------------------------------------------------------+
5 ^/ W1 ]& q6 S# f' cvoid OnTimer()
; j- V( f3 ~+ B6 ~) [8 t2 [{7 K5 g4 n, H1 w: Y$ M
MqlRates cotacao[];/ W" B% s/ n4 a8 A. d, `
return ;
3 H; z% I( l* x6 n/ r% p' Wif (negocios_autorizados == false) // are we outside the trading window?
1 u8 f, v% J% @/ r8 Mreturn ;
- o: n$ w! I! e9 m//--- We are in the trading window, try to open a new position!7 o' n; r: l" d6 M+ u
int sorteio = MathRand();
* O+ J, R4 m2 }8 q* @. V//--- Entry rule 1.11 g6 G( e- }" s7 L
if(sorteio == 0 || sorteio == 32767)9 Z8 P$ \7 S9 g# r
return ;1 l: I  [# f* n; X1 {
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy# r/ W1 v5 t2 C( A; U& m7 ^/ C- r
{" o2 Y' j5 Z; }0 H, c
negocios.Buy(info.LotsMin(), _Symbol);+ ?8 Y5 H0 Z- M* n$ x
}! E- k. W: ^) ^
else // Draw rule 1.3 -- odd number - Sell0 h) T$ M8 Q% a: k1 f- B  f6 \# A
{7 ?# t# k3 c! J2 ^( K: y7 d
negocios.Sell(info.LotsMin(), _Symbol);, Z) _, C& ^' }: y
}- I) Y' ?0 O1 q1 @# M
}
9 s4 @, C! y7 {, [9 b5 x6 B7 N//--- Check if we have a new candlestick...
' U$ P, X5 }' |' vbool tem_vela_nova(const MqlRates &rate)! x* a( C' c2 l7 y% W& ^" f
{
/ t* r& T3 U7 n& {5 d{, H: `6 m& \7 m; t) m2 a
ret = true;" V- H  u, w% x1 E4 E) l
close_positions = false;
0 G% Y' ~3 t* k; L}* b3 i! R( R- R7 G
else- x: \  g6 F: Y5 |
{8 M$ E& t( X: k( t# F- g# W* O
if(mdt.hour == 16)
! T, c% S4 g( }close_positions = (mdt.min >= 30);
- E  a5 j+ _. W9 V6 Z}' w# w: C+ a$ Y& e. N
}9 w. [6 Y7 l( {, C
return ret;) n) v/ B& u4 a. O1 k1 G$ i* a
}
6 Y* ]; m  ^/ B0 L7 M) J//---) |& }3 Z6 Z" t9 _2 [
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
+ F1 _- z) ~1 a4 ], I* ?8 t4 E{
) h% a1 r& w% f6 w) Q+ y5 Eif(PositionsTotal()) // Is there a position?
3 s1 m8 I2 s& {( N% I7 @{* A% h( {8 D! i! r; z9 n& f; F' a
double offset[1] = { 0 };
2 f9 j! d1 G+ F2 bif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?, [1 Z$ t$ A9 U9 Z
&& PositionSelect(_Symbol))  // Select the existing position!, X; e8 O8 R* w  k) Z
{4 o. e) y2 i# [; ~9 p
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
0 b* F# _) ~: c' B9 m' s1 {* Pdouble SL = PositionGetDouble(POSITION_SL);0 q* F- v& y; ~5 u
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
0 a7 G! ~- R: X+ iif(tipo == POSITION_TYPE_BUY)
( I0 o5 o1 f7 m5 G{
6 f+ \9 X/ I. S. Rif (cotacoes[1].high > cotacoes[0].high)7 x( ?/ a  N* [" i+ ?+ T
{9 Y/ ~- L+ N& k, G* l; K5 i
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];* Q& @( U$ l1 [# M
info.NormalizePrice(sl);! R& x/ ~' o; w
if (sl > SL)9 G1 |' M3 f: G+ C4 `8 R7 q* A
{
3 r* B* c# ~% Z! O& ~negocios.PositionModify(_Symbol, sl, TP);
  _- \3 L1 [$ w4 p8 H: y- c}$ @: l( `4 H) r' ^$ ~1 M* ^1 r
}
$ B, i. F- @, U}
8 C+ [! P- r! t, e, J9 zelse // tipo == POSITION_TYPE_SELL2 b9 ^( R9 w1 N4 m2 ]3 p8 \
{4 z2 r7 y: c/ U
if (cotacoes[1].low < cotacoes[0].low)# S3 J9 h) d+ j( @  G1 m0 d# ^
{4 a! D# N6 a9 z% k
return true;$ b- A  |0 j8 u
}3 |" J  B  ~* x; _1 t
// there was no position, V" q7 h9 _' v
return false;  b  k. M3 {7 [) @! G/ p1 G
}
% m) h* I: ^8 H- r! U+ f7 _6 b我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
- S& q( Z5 x! u) ]( V到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-8 02:00 , Processed in 1.849794 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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