私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA; g! u$ D3 v+ @4 t
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。* K, F6 Y1 C0 Y$ z* F
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。  d4 e# H* c% `- _4 Z
以下是制定这些规则的代码。) ?. B! z4 y! ?# [
//--- Indicator ATR(1) with EMA(8) used for the stop level...
7 W: `* O9 I  N# p# v% @- zint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
7 g) T8 [  h4 T0 m# [) Hint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
" V7 }! g. T# B* P//--- Define a variable that indicates that we have a deal...! r% r- ~- F4 G8 t* e. X; ^* S& e
bool tem_tick = false;8 T$ ~: \2 d1 D
//--- An auxiliary variable for opening a position
$ O0 L7 d" k/ C; S3 Y& M#include<Trade/Trade.mqh>( Z2 G$ C2 Y  J# @. o8 F' ^" r
#include<Trade/SymbolInfo.mqh>6 ^8 \3 O' n1 Y8 B5 `
CTrade negocios;( {* ]% F4 g# Y8 s- s" q4 D! a8 s
CSymbolInfo info;+ B) Z4 j1 `* T( D" d
//--- Define in OnInit() the use of the timer every second
: y% A  z  [: _//--- and start CTrade
1 o0 T3 v! {1 e  c) Gint OnInit()& w! N" v* B5 f7 T. v
{
/ @/ i  m9 N- T2 _. ~' j5 Y1 G//--- Set the fill type to keep a pending order$ p0 B( M$ z& f6 D7 u' U3 d
//--- until it is fully filled, o1 ]( @) _5 w, q: \7 P" W
negocios.SetTypeFilling(ORDER_FILLING_RETURN);' N7 _; b$ a7 `6 z+ M
//--- Leave the fixed deviation at it is not used on B3 exchange  j6 x6 E1 s5 v# }/ v
negocios.SetDeviationInPoints(5);
1 J+ c  x  [& C//--- Define the symbol in CSymbolInfo...# o' K9 b8 ~2 k: {" H
info.Name(_Symbol);
; y/ o  k' G0 ]* C# k//--- Set the timer..." m+ h" h2 y' K
EventSetTimer(1);* r+ H; ?  I% H/ k- W" h
//--- Set the base of the random number to have equal tests...
! e6 Q! E) ?7 t  w# Y0 W; r! SMathSrand(0xDEAD);+ }( X" z: l# j2 c& M! T
return(INIT_SUCCEEDED);- E( [: Q2 M) {6 c- T
}# t/ [/ m, I/ l4 b. y
//--- Since we set a timer, we need to destroy it in OnDeInit()./ ]" C7 E/ h. ^4 f6 ]; B& m* i! @: l
void OnDeinit(const int reason)
7 b  I! g5 Y1 x& u6 x) i{
) M$ }" w6 }  A9 s1 ZEventKillTimer();
1 X" X" c* v# a}
% Z% n8 o4 g& {  _  M) W//--- The OnTick function only informs us that we have a new deal
" A( \3 J- {+ c) ]. i9 Tvoid OnTick()
/ g7 R! M. ?/ x+ c. l{9 ^. [( u* b+ m8 s
tem_tick = true;6 g5 L4 j  |: X& K: W/ {1 v, t+ p
}' a  ^2 t% l9 Z$ t
//+------------------------------------------------------------------+1 `' Y$ K1 U' L# r+ n
//| Expert Advisor main function                                     |
4 i+ W  }* x: W0 X9 i$ E( n+ Z) J4 X+ R+ b//+------------------------------------------------------------------+; Q6 u4 X5 _7 _" g" O' b2 X) R# ?
void OnTimer()  z6 X6 h0 e+ ~8 u: K
{
& _3 f" C4 b3 x$ Y. WMqlRates cotacao[];+ ?1 e! m* e, I7 s; g  ]
return ;
' d" H' _- |6 G# A9 rif (negocios_autorizados == false) // are we outside the trading window?! O7 D6 w% H, v7 f: j2 X" D/ k0 \
return ;
" i7 c& W, b7 R0 \* `( U//--- We are in the trading window, try to open a new position!
* n3 u, T( \+ X' Hint sorteio = MathRand();
7 a/ f6 I. D: o* k/ ~//--- Entry rule 1.1
& p8 Q( H# U/ T2 P' Eif(sorteio == 0 || sorteio == 32767)2 B% H( O+ K* ~& g! ~, m6 Y
return ;, O) |! z5 N, w
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy" Q- l/ g8 M0 q7 G' `
{$ }: l3 S7 M7 Z! Y5 Q6 M
negocios.Buy(info.LotsMin(), _Symbol);! E# a6 l/ @- n
}
, Y- D! m) `1 b  oelse // Draw rule 1.3 -- odd number - Sell
- n4 j0 F0 r1 P# U{
6 Z8 B/ E* \; w/ i; ^! t7 F8 `negocios.Sell(info.LotsMin(), _Symbol);) W- L+ \# P7 I: O+ c
}
% R/ D) a% U+ o% W* T}
) N; k. t& q' d+ w. Y1 @//--- Check if we have a new candlestick...! }7 T% x* O! a, D6 C
bool tem_vela_nova(const MqlRates &rate)
- K) F7 w% v- r1 ]{9 N2 O# G$ I/ j# ?5 o: l
{+ m( a1 \4 m- m1 M. w
ret = true;6 n6 s6 Z5 F% a) S( c- i" \
close_positions = false;
+ g; `- _/ W" J# c+ K  h}( O" K2 ?$ E9 a1 n
else( D; ?0 v& `& X" L4 b6 z: N0 Z4 z
{% E  y' c8 s- E$ \1 _) i. X
if(mdt.hour == 16)
( f- H8 D( F2 K/ j6 w8 b. b: z0 u7 c7 wclose_positions = (mdt.min >= 30);" a  s/ @: `8 Q) w+ i( q
}$ N" R0 N$ A; g% c  N$ D: ]* R! ]. k
}
8 y) D9 m) G) q( Y: n+ m2 ereturn ret;
1 ?: D% Y8 Z0 O}
+ g: q! u8 y+ q3 D8 c//---
' H9 H5 w6 M/ C: cbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
1 L$ D) r2 W9 m% D( R9 R! [+ W; ]{
) c8 d3 Q* j/ L/ R# m! Cif(PositionsTotal()) // Is there a position?6 m6 |* B2 @1 a: b: R* P
{$ j/ A/ E8 P% C' Z- b" P! V
double offset[1] = { 0 };
* C# k% e& P  Y$ vif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?8 n- y( A7 |: M0 C. E7 T
&& PositionSelect(_Symbol))  // Select the existing position!. `; S3 s( M" G) K& t3 i
{
7 N) n2 w: o. O; }5 i# AENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);0 N  T$ r& m) }( z/ |3 q( E
double SL = PositionGetDouble(POSITION_SL);" p! Q( k+ \1 `- Z
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
! j, n2 c; e. v8 T) Qif(tipo == POSITION_TYPE_BUY)
: D- o5 l, M' l{. q9 O1 a: l4 J+ ~0 o
if (cotacoes[1].high > cotacoes[0].high)2 Y) p+ l% S" g
{% e6 Z: U2 e( v* y; \
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];7 i* z3 m. r, z* y& {
info.NormalizePrice(sl);. h1 L, z6 k+ x; C
if (sl > SL)
9 R! U" C$ X& ^" M3 T{* t- O% [* s. Y0 s0 P6 P5 p# J! f
negocios.PositionModify(_Symbol, sl, TP);
* t! y* K  U/ @( a$ s1 f& h}
5 d- k( [4 A# j- Y}$ p9 g0 h+ M7 X
}
1 B4 J# d  i! O" N5 @. `else // tipo == POSITION_TYPE_SELL+ b1 k- z  }' C% G' C9 C
{% u) l9 d. t# h4 b. t2 I
if (cotacoes[1].low < cotacoes[0].low)
. E- Y7 c8 {5 E5 A$ z{) M/ |" K- E& \/ N' j/ _& C
return true;
! }' G/ {, I4 p9 K! J1 M) o0 J}* z- v1 _1 c. K  \; }* {
// there was no position; H9 N( K9 g! V
return false;
# |) n/ C% f% @" f, A}) V2 ^$ w$ i# R
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
. }' E+ k: k, E( G! l0 F到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-4-24 01:51 , Processed in 0.464982 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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