私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
$ F8 }/ F# P. R& m- g在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
* U" i5 _, W5 ?. k为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
0 ?8 J4 I9 s0 H- d" t以下是制定这些规则的代码。
- E) U4 {5 ^* v/ h$ a: W7 e( j//--- Indicator ATR(1) with EMA(8) used for the stop level...8 e( \3 ~7 J: s7 h
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);. ^6 |  _4 Z; I0 ]* @, D3 M9 r
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);. o. T9 i0 G" ^$ P! E
//--- Define a variable that indicates that we have a deal...
/ F( Q8 Z& g. n; _) ?9 ~bool tem_tick = false;  M4 H6 E' d( D7 p1 L
//--- An auxiliary variable for opening a position
0 e# q+ D  L0 l7 J% e+ ?#include<Trade/Trade.mqh>% Z# B. c9 p0 L5 s( y" O
#include<Trade/SymbolInfo.mqh>+ t7 t! J# E3 |" H
CTrade negocios;- u$ b9 K$ U) ?6 m" n* @$ ?  n" @6 f
CSymbolInfo info;
; X! i) Q3 _+ M, I' D! W! F//--- Define in OnInit() the use of the timer every second- Y2 J  g0 O; Y. m; X$ r* M- V
//--- and start CTrade
6 ^, ?4 P0 k; M6 cint OnInit()1 }7 U. y: v- B* W, @1 o
{
0 u4 j) ?, C. c. t4 B" U% q//--- Set the fill type to keep a pending order
8 ?; G' S( @6 N' {" i, g//--- until it is fully filled4 ~7 Z8 P3 @; [/ n, A
negocios.SetTypeFilling(ORDER_FILLING_RETURN);( G. Z* B6 {& k3 ?! q: X
//--- Leave the fixed deviation at it is not used on B3 exchange+ g5 S+ `9 m+ e
negocios.SetDeviationInPoints(5);
- _/ C+ j7 [6 n4 Q. R7 Q6 B4 i5 Q//--- Define the symbol in CSymbolInfo...2 D, Q4 r0 @# T9 z: N8 y) G0 L
info.Name(_Symbol);
# i9 I# h+ v3 [5 f//--- Set the timer...
, m( H+ _6 @' o- {7 _( T0 xEventSetTimer(1);* L/ K! E+ R4 ~) [2 ]: M# A
//--- Set the base of the random number to have equal tests...
: t) w. @. N% b1 c8 _7 c6 o* lMathSrand(0xDEAD);
$ q- z+ f4 F1 Y5 N4 u6 mreturn(INIT_SUCCEEDED);+ U( ~; U! x9 @; k4 `  S6 P# s6 s2 b
}
: a! ?5 U+ r& H//--- Since we set a timer, we need to destroy it in OnDeInit().
. O, l' M1 ?1 C8 O2 }void OnDeinit(const int reason)0 o1 r* [, ~! g; S8 k
{
* b4 s  ^) q: Y2 Z4 y/ c* kEventKillTimer();% d: V! Q$ n* O& P& [) C$ c
}
  S6 D* S1 D- a$ c& S//--- The OnTick function only informs us that we have a new deal4 A8 B/ p) p% T" C$ z
void OnTick()
. d, }; U2 ~7 h: a( J{
& ]& t  t4 S2 ]( A6 V4 Ltem_tick = true;# \+ ^# Y3 G5 E4 x% J  L- b% [
}2 `( `4 f6 t4 X  ?4 K2 R
//+------------------------------------------------------------------+
5 |* a9 A* h2 ^7 ^" r9 n! J4 z//| Expert Advisor main function                                     |, j- l- w# D2 C; i$ h; D
//+------------------------------------------------------------------+
9 W6 r% }, R# C3 A( Avoid OnTimer(); ~) J- M+ v8 Y! z6 r
{6 J1 g% ?1 ^+ P$ ^. F
MqlRates cotacao[];& H$ u3 z# {9 X+ L4 W# I+ Y
return ;- h' A- t+ U7 S( |( C' Q$ `
if (negocios_autorizados == false) // are we outside the trading window?
6 ~+ }$ n# h' t1 X, _) f& [+ y; nreturn ;# l: _2 [+ D+ C" W/ x* s
//--- We are in the trading window, try to open a new position!" I) z. K; W6 \. r
int sorteio = MathRand();
0 P- D  S; p4 o9 f4 G9 w7 W//--- Entry rule 1.1
8 N* m" e, l: x' k) qif(sorteio == 0 || sorteio == 32767)5 ]2 M* \! A1 C3 F. _& l2 w9 h
return ;5 P) F' C" X- u! Q) e
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
, R' g1 ^4 @$ ~  E% F; |) m{
# i/ j- f' ~- p( M7 D; Jnegocios.Buy(info.LotsMin(), _Symbol);3 H/ ^' c- N7 T" @6 n" Y; ~
}( _/ \) a- F& a9 Y  K& t# y
else // Draw rule 1.3 -- odd number - Sell3 P' I# }, e2 b8 C  M
{- [7 Y3 y$ Z. @, u7 s
negocios.Sell(info.LotsMin(), _Symbol);
" U( X- B4 Q( a' p; V( p}
$ I7 K4 Z8 e: u5 p}2 a1 {4 O9 N+ c/ ~: S8 P: E
//--- Check if we have a new candlestick...
! W$ Q0 [( d6 u$ b4 I0 p8 M! u6 Pbool tem_vela_nova(const MqlRates &rate)
% j3 A- m: Y% `# f+ I& y{- Z& @2 m+ q, I( q
{
' S0 V& c6 c. {: s- C8 t3 j: Dret = true;4 x/ n8 [0 @" S( i
close_positions = false;3 _% u+ Y  c; r: y
}
6 e5 `/ c9 R; {else
7 z$ t/ t; R8 m* r{8 D. ?) Y8 s' E( g5 P, [
if(mdt.hour == 16)0 `4 ~. V2 H: c7 z; W
close_positions = (mdt.min >= 30);9 P1 _" s6 Q1 g$ d% d0 m  k
}
& w; ?% y; j5 c4 J3 |1 ?}1 w1 k- J. ~) T) ^$ K5 [8 `
return ret;
3 ^! |2 L, }% f" j# `, X0 P}
( d& x9 y( a5 f, B* Q//---2 y; r- A6 G; r  t
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
( C, Y. I; C6 `9 r5 q. x0 V6 T& C{& m3 w+ X& J+ ?- W. Q5 R7 E. A- I/ o
if(PositionsTotal()) // Is there a position?
2 J, L( ]8 A4 ?9 }{. h+ K% x  v/ O' Q/ T4 w
double offset[1] = { 0 };
' |9 }% ~8 o/ Y1 _- s# P8 R1 mif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?9 I" p1 v* u' `2 `/ v, r. `
&& PositionSelect(_Symbol))  // Select the existing position!
6 H, \9 \4 N* ^$ d4 a* [  A+ a{
1 \: C& C. U  n5 a9 VENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
, l) C' b4 `+ a2 j' B) x) o- B$ i" ?double SL = PositionGetDouble(POSITION_SL);' T. M5 |1 y2 p* p# W* f- H4 D
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
7 M2 m1 g  W0 a9 H: cif(tipo == POSITION_TYPE_BUY)
* h5 \" e/ m) ]  r& x{
* |2 }. p% C$ r# `7 l6 \if (cotacoes[1].high > cotacoes[0].high)6 J7 C1 f" |8 h# u
{
$ F! Q# E( A* c, |5 tdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];* H. q# Q0 V! Y# S% k
info.NormalizePrice(sl);
% \4 v; [6 z* d- _" bif (sl > SL)
5 a! B5 ^" U* z# P* L0 z3 _  V8 i/ _{- X# R5 U/ _# Z+ [$ }$ ~/ p" C: `
negocios.PositionModify(_Symbol, sl, TP);
$ N: f; T8 @7 Y3 [% e}
( [# J& }$ \0 C8 F}3 Z4 k/ C0 b) t' a8 O/ X: E
}
/ q$ n! R0 o" Oelse // tipo == POSITION_TYPE_SELL% e" X' `" _0 B3 O' R7 W9 f9 P
{
, w( }/ p+ I! o1 xif (cotacoes[1].low < cotacoes[0].low)
, O6 t) \* Y4 k8 n9 S: E) l{
, }/ F" V  X; g' r' S8 u5 areturn true;/ z4 F! p- B2 w# G+ ~% p0 u7 a' X8 P
}
6 o* k1 o" I& I4 E* r// there was no position
6 {0 d  V) H+ O0 U* R7 ?return false;
% Q5 s' h( |9 i: G1 R( R& S}) A  t" f3 d8 A9 l+ i/ [* t
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。* [: B( M( p9 k" }/ 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-14 22:03 , Processed in 2.816041 second(s), 32 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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