私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA  o1 b2 @! C, k5 d5 o9 N
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
9 j7 L+ q: z4 c% H5 W为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
- y; n$ g# q) Y. g+ R% D以下是制定这些规则的代码。1 t2 P: w$ V0 {3 {' l6 D3 n9 `4 w
//--- Indicator ATR(1) with EMA(8) used for the stop level...
# E' T$ t3 ]$ A  [! X6 dint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
. {# B5 d) @, I* D! Dint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
* R5 Y( g) I. K7 p//--- Define a variable that indicates that we have a deal...
8 H. {3 F) ?8 G7 x% [6 Fbool tem_tick = false;
' n1 U& l/ w$ k2 V: i3 z. s2 S//--- An auxiliary variable for opening a position; m" u* C( R  }, M
#include<Trade/Trade.mqh>
- g0 P, y- {( ^2 a+ h#include<Trade/SymbolInfo.mqh>
) v% k+ D- ]3 g& H7 f0 vCTrade negocios;
! k5 R0 d6 l5 G! v/ i4 `CSymbolInfo info;% v# j; j/ Z! ~% |, O7 n) W. G
//--- Define in OnInit() the use of the timer every second1 b# V2 j5 h- m1 B% q
//--- and start CTrade6 e5 E/ L6 S- z, k8 k4 q
int OnInit()
! r& F) o' w0 n5 v) b7 q{
; F  g( x$ y/ K8 E, U+ T$ g/ U//--- Set the fill type to keep a pending order
& T- G0 a& ?3 r2 R8 D//--- until it is fully filled, E) W: l8 M0 }# N: F! G' @( c5 ]
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
' d! a3 `- s& y//--- Leave the fixed deviation at it is not used on B3 exchange( [% h' @5 E! Q) {: s5 e
negocios.SetDeviationInPoints(5);& v6 ]' O, t, z  h
//--- Define the symbol in CSymbolInfo.../ L, j" e. n5 \) d$ A* V
info.Name(_Symbol);
# O  G) D% N; c//--- Set the timer...- \, G7 Y4 G% A5 g) h" I
EventSetTimer(1);! e+ v( Y1 W& |* H0 C: ~
//--- Set the base of the random number to have equal tests...
; J: ?) x1 B/ P& q. x! \MathSrand(0xDEAD);
8 N" x! j1 t% L" j; @& z, w( p6 B6 lreturn(INIT_SUCCEEDED);
1 w3 E- _4 t, i$ f  D}
: C, G+ `& r6 e% D//--- Since we set a timer, we need to destroy it in OnDeInit().0 D7 r1 e1 h7 Z* W4 }6 P
void OnDeinit(const int reason)
. X) a# Q) q( g; ^2 c7 |( W{6 N, z# ]5 s5 s8 b# ?/ o3 x
EventKillTimer();3 G% k, X' l8 i
}
& S7 b+ R* r2 E0 h+ H% \, k! Q2 d, l//--- The OnTick function only informs us that we have a new deal6 R: C2 v# R- ]: U6 D1 _
void OnTick()
; i) `6 a; X& u{6 y7 j8 Q! D$ t; P- A
tem_tick = true;
" t+ H' G0 x+ v* t1 C! Y}/ r$ N# H( h. Z# }/ e
//+------------------------------------------------------------------+: U1 o: `' a2 \, {
//| Expert Advisor main function                                     |% c/ c% H3 K0 q+ e( d/ s
//+------------------------------------------------------------------+9 M3 z0 Y- ?- Z
void OnTimer(); p  z/ I1 J  d- r
{
6 z7 c6 d. i, D$ O" U# ~MqlRates cotacao[];4 B9 L7 ?+ M; ?; P
return ;
8 j: X6 w3 }: M3 K9 V4 \) g" _# ^if (negocios_autorizados == false) // are we outside the trading window?4 V# S& @6 u% \, G
return ;; o% a6 h! ]$ G" H- C5 n
//--- We are in the trading window, try to open a new position!2 T: ?# h7 |. J/ u! q. x
int sorteio = MathRand();
' T. k( D% Z. }) L//--- Entry rule 1.1
5 B( v. n* t* L: Fif(sorteio == 0 || sorteio == 32767)4 `, v" k! N) W- z) r8 H
return ;7 r# U* q, g8 |1 d! S. S) T
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy& [, l9 R1 d+ t0 L( E# u
{
2 a$ Y) @4 |& J' o; {! lnegocios.Buy(info.LotsMin(), _Symbol);$ N% F8 E% I; J0 H) s
}
5 F  e( L. |5 m: p& G2 eelse // Draw rule 1.3 -- odd number - Sell. o1 b# d6 I/ K% I5 R! |7 Y
{
  h# P- O* M. x3 f' Z; \2 Dnegocios.Sell(info.LotsMin(), _Symbol);
; I9 z' L+ K& T; X9 o$ N& |}
/ ?7 i4 ^# n$ b+ m' r$ d}$ a8 z) G' T. R( A
//--- Check if we have a new candlestick...
; X" ~' R2 q2 t0 p* x: |- ybool tem_vela_nova(const MqlRates &rate)
  D& _: z" Y, @" J% U& }: ]7 b$ ~& ^{& l5 i* |$ V, P- R" J- J
{$ V( _2 J- A+ u$ ^6 V; g0 o
ret = true;
8 v/ Y. B) q3 s1 |) C; H3 L( Gclose_positions = false;: }3 I5 p5 ^9 Z% y$ }: @$ P
}
  `) m% v' \  P2 Z5 F  ]3 ~else3 c7 g# ^: T8 E  W( w
{, K+ `3 d  P/ V6 @( D: {% I( u
if(mdt.hour == 16)
, [' |* F. c, S; eclose_positions = (mdt.min >= 30);
" R/ n5 H6 k5 u" b}
5 z2 M. j, t$ \}- K1 ~5 |  Z' m/ D: X, r; |
return ret;
  C% }4 N1 q5 U}0 h/ Z7 h- ^4 H/ O" U- L
//---
" u/ T: d3 R4 c7 f4 r; }bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
2 i) b2 P" H$ O! ^0 U{
% Z7 L6 `$ E. xif(PositionsTotal()) // Is there a position?% P: s$ G1 `5 S4 h" B
{
" C! S2 {) F5 S5 l  [/ X/ \4 Gdouble offset[1] = { 0 };
! X7 h7 k4 ~9 A7 [  f! N0 h- Mif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?3 @& D# |7 U0 H2 i3 X6 [: V5 n* i5 ?
&& PositionSelect(_Symbol))  // Select the existing position!
5 ~0 ]- u% A' J# g{0 F- g2 }& \0 f2 k: Z$ l
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);8 N* M  Z. k8 L" ^
double SL = PositionGetDouble(POSITION_SL);
: U5 z, Y: S; l8 D: l# |double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
- f" j+ {! ^9 f! o2 E: zif(tipo == POSITION_TYPE_BUY)& h" [, F' D3 a; e. y9 U- E
{
, i' [7 G0 r$ m, ?if (cotacoes[1].high > cotacoes[0].high)
; O0 R7 O3 ?3 \9 E( Q2 n{
! n0 I. n6 i9 b4 J! O7 adouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];" c. y4 M5 }9 z, i. {! D
info.NormalizePrice(sl);' L: Q3 N5 f0 c& g( B
if (sl > SL)2 Q% s6 h, m, Z/ n% R! z
{
- W, f. f8 x! V6 d+ u" |negocios.PositionModify(_Symbol, sl, TP);
" \0 F7 D( _8 }3 E& a: j}. w6 u% K! }# ^' f
}
+ E* J& |- H$ w3 \2 W}
" h. A7 A- t5 [else // tipo == POSITION_TYPE_SELL
2 w2 h- R7 t( Y4 ]. N{+ k/ q& e2 R. F; G7 Y; y
if (cotacoes[1].low < cotacoes[0].low)
& k3 ]4 B: @9 x{
/ p7 L; a% P* I0 m/ K  M2 Creturn true;
6 q, F4 Q& B5 C: [9 L7 Q}, ~6 y+ H- ^  V( |
// there was no position4 |1 U5 n0 U; @, E3 r4 {; i$ }
return false;  o7 T( q2 J( Y2 A9 H+ P3 J* C
}
% Q4 L6 Q" j) U; O) ]" Y! V我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。7 u( I9 j5 _$ h' H
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-10 08:32 , Processed in 0.396811 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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