私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
3 g+ [* R9 @2 V$ f% O0 W在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。" ~* R3 t3 j5 _$ n2 R$ U
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。% a: C4 [- f- c' t# d+ ^
以下是制定这些规则的代码。% f, e- @7 U1 _7 ^1 a* s
//--- Indicator ATR(1) with EMA(8) used for the stop level...; k! M" w: _) z! V3 z, n* D
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);, y  K& P( l# M' ]
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);7 P. x& j% |8 M5 M) B: f1 N, R9 Z
//--- Define a variable that indicates that we have a deal...
7 }2 q; K, v/ {bool tem_tick = false;
( p! v) |6 ]7 r//--- An auxiliary variable for opening a position
: F7 l  ?* {8 L#include<Trade/Trade.mqh>( r3 {4 }# B7 ]' O$ {
#include<Trade/SymbolInfo.mqh>
, g! w" x6 f3 R3 qCTrade negocios;$ P- R8 @2 m, V% S; [& t
CSymbolInfo info;
* B' D1 J- |* s//--- Define in OnInit() the use of the timer every second
/ N7 P. t$ S: o4 M9 b//--- and start CTrade
6 @, Q9 q' P! C+ v. W! `; i9 T3 Aint OnInit()
, m, K, y5 f( R5 B4 ]+ b{
/ v% C, r# m$ E! b1 [//--- Set the fill type to keep a pending order
$ |& l4 v$ ^9 P- q" f$ R& I//--- until it is fully filled. a- U: X/ B$ N5 {
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
7 d' n4 Z  o. Q) d+ L  e0 K//--- Leave the fixed deviation at it is not used on B3 exchange/ \$ Y+ C% O' c8 A3 g9 |' q  d
negocios.SetDeviationInPoints(5);+ r6 b+ L; j- y) }" o  v% H
//--- Define the symbol in CSymbolInfo...
  \- \" \  q, {" i0 Z7 T' a5 Winfo.Name(_Symbol);# o3 z+ R" }3 Q/ h. E
