私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA+ S) C- I4 P& l% Y
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。. D' ?# {. u% {% F' l0 g, N. }
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
/ G: u' J3 Q9 f; O" o以下是制定这些规则的代码。: u+ x: Z0 L' n" k) A( I
//--- Indicator ATR(1) with EMA(8) used for the stop level...
$ a- [3 @4 z$ |# }2 Sint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
1 B: G( v! q, T/ Qint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);+ U, A" c1 [* Q# X* i& N
//--- Define a variable that indicates that we have a deal...
. v* T% Y; K9 I6 Z: u8 tbool tem_tick = false;
2 e' t0 b+ S5 ]! E//--- An auxiliary variable for opening a position
# _# e5 b5 G/ k#include<Trade/Trade.mqh>9 V# A  u( i/ ]
#include<Trade/SymbolInfo.mqh>
5 d+ @. {6 x. {2 G/ N- P  HCTrade negocios;
; g# n/ ~- L4 f6 ?) @9 M/ |# rCSymbolInfo info;
$ G6 R' g9 K, M' U% N. z4 W//--- Define in OnInit() the use of the timer every second: a" j3 d; B7 v! H2 L3 _7 v
//--- and start CTrade
6 S* H& W0 o8 Kint OnInit()
. }; L# z6 t8 ]2 P- H, @{
; ^" T5 e( z) @+ S: V$ j- E//--- Set the fill type to keep a pending order+ h5 w$ S; ~; J7 r
//--- until it is fully filled
% E  w+ i0 \' |5 ?0 l  Nnegocios.SetTypeFilling(ORDER_FILLING_RETURN);
- c+ J) Z8 F! o//--- Leave the fixed deviation at it is not used on B3 exchange+ T; D8 h+ P" J! P9 Z8 D
negocios.SetDeviationInPoints(5);" @9 F/ d0 s* n- ^
//--- Define the symbol in CSymbolInfo..., J& S0 B) t1 y# B& s
info.Name(_Symbol);3 o1 E( q% Y1 J. x. h
//--- Set the timer...
- i* C2 w& A6 J( x( p, [4 WEventSetTimer(1);
# I. o5 m  X+ W$ w. @" m% Y, I  w//--- Set the base of the random number to have equal tests...
, g/ U/ {: p1 F. @% @. y) d! kMathSrand(0xDEAD);
9 f- z4 f4 R* _% u# u9 j9 wreturn(INIT_SUCCEEDED);
$ y; \* Q2 X9 t$ C: V3 V}  E- _% g2 b; t
//--- Since we set a timer, we need to destroy it in OnDeInit().* w' G( A" d6 P
void OnDeinit(const int reason)
5 E5 v4 P' z2 m" H{4 S5 m# Q  }1 U; I# l' P" q
EventKillTimer();
1 S8 @) x6 P% Q, |' d  E) r}
8 V; ?% f$ P* S7 N. @8 P. B//--- The OnTick function only informs us that we have a new deal
; w  N" ]7 x  F9 r2 g& `void OnTick()* J* x9 z; d  k7 O1 d; ~* x5 `0 u
{
2 m+ f* _. q$ e, r0 q; ]2 y4 L" G3 B4 ttem_tick = true;
$ `4 s3 W  [3 ]. A& b5 q4 _$ K% Q}- |- N( O1 H6 l5 {$ a  \% C
//+------------------------------------------------------------------+
; Z# ^& c" ]2 \4 _) a) u1 E; ~//| Expert Advisor main function                                     |7 w, x3 l% v& [* _: ]
//+------------------------------------------------------------------+
6 `1 g% T  ?% b5 t/ S: Q, w" Pvoid OnTimer()" V, ^5 }/ J, }- [9 i* q- s
{
/ y$ @; e/ \- R( mMqlRates cotacao[];
) o3 ?) G# c! p( h& W- p  breturn ;
0 q5 j( V* Y& Hif (negocios_autorizados == false) // are we outside the trading window?; w' v3 I4 k+ }" ?
return ;
- Q& T$ c, v& [3 k3 f* I//--- We are in the trading window, try to open a new position!
0 G7 ]& Y1 b9 F' G7 U( jint sorteio = MathRand();/ S: p  f* S9 l
//--- Entry rule 1.18 ?8 O/ z0 i; @( M. Z9 {8 n
if(sorteio == 0 || sorteio == 32767)
; L9 R6 O1 x/ Mreturn ;$ p( K; X) R7 n* `4 j
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
; y- V* k$ T0 _; n{$ S  O* S/ ]3 T: I. z. J# }
negocios.Buy(info.LotsMin(), _Symbol);% @7 D) W5 j9 B7 f5 I
}2 A3 H7 \' D1 G
else // Draw rule 1.3 -- odd number - Sell3 P5 @$ f: w3 U/ x7 B1 r* l
{
8 I3 ^5 Z  t9 J! i2 ~negocios.Sell(info.LotsMin(), _Symbol);: g# u" |5 f0 g) G
}* o7 X8 C9 Q) e1 U2 C$ f8 i
}4 [/ U) Y5 e3 T" x; W
//--- Check if we have a new candlestick...
1 D& u; |8 k3 i& K& o6 ubool tem_vela_nova(const MqlRates &rate)
: i: ]/ _+ `: F- s1 [9 ?{! T* `3 N$ q4 C& z
{8 T' Q) v; v! D, W2 f2 ~
ret = true;* k  {- m* B4 ?  E
close_positions = false;: e( B6 b% _" N, H# [, ~
}% ]4 O$ d& R4 C  ^
else
  |+ P. J% {. M6 \9 s# u{
8 q1 ^+ L/ y1 `) |2 d4 C- ?if(mdt.hour == 16)
  P: ]/ D% a" U0 l! n. z% Fclose_positions = (mdt.min >= 30);0 ]. ^6 v# U, A4 l( b
}
, c( ?+ \- ?5 @0 b+ R4 |7 P}) @) X, @5 Y' W$ m- A  N
return ret;
: n0 X* l3 m# g9 i0 B' {  o}
- U5 w. D/ O7 u; q4 d$ X//---
3 Q8 t6 b2 P3 f. q" r! L  R& ibool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
! P( x+ p! S/ n7 [{. \" `1 h# J* c" r) w8 ?& ~
if(PositionsTotal()) // Is there a position?% i% j, V+ F: M9 M
{
1 m. N6 H- x5 _$ s. U* a2 V2 Ndouble offset[1] = { 0 };
: ^, q6 Y% n, Vif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
& F9 \: U- ]6 @6 k8 r% ~5 d0 q&& PositionSelect(_Symbol))  // Select the existing position!& |; e* g3 P7 S
{
, h! Z8 }; u3 H" RENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);% A+ Y. k! E- C: s/ G
double SL = PositionGetDouble(POSITION_SL);
! v' P) C/ H; C+ y2 Udouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
& |; X, j. K% C  l  C9 oif(tipo == POSITION_TYPE_BUY)
2 K% b* O6 @' J: W! b  C/ h{- M' k2 L/ Q" m  @2 c
if (cotacoes[1].high > cotacoes[0].high)
- u5 u3 l, K1 S2 r: ~( a% g' T+ {{
, h0 P% z. E, w* G* {6 o! l2 j& |double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];4 f' l  X# ]* ~. k& S
info.NormalizePrice(sl);; _/ R. s5 i; Y/ v
if (sl > SL)
2 m3 E7 G# h8 g0 g{3 l" P9 X% k. f7 ^
negocios.PositionModify(_Symbol, sl, TP);
& f8 u4 O! A9 I  u$ s# {}! |9 w2 X; @, ~7 b" y2 b, Y! M
}" x2 q9 b! r8 E
}
) d5 {- Q( d3 a0 ^, relse // tipo == POSITION_TYPE_SELL
! O: H9 n% u3 g7 T{8 W/ B  N8 X7 x6 t
if (cotacoes[1].low < cotacoes[0].low)
$ o4 f) P% F! t& X  m' a6 w{/ L% Z; y; T( C  f9 A
return true;
( J8 F# O$ h) x9 W6 }, e/ M: g}
. b) L+ w9 J( t/ ]% }6 Y! [// there was no position3 {. B. D7 V8 k: R8 h; `
return false;( [' W0 C! R# X2 N" W3 N
}% ?! C7 g, z3 l  }  u/ T  T4 W, w" k
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
+ X) z; z2 A3 n! Z到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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 00:42 , Processed in 1.907574 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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