私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
( V5 D4 o, i; M+ L! L( x/ n在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。7 l+ D: T# J3 b9 t
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。# Y. X) F; r5 p9 |5 G" w
以下是制定这些规则的代码。
- e( D( C$ q1 }- K+ f- o9 T2 X//--- Indicator ATR(1) with EMA(8) used for the stop level...& {$ F( E7 ]2 d0 R
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
9 I. l! d7 F( R6 ?9 Gint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
  ?5 D" o1 J" C0 S% Q//--- Define a variable that indicates that we have a deal...! l, b! f( j( l$ M) E, z. G, @
bool tem_tick = false;2 Q$ }2 v( e/ f2 F* H! V0 W
//--- An auxiliary variable for opening a position
; g" n9 L, Y& {* b+ O. G  ?#include<Trade/Trade.mqh>; [) [7 r; z. a+ j
#include<Trade/SymbolInfo.mqh>7 ^- o- z8 Z9 M# w' n# q
CTrade negocios;% c8 ]8 }- x9 j& e0 J7 U
CSymbolInfo info;
4 v* v! X. c) k2 Z' X) i, R//--- Define in OnInit() the use of the timer every second
/ k; u" L+ C/ W5 s& T//--- and start CTrade7 a+ M5 o' @  x" |3 L! K. g+ F. E1 K
int OnInit()
( X; H5 V, x' g( `8 v{
! C  K. O  H5 O. {//--- Set the fill type to keep a pending order
# f4 o: O# v. [+ [//--- until it is fully filled0 ^* f, X( Z2 y
negocios.SetTypeFilling(ORDER_FILLING_RETURN);# g+ Q- k2 z$ m& v% C$ E- x
//--- Leave the fixed deviation at it is not used on B3 exchange: a5 X4 t0 t1 V- a, }6 A2 y
negocios.SetDeviationInPoints(5);$ f$ [& q8 u: t( Y
//--- Define the symbol in CSymbolInfo...
' \/ F4 M: S7 \8 ?  x+ U6 C; a! Vinfo.Name(_Symbol);6 V) {- P! B" `% a* _  N7 ^
//--- Set the timer..., l; t. D. `8 u" N
EventSetTimer(1);
$ r/ b1 a+ G, r4 y/ k+ z/ T9 A//--- Set the base of the random number to have equal tests...$ ~( \3 X& V% U( O$ s" @2 C
MathSrand(0xDEAD);
8 c+ u# i! |0 g/ Rreturn(INIT_SUCCEEDED);
$ E' ]$ P( |1 Y5 z1 \: W* f2 b( |}" l/ m" c# J, V4 H# ^
//--- Since we set a timer, we need to destroy it in OnDeInit().
; q  a8 p5 X1 r4 v! b4 V8 c! ivoid OnDeinit(const int reason)
' Z( J+ O# S6 ^' Z4 E$ \{
( P3 H% |/ `4 E% O0 mEventKillTimer();( n7 V+ F  n7 }
}
* `$ J. q# w: k% x4 t//--- The OnTick function only informs us that we have a new deal! |/ _2 u- v  ^0 F( @, \5 @
void OnTick()
' b9 J8 ?  c3 B+ q9 z{! _0 Y8 h; d! q7 t* m
tem_tick = true;
- f0 b% A( F3 ?4 N3 x2 Y* o}
  E3 z& s  [  `8 f//+------------------------------------------------------------------+% m/ f" r6 c, a* f+ B" h
//| Expert Advisor main function                                     |* J+ d; U6 O8 s& {
//+------------------------------------------------------------------+
% A+ N' O" h  r3 P1 Nvoid OnTimer()$ V- V7 Z$ s# D! Z# v
{
2 ]! ^0 {" r3 s7 |: HMqlRates cotacao[];7 d  q8 r  g$ z, G
return ;9 A2 e5 d" l4 W: ]7 |( }
if (negocios_autorizados == false) // are we outside the trading window?1 E1 R; y! m0 @/ g
return ;
( V9 [) ^* Y" Y7 b4 G% t; _//--- We are in the trading window, try to open a new position!
6 P8 V) X. d% `7 bint sorteio = MathRand();7 B' l2 `0 v- {+ J
//--- Entry rule 1.1
& Y, `9 Q( {5 ^4 J5 w+ P- oif(sorteio == 0 || sorteio == 32767)/ k8 b  |2 K/ E3 V, {
return ;, S1 q0 V' Y* ~$ Q4 Z! p; {
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
" Y; Q% q- U$ N( y{
, |/ Z6 F" g% xnegocios.Buy(info.LotsMin(), _Symbol);
# v/ Q' [; u; y  w}" V, d7 E: q+ u8 `5 l5 H% A
else // Draw rule 1.3 -- odd number - Sell% C9 F& v  g& P9 Y
{
5 C/ M2 w2 q! t" n7 s/ ~negocios.Sell(info.LotsMin(), _Symbol);- Y# @6 ]$ K: _3 `4 ]
}+ g9 m# \7 a# Y0 P. T- d& l
}  f) S! y) ]* [! j$ D
//--- Check if we have a new candlestick...$ s3 m6 d$ Y8 R' g
bool tem_vela_nova(const MqlRates &rate)
7 Y3 H& m; w- G% Q5 c+ w{. Z  m) s6 A+ H1 a- T
{0 j% X$ z& R! H
ret = true;
9 T6 \9 l2 C/ m7 M/ Zclose_positions = false;
+ _- A0 n# P; u) F& Y}/ S' l- U  B+ X. C
else$ E8 L, X6 d1 y
{
% C7 Z, O8 h0 f- N# E9 q5 m; cif(mdt.hour == 16)
+ `+ ]! l7 A+ y* h$ r# ]! Gclose_positions = (mdt.min >= 30);& N$ z' P2 w; G8 |
}
' e3 L9 p- Y/ B! R5 `}
& f* c, G8 e" |$ P7 ]6 Nreturn ret;3 J; h, s8 A; ^1 S" Q. Z. O
}
  f7 X' [" s$ F: r# @! @8 a//---+ @2 R$ t2 |+ f: S0 u# Y
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])" }0 ^9 X2 T2 i# c! R. F0 G
{
2 Z( k% Y/ l& x0 A! a* k$ {$ z. Z( Rif(PositionsTotal()) // Is there a position?0 J  n9 C7 J+ n8 d9 o+ z% `, X* H
{
; ~7 }) u8 Z( H3 edouble offset[1] = { 0 };6 [7 o' s! j( e# ?$ \
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
3 Q7 R, m+ ?3 n3 m! W&& PositionSelect(_Symbol))  // Select the existing position!
" I" h$ v' D" @: P{
7 [: U, B# h( I) R% R$ ~ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);1 y/ U2 \  {2 H( r5 l
double SL = PositionGetDouble(POSITION_SL);: r- o- w! H. p! X3 h. J9 e  y
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));! D/ V+ y. C1 i& K
if(tipo == POSITION_TYPE_BUY)6 s9 J) g/ @) M, n0 v
{
; V/ v7 [/ o& V) K2 Yif (cotacoes[1].high > cotacoes[0].high)
: ]9 ~- ?* d! P" A6 m3 r{
) e* }, z* H3 v- b5 Ddouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
; n( l& R/ I7 F0 M( Sinfo.NormalizePrice(sl);  g/ l3 w5 [/ b5 f
if (sl > SL)
) a# D+ g2 u- d: c1 {0 @& S/ g. X% ^{
2 r, u8 R$ I/ `- q: v, n* z5 lnegocios.PositionModify(_Symbol, sl, TP);9 d  q, z3 \! Q" _/ w
}& }' v9 |0 q! a# _
}
7 |  g4 O  }# M( V% [* ~% Y}5 V0 s4 X# [0 u1 E2 c8 ], z( q
else // tipo == POSITION_TYPE_SELL+ J; W; S; Q* M$ J! v' t
{, ?  z) o- F1 H4 ~7 _
if (cotacoes[1].low < cotacoes[0].low)
; X/ [- f# J$ O! ^  Q{
" f7 w8 v  s& a. \4 _: A& {2 P  Jreturn true;& N. ?# R  ^& _0 s) P
}
+ i4 O/ c, X5 M1 W6 ?// there was no position: n# z# l& }- `2 Z8 H, N
return false;* }5 k# f# _( W  \% H0 u6 S- W3 l+ q8 _2 Y
}; C* f3 i; H& E
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。$ A% Y8 I+ U& _* S# F  w$ n7 x
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-17 18:43 , Processed in 3.228864 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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