私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA* F, ^, y9 y; e
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。% a: o- M* u& @, W1 ]0 n+ K
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。9 N9 y0 q6 n0 n6 V% |/ I
以下是制定这些规则的代码。
; l, r9 J# X7 i1 M//--- Indicator ATR(1) with EMA(8) used for the stop level...2 s* Y- _- ?7 ]6 L* W. ]$ k
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);& z+ ~/ h+ c1 @0 o. y
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
7 _& r1 g4 \5 x//--- Define a variable that indicates that we have a deal...
0 k$ w% r! D) |$ V  w, nbool tem_tick = false;- l3 s" d3 x3 R7 `
//--- An auxiliary variable for opening a position$ M) }/ U) Q  H" K- r
#include<Trade/Trade.mqh>/ U1 ~% u+ c# Y0 |( L; \/ ^: B. m
#include<Trade/SymbolInfo.mqh>$ U$ W5 J; z( [$ J
CTrade negocios;
3 d  v- k1 r, Q! c4 eCSymbolInfo info;" b7 k, w6 T' Y  C8 K* w; W$ s! c3 g9 A
//--- Define in OnInit() the use of the timer every second
* R2 A0 N! p1 {, x//--- and start CTrade
: W& v+ Q# }% B4 G0 X" Iint OnInit()
' F( r" O2 H( M4 C. U2 z7 C{! m1 U8 t$ h: I6 W
//--- Set the fill type to keep a pending order
% C+ S9 [0 i  T7 q: w% m% J//--- until it is fully filled
! g% D* l, D9 [$ z& Dnegocios.SetTypeFilling(ORDER_FILLING_RETURN);, m% [4 l* w4 _# j2 H$ U
//--- Leave the fixed deviation at it is not used on B3 exchange
) I+ A1 H: X- Z+ A; O6 w. wnegocios.SetDeviationInPoints(5);9 T# h3 `& q% @6 s& H, k( M2 Z
//--- Define the symbol in CSymbolInfo...4 E. e8 d1 Z1 t  ?- e4 ^0 z) y) c
info.Name(_Symbol);
' [. b) F7 Q. }- h; f2 Z//--- Set the timer...
$ e( M% j4 J" i% q) O4 m* }9 eEventSetTimer(1);7 p6 P; i9 O& x- J% E) K# ~
//--- Set the base of the random number to have equal tests...
1 H5 n  x/ P' F1 M' Y3 AMathSrand(0xDEAD);
, W& }2 c# G3 u! nreturn(INIT_SUCCEEDED);
) N9 A6 u' p: }9 A6 p}
$ A* U$ F$ _" V. b& y6 S. J//--- Since we set a timer, we need to destroy it in OnDeInit().
! G1 B; B2 D; \void OnDeinit(const int reason)
# T1 |- [6 @9 j) F{
, v/ O/ p1 P! h2 ]( j8 lEventKillTimer();& p5 ~4 N2 E5 j
}+ p% s0 C5 k& R; ^. A% E" P
//--- The OnTick function only informs us that we have a new deal
; o+ s# b1 H, \4 pvoid OnTick()* a' U6 B/ d' {( ], W  g2 |
{
' _% t, i4 f3 X6 W/ B/ p4 c8 ?/ B/ Item_tick = true;0 P. L0 y. |6 E8 [
}/ r' m9 t7 z6 S2 O: M
//+------------------------------------------------------------------+
: ]' A+ W  j% F//| Expert Advisor main function                                     |$ b8 K: R% o# `, _
//+------------------------------------------------------------------+
0 h/ h' s1 Q& H$ avoid OnTimer()
% J5 l6 }  d. ^6 o% X{! k/ |4 k* @0 o0 y  g) P
MqlRates cotacao[];
/ E6 t/ V; n+ [; e8 Q) c- sreturn ;
9 J' O* s- ]) U$ V& I: K9 oif (negocios_autorizados == false) // are we outside the trading window?$ O0 L& }; `# _, k' d
return ;  [  |: l8 y5 D3 U# b" K" M
//--- We are in the trading window, try to open a new position!0 v$ j0 Q8 z0 Q# _7 j
int sorteio = MathRand();
& _. p; K& c* W; Z* \  U//--- Entry rule 1.1
) `$ \* G* q7 ~6 f" W+ Tif(sorteio == 0 || sorteio == 32767): ?6 R  F$ n% ~: k  x$ b
return ;
, E- ]8 u- K! D: z' h; `if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
9 C8 U! k6 G1 d. v: k{
1 U6 N" E# K3 u$ ^+ X9 I0 o/ S0 enegocios.Buy(info.LotsMin(), _Symbol);/ Z1 h) l  N' x, F" D# y: j
}8 w3 [* v# K9 \. d
else // Draw rule 1.3 -- odd number - Sell
5 [+ x0 M4 M- J4 D+ t{
5 T  ~5 }# X/ `9 p" T+ k; Enegocios.Sell(info.LotsMin(), _Symbol);
8 A5 g  v2 X) m}4 ?$ c1 b+ [5 M# {6 L
}
# p/ x. }7 w# R0 d2 J; U9 L+ q//--- Check if we have a new candlestick...2 S- v" o# n7 w
bool tem_vela_nova(const MqlRates &rate)
& w7 O; f% H6 b! {# A/ k+ t2 ~{. K4 E& y: G5 _6 _4 J  s
{% T8 C' I0 a. _0 J  @! k" _" m0 ]
ret = true;$ w, U+ g, V" U, i
close_positions = false;
+ M. q9 ~0 i' w+ v9 |9 }: J}! i; C3 {( T" ~" p4 S& l$ K( V( {! }
else/ d0 k! B1 [2 Q( H% F. o2 I
{( ^- d3 _/ r: n, E9 [
if(mdt.hour == 16); l- ?) J1 H+ r$ f
close_positions = (mdt.min >= 30);/ h" [" H* }4 ^, F0 c7 y# D
}0 R* u$ k. d; `
}9 s. }7 k+ p( t
return ret;) O: `" f  V8 m6 \! E  ^# }8 P/ C# c
}
+ U1 B3 E" a. t8 Q& P//---
; _. T. Z2 v4 F5 D' J4 Kbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
6 h- e( y1 v  `8 L7 x' Z{2 S0 f7 Y' ?4 u  u- v7 T' H5 U9 N+ V  t
if(PositionsTotal()) // Is there a position?
: Q6 M% O- U" C* R& ^' c0 ~3 i1 r{
; E; s8 r$ B. {5 y; Mdouble offset[1] = { 0 };! s' j% P: k! S9 Q
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
2 v, Y; h, n1 D. i3 b* d: e&& PositionSelect(_Symbol))  // Select the existing position!
; k  Z* w/ {% b1 }( G$ R{
- ^; x% ]" ]3 G% h0 W8 O7 @! c$ BENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
9 e) V5 c/ A) O3 z9 Hdouble SL = PositionGetDouble(POSITION_SL);2 d3 D9 K8 T* o
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));: D$ ?! _1 A& W% l4 p
if(tipo == POSITION_TYPE_BUY)  w6 {6 t( N$ p/ T: y4 p$ w& O
{
6 L6 B- ]. o5 L8 _: Y& h& Y. e: a3 |" Sif (cotacoes[1].high > cotacoes[0].high)) f$ y3 ]- v: F
{: V* j) V$ `, u3 N/ e2 E
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];# d. V% x2 g8 v3 {. I
info.NormalizePrice(sl);5 Y6 d( `2 H3 F' q! |6 t
if (sl > SL)
: x/ W" {5 N+ s  E" U{! V. v, `; l" s! H0 u
negocios.PositionModify(_Symbol, sl, TP);
8 |  R$ y# @" l8 `3 Y# W}
: E$ ?' Q9 w2 j, w8 L/ \: y; X}
3 l" D. b3 V$ I3 ^}
, X# ^2 V  k" s# |else // tipo == POSITION_TYPE_SELL
8 i. `3 F' h+ _# C! k{  [: ^( g, q% V/ J
if (cotacoes[1].low < cotacoes[0].low). M5 o; h) o. a# c0 ~# S5 m' T
{9 c; f  g6 W9 Z. ~# {3 p, ?& V7 C
return true;
: F+ K5 Y  W7 J5 A- C( `}! G8 y* s- R' A+ c( G4 F
// there was no position
# K1 V! a! P2 _9 {' c* Ireturn false;
8 L! C- v8 z- Q0 |. d2 c: h}. r& p8 p  B% n' b) f  b+ g( O8 d
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。# w: j; u' P* T3 B* j, g
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-25 21:09 , Processed in 0.706182 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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