私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
4 I" B% |3 N, j0 ^在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
! F  t9 k0 P7 }4 @; _: i1 y" F, ^$ t为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。# }* l$ ^/ y9 d/ f7 M9 A
以下是制定这些规则的代码。
! S/ e+ W4 w" v- I4 k% `! R- l1 I//--- Indicator ATR(1) with EMA(8) used for the stop level...
+ h8 E3 s5 i8 R# `) Eint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);# v; r5 t, J9 O' Z% k
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
9 f+ @. }! q  r$ H//--- Define a variable that indicates that we have a deal...
2 T& x, v, D6 Mbool tem_tick = false;5 O, g% ^, F* V8 m, @  E+ {+ s
//--- An auxiliary variable for opening a position
# T7 D- J! O7 N$ D2 x#include<Trade/Trade.mqh>
- ]3 l, [8 P( N/ G6 c* c#include<Trade/SymbolInfo.mqh>: L6 f6 \1 E  M1 [7 R4 x0 I% m' }  `
CTrade negocios;! D! s" W7 }2 h* u' Y3 w
CSymbolInfo info;
$ d% K" c  {# ]4 p1 v  T1 B//--- Define in OnInit() the use of the timer every second
) I: ?0 T5 O* |5 W+ M1 l/ F//--- and start CTrade
# L6 o! o5 p- h* ?7 i4 ]3 q9 h- s& yint OnInit(), K& [: n. Z) N
{
  q( r& S7 q* M3 @- n2 \' [! p//--- Set the fill type to keep a pending order- W. D; X/ I; l& G5 V
//--- until it is fully filled( K) K6 z- j$ ?" S+ P+ ]& f$ q6 O
negocios.SetTypeFilling(ORDER_FILLING_RETURN);' h" d$ T3 J7 e2 J7 ?6 h  o2 C2 \: `
//--- Leave the fixed deviation at it is not used on B3 exchange
+ ]4 h4 L% L; t$ K9 g" W/ ~2 knegocios.SetDeviationInPoints(5);
4 K0 U( U# O7 m. ~0 D/ W//--- Define the symbol in CSymbolInfo...7 m) n5 c. W2 a+ Y
info.Name(_Symbol);7 p+ }+ C) c/ h4 A& X
//--- Set the timer...0 W( v: F$ L, V0 i: h
EventSetTimer(1);* [5 D& Q7 w( `) P- P& C: O9 m
//--- Set the base of the random number to have equal tests...
) E" ]! B' W4 C/ [MathSrand(0xDEAD);
- ~* f# E/ O4 X& mreturn(INIT_SUCCEEDED);. B- l) }/ N$ l$ d
}$ Y" M5 |- h1 u8 x$ F! A8 V
//--- Since we set a timer, we need to destroy it in OnDeInit().
: i, o; O+ z! s) m0 F) z2 Dvoid OnDeinit(const int reason)
/ f1 x' W# b3 @/ j0 ~9 k{
5 D* \5 d. G7 f" [) QEventKillTimer();
# T' i* h! d2 |5 i; B}. M) T) H7 s+ U5 ?
//--- The OnTick function only informs us that we have a new deal
. j- o2 _( s8 Mvoid OnTick()
7 F- I$ K# e" s8 T$ B* g) @{
# |6 L8 Y8 G9 m5 Ftem_tick = true;% H3 x& ^" F7 v" [, k9 R
}
  G- a1 ^) \& A1 h" h//+------------------------------------------------------------------+
4 s& e+ W, D1 ~3 `2 Q//| Expert Advisor main function                                     |' r( e8 g9 y" `4 [
//+------------------------------------------------------------------+
% L: p: z/ T2 i* t2 A. Y# `void OnTimer()- C0 r3 Q" b6 X3 Q- a
{+ f; i, Z1 d0 i9 x# x# I# \
MqlRates cotacao[];
0 @/ P6 X6 w" f+ \+ @5 s9 vreturn ;/ |! O7 D/ ^2 K6 K. h: d$ @
if (negocios_autorizados == false) // are we outside the trading window?
5 r: B9 y  o4 h6 sreturn ;
# G/ ^, G$ z  U' N) \7 ^1 o" _! Q4 X//--- We are in the trading window, try to open a new position!1 |6 l1 p) N' G: y" x6 O. U" ~: k7 e
int sorteio = MathRand();
4 c4 u* m2 u7 C//--- Entry rule 1.1  m0 P5 ?; \# ~
if(sorteio == 0 || sorteio == 32767)7 A1 m$ h( T# v7 G/ B7 Z0 C" c
return ;& l8 \; O+ o, F
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
9 n6 K' b& \/ u  \6 I# L{7 M* Q4 |, M- W9 T
negocios.Buy(info.LotsMin(), _Symbol);
/ q  Q$ n8 v- k) q% p$ S# X}
: w0 M% V5 A. k1 J( Y& ^2 A( f. ~else // Draw rule 1.3 -- odd number - Sell) x9 S( g  W- N( _9 }; V1 x
{
9 m; J2 Q6 k4 Enegocios.Sell(info.LotsMin(), _Symbol);+ |+ T$ I6 r% L$ {
}
7 @0 p. u% q& k- F0 K+ \}
5 F3 C$ }# G; Y//--- Check if we have a new candlestick...
) [- D2 _& W( K' jbool tem_vela_nova(const MqlRates &rate)
+ O4 o( ~3 ]1 D" B{
7 u4 Z1 a' A% C; q2 L: V% E{
' \$ k" e9 ?; n% N4 sret = true;
( `- r2 S3 w3 p+ X- f; O$ yclose_positions = false;* ~$ ]( w+ ^3 ^6 {) r! ]
}
$ c; K$ r3 j/ E1 i3 E2 Oelse# l+ Y5 b- a+ ^& r
{, S; W8 R" n) y; v& x# L
if(mdt.hour == 16)
( E" h1 ^1 t5 V; X# ]/ [close_positions = (mdt.min >= 30);6 ~$ d( ?" U0 N. I1 [) }( j& `- o
}! _. M* O2 r! h
}
& F% |! Z4 z( |5 h& H6 Y9 q+ u# dreturn ret;# G4 S8 x  s4 O
}7 x! A1 @4 Z9 t' y4 i7 H
//---
" W) h& M0 T- H/ a( `2 A8 Vbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])  ~& T# a% A9 l) ~* ^4 ?" U3 |
{
1 i. `" R5 d6 b; S. [) |if(PositionsTotal()) // Is there a position?2 V0 K* {; j  n- F6 @/ m: P' d2 z5 a
{
: a6 B* r& \: F8 O* hdouble offset[1] = { 0 };4 y7 D, j' F* O) E2 P' c8 s# e9 t
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?; N0 R% h3 t/ c$ m, U; l3 o
&& PositionSelect(_Symbol))  // Select the existing position!
. w/ R4 X0 y. h9 E. D{9 ^. n( E1 q2 x5 m2 g6 f
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);! B' i: s; j  q. u, D$ H( V' p
double SL = PositionGetDouble(POSITION_SL);' ?8 P9 j& p* V
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));! H$ a9 |  R6 C) _9 b
if(tipo == POSITION_TYPE_BUY)
/ C% o7 B# t2 i- S0 s{
! ^! h) X; [) t: o2 ~* b( [4 ~if (cotacoes[1].high > cotacoes[0].high)
/ ^: |- o) u" d{& E8 N! q  v+ F/ q! K- h
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
/ U+ h( Y3 D# t" g& s' o: p) Finfo.NormalizePrice(sl);* F0 l* }4 o+ f( |- I) U1 f
if (sl > SL)2 f. W' u1 Z) w( x! }2 x$ K
{. }, E0 k" o4 _4 w7 s+ ?7 `* L
negocios.PositionModify(_Symbol, sl, TP);+ G7 l7 \0 g2 f7 g2 Z
}
) p4 `! n0 c$ ]}1 k: O, r5 v5 a  F3 o
}
  A4 r' p5 X$ y/ g; Zelse // tipo == POSITION_TYPE_SELL& d+ R0 _/ H+ \0 P/ {
{
4 O& H9 ]1 ]" D% q$ ~; p1 Hif (cotacoes[1].low < cotacoes[0].low)
3 D+ T) T, B6 D{: i9 X7 d% s/ Y* s7 r
return true;
% _/ t, b' {3 X% B}! j2 D1 u) k* |. ]5 L- M$ e" e
// there was no position/ G8 g: H) a9 p) v
return false;
* V! O. _  Z& m7 r}7 I9 I0 H, M! y1 m: B: b
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
9 s+ z! b; l- N" I; {6 s* C" S" J到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-25 20:52 , Processed in 0.432231 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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