私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA& B: Z. a* R3 R( V) V
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。% D: I% I  G; C3 Q2 H; V
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。& w7 }4 g- k% U
以下是制定这些规则的代码。4 y/ W  P7 X) O% A5 \
//--- Indicator ATR(1) with EMA(8) used for the stop level...7 |. F$ ?7 \$ r! A3 h. j8 {
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);$ Q2 \2 U" T4 m. y% a3 C
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
* ^( A* Z- q4 N$ i' ?$ J' a//--- Define a variable that indicates that we have a deal...
, h8 B' p5 O/ x$ H% U: O$ Hbool tem_tick = false;6 V! j) D6 G. J* W$ w+ [7 k
//--- An auxiliary variable for opening a position
* r% Z1 |+ ?# g#include<Trade/Trade.mqh>
/ c( N8 u1 C( s#include<Trade/SymbolInfo.mqh>
3 c5 G, H, ~& {5 P( @1 m& BCTrade negocios;5 y7 D% C0 K9 U! }- o0 s) `; X+ V. _
CSymbolInfo info;: f# ^) w6 x$ w. [( T
//--- Define in OnInit() the use of the timer every second' i; R+ {$ c! l+ Y9 u
//--- and start CTrade1 y% E7 s% A% d0 E; {3 J) h
int OnInit(); R- ^" z9 Y' E1 y! ~* ]! i+ j
{+ a) B2 P5 b4 l# l" c9 x: x
//--- Set the fill type to keep a pending order
. p% R: p; y- [6 x1 m$ q//--- until it is fully filled2 t8 j) a- S$ i$ f2 G( h$ ~
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
2 c  L/ L1 l8 ]* z- u" b3 z//--- Leave the fixed deviation at it is not used on B3 exchange
2 ?. |0 e3 Q$ v% fnegocios.SetDeviationInPoints(5);
' K" |# h4 R# F0 e' e$ y: j//--- Define the symbol in CSymbolInfo...
6 N8 }! C: Y9 L. ~: pinfo.Name(_Symbol);
3 g' [! p$ r! e3 }: c& @) k//--- Set the timer...
# m* y* n3 o* p% m3 Q' QEventSetTimer(1);
) L, c: i8 ~* I//--- Set the base of the random number to have equal tests...3 O, G& J# Q. |  M0 [+ A) k2 T3 c
MathSrand(0xDEAD);3 E2 M+ X1 I4 ~3 r4 m
return(INIT_SUCCEEDED);: O# Z2 _" D, [, `: Y$ {  j
}( O- K) S* ~2 z* F8 \' K0 Y" {
//--- Since we set a timer, we need to destroy it in OnDeInit().  f# ?* m! f* c' S# |
void OnDeinit(const int reason)# }' m% O6 S" F; t! b4 z7 O
{
: S8 V! U5 c, B- k8 K- ?- ~, |EventKillTimer();1 y3 V% D% k6 D. J( g+ ^1 R- ^
}
- P& ]6 N. C/ T0 e# L' Y, t+ j//--- The OnTick function only informs us that we have a new deal
: ~" ~( M) B. ^void OnTick()
$ k- R6 g$ d3 W{. ]: I' T; |3 d$ o4 ~6 l: I
tem_tick = true;$ [* U) |" W) t/ A' Q  d2 B" k
}
! V0 _' T% o. t//+------------------------------------------------------------------+
' x+ R0 W+ n" z//| Expert Advisor main function                                     |; U* y0 M0 \- J2 r, b. \: E
//+------------------------------------------------------------------+1 m+ ~/ X& s2 K  y8 @
void OnTimer()7 Z- o$ h$ ^5 f) T$ r' K; |
{
/ P2 _& v2 r  u% IMqlRates cotacao[];
$ V. A6 Z# u! p2 d. B$ Z5 @( q  creturn ;- J" C+ s4 L5 E& Q, U7 P: X3 }
if (negocios_autorizados == false) // are we outside the trading window?3 @$ z& `8 Z0 v7 ~7 l) x
return ;
- `+ X2 y; p9 `5 z//--- We are in the trading window, try to open a new position!+ a- V0 p$ Y8 R: z  X9 u' y. K$ \
int sorteio = MathRand();$ R# O( ?& L& d4 S  Z' C
//--- Entry rule 1.1* d  ~0 f# e" i4 F% H' ]
if(sorteio == 0 || sorteio == 32767)
/ J- h! _( j3 ^5 Y: m9 D+ K' {return ;
9 F  M) X# b6 dif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy# v- T, b. e/ q" v
{# S% E' I; q) t2 h% E* U) y
negocios.Buy(info.LotsMin(), _Symbol);2 N: |" V. w" `- f' ?$ s
}% ^( H9 T! }  F8 L) v, T5 g
else // Draw rule 1.3 -- odd number - Sell) l9 L2 b/ G0 R0 [5 k1 U
{
9 _0 I) o! C  D0 V8 f$ Lnegocios.Sell(info.LotsMin(), _Symbol);
- `2 v' m2 L. W}0 Y" @7 z) v* f8 s0 z9 V
}' y$ D+ c/ F4 }0 V. ~, |
//--- Check if we have a new candlestick...' X) ]8 }6 ^( A$ L  S
bool tem_vela_nova(const MqlRates &rate), C8 P1 @' K$ y4 _6 h! [1 B* l  J
{7 T9 I+ [. b' \" s) L
{
2 T) ^/ d$ H/ V4 L- iret = true;6 L/ }7 ]1 C3 B! x8 t- E
close_positions = false;  o" B; r( O1 {/ F! r
}* K( i! ~9 `2 d" V6 P
else
" T+ |2 f8 ?0 K1 ^  z# w; `{# B; i7 Y  }* Z: Z- y8 J
if(mdt.hour == 16)
' o0 Q; `) N; M1 x2 w: I& rclose_positions = (mdt.min >= 30);
# k* b: e3 t9 z: H0 Z& |+ j2 D}
' W& |* ?5 n9 \* M( ]}. ~  B: p) V* z/ p. n) x
return ret;2 r) p6 I! K' X* X( B, D
}
+ u+ q% ?# [, i& N//---5 S  d3 J3 w5 [: J0 x
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])+ D! T; H: q! n7 g' ^! C( u
{
/ M9 ^, n! Z/ N8 H6 Nif(PositionsTotal()) // Is there a position?3 X6 K7 q: F% _/ @/ d, n. R3 G1 Z0 \
{3 q1 }6 h5 s2 z! A
double offset[1] = { 0 };8 g. W+ L, y% \, v( i  D8 Y/ c! q
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?) v9 G! L' M$ L, z1 B+ d
&& PositionSelect(_Symbol))  // Select the existing position!
# a3 W! m' W' a' H% x( N{
9 w: W0 Q) `  z3 ~ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);5 b% z+ d4 U) t$ ?
double SL = PositionGetDouble(POSITION_SL);7 _) g6 E/ ?9 x( ]1 ^
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
2 ]4 X! P1 P( q5 B) j  @0 fif(tipo == POSITION_TYPE_BUY)
  ~; Z5 O$ `& S9 u{' w7 z5 j- N  T0 e' n( D
if (cotacoes[1].high > cotacoes[0].high); ~; e1 I: P2 ~4 O
{1 O+ P. r; t- `" B1 X
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];8 ^9 {  x4 \1 N  i( S* f# v
info.NormalizePrice(sl);7 V: e# p  _" s" |, Z
if (sl > SL)4 r% p8 c" x, |
{
* F8 p& o1 N5 o' K( `4 V6 ], |negocios.PositionModify(_Symbol, sl, TP);
" Z( Z2 a8 ^4 K$ q  E! o; ]}
3 e/ r* y0 d% l# l9 Z" b/ _3 j}5 A) a3 l- Q% l  N" q- y
}( b7 F. `# j+ a( j1 j
else // tipo == POSITION_TYPE_SELL8 Q5 }) {2 {4 {$ {
{
: k: W" u8 }5 E$ p  J3 H4 T7 eif (cotacoes[1].low < cotacoes[0].low)
, B0 ^' M  [2 E! w& g{; ~; s# q0 K: P) S* }, @9 O
return true;
/ k  z- |% ]" m- h: Z}; S4 R) ^1 h, d4 Y( F8 _) i. D
// there was no position7 }8 h5 q* C" f5 ^8 c
return false;+ P) ^9 e0 k$ J# B+ Z/ g3 b, d  R3 w
}
, G5 i/ s* ^) R0 |! x我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。. X: }* f. H7 M# p' `. 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-18 03:41 , Processed in 0.408681 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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