私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA3 ^, F5 S6 \% R( J3 h- r
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
! U7 J* ^# i4 l. _9 d: Y5 h- @为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
/ @) X( a5 I; G( C, K3 y以下是制定这些规则的代码。
& j# m% ~  I6 N6 u2 X//--- Indicator ATR(1) with EMA(8) used for the stop level...& ^; C. Y9 U8 u( m
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);: M/ y5 `7 K2 ~/ }8 {2 v
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);# Z5 c7 [+ T0 l4 P: u
//--- Define a variable that indicates that we have a deal...* \1 b: W) ~  h3 Z" H/ Y) h
bool tem_tick = false;
, I/ ?9 K! S2 L7 Z* m//--- An auxiliary variable for opening a position
# O2 l5 u* ~$ n6 g) ^+ Z' w$ T#include<Trade/Trade.mqh>! a( q7 h8 a5 u% H2 n% v$ m
#include<Trade/SymbolInfo.mqh>
7 I: y$ g1 p" h3 P* g% G- QCTrade negocios;) k9 @% Y9 j2 [( c6 j
CSymbolInfo info;
3 }; B! |) q" z9 L//--- Define in OnInit() the use of the timer every second  V- S" C1 `: {. I- }" g5 {
//--- and start CTrade: ^0 z# `+ Z, o
int OnInit()( ~3 ^' C* y6 s- o7 }, d( P
{/ o( j/ `) z: \# p; B
//--- Set the fill type to keep a pending order$ {0 H* ?: Y5 e# E) A
//--- until it is fully filled9 l( T5 R; d6 u# r. x) ?5 q2 g$ z
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
, G: g6 ?, v' g# P# @//--- Leave the fixed deviation at it is not used on B3 exchange* t. w7 e: ]! V/ Q
negocios.SetDeviationInPoints(5);
& D; {) X+ u3 J4 h//--- Define the symbol in CSymbolInfo...
, {5 Q: ]7 M5 e; C/ @% J0 a- ^, M4 Rinfo.Name(_Symbol);
! D" i, N9 I1 U* c, P//--- Set the timer...' q1 B8 S! g, k$ i
EventSetTimer(1);
* C2 Z) q; C3 L% {6 I9 k$ g) s, {//--- Set the base of the random number to have equal tests.../ Y1 ]$ G2 t+ B6 g, k) N8 N& Y
MathSrand(0xDEAD);3 k' I3 i- O* y: t3 u4 z. b9 U4 S
return(INIT_SUCCEEDED);
. D9 W. l- i2 ^- g7 b* R}, O$ J* R; m! _5 W
//--- Since we set a timer, we need to destroy it in OnDeInit().$ i! E8 e; {  O0 |& H  }1 r1 f  N4 q
void OnDeinit(const int reason)
# g7 m& b4 F9 Z* ~) f6 q{) k# ^! Y0 V% U# d4 ?% j  M$ ^5 H  I
EventKillTimer();8 p/ ?2 e( T4 a
}
0 i+ e5 `3 H- J# g' a) E//--- The OnTick function only informs us that we have a new deal1 S! X" ?9 v  e/ W: ^& ]  I
void OnTick()6 l+ [3 P/ @6 J) ?% ?+ n8 |0 Y6 s
{
( S0 r' M* t  P  w" Item_tick = true;
, ]# ^: I) y0 R* ?}% [( A" O' U# l3 Q. x9 d! \$ l( z
//+------------------------------------------------------------------+  Z' }4 h& A' @
//| Expert Advisor main function                                     |: O* s; [$ @/ M4 G! S* {2 n3 }. i
//+------------------------------------------------------------------+
' d* N/ U- c% Q5 [void OnTimer()2 l: ^5 A- ]/ D4 f5 @
{
7 \& C1 Z! [  N/ q! r8 P1 bMqlRates cotacao[];/ S: z  K+ p* O- Z
return ;
8 L  s( ]( c' w; Lif (negocios_autorizados == false) // are we outside the trading window?
, s6 b* Y5 R$ G) Y' f) Xreturn ;, m, W) x$ b0 j+ |5 Z
//--- We are in the trading window, try to open a new position!- ~1 f9 a! R: U0 D
int sorteio = MathRand();
, ], N, u! |8 A7 Z* ?//--- Entry rule 1.1
2 Y0 Z' `& C- `2 V+ n* ?9 \5 b, yif(sorteio == 0 || sorteio == 32767)
( S$ ?% j. g' Z, @$ {return ;
( U6 n1 [# I, cif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy3 ]7 V5 f8 K' t
{
' {) L+ p  e& u0 G; C1 ~negocios.Buy(info.LotsMin(), _Symbol);$ {) i/ ?) P, b1 p! q" D
}/ ~# p2 i" y# b1 J, p# w
else // Draw rule 1.3 -- odd number - Sell- a" P4 M' v; t9 c) c; e+ P! w! T
{8 P; R+ p1 h. }1 a! u
negocios.Sell(info.LotsMin(), _Symbol);; f9 }& O) ?1 N1 o0 R$ I; I4 o% ^
}9 x! L& Y# x) u. `  c7 I% V
}
6 h/ |. p5 \- R& O2 j" A5 N& H//--- Check if we have a new candlestick...
! Z& W! W& e( ~; I" U" p8 sbool tem_vela_nova(const MqlRates &rate)
7 W* b( t8 [" L3 ^- h8 Y7 |5 E8 P: a; \{8 w2 R4 M3 l  p
{
1 G$ [" @  x. wret = true;+ H8 ^) ^" J* y2 m1 ?# I
close_positions = false;8 I" K# t7 H8 @/ @
}
: l% x& h4 l- i& Pelse2 w+ y! l- I' N4 J! ?5 n
{
# {( K$ F$ j& lif(mdt.hour == 16)
0 Z/ M; _0 z* o7 K; z! K: q1 N. s' P5 Vclose_positions = (mdt.min >= 30);
( K7 T  g/ ?7 m, z* S}- R- `0 p  B. k! z( H/ ]
}% b* a- t3 `6 b" L# v& Z8 d& L
return ret;
  g% T( R0 R5 u- K  m}( F, G' Z. T% L7 d5 j
//---# [5 K! Y6 w) r' E2 W# u' {  R
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
7 {" r6 `5 i3 {5 k1 K{
! g* `5 Q+ ^3 p) r5 s/ Hif(PositionsTotal()) // Is there a position?" e" D4 }& p; r& K4 H; E
{: K( `$ Q. ^  L1 [' ]
double offset[1] = { 0 };. P1 X) a1 p9 p
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
8 b" B' Q; K- E: o' \* f&& PositionSelect(_Symbol))  // Select the existing position!
0 k- z* L; H: G' N' c+ q- W{
5 D: p. U0 l9 k  M; WENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
6 ^/ t5 `! [7 d' Idouble SL = PositionGetDouble(POSITION_SL);9 y" ^0 A& a. |0 J; w2 c/ f+ x
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));6 [; E: H/ c4 A; ]' ^& L7 }8 p
if(tipo == POSITION_TYPE_BUY)
2 b- |' j& B* J9 a2 a/ H{+ B/ d+ y; ?; C( l' D2 t+ }
if (cotacoes[1].high > cotacoes[0].high)
' z6 O5 D! j( D8 L{
( {' l3 U9 w* }$ Q4 K5 {- Ddouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
. p3 T4 r3 h: n0 G% c1 F' ?info.NormalizePrice(sl);# l( D3 b% W2 F! n' Z  ?4 o
if (sl > SL)
, j1 @0 e* y. t+ v2 C{
' s7 U5 R6 W& ]8 Unegocios.PositionModify(_Symbol, sl, TP);
7 }0 N- Y  [& M. D. i. R9 Y}: D7 o* N+ `, O$ C7 l1 D8 @
}
- H, x" k2 i( L0 S* q. A' t}, Q5 C  v6 Z5 D$ a' r( o
else // tipo == POSITION_TYPE_SELL3 a$ `) q! E* l$ P) N8 n' c4 N1 B
{  i5 o5 `) J7 f, t+ r# Z
if (cotacoes[1].low < cotacoes[0].low)! I& s! F% X( h4 i9 Z% D
{, X: I- z% V& s3 r! d/ G* K
return true;* [  F5 [( c" K; \& }
}
/ k% v! u3 s& Z! A// there was no position8 f  I/ M0 a2 C1 w
return false;% t' e+ C6 `& E- a: E7 d
}
* [. d4 u- ?! [; k- M我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
: k. i8 z9 X3 c2 u* k; Y* 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-28 04:41 , Processed in 3.029832 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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