私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA* E, _( S" z  A$ v9 C
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。0 h. `' k) p8 t; l$ m0 n' [- h2 o# U
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
+ z1 N5 T% L: V) [2 x+ T; b以下是制定这些规则的代码。
( ]- U$ k7 W( r, o2 L, a  i2 \//--- Indicator ATR(1) with EMA(8) used for the stop level...
2 J! q1 V0 X" P! n/ Q) S8 Gint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
6 G( D, J6 L! y' m2 fint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);3 Z* A4 J( E' U' R
//--- Define a variable that indicates that we have a deal.../ R$ _" I3 E) S$ `* v
bool tem_tick = false;# h0 }' c- A+ y( ^# c# W
//--- An auxiliary variable for opening a position" {5 u5 A3 G9 \$ A" g1 m
#include<Trade/Trade.mqh>' O" L' }& G  H9 T: M6 S
#include<Trade/SymbolInfo.mqh>
8 I2 g7 v- S+ X( M4 t, U+ hCTrade negocios;
8 w  K0 w1 ?: gCSymbolInfo info;) |+ ^2 R* g) Y3 ^4 f, u
//--- Define in OnInit() the use of the timer every second) O$ v% [$ v3 G
//--- and start CTrade: l0 `* k' I" s- f/ [5 d
int OnInit()  Y! _- n1 D" c! \
{0 @- d; N8 D5 n4 N  |' S
//--- Set the fill type to keep a pending order. v3 h" {) W( F: w: {% A# b: X
//--- until it is fully filled; o; |$ N, y) l, \
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
: |6 C2 G" G/ Y. \7 y& `. N7 y//--- Leave the fixed deviation at it is not used on B3 exchange1 }2 _& X' z/ h8 D* X: O. k8 h. P
negocios.SetDeviationInPoints(5);, w' o6 f$ i! s9 [
//--- Define the symbol in CSymbolInfo...
$ V7 q2 k7 D4 ^4 d1 u0 A8 x- ginfo.Name(_Symbol);
2 G9 K: Z, d* H. j, n//--- Set the timer...  t& a) K' F: z: [- F; O& X1 d$ `. \
EventSetTimer(1);
2 L( W9 \- k2 d: r//--- Set the base of the random number to have equal tests...
- a5 V, p; {* I4 FMathSrand(0xDEAD);# z9 C! l" r% Q) t% ~0 N5 Y
return(INIT_SUCCEEDED);- ~8 D5 a0 l' {1 J8 \# q9 P
}
4 }. L1 Z; g) y5 W+ m* t7 x//--- Since we set a timer, we need to destroy it in OnDeInit().
' d+ @* l7 @6 [( `1 M' H( U' _5 ~/ ^void OnDeinit(const int reason)
8 J5 ~: ]2 {8 N: Q+ L. G{8 T5 p) E* \8 r5 O% e6 s
EventKillTimer();
& h7 t/ u; V# z% {- [4 }8 Y}
! Y3 A8 g, Q& E//--- The OnTick function only informs us that we have a new deal$ G' C6 ]# U: f, ^4 u+ y/ y
void OnTick()
' I0 G2 {% a2 t{
+ S" R  L2 z$ [) P: U8 ptem_tick = true;( y+ t; |( ~1 ]6 o) h1 c9 P
}/ @. x" a( h( L, _) Y3 Z
//+------------------------------------------------------------------+0 J" f% d. O, z5 q2 i6 X! Q
//| Expert Advisor main function                                     |9 N$ D1 E6 ?' c9 f/ m% B! k
//+------------------------------------------------------------------+/ ], c: o# e! L  e( x8 O8 U6 q
void OnTimer()
. x; M# t( I2 t  n{3 H3 D  w; h' v' Z/ R2 I( p( ?
MqlRates cotacao[];
9 I) t: g. q6 R6 z' Q* mreturn ;
3 Y+ V( T4 M9 K4 B- Jif (negocios_autorizados == false) // are we outside the trading window?
7 Z5 w. G) [8 D1 Jreturn ;
! t: H) Z9 D0 E0 q  k- ~* @/ e//--- We are in the trading window, try to open a new position!7 f/ {) x& a4 P2 X& k; f. }- _7 Z
int sorteio = MathRand();& m( p+ I' K/ q8 V) M  @' Y
//--- Entry rule 1.1
/ Y  `/ ~: \/ C/ U& c2 t! @0 kif(sorteio == 0 || sorteio == 32767)
( B/ G, N9 w4 G- ]+ r  Dreturn ;
& b! U0 q( F* u! J( B  Aif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
  q4 n$ m; O2 m5 \$ s{% m1 z7 O6 f9 m2 ?" Y* E5 G5 s
negocios.Buy(info.LotsMin(), _Symbol);/ f8 \* ?6 D6 c8 z$ n
}
' O3 @+ H+ ^; b& B5 [6 u; jelse // Draw rule 1.3 -- odd number - Sell
; j  P1 `( V7 h9 E+ |" r{( U+ A" M0 O! X1 O# u- v& B# X1 [
negocios.Sell(info.LotsMin(), _Symbol);+ m5 y* Z; T* Z& F
}. l- x! d& A: g( |/ ]9 C6 M" u3 L
}5 }! I9 h- k: J8 {$ r9 s9 h6 F- |
//--- Check if we have a new candlestick...
: r) T) f6 t6 fbool tem_vela_nova(const MqlRates &rate)
, }0 _; G6 S1 Z5 e- @8 G2 D{
3 e4 ?( e3 J0 W) R6 V{
4 v. A; S4 s7 C- l9 l# S+ \. D  k8 q. Sret = true;, D  u/ @$ ?, R
close_positions = false;
8 `% G5 {6 m5 d1 {/ N/ g}! R1 A$ c( c3 F3 t
else
. h( r+ w6 ]' M; x* _; R, W{, t; f% m6 r' o% j' u: q
if(mdt.hour == 16)
+ B" U, E1 ^! @5 aclose_positions = (mdt.min >= 30);
- a! b% w( O, O, W}% E, V" a, Q: e! y
}
) D0 m/ g' w: p6 Rreturn ret;
; W8 K" r$ X5 G) t" e}. t) B! D' {" y& V
//---
5 R, d$ `' q+ Q4 r; n( [bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])+ x6 n  m  k. X2 L3 }1 v/ [
{$ h7 A5 |8 s& M2 h
if(PositionsTotal()) // Is there a position?
% T- \: \+ n9 R% y' F7 v{' S! I6 c0 Y) g2 \; H, C8 E
double offset[1] = { 0 };% V% q: |" M: q  o- ]; ^8 e. B3 ~
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?2 V2 ?, o8 n+ |: G9 Q$ w
&& PositionSelect(_Symbol))  // Select the existing position!. E/ J2 A* h/ w( H# O; F
{3 [/ v( n) [/ @! k1 P$ i9 Q
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);) o# Y$ n. ~! b: z4 o
double SL = PositionGetDouble(POSITION_SL);/ J' B2 r# m: h/ o/ G9 s
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
3 H, r1 q! i6 S1 dif(tipo == POSITION_TYPE_BUY)% Y4 o" J% u; k# m# d
{
, D. C8 Q2 C& B8 q2 Aif (cotacoes[1].high > cotacoes[0].high)8 ?0 T5 e) p9 D0 G8 F8 A/ N
{0 Z8 L- H$ q& g; a: w
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];, ?" @9 q* J9 p( j( ~' E
info.NormalizePrice(sl);
, V+ X6 ^# }4 |- Fif (sl > SL)
2 G% w) O1 G+ F: P( V/ R* f{
( ?4 D! \, A" ]7 a, D- qnegocios.PositionModify(_Symbol, sl, TP);
; z' ^5 _" A2 e3 ^9 [4 c1 [}0 d$ h* S9 j5 ]* I5 n- o, g
}
! o( y& t% Q6 a6 N  V* U}
" P9 ~" `& V6 Q: Uelse // tipo == POSITION_TYPE_SELL
1 a/ _/ g! |0 N: H$ W7 Y{7 D% E& Z- j+ [& e
if (cotacoes[1].low < cotacoes[0].low)1 s. P+ \2 |; ~, f: ^1 w
{
* g# ?+ F& O( y- v5 Jreturn true;
% l+ U! p2 X! q' V9 O7 w$ R2 @}
5 z6 p8 B  I* C5 J4 w3 H6 N// there was no position9 i+ M! [7 `' o& x" G
return false;/ l; q- p" \7 T* E
}) p6 j! G' S+ D' S
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
2 j9 p1 x: g9 E9 O$ o+ I7 O. J到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-14 20:19 , Processed in 0.503360 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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