私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
) O3 A! R) j5 G( |  @) j: [5 ~在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
: @# r& R# ^  z  U0 @! L为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。" a( G- {, i5 f1 K: X
以下是制定这些规则的代码。3 t6 [% i* T; F3 A& r
//--- Indicator ATR(1) with EMA(8) used for the stop level...  s3 t9 L* j' s( \- N, A; M
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
1 \/ c8 L4 q) m8 G; [5 m1 dint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
9 M$ \0 y* L( T) U% ]//--- Define a variable that indicates that we have a deal...
7 X" U/ A& c: E5 vbool tem_tick = false;( k0 r, k% w3 w& P' r
//--- An auxiliary variable for opening a position
0 g9 ?0 n2 v* |* B#include<Trade/Trade.mqh>$ A5 W4 \: K( J. G
#include<Trade/SymbolInfo.mqh># l. Q% z& O1 ^! |; C. m* v# T/ u
CTrade negocios;% x4 ^& [+ v2 ^% ?9 g- K7 _( N
CSymbolInfo info;
, |, x7 H5 f" A: Q//--- Define in OnInit() the use of the timer every second
$ O+ F  P; C1 G# t//--- and start CTrade
& l; y0 T5 ]! \7 c4 Yint OnInit()
% ]# a: q- L5 E2 k{- B2 o' a5 J& `9 V4 b1 W
//--- Set the fill type to keep a pending order- N! {/ \% e% X; E+ k8 j
//--- until it is fully filled
8 O0 i3 r1 E7 O; o+ Q$ [negocios.SetTypeFilling(ORDER_FILLING_RETURN);. W/ j+ B' K6 Z
//--- Leave the fixed deviation at it is not used on B3 exchange6 x# J; Z0 J8 q9 D6 D( F, R1 m
negocios.SetDeviationInPoints(5);1 b8 j$ [/ J/ U) T1 d# `
//--- Define the symbol in CSymbolInfo...
3 f* h! A: u* D& Pinfo.Name(_Symbol);! W9 K$ W% G1 P5 f- L
//--- Set the timer...+ k) V6 V; V$ Z6 k4 f9 X
EventSetTimer(1);+ k$ O; }* o. C
//--- Set the base of the random number to have equal tests...
) N; @% U- j; }' S1 d& {# aMathSrand(0xDEAD);" N' O" P' ?2 W3 E8 R
return(INIT_SUCCEEDED);
. h8 E; e' B+ G}
1 m) E. P% i9 ?+ W! s9 }//--- Since we set a timer, we need to destroy it in OnDeInit().$ j. [2 B3 C# o
void OnDeinit(const int reason)
: u" n8 `' q* V; t) E{% ^& C, ^2 ]. ~
EventKillTimer();# [% `' @+ }+ ?6 P: A
}0 x9 j1 J% }  e0 h  R6 e+ ?5 H2 ]
//--- The OnTick function only informs us that we have a new deal7 x3 j4 I! ?$ z' k
void OnTick()
6 Z* ~  X2 L* [$ t; D0 E* S' {{" H% W1 Z2 }' x8 M' ?1 ]
tem_tick = true;
5 H5 `3 ~- _  N3 S8 p" {& b}% V' \/ F0 y3 c1 V* l5 X
//+------------------------------------------------------------------+0 z9 B3 `0 o3 Y. @! G
//| Expert Advisor main function                                     |3 R! r1 j1 v) F0 I' c% ~. Q+ C! Z+ T
//+------------------------------------------------------------------+9 X* d/ g; j, ~) Y8 {
void OnTimer()6 P- ?8 a7 ?* G. t" h
{5 E& ^- H5 c3 \' F, U' s& v5 I
MqlRates cotacao[];( i$ H- c& }- \: D
return ;
- x7 H: p& `! c! @8 m* H: Lif (negocios_autorizados == false) // are we outside the trading window?
6 G) F# Y8 D% g' k( C/ K0 G  J" c. W/ {+ qreturn ;! J! h, Y2 u4 q: C0 [
//--- We are in the trading window, try to open a new position!
. j" g* j) @/ [" s) M/ ~# a# wint sorteio = MathRand();
' S! B8 K* p* W( X: i% O9 \//--- Entry rule 1.13 d6 R# D7 D  v( @2 |- A
if(sorteio == 0 || sorteio == 32767)! j- W; @2 R6 p: s* s9 Z* s" r" r
return ;! {% z7 w* ^- K3 X) Y+ {6 K- Z
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy0 X# f& N/ M5 K! a! z% g
{& ^- O( ^6 @, f6 Q- `
negocios.Buy(info.LotsMin(), _Symbol);4 F6 j: d# W; J
}8 c* K# T% {! g" a5 ?4 h% X
else // Draw rule 1.3 -- odd number - Sell& U$ `+ g8 d9 l% M% _4 B$ c
{. f6 N  B' E) }/ n8 w$ n
negocios.Sell(info.LotsMin(), _Symbol);3 ?" J: I( H' L5 g6 K9 [1 V' @; [) c
}
$ K7 U- e& t# J2 d. E) I# @  ^0 N}
+ z6 V5 g% X0 O7 @& g//--- Check if we have a new candlestick...! d  g9 w" X. s7 x2 @; h
bool tem_vela_nova(const MqlRates &rate)
4 d3 A, L5 G/ ]7 ?6 s, f! `{
& D7 J9 U- ?6 b8 n+ O  N{
( J+ y1 z/ w2 ~: z6 Vret = true;
; ~% g) x8 @  M% c; }' _close_positions = false;
9 z% f( G& d: _" F/ @& r. h}
  P) i2 J/ W7 v- W' j/ b3 eelse* S* E- h" F0 M
{8 w6 s0 k( t: U  t3 `2 t+ o
if(mdt.hour == 16)' E# N7 W/ A% d8 O$ p, l+ w
close_positions = (mdt.min >= 30);- u; m2 b' t' r$ Y# u
}  m: u3 g4 k  L# N
}4 ^' ]) X8 k8 }+ r, r+ P! I! j( C
return ret;2 k9 R# S/ a6 j" H. u6 t+ `
}0 b& ?: k- N" E+ m/ _9 p1 d" B
//---, Q2 u) A$ c- V- A8 l* c1 L
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
, ^0 R7 R( r: r8 E* }1 J# N{# }) w9 a5 K, a- m
if(PositionsTotal()) // Is there a position?3 q. h- r8 P9 H+ e# k  G
{
* `0 A/ H2 d& C1 l4 qdouble offset[1] = { 0 };9 X5 }. d0 d" s
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
8 C; y% I- v0 [&& PositionSelect(_Symbol))  // Select the existing position!6 ^) S: b0 i2 h# X8 z* f9 ?
{
  T+ s1 ]6 T* x$ w1 n! uENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);" I; n( X7 k3 l2 s3 i; {! w% ^
double SL = PositionGetDouble(POSITION_SL);3 s0 v2 M3 N+ t' R+ O
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));+ Y2 p3 z) V, R# r) M
if(tipo == POSITION_TYPE_BUY)
/ f: ~9 D; t2 W& r" a3 \{
8 ?7 X# A8 ^* xif (cotacoes[1].high > cotacoes[0].high)
8 X' r+ T) n! C0 L1 |5 Z0 q{9 F% Q' Q, @) M- r* X* C: G
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
9 o& E$ g( e+ U5 Z; d) h4 binfo.NormalizePrice(sl);
/ `& \1 C8 L4 ]1 }if (sl > SL)3 ^1 C- L1 D9 J: C
{
) f( p, Y8 a) o0 [& ^- |negocios.PositionModify(_Symbol, sl, TP);# _0 v6 O' u' v  h, p5 h  ~1 f
}. A" P3 \1 y& A1 ^% v( a6 U. e
}* b8 E7 M# F* m7 B- d
}% _1 r1 q- E" q
else // tipo == POSITION_TYPE_SELL
0 A1 F. L5 n  J) r; |{: }2 b; Y1 S3 H2 {
if (cotacoes[1].low < cotacoes[0].low)
$ }4 ]9 W7 n8 B0 x2 W& p{
7 F: D5 E: v7 x! N+ S8 n9 I" Hreturn true;* A# b- d4 j' _2 n, b
}
) S2 F' Z: _4 S1 `6 A// there was no position
$ }# e! I& P' r6 k0 h% Sreturn false;' @  J' o! s2 b& N
}
2 W+ v# ?/ U0 G8 ~2 }: Z6 Q我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
% z: ^8 ~! ^4 ^0 e1 r/ E4 q7 `到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-5 22:29 , Processed in 1.762398 second(s), 32 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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