私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA- y2 d* B* M4 k- n
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。( p, ~: s: }- q8 I1 X6 x: {9 ^
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。+ l( u2 `$ V. Y
以下是制定这些规则的代码。) Z& u) _* B4 I+ ~* t; y
//--- Indicator ATR(1) with EMA(8) used for the stop level..." u" [. ?0 |2 A" W
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
2 m0 f+ V& `. W" J! V/ ]. r8 M  Sint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);2 p' t4 ?# j1 E9 Y7 S
//--- Define a variable that indicates that we have a deal...
' U1 j7 y% n, {: c. G( k) s/ `bool tem_tick = false;: x  U8 z% k) A$ r2 v  r% u1 I& S
//--- An auxiliary variable for opening a position
% X" K* a3 k! `/ F, C+ ]- E, A$ j% m' O#include<Trade/Trade.mqh>
4 J% E( Z2 t' Y0 j; \, T4 b, f1 v) N#include<Trade/SymbolInfo.mqh>
. z4 A4 f1 _! i4 t7 c( ACTrade negocios;
& w5 d  e! }. }+ aCSymbolInfo info;) a6 E2 f! x3 h, x* M
//--- Define in OnInit() the use of the timer every second6 E2 I  `3 y) m1 @# K0 V
//--- and start CTrade
% H2 b, n+ F: Q& E, y; iint OnInit()
& B" M3 @2 U9 }4 z. W+ w; {6 L{# U% e& e0 o0 e; X8 w' X- k& k, Y) q
//--- Set the fill type to keep a pending order7 g5 ^9 z- W0 M/ `! Z; d# U
//--- until it is fully filled
, N* x  N7 M2 \9 W, I" l* Bnegocios.SetTypeFilling(ORDER_FILLING_RETURN);9 `  v* h4 i& p3 a+ Z6 t# O
//--- Leave the fixed deviation at it is not used on B3 exchange
4 P1 `) B/ q9 Rnegocios.SetDeviationInPoints(5);
3 O$ i, E/ t3 x//--- Define the symbol in CSymbolInfo..., s0 Z# x8 R# I
info.Name(_Symbol);
9 j) M8 f; [8 g2 b7 f//--- Set the timer...
- h0 l2 K  e. p/ q; L  s/ PEventSetTimer(1);
; g6 b8 f# N  q- N6 P+ a8 S//--- Set the base of the random number to have equal tests.... w# [* V, ~% N0 {/ Y
MathSrand(0xDEAD);! M) `* b& `* v# Q
return(INIT_SUCCEEDED);
5 x4 t% b0 P4 }- w" T}
- k/ e; m: |4 C; W& l& }//--- Since we set a timer, we need to destroy it in OnDeInit().4 G+ K3 o% C/ H+ O) V1 w
void OnDeinit(const int reason)- Z7 n' T3 M7 @& {) Y  Z" Q8 p
{" p# y7 x, p, m% d8 _
EventKillTimer();
0 V% e- c# e4 f7 J' y" K8 C}0 j: F# l. B* u6 l, i, |+ S4 q
//--- The OnTick function only informs us that we have a new deal( o! S9 d3 q' z7 O
void OnTick()# z+ C* u( u1 Q/ K4 ?2 J+ b
{
) K& c( {3 f  W: s. Ttem_tick = true;- j* R$ ^5 D3 N$ s' |* D) r1 B
}
5 J( n7 B+ |# p6 D( Z+ j( j1 y2 W//+------------------------------------------------------------------+1 d9 U0 K0 s* p3 K5 ]: s
//| Expert Advisor main function                                     |
  c- v; j  i# q# |3 B//+------------------------------------------------------------------+4 d; a% h! w/ F% `9 R7 P2 E6 m
void OnTimer()' n( k" C9 v; ^' H
{
* R. z& [$ {& U/ n* TMqlRates cotacao[];
. I* z3 P; D" r  @0 ]' L5 {return ;+ f) m1 W/ }  ]7 y! u( h( v( b# e' n
if (negocios_autorizados == false) // are we outside the trading window?/ }+ \7 X( i7 R1 ~+ F
return ;1 t1 j" Z" q8 G! z9 m; j
//--- We are in the trading window, try to open a new position!
( i; X' A0 L7 _int sorteio = MathRand();  n- k* F, \4 t- \5 W8 `. m
//--- Entry rule 1.1
- @/ q% B( g: z. Q! f+ tif(sorteio == 0 || sorteio == 32767)
2 Z! F% a2 R4 v; b6 c. q8 Qreturn ;
/ E$ Q" @& C1 @3 S8 oif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy/ b8 r- B0 o! D/ b' X+ m
{+ a$ Y( Q* C; _( D' F4 _# V# y
negocios.Buy(info.LotsMin(), _Symbol);
# t$ b+ D9 q0 p) T! N" |; j' q}9 G4 i4 t* W8 Z& _) a
else // Draw rule 1.3 -- odd number - Sell3 k9 T7 I+ U! D) F4 B
{9 z+ ?) p* S( z6 z4 T: k9 C
negocios.Sell(info.LotsMin(), _Symbol);) A# n5 j7 D; n# r: u$ r* v+ @
}
4 v" g! [" u6 l4 e}/ X- S* ?, x: T. v- U& u4 o( r
//--- Check if we have a new candlestick...! f5 \- l* O5 r9 u5 ?0 L
bool tem_vela_nova(const MqlRates &rate)$ x/ \) v- S7 u
{
- ]* i: x' H3 o( c" ?! f& s{
9 C& X3 ?6 |; Q/ Qret = true;
! o- s$ L/ v6 dclose_positions = false;  j/ o. U& F1 _. w
}
9 @  P- r' P3 x: u' kelse  F# t( U7 S; Z: M3 R/ t
{( y2 c9 m* B8 l/ @: D
if(mdt.hour == 16)
$ v6 E" s5 g6 G, u& x+ {: yclose_positions = (mdt.min >= 30);
* S7 K* D% e4 K: L! t9 v}
* ~# \9 R/ N+ ^5 _; \}
* g/ c- p+ m* i0 ~3 kreturn ret;" Q1 D) ]! K$ B) o+ }' ^# d
}
& V" r* N' ^  |, q& @, r//---
9 Y1 n7 Y: o+ h/ pbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])- I) F' ?  S& Q) w3 E) N6 w
{: {" Q5 O2 V4 R' @) U! H' n2 P' B  A6 [, y
if(PositionsTotal()) // Is there a position?
- S' r1 [+ n# u9 e  s! A{+ g! V  g3 x* `8 i8 O5 A/ J( n0 N
double offset[1] = { 0 };1 w- h% R5 v0 ]
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
% q+ C8 E) F' }&& PositionSelect(_Symbol))  // Select the existing position!
0 L! ^/ H! }1 Y5 y; V' i( H{8 {; d3 J7 _2 Z- |" S5 x
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);* x9 N( x7 n& A" ^- `) H+ H$ c& s
double SL = PositionGetDouble(POSITION_SL);) t6 I3 y3 k5 A4 A, K
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
* a9 l: T0 k3 @if(tipo == POSITION_TYPE_BUY)
0 Q3 o9 b* ^1 I% h$ U# Z) C8 t; X{8 O! ?  M  v  m" Y2 z- g
if (cotacoes[1].high > cotacoes[0].high)* {* {3 p9 X/ b# B; A$ N
{: }7 y9 I) k+ L
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
$ X% D5 q- J+ A- f6 T, l. t" G$ ginfo.NormalizePrice(sl);
: H. x/ d9 L; d7 \; y" Cif (sl > SL)3 u3 }( r9 b, ?" N0 A6 @
{
7 T' m, }1 I; Jnegocios.PositionModify(_Symbol, sl, TP);+ w  ^# _( T+ ]- {. X
}. h: c2 F' |# t: z$ ~+ g3 Z
}, {0 X+ G7 j' g- m; j4 u
}: z% v3 B9 Z9 l2 X  T
else // tipo == POSITION_TYPE_SELL
& d: Y% X! [, \+ }$ D2 q; V( D{3 j4 D' }& W" {  ?, e
if (cotacoes[1].low < cotacoes[0].low)7 q. o  B$ p. ?: m- S
{
; O2 C+ D1 P( B. wreturn true;6 k) w; e* v7 b
}
4 \2 A  `$ W0 h% N// there was no position! s0 s4 L- r& w
return false;
# M: P0 j, [. @. h}
1 C2 H# f0 H) l+ ~3 n: O2 h我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。/ n; i4 p3 D1 r( p
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-19 10:18 , Processed in 0.928775 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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