私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
' Z# H$ g6 R% Y1 w; C在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。. a! R% M- v6 \4 M8 M5 ^# n
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
" u  x. n# @3 d0 |以下是制定这些规则的代码。4 O# S8 A4 ^4 q5 b! \7 n8 Q6 _9 |
//--- Indicator ATR(1) with EMA(8) used for the stop level...
. q7 _7 K( A# @7 [; x( P' w' {int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
* ~/ u# s* W& ?2 d$ X" Hint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);2 X$ F& r1 {' A1 I! ~  m4 Y/ [
//--- Define a variable that indicates that we have a deal...
( v2 C; Y, J! S- m) l/ Kbool tem_tick = false;* s; Y! {- ~/ T
//--- An auxiliary variable for opening a position, Y* z6 n. @) X; I
#include<Trade/Trade.mqh>
: I( j5 s( ^9 ]5 E8 s#include<Trade/SymbolInfo.mqh>8 j# J2 `. Y5 v0 P6 c& @! O. x+ c
CTrade negocios;( Q2 j  U% A. J" q
CSymbolInfo info;  M1 Z. C+ ~# U. y
//--- Define in OnInit() the use of the timer every second
$ S- f6 D0 X. o6 {//--- and start CTrade
; J& b% w; p6 q4 Cint OnInit()
9 f# `# v, ]" G5 B2 c{
9 J1 k$ D' _) g  v# g+ _3 @//--- Set the fill type to keep a pending order
% S, u5 J# G$ t$ ^. Q4 T//--- until it is fully filled( h3 a) L2 t4 |1 A
negocios.SetTypeFilling(ORDER_FILLING_RETURN);. w  U$ H! r" {. @
//--- Leave the fixed deviation at it is not used on B3 exchange
6 C9 D3 t9 G( n; c% z8 W7 Jnegocios.SetDeviationInPoints(5);
, S0 v. Y0 W$ b3 G- t+ r//--- Define the symbol in CSymbolInfo...
0 h$ Y% g! x% J8 Sinfo.Name(_Symbol);
' z* O% W3 P2 e# N1 q; [//--- Set the timer...
5 u* p& r6 M3 O0 ]2 D1 SEventSetTimer(1);( M" v; u* H) ^/ ?* J# ]$ _
//--- Set the base of the random number to have equal tests...
* E: \1 R7 g" S+ v8 Y7 uMathSrand(0xDEAD);& X) ~) J2 W' ]6 n
return(INIT_SUCCEEDED);* S/ S! S8 h2 w
}% ]; K' [- B* U! n6 `# {. T
//--- Since we set a timer, we need to destroy it in OnDeInit().
2 `4 Z" p' B( q8 `4 ?void OnDeinit(const int reason), s5 K: M+ f! E6 c' b( @2 Y; M% b0 m
{
% p! S; ]; a5 |% Z' }; m+ R% PEventKillTimer();  K7 U% W# X: v, ^" d; F
}  ~8 z5 O- ]* M# Q1 _- j( U
//--- The OnTick function only informs us that we have a new deal
% F8 L+ N% B( W, O! ~* n( |1 qvoid OnTick()
2 i* S- k3 B' j8 e) t( r5 K! ]; |+ _{4 {$ Z  f" T* m: l% r
tem_tick = true;6 Q2 M, U5 ]" D8 {5 O
}
) r6 w# y$ `# b# l, a//+------------------------------------------------------------------+
4 B9 |! w' T7 g//| Expert Advisor main function                                     |
. z+ u. {& ]+ h5 v6 B//+------------------------------------------------------------------+% M6 \4 d7 P1 U6 \9 R
void OnTimer()5 G* |0 u0 E; S! q
{
: I. {7 v$ E; Z4 P1 o1 Y( f: O8 {MqlRates cotacao[];
$ \; s2 U( y, o3 t) e% i( H& E9 Preturn ;
. m5 k4 X0 a- B- Nif (negocios_autorizados == false) // are we outside the trading window?1 J9 Q' w% p* h+ ]6 u! m
return ;% J2 [' k# d  ?; {
//--- We are in the trading window, try to open a new position!; t/ j# e8 F- K$ z& d# H5 a- d4 d8 a
int sorteio = MathRand();/ K; i. E. h( y& o5 z( ]0 X
//--- Entry rule 1.1
7 p+ n1 m- }$ o* W2 vif(sorteio == 0 || sorteio == 32767)
6 _. l4 A/ G& ?3 ?5 A; d& ~8 ]6 yreturn ;, ], a2 J! u# e# t
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
$ j$ w8 g! c# Z: W  U- U. y. y. R{/ B9 a7 B: J$ R# j
negocios.Buy(info.LotsMin(), _Symbol);
( V# i7 z9 n" J) ?8 t}* k0 f7 g4 X. M. |. o$ d5 d7 ~
else // Draw rule 1.3 -- odd number - Sell
+ C6 }' T# F2 w! H  {! C{. F* i2 i% m7 P4 y0 E. r- [
negocios.Sell(info.LotsMin(), _Symbol);. x0 E* S9 W0 O& G! Q
}
/ ]. }. N2 D' {5 N, {}) D7 [7 V; o8 o' L
//--- Check if we have a new candlestick.... I0 @; ^  N/ t% I, o8 C
bool tem_vela_nova(const MqlRates &rate)
  v6 ?  \  Q  W6 F, _# X{& g  q3 r. C1 T. \/ G
{- ^/ O3 l$ G. X
ret = true;7 |3 U( J- A& j8 q8 `1 M
close_positions = false;% l& C5 Z0 V  F8 R0 V- A
}3 E3 X. m" f$ f: K) x" n
else$ l3 U" J( D! X& d$ m* u
{
% H/ M3 z0 W( x- Pif(mdt.hour == 16)
+ a* o* I! o  g9 @  Fclose_positions = (mdt.min >= 30);
8 P) J( H2 y1 c6 R}) [- h7 J" d) D2 i
}: S& E% X* v: g8 Z
return ret;+ w" i8 n5 P: \4 A
}
6 _: P, r; d+ L. l//---
/ e, f9 h, b, v' [$ obool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
+ t) N5 {7 Z% Z' q4 ~{$ j' i+ h% l0 i/ w( {
if(PositionsTotal()) // Is there a position?
( s! t* K: q( E3 _0 `$ Q$ W{; r# @" P) l& r, B- ~" P2 _
double offset[1] = { 0 };
& H* A) H2 t6 Z+ ?- uif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
/ ]. ^, E: ]$ K% A+ \& G&& PositionSelect(_Symbol))  // Select the existing position!
! j5 h1 k% b% @: H, B' w# V{
$ E" I  p* W. NENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
) l+ s- C0 L, U5 `/ bdouble SL = PositionGetDouble(POSITION_SL);7 c/ ~1 K7 y) w1 l1 X, C
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
; h, V2 L+ S3 d# C! Aif(tipo == POSITION_TYPE_BUY)
8 u5 V1 j; d% t: g. G* K{
# u' j6 ]# N) _3 Q* z/ lif (cotacoes[1].high > cotacoes[0].high)1 A6 F7 r2 p  a" I- L, W& Y. w0 u
{
" {' _$ M0 ~9 M# Z$ H* Ldouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
( Z5 ~) P6 E7 {% V- C( v1 Cinfo.NormalizePrice(sl);
4 e6 x% D% T5 j! Q" sif (sl > SL)% x/ w: ?% ~6 s; k
{5 P! h, R$ x( [7 ~
negocios.PositionModify(_Symbol, sl, TP);
* B% i! V" [  `* ^, A* J}" F9 W5 K0 Q( N7 D9 v* N* s$ P
}
  G/ G4 e" }. u}# W2 ^/ c+ B( [- g8 ?  H6 J
else // tipo == POSITION_TYPE_SELL4 E3 [5 N" f9 i/ w0 }
{2 R3 d7 L! I. V: v% f8 J
if (cotacoes[1].low < cotacoes[0].low)2 S5 j: a$ x: g) \1 E# [. [7 n# U
{4 M8 N; d3 Q% J: L4 j' U. O1 P
return true;$ S& `) v  V8 A# u/ `8 p$ @
}9 C9 a+ N3 N, }" x4 _
// there was no position
0 f: z% K* d+ R: A8 Ereturn false;' N2 f- g% O. C
}
2 C" r* q% A! A( C我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。& {+ f" U0 Y; U% Z2 z
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-11 18:10 , Processed in 0.416008 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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