私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA( B  W3 ]( u# k$ M
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。, B/ x: m, Y0 ]
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。4 [! ^4 ^. @  W# L1 Y
以下是制定这些规则的代码。) e0 w6 V4 U* O8 m
//--- Indicator ATR(1) with EMA(8) used for the stop level...
" u3 M6 y9 {+ Jint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);! S9 j7 w, ~2 d# ^+ P" I6 u
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);$ K: s6 E  Y3 |+ u& X
//--- Define a variable that indicates that we have a deal...
/ R9 A4 Z; E) P+ A9 R7 C& r# Jbool tem_tick = false;
% a: y* a8 m, C" d* H//--- An auxiliary variable for opening a position
3 a  P) O- g; q0 M$ L#include<Trade/Trade.mqh>
/ z. \8 {+ F0 \' U- g1 o1 Q9 b#include<Trade/SymbolInfo.mqh>
& M- j1 M2 h- b" Z& U; }CTrade negocios;
9 C7 ]$ T% _; t4 s3 m1 _CSymbolInfo info;
: Q) g+ \- p# a% E1 m//--- Define in OnInit() the use of the timer every second* N5 |8 H1 s4 j
//--- and start CTrade9 r! N8 O5 z& M7 {1 ]7 `! d7 |2 p
int OnInit()
  n# @4 p' m& j* s$ n) c0 ^{" A& Z' T9 \/ ]: j8 W
