私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA: ]  p: C, [; K! `# q$ [
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。; ?1 ^. L. J1 D: f- }4 `+ A
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
. f: K9 `4 s0 [' F以下是制定这些规则的代码。$ P5 \# K( G! a/ \
//--- Indicator ATR(1) with EMA(8) used for the stop level...+ {( H/ |. F$ h4 {1 r9 i# }# b
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
$ I2 q- E; X, r: J' Q) b9 bint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);& q9 P+ j+ C6 V: d% E; X
//--- Define a variable that indicates that we have a deal...
% `0 ?) Y, _: ubool tem_tick = false;
( r1 c' L  p+ }6 Y9 v//--- An auxiliary variable for opening a position
8 |2 ]7 Z$ b9 h! r+ @0 d#include<Trade/Trade.mqh>
( `- i  Y% s3 C1 }! o. c! W% `#include<Trade/SymbolInfo.mqh>
5 Z, \! Q4 ^( \1 F% \" _* DCTrade negocios;0 ]: s  c6 ^1 L" |9 N# @3 X
CSymbolInfo info;" I* a% B& Q1 _- M4 t8 o0 b
//--- Define in OnInit() the use of the timer every second
! j4 ~: A8 |  |8 n. M8 ?1 M//--- and start CTrade$ S% T2 w" Y5 d( R( B
int OnInit()6 u, N! B8 k" I7 s7 G+ W- i
{
  O8 E# u! Z0 _8 K/ E5 Y//--- Set the fill type to keep a pending order, N* A; Y- R0 s7 v6 V* f
//--- until it is fully filled9 d; E2 `* ~2 D: q0 l3 ~. E
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
6 |! `7 F' _6 i7 F, f4 |( z- O//--- Leave the fixed deviation at it is not used on B3 exchange! C: W. }$ S* w
negocios.SetDeviationInPoints(5);
3 n2 a0 ^8 A9 n: ^( f//--- Define the symbol in CSymbolInfo...7 M2 z; ^/ F8 ^7 l0 E) J5 b% N& T
info.Name(_Symbol);
2 N  L+ g# c1 u  U" P0 f//--- Set the timer...
$ h2 Q$ h3 o2 S4 c1 K5 l! X* pEventSetTimer(1);
) O. N  R& o0 l# A( p/ e+ b8 @) e2 R//--- Set the base of the random number to have equal tests..., @" Z4 P2 E  S
MathSrand(0xDEAD);
* I  H7 ]) Q2 Areturn(INIT_SUCCEEDED);) S- l# x- n" ?; I) j: M; C$ r5 @
}6 G0 {$ t& o) Z# V) r; H" ^/ {
//--- Since we set a timer, we need to destroy it in OnDeInit().8 b8 I( m4 c" x5 K/ b, Q
void OnDeinit(const int reason)  ?3 I6 w. r  ]5 ^) G/ F$ A2 n
{$ A+ z& D& }7 }2 e2 F  |8 H9 B1 r
EventKillTimer();
$ T5 t0 O  T- {6 b- w. r; S0 ?}; v6 [( Q4 a- @% u& O) u- G
//--- The OnTick function only informs us that we have a new deal
0 ~5 C$ b% T4 b9 @* C6 K1 F8 R: M6 pvoid OnTick()
6 }5 n5 |$ x" Z( w{
1 w$ J  z; u5 |  Ftem_tick = true;
8 N. l# \! c* |: Y) y}
* d6 E4 [5 g# F5 T//+------------------------------------------------------------------+/ W, m$ o1 ]* _$ x8 `; Q
//| Expert Advisor main function                                     |
5 i6 H6 B$ V4 A5 q% E' P7 e//+------------------------------------------------------------------+0 m7 u9 i# R5 d
void OnTimer()
- @$ E9 \+ q+ {$ J4 F8 G9 y* s{
1 E, W. g1 E# x" JMqlRates cotacao[];& u; b: ~+ `7 ]2 `1 x2 V( C
return ;4 E' e$ [: ?! w" t! E8 _! Y5 Z) l
if (negocios_autorizados == false) // are we outside the trading window?
; Q6 e" O* `" u6 [" m0 |return ;$ {" ]! p, j+ Q5 f) C3 g4 b
//--- We are in the trading window, try to open a new position!
3 b' P1 Q4 O) G" }/ ?3 ~2 h% _: }$ {# O6 kint sorteio = MathRand();
- o. m$ f% c0 p6 |6 [//--- Entry rule 1.1# y% C% ?% O7 m* u2 z' J3 @1 J  u7 Q
if(sorteio == 0 || sorteio == 32767). p: w" M% H4 t% G: q; i2 w
return ;
4 R6 D& ]1 h$ H: i* hif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
# k+ |2 I1 |: |( q8 i- ]{
; ]) B) m# W% lnegocios.Buy(info.LotsMin(), _Symbol);, U' }1 F" A* D. |0 x
}* z# o# E. v/ L; U) O# I; I
else // Draw rule 1.3 -- odd number - Sell
" }, p7 f! y0 U{7 h5 |; S# Q/ Z5 @) d# \: q
negocios.Sell(info.LotsMin(), _Symbol);+ }) B* s" |6 U6 ~4 C: t& U, E" `9 H
}
' g+ l( U! V9 f}5 X$ y" Q% P% e! k4 q% \+ t
//--- Check if we have a new candlestick...( ^+ S. [- w2 K2 b) T9 h6 j) a
bool tem_vela_nova(const MqlRates &rate)* f( H2 A) |( L2 W; n7 {# P
{# i2 e& b- |1 Z! t
{) }5 K, U+ c8 h7 w3 E9 X# W
ret = true;' N% w2 b, V" r" c7 h5 x; i; f
close_positions = false;' d0 [3 i$ C3 y6 L. z
}
  d2 D, O( T, b) X( Y0 R, selse, o; \5 a4 Y# f( T4 ?& ^2 C& S
{! O. M( D* t  ~1 k) I
if(mdt.hour == 16)
0 i& ?' G( t8 b' K& Jclose_positions = (mdt.min >= 30);) k& t" _2 a: b. s; H
}8 x! g1 J7 t( z. ^& S7 b
}
+ ^: j8 E7 B7 d8 u" `) [* _return ret;
' s9 @0 Z5 s: M}2 r' W* v, d8 a/ a3 @/ D
//---
/ W' n3 G9 |- H9 t9 X- x/ ibool arruma_stop_em_posicoes(const MqlRates &cotacoes[])$ G0 D$ m5 k  Y, T
{+ t9 D, @( c7 a2 U/ J9 K9 \* m& x4 ]
if(PositionsTotal()) // Is there a position?
" I5 [, K( P( U( H9 U) l{3 m' l- H: ~8 X  q5 E
double offset[1] = { 0 };8 f! x* E+ n& i  N& F* e
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
3 ~- ^( Z5 D: [7 Y&& PositionSelect(_Symbol))  // Select the existing position!
! F1 \5 ?) k9 C5 L0 {5 V' W1 ]9 t0 e{  _  A; D1 i# N2 c1 B
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);9 i5 W+ B* m4 T3 A. g8 S) v' x
double SL = PositionGetDouble(POSITION_SL);
$ p9 o0 r' v$ ]$ ^double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
, n$ t- D5 k/ o+ k1 z4 B" X$ ?if(tipo == POSITION_TYPE_BUY)% e& s( K+ F1 K: k& N/ |
{. k* R& R+ b5 H$ c0 r
if (cotacoes[1].high > cotacoes[0].high)' r) Q( ~4 R+ }" E
{
9 ^5 {4 E7 V2 L# S0 B0 j9 A* Z3 x/ Pdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
) f6 s& b  Y+ @. I% t! S. ^3 J; k& N( N3 rinfo.NormalizePrice(sl);
$ K9 u) [! g0 {2 hif (sl > SL), `! N, E& D0 q8 R7 h8 y# X
{
5 N8 o' D: U- enegocios.PositionModify(_Symbol, sl, TP);4 e1 _8 }% c2 n- s+ M# o. ^1 ?
}, c  \0 K3 H$ ?) [, J+ Q
}
$ `' g) @* G( m" w$ B, ^  W# p}
- k+ J2 r& f5 [3 f4 k/ relse // tipo == POSITION_TYPE_SELL
9 Y( K% ~, }8 a( u( R{
# G* F( u. Z* j2 j: p' F" U! Pif (cotacoes[1].low < cotacoes[0].low)
1 |  s. a- H$ ]1 J" v6 T8 D{8 E- Y- ?' L! d3 c
return true;4 I! l2 O' z; q. ?
}+ N( X8 n" i; ?( K
// there was no position
8 |8 v# j7 Q7 w, {return false;+ W+ c+ b) `* n8 A2 x% t
}6 s+ y* X' f- w9 x, O! h
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。) O6 A$ l" g, v; l' s! O5 A
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-2 18:06 , Processed in 5.850696 second(s), 32 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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