私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA, b. Y0 e! m: M1 _9 u; D- t: G
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
3 E6 x2 A( L1 [+ U1 c为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。' d. n* s+ Q' H: |1 ?3 }
以下是制定这些规则的代码。7 I6 k! k: j- P2 c/ V
//--- Indicator ATR(1) with EMA(8) used for the stop level..." Y- }. z$ ~" Y: D& q6 |9 E
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);+ l. Y& K+ s# g9 C# m1 b
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);# u' K* h% l' @8 t7 P( J7 y! {- a) c
//--- Define a variable that indicates that we have a deal...
$ W, y' U0 [8 c/ M, e3 F+ gbool tem_tick = false;
8 U! m% h+ I8 W5 @( v% \* ~5 I//--- An auxiliary variable for opening a position
) D( [) A" w; V9 s( H#include<Trade/Trade.mqh>
9 @, r' m7 W* Q$ H# M6 d* z#include<Trade/SymbolInfo.mqh>
- h$ [: G# b/ y* V$ nCTrade negocios;
& S: o: w5 O0 fCSymbolInfo info;
3 ]7 {7 E" M8 Y6 s  t//--- Define in OnInit() the use of the timer every second4 M- T+ M: w% D" J1 T6 B
//--- and start CTrade0 |  f$ a, [# [. v$ H
int OnInit(); X6 c6 G$ w) C# Q( r; D* i
{
' |2 N, z8 m8 s1 d//--- Set the fill type to keep a pending order
6 O: o+ K  x) o9 y" `; M2 f//--- until it is fully filled
, t+ f2 k; L  N9 V" ?6 R6 m# Y/ @% rnegocios.SetTypeFilling(ORDER_FILLING_RETURN);" m0 m) q7 U+ K
//--- Leave the fixed deviation at it is not used on B3 exchange
4 s- d. x' j7 V% k4 \4 bnegocios.SetDeviationInPoints(5);
; j0 b! l' {7 I' M4 q//--- Define the symbol in CSymbolInfo...2 K  W7 d0 ?( x  v3 b, D3 p7 r
info.Name(_Symbol);! e. e0 R0 u; s
//--- Set the timer...
5 I9 r  b0 Q# R# HEventSetTimer(1);
+ Y) y7 k' I* L1 e//--- Set the base of the random number to have equal tests...
5 `% W8 x6 p: j2 ~& JMathSrand(0xDEAD);
. h5 ~9 Z+ z% d7 ]& s- |+ J! P3 E8 V2 wreturn(INIT_SUCCEEDED);
( z' y7 W3 n( T  q}
, I9 ~6 [* ^7 S7 h: ^8 d- U//--- Since we set a timer, we need to destroy it in OnDeInit().4 f# u) v! y: w6 J- b+ Q6 r
void OnDeinit(const int reason)( F) o# L2 t0 G+ m7 v. s5 {7 ~
{" X5 h8 r" V5 @$ s- G1 H4 x
EventKillTimer();
6 g5 m+ n0 A( f% K9 I# g- t* d& }1 [8 I}
7 f) [- E" p, Y//--- The OnTick function only informs us that we have a new deal
. F* {- h0 J' Y- Q6 }  v4 bvoid OnTick(); w+ i, \5 x* p; {4 S
{8 j6 \" p/ b/ q0 y
tem_tick = true;* u# z8 P: z9 u" V+ m9 j  o9 w3 N
}
: l/ e( Y- z0 d8 k//+------------------------------------------------------------------+; S0 r1 N& I7 y$ i* T1 [: H7 Z5 `
//| Expert Advisor main function                                     |
: s4 [6 s! Q$ L* [% g. v8 L% N+ o//+------------------------------------------------------------------+0 Q5 q/ N& h$ |  q0 e
void OnTimer()# `2 ?; J! v4 T$ A
{
/ p/ P0 C" Z0 p, B8 O% xMqlRates cotacao[];9 m! O9 n( C8 d" K; o6 n
return ;. r/ {7 y+ f" D3 s
if (negocios_autorizados == false) // are we outside the trading window?9 Z- `/ g: o( g8 I/ g
return ;- ]! a( P5 T8 y* d+ v
//--- We are in the trading window, try to open a new position!
# A- J  I4 n9 lint sorteio = MathRand();
1 j* q3 @: y/ }0 @. D! R//--- Entry rule 1.1
* z2 R# W( s7 |6 [' V  hif(sorteio == 0 || sorteio == 32767)4 J2 u0 u$ u( G6 Z
return ;
# A! W- @4 n5 G. V+ A4 s3 {/ xif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy* B" B' Z* S  o& c
{, X1 O- Z5 {7 H; Z. X& j
negocios.Buy(info.LotsMin(), _Symbol);2 V' k$ v# C( J: a4 R* t7 m
}
7 r3 X" Q) [9 x' H7 E4 {. K7 s1 Nelse // Draw rule 1.3 -- odd number - Sell0 [( p3 g/ j( @& W0 c; @
{
/ r0 y7 e$ Y0 h; `2 s4 V/ v* @# Enegocios.Sell(info.LotsMin(), _Symbol);, q/ l+ e& m6 D& T7 ~
}
4 P( Z- W9 L# _& j}
4 Z. ^% M  ~; x" o+ N//--- Check if we have a new candlestick...( W) A- [% u$ a; U
bool tem_vela_nova(const MqlRates &rate)
' B5 |  L0 f9 A: H" h{& [" U/ v) k6 l: ?
{1 T. q. ?* ?4 Y) l1 i+ V, p
ret = true;
6 B$ M# T% s8 B! cclose_positions = false;% D$ T  L; `$ t
}, j2 Y' p/ k. \* Q, s: i* U3 V
else8 Q" Y  F# _/ i. J! l
{3 e, j# ^5 \  y' R
if(mdt.hour == 16)' e, d: A0 S  Z7 I& v- v& ~
close_positions = (mdt.min >= 30);4 O# C; t9 A* K  |4 C( q7 ~" }
}
( ]3 o  k9 b8 T3 s) V- F}
# T; K3 v; S3 T" }  k0 L* _1 mreturn ret;7 i4 f# q" V% j$ }, ]! M) G
}$ _* k1 n4 B5 u: \" u9 Z/ c
//---
: B% B9 c7 d) V' r3 W: `! Nbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
4 N4 ?) G; `! ^, a{; b5 ^# R; z6 r' O; x6 x" t
if(PositionsTotal()) // Is there a position?
5 U' Y9 }% u4 r6 F" h{
$ V3 @/ @; c9 S6 sdouble offset[1] = { 0 };$ I0 u3 O  D* E( R
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
, m3 U/ |5 I, B* t  o( t$ @  p& G&& PositionSelect(_Symbol))  // Select the existing position!
2 k6 n4 A/ x+ }9 E: h) r5 R{
; ]+ \; m3 d, G$ HENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);9 J6 [, S, M% a6 {4 _
double SL = PositionGetDouble(POSITION_SL);+ v' \/ {! l  Z0 P8 W  R. _, p, H
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
: B; Y2 E" R( \% J' W+ {if(tipo == POSITION_TYPE_BUY)
; `1 x$ w  h% m3 q{# E4 s9 ?% T0 ?7 N& n
if (cotacoes[1].high > cotacoes[0].high)
" t& a) e+ K8 ~8 K{
& _  S- ~+ X: m% i; P( I* y, idouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
  T7 W7 w& z. Y0 @0 ]& ~: [- ^info.NormalizePrice(sl);$ _0 X4 ^/ R& _$ O2 |, J# [
if (sl > SL)7 t9 }, m0 J. j; P" r1 \
{' ], z4 _6 ]0 y3 ?7 E+ n5 Z  F. S
negocios.PositionModify(_Symbol, sl, TP);
: E9 g& D- }* c8 v}
# F' _" K3 u' \9 q* k" i, s- p( C1 l}
) }3 K8 T% r  R8 N$ ^: o9 z! M}' T* |' t3 m' I, e' H
else // tipo == POSITION_TYPE_SELL" V3 e& N  G% B2 c3 M; F
{
( r0 C3 e$ ?" P2 T. K$ |5 _if (cotacoes[1].low < cotacoes[0].low)) e3 c- ]' Y& R2 V% J" z- U
{, s6 H6 T: X6 q" r$ l4 p: c: a
return true;* f$ |- l9 o( @
}3 r) o4 w) N9 `
// there was no position
8 a7 k' p) F; }, {5 areturn false;: z. T- R0 Y( P3 V& @8 L& i/ ~' q
}
2 R7 {$ o- Q6 n2 X我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
; d! U% O/ ]' n, E到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-14 14:21 , Processed in 2.426175 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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