//--- Set the timer...
, k$ A  k- [# h4 z3 D) D# n1 K5 qEventSetTimer(1);6 x# o' x) b( L! z5 Q
//--- Set the base of the random number to have equal tests...
+ C  a  Y/ j# s8 r2 k* i( y- HMathSrand(0xDEAD);
: t9 D. `% R7 f1 ~return(INIT_SUCCEEDED);% _) N+ a7 L+ E, I3 r- z
}: v! I3 G. ]; `" y! q8 D
//--- Since we set a timer, we need to destroy it in OnDeInit().
' y& C1 e/ l9 W/ H* B4 _2 ]void OnDeinit(const int reason)! [  W8 Z; r" _
{) w- b8 n6 l/ b$ y
EventKillTimer();
; N" S9 Z0 R" M' A; ^& X7 W0 F}
6 x( N. _9 B1 L" X) l% @( y//--- The OnTick function only informs us that we have a new deal, [; _  I! ^, F3 \2 T( E! ~, a# _
void OnTick()  N) t; g/ }7 |' @
{3 `  a" q0 f2 y2 y( O6 l
tem_tick = true;+ c$ e5 M! h$ A! `9 |; `
}
: ?6 a$ [6 n* H9 e* m( b//+------------------------------------------------------------------+) f+ U/ {: N* m* E! v2 f
//| Expert Advisor main function                                     |
) A, z) h+ e: `) {6 h//+------------------------------------------------------------------+) z5 P' z# N* S" u
void OnTimer()- ?% h8 ~) t4 J) w7 e6 P8 Y7 i
{
7 j7 o- j% D" _2 }  a9 `$ Q# x; LMqlRates cotacao[];  X2 B" j7 U3 k; ]& P9 G6 z- S7 \
return ;. T& P8 @8 Z- h, l. u& p
if (negocios_autorizados == false) // are we outside the trading window?5 `- c& X! F1 s
return ;6 ?/ J' j1 y+ i. {2 g
//--- We are in the trading window, try to open a new position!
% p0 `7 {) s; g% X8 N" {/ wint sorteio = MathRand();
/ ?, H7 ~6 y  Z//--- Entry rule 1.1
. }; @0 z0 ]% a' l  H$ N- jif(sorteio == 0 || sorteio == 32767)2 i" j/ P! o4 x3 w; q+ B: g' |& U
return ;2 ?3 S6 y7 o/ A+ _5 D3 g
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy8 ^  j* J" i$ V/ R" ?+ E
{' I7 F! o2 \' N. F
negocios.Buy(info.LotsMin(), _Symbol);
9 t  t3 o, O; D0 M& a+ U  t}; W0 h* w4 Y+ L0 r  s) w% @! J
else // Draw rule 1.3 -- odd number - Sell; s* r$ H6 e; X4 f
{* P! H6 A, W. v! D1 D
negocios.Sell(info.LotsMin(), _Symbol);( D/ k0 i9 I6 s, h  Z: g
}$ X, g1 f) I3 a1 Q, D( d8 _
}
9 I4 m$ ~( ?. d5 k: K//--- Check if we have a new candlestick...) Z- D% h. F' [) q  w
bool tem_vela_nova(const MqlRates &rate)
2 _5 R2 I5 O1 x$ i5 D{
; [+ @  w0 S# [2 B{
$ D. o" C( d# T# l% [" M4 W' rret = true;- Z, z7 g7 l" a4 f/ y
close_positions = false;
' x2 t: q$ s) v3 [6 {! y}
" e& N9 m& i! n6 Ielse
( u2 l9 s" U+ x{# S$ d) {) A3 t/ n8 Q
if(mdt.hour == 16)
9 E! O, M+ c; H" l; ^close_positions = (mdt.min >= 30);
$ {6 _+ F; ]$ w, g}
1 {+ T& g4 M/ l2 X4 W! K}
* R  Q0 ~. l! a- S4 a- c: Jreturn ret;$ B: N+ X% u$ ~+ r9 r7 P
}: S: ^' ~0 a# r
//---3 Z* u0 v. X. S
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])1 D) E2 e4 ?8 B( f% [9 m
{
! ^/ o: D2 E/ E3 E1 y) n' w) V! Aif(PositionsTotal()) // Is there a position?
+ q, K; K/ }3 L* y{+ n% p5 r) ~. a
double offset[1] = { 0 };
2 M& ~* j$ n3 Y3 G( [$ C( cif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
& N: |3 K. a+ E+ _6 z- f$ `&& PositionSelect(_Symbol))  // Select the existing position!
7 D# j) R5 p/ W5 s2 B{
( A0 O  N: D( l' u3 {ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);4 o2 ^. i3 r7 i, f/ c  U: I
double SL = PositionGetDouble(POSITION_SL);$ S3 o/ |1 j" Z0 G4 Z
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));8 Q% P; s: g' \! m7 x; W/ ^
if(tipo == POSITION_TYPE_BUY)
, a1 U2 l; L# j* ^3 c{2 x. R# [+ e7 t; R# q+ a& ~7 O3 g
if (cotacoes[1].high > cotacoes[0].high)8 P: A) z$ r# O# O9 W  d) {
{
: D( g+ q$ Y1 w! a+ ~& ?) Bdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];6 K2 w& u! p  t
info.NormalizePrice(sl);, D# p3 B3 o( `: [
if (sl > SL)9 {9 ~$ p" |6 ]$ z& ~& R
{. M5 D! V: n' l) ?; K, S. a# V. o
negocios.PositionModify(_Symbol, sl, TP);# q0 R0 a0 }3 g
}
" h8 V2 N5 G2 j# R$ @}& c9 G! m; c# X, z3 T
}& l/ K5 P! G+ Z0 @. r
else // tipo == POSITION_TYPE_SELL' m7 P& H/ ]/ ?; z
{; r+ Q: H" x: H$ A' G1 u
if (cotacoes[1].low < cotacoes[0].low)2 X- g& {! V5 ?/ D1 p' c3 b
{2 L7 d+ O  k8 k6 y$ z
return true;
* _- d# p* C5 f}# V1 H" h/ y8 K' z& {
// there was no position
% s$ ^9 K0 n: \5 w5 c! F+ S3 v$ nreturn false;% j% z. ^6 |7 q: @/ ~
}6 N' F9 }; m( o, u
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
1 ^, O, n( n* F2 S! z; \. h到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-21 10:32 , Processed in 0.435257 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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