私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
) F, \' Y# j/ G5 R/ L: H& h在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。- \2 T1 z  p, r  _2 x. D6 I, R8 K
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。3 B9 i  _9 ~3 T/ B; Z1 x
以下是制定这些规则的代码。
" j: |) Y! T% o* ~+ W5 \0 l  d//--- Indicator ATR(1) with EMA(8) used for the stop level...
) z* f  r2 F+ [  cint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
& U" N8 t7 J. F! h: y/ D: n( ]int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);; C) N2 c  y! M7 \7 b. [) I' Y" V+ H
//--- Define a variable that indicates that we have a deal...
; y8 _/ x' p5 @2 K" U* _! Nbool tem_tick = false;7 |: Q* ]9 L  r0 i! I
//--- An auxiliary variable for opening a position- Z4 C. `# M! Z1 C) [
#include<Trade/Trade.mqh>" O2 o+ U# Y, u+ B" b2 t6 l3 {
#include<Trade/SymbolInfo.mqh>6 N: Q, j" H8 {) j) H
CTrade negocios;# h. h# H% `$ Q3 `+ C
CSymbolInfo info;3 G! v- `- J  Z5 I( f
//--- Define in OnInit() the use of the timer every second) G! _3 ]7 O) m& Q
//--- and start CTrade
  N! d: L6 R: u3 F4 @( Q6 Eint OnInit()
2 A" P! \- x) f3 F( I{
6 w. d" j0 k. I- b: S! u/ I, w//--- Set the fill type to keep a pending order+ O6 @1 J% O1 D  a1 L/ L3 h
//--- until it is fully filled5 E$ ~0 `2 ]$ U$ m
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
8 z: t& u9 c( [* [7 D8 W' p//--- Leave the fixed deviation at it is not used on B3 exchange
2 t5 U+ a7 a7 K5 M% a  i& o* ~% lnegocios.SetDeviationInPoints(5);
3 U+ O* f0 W* k: G& Q//--- Define the symbol in CSymbolInfo...; J) u+ R' g" X& s/ J  w! U( C, a
info.Name(_Symbol);
/ z8 A* \* v: `" G: k/ G4 c. c//--- Set the timer...4 z: y8 o! i5 u9 ^$ b9 g2 G/ _' X
EventSetTimer(1);
/ t) f% C1 G9 H) L% W//--- Set the base of the random number to have equal tests...9 K3 q- i. H. Y) e1 G6 B! K
MathSrand(0xDEAD);  V: q% q! d/ t' |3 f
return(INIT_SUCCEEDED);
8 @* \1 }2 C5 F. }0 E, Y* t2 }4 x}
- r/ H. Q* ~$ m4 W9 e//--- Since we set a timer, we need to destroy it in OnDeInit().
* A2 g# m( u3 P9 j8 Bvoid OnDeinit(const int reason)
( K5 {' u1 ?1 i{
# s- O, `0 J5 t8 U2 x5 a7 dEventKillTimer();) ?8 V3 A9 q% U5 r# q" b5 Z7 T( e
}
2 Q1 L9 F# ~$ {6 X; j//--- The OnTick function only informs us that we have a new deal
- T, \+ H- \0 Pvoid OnTick()
3 F7 t# _1 e3 q: A{
  h. \8 g" k' r: [tem_tick = true;
0 ?' s4 S$ N9 k  g}
9 l- D  O9 q( k/ O* z5 o! t  z//+------------------------------------------------------------------+3 ?# c/ Z" n% H7 }3 G9 ]
//| Expert Advisor main function                                     |) g0 u7 V0 s: {* |: ]: O0 \
//+------------------------------------------------------------------+4 E1 [% j  [# Y: C& ?6 _5 P
void OnTimer()
, S+ V, S7 S1 g. B( a+ ]{
% V. j. ~. V$ y8 U; v. ?MqlRates cotacao[];
$ s; [8 w# G2 Treturn ;& T" [" e5 b3 N1 C: i. o
if (negocios_autorizados == false) // are we outside the trading window?
3 L9 m9 r8 n3 G" {2 q9 @0 Areturn ;% x0 V, a( W* U% k$ W- n$ ^. r! u
//--- We are in the trading window, try to open a new position!
1 c% b9 }+ ?4 d6 ^0 I0 c3 U, Kint sorteio = MathRand();2 F5 p5 C( _: L2 `$ w, P
//--- Entry rule 1.1* A& W6 M. v8 S$ x7 L4 U
if(sorteio == 0 || sorteio == 32767)
  p# V' D9 {- q) i$ f- F: Lreturn ;/ O3 E* Y, x( J9 e
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
6 e& ^6 h  _+ A" ^6 f9 d7 ~" E+ u9 f- y{) n0 J; |+ f! ^0 T2 u& \
negocios.Buy(info.LotsMin(), _Symbol);/ G# G; v8 h0 o
}% G  T& i# F0 f, R: t! m
else // Draw rule 1.3 -- odd number - Sell
7 ~8 N% G; f4 I  M6 R{. N! a! `2 q" b! p& Y5 U
negocios.Sell(info.LotsMin(), _Symbol);* a2 k( ^/ n8 K/ {
}1 W* |6 E6 e6 ^2 s2 H
}& B; I' {, H$ E+ A4 y) k
//--- Check if we have a new candlestick...
" g' F( V, {5 E6 w* |' obool tem_vela_nova(const MqlRates &rate)9 o4 R% q2 R5 |8 H" ]' ~
{: z7 V0 z  x+ r& b2 A/ }2 e: q: @
{
8 a, f: C" L- f  cret = true;
3 j3 d4 t6 x' v3 d, Q0 {# iclose_positions = false;
" j7 B$ U: L) N* ~; Q  g: {}
  ~: h; t" z$ ]1 jelse
0 O, D6 b6 V; \/ b0 r{5 R7 f6 Z: `! I0 V" a
if(mdt.hour == 16)
5 q  V5 j: a; m( j& ?close_positions = (mdt.min >= 30);
2 O# ]9 U/ b9 K8 [}1 x* l- G/ O% S% Z
}+ R/ r$ d% {' S6 f
return ret;7 i* ]/ o6 [; @/ y: `( A* Q
}, H, @, }) E4 Q
//---
) l1 d) p' ~, \2 ebool arruma_stop_em_posicoes(const MqlRates &cotacoes[])2 C: p" }8 M) T( V8 D
{* \( f5 B- _/ O
if(PositionsTotal()) // Is there a position?+ e( r. K6 L- t+ R1 P& X8 B/ ~
{
" d" p0 W, ?9 P! Wdouble offset[1] = { 0 };8 I9 p- o/ U. @) G" t6 ]& L- D- S
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?/ y: u5 k1 p- I; O* [
&& PositionSelect(_Symbol))  // Select the existing position!5 Q! N  R) V- y- V% Y3 A
{
- I- b% C+ S4 G! X4 G; aENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
$ P' P: G; E+ ]2 X: V5 b# Jdouble SL = PositionGetDouble(POSITION_SL);
) x$ S2 @. c( ?- Qdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
5 f8 t9 h' U: w# y& g; Gif(tipo == POSITION_TYPE_BUY)0 J% k- {5 y8 ?8 J# o
{! Z4 A9 Q7 \5 ]# m) l+ ^5 f1 F
if (cotacoes[1].high > cotacoes[0].high)
2 t5 b% B) a; }# Y8 I" A{
- P5 z; }8 a- l2 _1 d% V: d7 mdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];. _- d5 }: N, M
info.NormalizePrice(sl);- K) R; a; Y4 ]- b3 ^
if (sl > SL)8 I; K6 ], t( e, K% F/ S
{! O3 X+ i0 r( {- d, @, X8 F. c: q
negocios.PositionModify(_Symbol, sl, TP);
$ t; v$ A% l. s" L1 B9 }+ s& T}
; u! s& k2 f; h" d: {! b$ M! _}' Y6 W4 ]2 Z( b* r2 [
}; J& X; e& f+ }& R* h
else // tipo == POSITION_TYPE_SELL* \; M5 x. U9 T& x2 y! f
{7 O" z3 X: T$ _" ?) r! \' ?
if (cotacoes[1].low < cotacoes[0].low)
( P' _' k4 T2 }" O3 H: v( e{' l. H& V8 F6 [# R  b: T
return true;
# A. R7 C  s* X}: L4 w  c. l8 a8 W; @. Q7 U
// there was no position
" c# `5 t+ `7 @0 P+ D' Creturn false;) ~( p. N8 d( n
}/ p3 [. E/ l% b. [" ]/ c  s
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
% M  _# q% s5 c# q4 R  h8 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-5-3 22:22 , Processed in 0.433989 second(s), 32 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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