私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
4 J9 E5 m: O" n" H6 k2 z在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
  Q3 g9 E* J: C0 F0 Z+ D- \为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。- D4 m; _/ P7 [/ F3 i/ x, J+ |
以下是制定这些规则的代码。
8 O+ ^" n: I" c! [/ A, m9 S//--- Indicator ATR(1) with EMA(8) used for the stop level...8 U2 C4 g/ U( s$ q; ]2 C. \
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
5 O3 G# f& ^! b9 j7 C: Jint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);% o: L/ a2 Y% B# f; S8 j
//--- Define a variable that indicates that we have a deal...
2 ?8 r1 {+ n4 `3 |& G$ y7 bbool tem_tick = false;
4 x: _9 c- o3 z. N1 s8 C+ m: A, x8 G- d//--- An auxiliary variable for opening a position/ S; I& I+ V; j$ w
#include<Trade/Trade.mqh>
+ _% r2 z! ]) s" r3 m#include<Trade/SymbolInfo.mqh>
9 M+ ?" \& E4 J9 FCTrade negocios;: h' e+ X5 Q" C2 T6 G
CSymbolInfo info;6 b$ V, F/ J7 Q6 g
//--- Define in OnInit() the use of the timer every second+ z1 z9 i  S7 \+ O# F: v# e; |
//--- and start CTrade
' }; G5 T, n3 Q* Jint OnInit()& n( l( V' @/ L0 g  [
{0 l) E1 n/ d7 A5 W2 ^7 Z
//--- Set the fill type to keep a pending order
% D0 s; k$ L0 L, R//--- until it is fully filled7 Y+ y7 X* F7 R, s& Q
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
+ f& E1 \4 Z5 m  S$ s1 ]0 X, I//--- Leave the fixed deviation at it is not used on B3 exchange' R7 e2 h. A- o0 _4 A
negocios.SetDeviationInPoints(5);
9 U* ]+ A! P8 {0 J$ i//--- Define the symbol in CSymbolInfo...
9 K2 S& Z. P# f2 Y$ Tinfo.Name(_Symbol);
2 k& h; b" J! \: M2 g' l0 ]//--- Set the timer...
' U) r- B% ]# wEventSetTimer(1);; V# ]# @7 I2 S6 c# x/ F1 I9 b
//--- Set the base of the random number to have equal tests...
/ D7 y3 }: U5 k* ^2 b. VMathSrand(0xDEAD);* M, [. H8 {! j* l3 V
return(INIT_SUCCEEDED);
4 L8 T! g9 X" p7 f8 O  f9 n}
- C% B5 G4 U* Q//--- Since we set a timer, we need to destroy it in OnDeInit().9 t3 ?+ A  V# K( s' W7 l
void OnDeinit(const int reason)
  @& L; Z+ f0 y1 S1 |9 k( h1 ^{
* h' S! ?( O& b$ C5 F8 iEventKillTimer();
' G1 T% v; B( B4 I0 r8 }}, E4 L3 z0 K7 [! _! p
//--- The OnTick function only informs us that we have a new deal: S8 \. |7 |% Z
void OnTick()
. ]  X& x% u9 I+ U* X3 S{7 p/ B8 W0 Q8 I) G* B
tem_tick = true;) x" O  a; i! ^" H( ~5 m1 ^5 E
}" v  s5 T7 v7 y. i  K/ y. X
//+------------------------------------------------------------------+: c; @+ A4 o9 V( n# G1 L
//| Expert Advisor main function                                     |& w1 o, M# \7 O2 T! ^6 L( G1 L" O
//+------------------------------------------------------------------+2 p5 P+ r/ ]+ z3 Y  o9 V5 r. Z- h
void OnTimer(), r& v9 t! q  d8 C' b
{
2 Z+ U9 t2 e8 U2 D6 n2 |MqlRates cotacao[];, W5 q, H+ Z! ?1 l% T7 P2 x/ T
return ;
% O' G0 R- p: O( J* T5 Q9 i8 ?" pif (negocios_autorizados == false) // are we outside the trading window?( \) e6 j7 n' A7 X" r
return ;( v) p' }" V0 d8 T; Z( S
//--- We are in the trading window, try to open a new position!! P7 @* |( h8 A- ^8 w' `7 _5 W
int sorteio = MathRand();
7 V( }# J; z6 L6 b0 y/ d//--- Entry rule 1.1
+ i& W! A4 \/ |- i( p" Wif(sorteio == 0 || sorteio == 32767)
$ H: U& P+ D7 Preturn ;9 c( S0 D# v' j4 H0 ?$ g  Q
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
' F/ \/ [0 H- @" a/ c{2 f! h, {% t4 c$ K9 L7 U5 e# v
negocios.Buy(info.LotsMin(), _Symbol);$ T- y# A7 I$ D# O' r  w/ Z
}
+ j) q5 y, i2 L" c1 R( Velse // Draw rule 1.3 -- odd number - Sell1 M  |# r% P( ~" D& \& y) m
{
/ |! r: H* |# w. L& Mnegocios.Sell(info.LotsMin(), _Symbol);, Q+ l2 ?1 N1 l. c; Z
}
4 u' k! {/ M6 N/ v2 L}5 v- X2 J4 Q0 l, ]* J, G
//--- Check if we have a new candlestick...2 T" N4 l* x4 _& x
bool tem_vela_nova(const MqlRates &rate)
  Z4 h- T- k% M4 F+ G. n{
( N! a' v+ n6 r5 f# i4 a% @! M{
4 j. Y1 |. c* aret = true;8 C3 ^# S- H" Y8 a
close_positions = false;
( b" C% a' k+ D' @, p6 e" u}- S$ Y4 E" N1 W" W( J/ W
else  G) J$ e. g( y. x
{
$ }8 T% y# ~) s. ^* I- Sif(mdt.hour == 16)
0 ^! m7 Y2 f1 Lclose_positions = (mdt.min >= 30);/ x$ L" m4 e0 z  Y" D  z
}5 Q. Y3 Q8 E6 B8 e( R7 g: d2 G8 s
}. n! X' u" N! B. E+ m) Z. C- \5 H
return ret;  Y) |; q; E- B( y! [
}
: b9 J  F0 @6 T; e$ I( `% V) N8 W& q//---# Q: i  C* g" e& E' X% c8 M5 [
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
# W3 y' L! c3 t5 f6 U* D- [{
1 h, M+ x* O& n* z: Hif(PositionsTotal()) // Is there a position?
& y1 G5 M# J" S. U7 k9 Q9 }) @{  E- c8 q1 I/ L
double offset[1] = { 0 };
! ^9 I2 p& h, |) p6 ?" ?if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?0 @; Z8 D/ Q. }
&& PositionSelect(_Symbol))  // Select the existing position!
' U) B- p; |( m% ~# i& o{
6 c/ N! \4 T, V" }$ p8 GENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);' I# O0 ~& \0 I  q5 {& m+ s6 N5 w
double SL = PositionGetDouble(POSITION_SL);4 f! j5 A; `1 E% y5 [
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
9 X/ F  |2 L/ G, Gif(tipo == POSITION_TYPE_BUY)
1 f- |$ f' s' t5 }" H7 ]8 L+ x4 ?0 S{* l) `" m2 x, A  i# |
if (cotacoes[1].high > cotacoes[0].high). m" i. X" e9 l2 y2 }' M$ ^
{  \7 d" H* E, h# X  l" q: w
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
* j: u# s1 I! o- N$ F, Vinfo.NormalizePrice(sl);
% @% j1 }5 u9 y, W: ]8 c$ k. A4 Aif (sl > SL)
7 {8 e) f: F  M5 S+ \{
& o9 R  l/ o, z+ xnegocios.PositionModify(_Symbol, sl, TP);; ]! {& k; t) E6 R$ R0 V" D
}
- |! ?. S" T5 i9 R/ E. @0 Y}
: r1 _5 M4 }9 j7 x, X& s}8 _) U% {6 z  u6 r
else // tipo == POSITION_TYPE_SELL" Y, N( {" h2 }8 k
{
% e* N* g5 Q+ Q5 U: |if (cotacoes[1].low < cotacoes[0].low)* w9 g/ o, N1 d: Y, B( t
{, E: g& j( S) ]- Q1 B$ d
return true;6 r2 E) }. {1 H# {
}2 y+ V& O  r+ b8 B1 r
// there was no position
2 W+ P1 W& o' c1 G5 greturn false;
/ d# ?' c2 k. Y}
2 X9 W5 z' [; H. L我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
! T: v1 b6 k2 B: `到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-15 08:51 , Processed in 0.988530 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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