私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
' R$ V9 t* ~3 U9 c% b: d在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
- q4 q* j; `2 C为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
" i. E9 a& i2 D( w' X8 H6 L5 c以下是制定这些规则的代码。
3 g; }! n. ^0 |) H//--- Indicator ATR(1) with EMA(8) used for the stop level...
0 O" P" L' @, j/ Oint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
: C" n. J: Q4 Rint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
/ x2 I2 x2 e  K//--- Define a variable that indicates that we have a deal..." C% Y0 [0 G& u$ i# C8 q* q$ W
bool tem_tick = false;7 b9 K$ P" n* v- n% x
//--- An auxiliary variable for opening a position& {1 @1 G) {3 u
#include<Trade/Trade.mqh>
& K1 ^4 O5 c0 e4 l4 R, l( f6 ]2 I#include<Trade/SymbolInfo.mqh>" a$ B9 R" a! {* O+ h0 T( F% v
CTrade negocios;0 O+ ^& S; N+ k+ T* y6 z6 O5 a
CSymbolInfo info;
$ U! U$ X- d* {( K  |6 a//--- Define in OnInit() the use of the timer every second
( f2 e& m7 }7 u3 ?4 F//--- and start CTrade; z; n: K( B- a6 [7 l
int OnInit()
/ B2 Q7 a0 Z. K* B# g. l{- l* e6 q; h  j
//--- Set the fill type to keep a pending order# q/ _% P$ h& }3 K
//--- until it is fully filled
% i, p6 Q5 m5 _/ H2 `( E9 O5 U+ ?negocios.SetTypeFilling(ORDER_FILLING_RETURN);1 I9 x2 S/ {, s% w. E
//--- Leave the fixed deviation at it is not used on B3 exchange# A. e# G8 P6 p! ~1 ~
negocios.SetDeviationInPoints(5);# S8 B: E! ?6 ]9 h/ T
//--- Define the symbol in CSymbolInfo...
8 P9 V7 b3 `$ H  t3 k" l6 iinfo.Name(_Symbol);6 n) q* a9 {. a' C' i( x! _" D  K
//--- Set the timer...
# v) {/ \/ o, s" i+ eEventSetTimer(1);
" y5 g! G% [+ R+ d//--- Set the base of the random number to have equal tests...# Q2 T  {4 ]2 Y5 t
MathSrand(0xDEAD);# _; s3 H/ L8 g& e5 P+ c
return(INIT_SUCCEEDED);3 X- e! ~3 F1 n& P: T
}8 J* U8 M) D9 N8 [
//--- Since we set a timer, we need to destroy it in OnDeInit().
0 P+ e5 j9 G7 C6 i6 r5 Yvoid OnDeinit(const int reason)
0 z4 z9 ]8 I/ ^5 p7 ?. q{( f  m7 _5 s$ z& T' J
EventKillTimer();6 {+ ]- z7 U4 M$ ~: \: B0 w. Y
}1 c. A" S* Q  o& n" N
//--- The OnTick function only informs us that we have a new deal
0 ~" U3 A* f  e0 d2 C3 `9 N. I; jvoid OnTick()! t3 R7 h4 r7 k( \/ ?; E! Q' B
{- y1 j, ~- y& e, v5 V4 g
tem_tick = true;
- I0 C9 V! I, c/ l* r1 k1 N2 s}  n: k1 y" K& T6 \
//+------------------------------------------------------------------+
  {4 h2 T% P2 }) F! B0 V//| Expert Advisor main function                                     |
4 N. j* s1 D. q! L- @//+------------------------------------------------------------------+: ^2 G2 k- w' T4 {( c
void OnTimer()
# R$ L: C0 x- C% W- \" i1 g% h{
. |% i' Z1 }# KMqlRates cotacao[];
& c! K  Z! G8 ~5 xreturn ;2 A7 ^8 p  [+ a1 T
if (negocios_autorizados == false) // are we outside the trading window?/ d  {- x0 o7 r2 o5 _
return ;7 T) }# s3 `' Z- _0 R6 r0 C
//--- We are in the trading window, try to open a new position!
1 h0 a4 f+ H, P  _% P; s: h; Z- ~& {int sorteio = MathRand();
0 {7 A' O, R8 E9 K% z& h9 V//--- Entry rule 1.1* `( J5 A7 }' b* T4 y  F
if(sorteio == 0 || sorteio == 32767)
. C" u" [. G. E. X# qreturn ;
% ^% E6 O! H1 j4 F( eif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
& m6 y1 Y% m6 J' y{
( @( |1 O1 Z& Rnegocios.Buy(info.LotsMin(), _Symbol);1 Q9 j1 r# J3 v  @" |
}
! |5 \. J- O* q' w4 `/ ?0 kelse // Draw rule 1.3 -- odd number - Sell
0 Z* o% I( ^+ A1 O{5 o: Y0 }6 W3 k' o) T
negocios.Sell(info.LotsMin(), _Symbol);
% c4 g' B% ?) o4 t- F" _7 E- U9 k}
% h5 o0 U9 u, F  d}
5 U9 i, _& g6 q4 L7 Q: |1 l8 }: M( a  l//--- Check if we have a new candlestick...: P# H; {5 |9 K# m1 B4 v# H
bool tem_vela_nova(const MqlRates &rate)9 p& T6 r+ J  D+ H
{% r1 ~8 ^! ?6 n, ]/ |5 ]
{' K2 Z# J  |) N% j7 ]4 d! w
ret = true;: a! {& R  O5 G6 p
close_positions = false;! u) l9 r, t6 p; @6 @
}, v7 U" U# z: P8 y: \, ]
else$ ?3 K7 O5 K: V' B& h7 R
{* `' m% y" n$ R+ }" `' g* g
if(mdt.hour == 16)
* y' C* B) H. C  p; {* D  X" c0 [9 Iclose_positions = (mdt.min >= 30);/ @5 z9 j4 v( V" u+ \
}1 U" J/ x% N' c) |3 v/ [
}/ a3 X' _, E9 O5 E
return ret;
2 W. {2 T3 ]7 E- K% Q$ A}% @* K7 `6 S5 i4 Y% Z1 G: N
//---7 ]6 {+ Y' m: ?) d
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
, N9 j) ]% D, j' @% [3 ]: c{
/ z1 j: i$ k) ~; @0 Qif(PositionsTotal()) // Is there a position?- {2 I  n3 Y. a0 D. C6 @# Z* t
{
9 j8 {. Y& b9 d6 i/ y8 ^9 ddouble offset[1] = { 0 };6 t+ z1 R# F* l1 b
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
4 ?1 |. Z: q* b- w. i( k&& PositionSelect(_Symbol))  // Select the existing position!
; {9 W( \: ^' C# m0 ~6 `5 J6 I{3 |2 _$ q# ^3 y# S
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
" R1 F# X! R, H: N0 Rdouble SL = PositionGetDouble(POSITION_SL);
: \* Y, U8 h% W3 W2 v2 F( @. Jdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
. B5 l% a( n6 E% P) K7 |2 ~$ nif(tipo == POSITION_TYPE_BUY)+ W3 N* N5 w# C' q
{* H. g8 h0 X) m/ }$ P/ A# \4 I) K- e
if (cotacoes[1].high > cotacoes[0].high)
$ f; Q$ F" D2 b3 F! N{8 d2 j4 ?# U) X- J
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];  O/ [! U1 V4 P8 E4 N
info.NormalizePrice(sl);, b, ?2 o' N3 m
if (sl > SL)
; I, L" q+ a& y1 n6 ^! Z{
7 y2 c' X9 m9 k% {7 x) y- \$ jnegocios.PositionModify(_Symbol, sl, TP);% O, n% T- F9 K
}
4 U3 d; j# k6 f0 H# z8 H1 j}; S6 ~( Q0 l% x) Z3 |) [
}
* g: [1 C5 |$ K. Q+ Jelse // tipo == POSITION_TYPE_SELL* T5 E: s& }" L4 w( I" J% G
{$ P% w" J; W8 D7 T+ I3 M" \
if (cotacoes[1].low < cotacoes[0].low)" o' v- K; `/ S9 x+ i: T
{
# @' h1 U' D3 @, E4 [return true;
# h, {* H; `. ^}
% |( ^) ]) `* n0 _0 G4 A// there was no position" b! ~5 M: j8 j- n" g
return false;% Y* H! v3 ~( N
}
) x+ X' T) _1 i( o) @* g4 G我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。; D& I- a. e. r. {/ W( c* D
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-23 00:31 , Processed in 0.437682 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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