//--- Set the fill type to keep a pending order2 }' U* f2 {. r# B  N- ~
//--- until it is fully filled) Z8 o  q$ A; K1 m9 j
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
8 p) O8 [; U+ E1 p. ^, T//--- Leave the fixed deviation at it is not used on B3 exchange$ @5 I1 W" Q6 u% G; R# j
negocios.SetDeviationInPoints(5);
6 ]1 N3 ^9 |3 [1 e//--- Define the symbol in CSymbolInfo...1 ~  R; D! r; L2 P
info.Name(_Symbol);( b) e+ ]) D, I
//--- Set the timer...
9 D, p6 h) y. K2 I; cEventSetTimer(1);+ o0 Y+ g8 {3 D2 B
//--- Set the base of the random number to have equal tests...
$ t; W1 T+ t) Z* F0 `/ t: QMathSrand(0xDEAD);1 l- p. M7 m2 }$ `" H; V) E9 B# Y
return(INIT_SUCCEEDED);# }0 @6 V: {' G4 C
}
7 _& K+ J% ]" T8 w2 y$ ~//--- Since we set a timer, we need to destroy it in OnDeInit().+ i; k2 N. Y' }* B6 J* v
void OnDeinit(const int reason)
8 a+ {6 _2 D5 G. \{- ]. c; {& }. T3 y/ i
EventKillTimer();9 p# E  @% u6 `
}
% ?& C, D  U- q9 ^( G: o( F//--- The OnTick function only informs us that we have a new deal
3 o. Q  X, l- [" ~, c: Vvoid OnTick()  E6 [8 @$ Z" K2 \$ T
{1 n* ], `, W) h9 O9 i
tem_tick = true;
) ]0 R1 e. K5 W' _2 g. N, m}
- R9 v% M9 |; @4 N6 m//+------------------------------------------------------------------+
  \' X0 `! ~) `0 b6 a! p//| Expert Advisor main function                                     |8 _2 B8 I) O8 T( h* T+ E, Y1 q) U7 j
//+------------------------------------------------------------------+5 V; n6 w! c" o! P1 V1 R! f
void OnTimer()
$ M9 b0 a# B+ `{
7 b0 a+ k; J  }4 _( N0 PMqlRates cotacao[];
5 F/ t2 r% x' X  B, \% s# M; freturn ;$ E7 |/ t/ f/ }2 v4 E
if (negocios_autorizados == false) // are we outside the trading window?# {  X; I0 l* c/ U3 s- s+ B
return ;0 c5 j  L$ Y6 @" Y3 Z( z: I
//--- We are in the trading window, try to open a new position!6 h6 T. M" K/ S9 P
int sorteio = MathRand();
2 i. F. v4 ]" W* g; s//--- Entry rule 1.15 W3 t# V4 J) ^# ]) ?8 o
if(sorteio == 0 || sorteio == 32767)
. w! ?# w; X4 ~' Nreturn ;
5 ~0 a& w- k  z: ]- iif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
+ ?: {- s; H0 S! v( y% u% [8 G{
: S  g( j; w) K5 f) y! ]+ {, Wnegocios.Buy(info.LotsMin(), _Symbol);; d- V  X/ D, C3 e* I( i
}
- N! J/ h& }1 l& b/ kelse // Draw rule 1.3 -- odd number - Sell
4 `, a6 ~. x: o) x' j3 h$ V{
9 s! i& R' b, {6 G8 e. Bnegocios.Sell(info.LotsMin(), _Symbol);! H8 N6 s, u9 V; I4 u9 @1 R! S2 Y
}
( m/ ]2 o) H1 ^3 D}
' n4 Q! C$ q3 _1 V) q, o//--- Check if we have a new candlestick...
: |+ J! M4 e1 t6 T$ ~# ~+ p. Rbool tem_vela_nova(const MqlRates &rate)  c* }  s0 R4 E% `5 d4 C4 u
{7 b- {  e- I7 {  R( ^
{0 G7 `6 c4 k; {( `9 ?' q$ B3 X
ret = true;
" E3 V  U/ m5 n" e2 oclose_positions = false;( H! ^9 m6 K, o( w# r
}' t  x/ j9 t! u. g5 s5 ]
else4 v( E. R: k2 |
{- ^* m) p" o0 F- Y- U
if(mdt.hour == 16). t0 }3 J' R, U) T; v0 p
close_positions = (mdt.min >= 30);9 i% q2 J/ W! u' s. x
}
* a8 ?1 y  o1 j' \6 M}- x2 ?: m" `+ Y9 R
return ret;! M! H4 ?# ~1 Q8 x4 G
}
- }6 Q) S) A8 z1 V4 p//---$ Z4 m7 u0 z2 M/ e
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
. e1 G6 J# k& c. E) X9 T) e! g{
! D% |9 u0 R$ Z  iif(PositionsTotal()) // Is there a position?
" x; c: _( @  w" U9 y" L{
5 q9 x6 O! Y; z# Q0 d. Zdouble offset[1] = { 0 };: W* c! L# y$ @! f
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?3 o7 M- w& W8 i  N; B
&& PositionSelect(_Symbol))  // Select the existing position!8 @; M. x, j) W+ W3 z- Q9 W5 c
{
1 `- p% V2 W$ j& C1 V. c# F, |ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);/ U, i, }3 A# T1 m. x7 o
double SL = PositionGetDouble(POSITION_SL);
2 f4 B5 E6 Q' ?: J3 Pdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));5 H7 M1 Z+ W) g) m2 v% W
if(tipo == POSITION_TYPE_BUY)- B' \2 U' H( c3 ]& N1 V* G. E
{+ Z5 ?8 V# r( a6 V
if (cotacoes[1].high > cotacoes[0].high)
$ t. X" }/ L, ]( _2 h( j{
9 r* g. ^8 p: E( Ddouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];* ]% ~/ K0 \' j! V6 D5 g1 l7 Z
info.NormalizePrice(sl);/ d, ?2 Z+ n( K% N/ _
if (sl > SL)
( b! |5 ]0 g$ a" {$ W' J. k& L' z- [{
' J$ m: t- N- f: w3 Cnegocios.PositionModify(_Symbol, sl, TP);
+ F' d! z* D, L" H1 k, k& \}0 g  f8 y- d2 X8 `6 h  h
}9 B) \0 D# V9 e. G0 `& O8 O) o
}* p, d5 ?! O  g# G1 q; a6 [2 V
else // tipo == POSITION_TYPE_SELL" t* M" s% @; t7 B+ m, V
{
* L+ z3 S) D- Y9 [" {! }# ^4 b5 ^if (cotacoes[1].low < cotacoes[0].low)1 {) _8 a9 F. ]0 V. o9 ]# @
{
4 @( W5 r8 z" W$ ~' b) xreturn true;
4 I4 v) u! B& ^. h# @}- i' s0 \2 z' B: {, G* h4 i
// there was no position( ?  s: e$ F' b* a5 [
return false;
0 H% ?- @- J* s' H; e" G}
: ?+ R7 _0 G8 v8 k我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。( K) {' U+ I" p5 v  n0 ^
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-18 08:01 , Processed in 0.606786 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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