私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
' ~: n- V; b$ ^2 W. I/ P在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。* Y5 [9 ?& i" e
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
, f$ ?5 J- ?7 A$ W2 M) a以下是制定这些规则的代码。
3 @- r* R" i9 |. Q$ f) X7 G# m//--- Indicator ATR(1) with EMA(8) used for the stop level...
4 V7 g# D. ^$ D2 Iint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
8 n  @& }- K& }0 Sint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
7 D0 w; Q# [) {. A& X//--- Define a variable that indicates that we have a deal...( O: B( z' ?9 B; r! b
bool tem_tick = false;& l/ p% p& }% X4 \$ ^
//--- An auxiliary variable for opening a position
$ ^, E# A% q' Z" b#include<Trade/Trade.mqh>
  P+ [' g# l! M1 E# b# H#include<Trade/SymbolInfo.mqh>
8 s- Y! k0 z; W4 q9 bCTrade negocios;
8 C) s) g- T% w- E4 f# I! u1 mCSymbolInfo info;5 R: M$ h( |2 h$ f
//--- Define in OnInit() the use of the timer every second3 g4 ?; v) c2 W. s) \- o1 G
//--- and start CTrade& u+ r7 n& d+ B) \5 r6 t
int OnInit()& _6 b0 h- K& g" c; E9 Y9 }
{* T/ @7 L; w- e% `; A3 p+ z
//--- Set the fill type to keep a pending order& ~( e$ y. B, s! s# B. D
//--- until it is fully filled/ |) Y1 d4 R" a5 m% a$ O- G
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
, Y1 O, [# @5 a( X! i2 |, b5 T//--- Leave the fixed deviation at it is not used on B3 exchange
, _# z% f4 J  n6 q' I# \negocios.SetDeviationInPoints(5);% ]# z4 n/ o& e' H8 M8 {. ]6 J- ~2 X+ ~
//--- Define the symbol in CSymbolInfo...! V4 n+ m5 n4 h( m" m  R- _
info.Name(_Symbol);
8 U& O9 O1 G, x//--- Set the timer...4 j$ ^) L1 X" Q0 g  q* E3 [
EventSetTimer(1);
2 i# W+ ~. v% q% f. z//--- Set the base of the random number to have equal tests...- F3 S; Z4 B  @: k
MathSrand(0xDEAD);
5 G* v7 ?# w* O/ |+ M! wreturn(INIT_SUCCEEDED);
1 P: G3 |1 ~+ c7 b% {) i0 z, T' [8 R}
) _+ |2 e- O6 r* ?" p, M5 ]//--- Since we set a timer, we need to destroy it in OnDeInit().
6 A, V; I! `: I) vvoid OnDeinit(const int reason); E% h" m6 k/ e1 @) X# F5 G1 q
{
' N, ]: }% D, H, a# y/ `EventKillTimer();! i. b4 @* v5 o7 p1 A! t
}! l3 Q# }* E( z& E2 x8 B6 C( A0 ^* n
//--- The OnTick function only informs us that we have a new deal( V2 y3 N" Y3 |# `2 j
void OnTick()( L  g2 K; ^4 k2 w; ^* Q
{
9 }0 ^1 O  [+ \# ~- v2 ^" Xtem_tick = true;6 E+ p, M9 y  D- w9 l4 B* Y
}
5 q- J+ f0 ~4 H2 k3 A7 W//+------------------------------------------------------------------+7 o0 @1 A) A, T, ?( C
//| Expert Advisor main function                                     |* b9 O9 x, i1 m1 t, }' t; |1 f
//+------------------------------------------------------------------+
) d) N3 q8 \. c- s1 j" pvoid OnTimer()3 p1 f, |* }% |/ t
{
4 q2 e* ]* e; m3 ^1 w& vMqlRates cotacao[];3 p  e- I. h1 Q0 N' p9 G  u# q
return ;" P! Z/ @: f3 v/ c2 l
if (negocios_autorizados == false) // are we outside the trading window?
8 c: d$ `: Y1 v) Q6 Ureturn ;& X% j8 i/ z. [  q# I8 I/ c
//--- We are in the trading window, try to open a new position!
6 l. X7 c) m" z8 e) }int sorteio = MathRand();
4 \6 o7 s" J) A3 y//--- Entry rule 1.14 q2 S+ s/ F' m1 p+ L4 e
if(sorteio == 0 || sorteio == 32767)
1 J, X/ Z3 [% i1 }2 Dreturn ;
  l2 O5 F! }; j: I' t9 Aif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy1 X9 q/ i( ?' G, S2 i& w0 S
{$ J3 d6 j* j0 B4 C* D
negocios.Buy(info.LotsMin(), _Symbol);2 K, z3 ]+ b, p  w# c
}
0 C7 A) O( b( v2 ~. g' eelse // Draw rule 1.3 -- odd number - Sell: f$ X4 e! k" S3 t
{; |3 F6 V5 m  L3 N/ K( b
negocios.Sell(info.LotsMin(), _Symbol);  \) J3 N' C4 F/ r( r$ }' }. b
}
- L* x. w( z, H7 S6 d}
* r2 J; Q$ B- a//--- Check if we have a new candlestick.../ `, a3 k# Z& D" O/ a' H% h5 L
bool tem_vela_nova(const MqlRates &rate)
* N5 H9 u4 I* K: M{$ b9 x4 F2 {9 H2 [8 s! H
{
6 }, y( f0 \  [+ l3 M8 }ret = true;0 ?( U& M7 ~1 K
close_positions = false;
, Y* d0 }* B( z, ?: d$ y+ Y+ A}) l/ u! ?( u" ~9 I
else
/ Z. d5 j: ~: m/ X$ ~$ s# C9 o{: J7 k$ i$ ~5 p8 @+ E* e" A& D
if(mdt.hour == 16)0 }  U: U5 l# a2 X, x: L* m) x
close_positions = (mdt.min >= 30);+ X# U( F" _! q. m, Y4 K
}! z+ F7 ~2 ?0 Y* A
}
, _; t. n6 D5 E2 x7 s  x+ R$ V* Ireturn ret;
3 K; t1 U' j: D( N: r/ I  }}2 `5 v+ A* f9 w3 ~0 |& z4 ]
//---; U& D9 ]- o. ?( D: K
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
, ?+ ]& K9 C* y{; |/ B3 N/ f) ?
if(PositionsTotal()) // Is there a position?
' m, h! A; W6 T{) T$ m% l% Z% t+ c4 \
double offset[1] = { 0 };
; ~! B! ~- I% E" ^if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?4 Z- ?1 [) f. j: e4 o$ [' z3 q' [
&& PositionSelect(_Symbol))  // Select the existing position!
* S6 l5 s( ]$ ]{( ^, W% H% ~8 z+ Q* U/ j+ ~
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);" f4 W6 z+ X3 f, [- `5 A
double SL = PositionGetDouble(POSITION_SL);* ^% O! ]7 m6 p# t6 I$ `6 v
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
0 d" O: P3 \1 {, n' Hif(tipo == POSITION_TYPE_BUY)8 E0 ?/ u. h" Y5 L8 W
{
- w" l( Z/ X& Iif (cotacoes[1].high > cotacoes[0].high)
4 k0 P" F; }$ E/ v9 j{3 W$ M2 s9 \6 Y+ l, k* }
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
; [2 m+ I4 n5 }info.NormalizePrice(sl);( \& ?) _! Q$ _; B5 V) R( U0 `
if (sl > SL)1 c4 A0 ]0 [! a: Y4 z
{4 b, p& n& j* p) L
negocios.PositionModify(_Symbol, sl, TP);
  L8 F2 F9 b( n  W5 X}
' m) D- M8 G$ _. N; a}
- ]- N; N/ z% _}8 U$ F  ?8 V7 h9 O4 }/ h: f! f
else // tipo == POSITION_TYPE_SELL1 X) o0 T" x# S$ o4 V% ^: r8 l
{' @$ g3 S, M. ]0 L1 F3 k. h. Y
if (cotacoes[1].low < cotacoes[0].low). ?& s) P: T! A) V( h3 p. u3 s
{
% n& I7 O# L4 t4 V  e5 }! i+ u# ?return true;
: o4 s6 Q- x$ n}
+ D( `6 `& |+ A/ z3 X: d// there was no position
  i+ l2 k$ C! Z  q1 v/ Jreturn false;4 k2 s" z$ h' h, ]
}  O. }4 m  r3 z0 j2 k5 s
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。6 j4 F$ i9 P1 J) o4 j
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-4-18 15:15 , Processed in 2.391074 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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