私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
- Y- ~8 ~6 a& |) F. y在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。$ E! u" m* E' P- b- n: C4 X
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。$ e: _! E  i% T5 {. U$ t' {1 N* G
以下是制定这些规则的代码。5 l" E3 d) O* s( t& [* G: V
//--- Indicator ATR(1) with EMA(8) used for the stop level...; s! T/ U  c! L* h+ R9 N( z+ s
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
$ {$ x) i/ \) Dint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
" C! r5 ?; j5 |# F% K4 t2 }//--- Define a variable that indicates that we have a deal...
1 r2 u, N- a( K# m& gbool tem_tick = false;. G1 ?) t& ^" i. y  J4 |
//--- An auxiliary variable for opening a position
4 w+ j: a5 u& V) s4 s#include<Trade/Trade.mqh>' w* a; t: N  j8 i' p2 V
#include<Trade/SymbolInfo.mqh>
2 m2 ?: f8 @$ z! B: S) gCTrade negocios;
7 M% g3 e2 t. U2 {5 aCSymbolInfo info;
, G/ q1 U4 D) ]- l, _//--- Define in OnInit() the use of the timer every second
  |3 v: C4 |$ u% V: F- u8 f& a//--- and start CTrade
3 R9 u8 n6 K- p. r% m$ _int OnInit()
( H: U# u/ `$ E) H3 x- \1 K{
5 t  b7 L) f0 |$ X//--- Set the fill type to keep a pending order' b1 _: x  G8 S2 e0 t" A  [: R% t
//--- until it is fully filled* `. ?2 O# D( R
negocios.SetTypeFilling(ORDER_FILLING_RETURN);4 ^- K- p4 o, D' X8 `% K/ T- e
//--- Leave the fixed deviation at it is not used on B3 exchange: g- R' p. G% n' t, o' m% q3 [
negocios.SetDeviationInPoints(5);
% n- t& R  v* t: `//--- Define the symbol in CSymbolInfo...
' v9 O8 |  m, m6 V$ Y% q% Pinfo.Name(_Symbol);) a; V* V7 r  n" [; M
//--- Set the timer...$ T8 k0 h/ `& v9 J& T
EventSetTimer(1);9 Y8 x" o% I. @0 Z2 ]* l) Z
//--- Set the base of the random number to have equal tests...
% A8 A. c0 c' U0 aMathSrand(0xDEAD);
' r3 u4 J5 l/ u+ nreturn(INIT_SUCCEEDED);
: a( B; f7 a) c5 U7 E! T0 U8 ?}
3 g) d* y: y% G2 S5 n* _* }3 R//--- Since we set a timer, we need to destroy it in OnDeInit().( B" y( i5 N2 J6 G
void OnDeinit(const int reason)3 j, _1 Y9 ?2 p. U* M, P6 \4 {' `# L0 t
{" Z# y2 s1 F0 }; ?, W; T. l+ ?( B) j
EventKillTimer();& l1 P* @+ V' }1 d: G( N$ s4 a% z# i
}! b" H3 s$ f3 Z" K. p9 T
//--- The OnTick function only informs us that we have a new deal  a, F3 n5 i! \0 W/ p3 t1 O, p' P. f
void OnTick()
' H$ k' u1 Z7 W3 @- Q{3 b. J' n* j9 h
tem_tick = true;( w6 L+ m5 T" n! w' B$ [9 N
}5 T2 G0 G0 U" p4 o% [; m  x
//+------------------------------------------------------------------+
( k" V! L8 G# K& k1 b- h, y( G5 [9 a//| Expert Advisor main function                                     |
  L! c( R2 a6 ?/ Z! W. C7 n//+------------------------------------------------------------------+& P' F' c4 n% R* y* S6 s/ j
void OnTimer()9 T  l0 Q2 W/ z% G
{# k( c. @* p5 V: Z0 m, D. k
MqlRates cotacao[];3 c. S6 n4 g6 J! k- B9 ?' u3 `8 q; Q
return ;
- m$ i% ]+ w4 W- X( d. }2 y4 a; |+ lif (negocios_autorizados == false) // are we outside the trading window?
# ^  P( ^; u. R7 E* l2 Lreturn ;
& O+ F# _/ }3 w7 g+ g//--- We are in the trading window, try to open a new position!4 X% j9 B, s6 Q  T, y3 a
int sorteio = MathRand();, y$ W( R$ M3 p4 N
//--- Entry rule 1.1
: A# W8 P5 n2 i% {4 v" `2 n3 fif(sorteio == 0 || sorteio == 32767)
# Y: E! M+ d2 Q! z$ F4 r3 dreturn ;
4 [+ \- A7 a' T# J. y3 W3 nif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy" {4 `6 H% B$ U$ f
{5 U9 }, L2 y8 @& D, W7 Z, J
negocios.Buy(info.LotsMin(), _Symbol);
  N0 y+ E7 E6 B7 W! P9 |}
+ K6 T) |2 b- |) |+ gelse // Draw rule 1.3 -- odd number - Sell: }9 n8 `$ U+ q
{
# E" B, z" r4 M) ~$ ]2 F3 r* Knegocios.Sell(info.LotsMin(), _Symbol);
& i' }# m. G1 ]}- Y0 G8 @0 [: ~. y- Q; ^0 [
}
$ F9 o8 v* h& u: k//--- Check if we have a new candlestick...
3 l4 D3 w# s$ k3 U. gbool tem_vela_nova(const MqlRates &rate)2 Z/ X6 N. \$ J& O
{
0 z" n; B5 L9 o3 U4 l{
, v+ s8 x. |* P6 [2 T+ u9 bret = true;
3 t1 \4 I3 m  M, Wclose_positions = false;  D4 @- A8 S2 G) q2 g7 d4 p
}
7 D6 M* J4 b; R& S  L* |8 u* N( nelse+ x2 h# @2 I' _( y/ j- F
{  O4 k: w1 u. F6 X& r
if(mdt.hour == 16)+ p& o+ H5 N* w* ~5 m
close_positions = (mdt.min >= 30);+ Q3 S5 r4 V* [) @
}
& w- y) t( L- E/ [}% o) T# p& B! s( \9 O# j' k
return ret;  O) ^% m0 L0 r0 R4 q$ s
}
+ F/ y+ h. [$ [. J- ^! E//---
9 b% B; q/ c7 [% ^/ F- p" }bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])2 S; N+ v- ?( c: t
{& m* l% c. y2 T3 `( ]4 a, {
if(PositionsTotal()) // Is there a position?
3 T$ Z! B* P5 x+ _{
3 F4 Q5 }1 N/ {( x& g: }9 F4 k6 cdouble offset[1] = { 0 };9 k; t/ c; Y% |; u, N
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
) l. T+ i1 o' f: d7 m, C&& PositionSelect(_Symbol))  // Select the existing position!! p; ?- k0 V/ S: e
{$ W* |2 x+ t; `. p$ R+ \5 y
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
  |5 v' a" K( Gdouble SL = PositionGetDouble(POSITION_SL);- w9 y: j+ T3 E. ]1 Z& P
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
( l7 M1 [7 B. L" V9 j6 xif(tipo == POSITION_TYPE_BUY)
* P% S* }+ {$ n$ e2 _8 `  M4 W{
9 j( b$ d* O8 B' xif (cotacoes[1].high > cotacoes[0].high)1 V' @- O1 H5 ]  ~5 f6 }
{6 c9 O5 B% z, S) O" ?
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];' ]8 W( {" i) ^7 K
info.NormalizePrice(sl);
! R6 Z6 X" G& m. F1 tif (sl > SL)/ o* F" N9 [/ K/ `
{( J, a. V' [2 _& H$ M$ I& b& h
negocios.PositionModify(_Symbol, sl, TP);) D- o, M. O$ \% j6 }8 F
}
/ b5 j5 E4 ]& }2 ?& l' K}- z! f2 `) B2 w( [5 f' a/ C+ d5 B
}
) L8 w- O6 o2 A7 q7 {else // tipo == POSITION_TYPE_SELL
% q8 b3 x0 Y5 w/ G- f- [{5 o; i, p: L* }$ H" m) u( F
if (cotacoes[1].low < cotacoes[0].low)" C; g# R+ s% K# Z. ?' ~& @- e
{* P" X: m4 M8 s: |5 o2 l
return true;
! a/ Z7 t- C8 n5 t* [}
# J8 J: D- a: B- J// there was no position
. C; ^" d+ s3 H8 g- y/ Treturn false;
6 Y9 f3 e5 _, n. X  t}" j  N& {0 |% H& k
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
! J- r% c5 u  {1 l4 E) Q/ ~8 N- z  u到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-22 17:58 , Processed in 3.140591 second(s), 32 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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