私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
+ s7 F" d' ^7 [2 z在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
1 ]6 L" w9 Q" B. J2 S7 Q9 G为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。1 _6 I/ @: N: d6 W3 C$ \
以下是制定这些规则的代码。
7 u6 e9 C6 p" A4 l) B0 G' R//--- Indicator ATR(1) with EMA(8) used for the stop level...
, V# }4 k! S+ M/ `5 t9 V" @int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);  S5 d8 a- T3 s- j9 L, _3 J' i
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
' n# p/ \0 }8 ~% f9 C- Y1 Y6 Z//--- Define a variable that indicates that we have a deal...
& X4 }0 [+ \! T2 g0 Rbool tem_tick = false;
5 E: o2 E& o' h% s3 j  o) N: M//--- An auxiliary variable for opening a position  R: D4 f! @: Q8 ^  ^+ ^
#include<Trade/Trade.mqh>
$ e" X/ \6 H- B% L- d3 E  m#include<Trade/SymbolInfo.mqh>5 A. v  v9 t+ _/ o
CTrade negocios;4 c. q6 ]! H- V1 B+ x! m; u$ `& Y
CSymbolInfo info;. r# o2 P, T, b; \7 X
//--- Define in OnInit() the use of the timer every second
' z: w( h9 B5 N//--- and start CTrade- N. t; ]& v2 h% K4 }
int OnInit()
5 ]5 G6 C( t0 z{
- k& P5 Z6 b4 V& m. K& T//--- Set the fill type to keep a pending order; H) u' z' Y3 x! s4 C% S# o
//--- until it is fully filled( a8 P; g1 U/ J; h# [; v% T* ?
negocios.SetTypeFilling(ORDER_FILLING_RETURN);! t2 z7 J* O7 M4 Y$ p' J! H
//--- Leave the fixed deviation at it is not used on B3 exchange
* C1 d9 f% n( Mnegocios.SetDeviationInPoints(5);, g/ |4 @( n% m
//--- Define the symbol in CSymbolInfo...7 ~3 c/ ?$ d) u
info.Name(_Symbol);
- K  }" ]% P9 G5 N+ ^. m4 L//--- Set the timer...
+ f5 ^$ l. u, X9 H, wEventSetTimer(1);5 f9 ~, K* n6 d4 o; G& d
//--- Set the base of the random number to have equal tests...
; ^: D7 h% R7 ?. y2 [' v6 tMathSrand(0xDEAD);, ~( Y) A2 u" g6 x
return(INIT_SUCCEEDED);
- n( i7 O5 O/ k, E, t1 a! E9 [}/ D. F, Y9 ~2 p* J% G
//--- Since we set a timer, we need to destroy it in OnDeInit().
3 @; X+ k) f  N; \/ D: g  Gvoid OnDeinit(const int reason)
8 }. o) E* E+ l: w{
+ W; c$ f8 d, G' ?0 w& tEventKillTimer();) `, ^7 U8 _* W- ?, X0 t: g
}/ B: x! H9 k! J* V" y8 r, ~
//--- The OnTick function only informs us that we have a new deal! N/ `% M+ Y. s' j2 o5 y
void OnTick()
9 ^6 z# O7 E4 A{
( Y/ ]& A: ^6 K; Wtem_tick = true;2 M' M9 S, i5 \2 y
}: f) c) d) H. W& u) X* o, q
//+------------------------------------------------------------------+0 ^6 B/ O9 l( s+ T4 M' E; O  s3 Y
//| Expert Advisor main function                                     |/ }. Q$ G# L+ j1 X/ c  y
//+------------------------------------------------------------------+- O7 i+ }0 u& t; u' r  Q& q9 j
void OnTimer()9 v$ p/ @0 o( y, k, N$ B
{
  M, i' m. ^0 }/ D) Q- iMqlRates cotacao[];
+ h% @2 M- c, b3 |& Qreturn ;
* [& y  }  S& E# w, A% q' T/ oif (negocios_autorizados == false) // are we outside the trading window?
8 a8 Q9 A4 c3 J* [  xreturn ;" @) r# _, j/ A! ^& e6 w' O" F
//--- We are in the trading window, try to open a new position!* S% m4 x, I+ l, N4 S+ z) a0 E4 J
int sorteio = MathRand();6 m7 I) h% ]9 N
//--- Entry rule 1.1
. {2 O& h, D. h* i$ R$ F& Iif(sorteio == 0 || sorteio == 32767)
' l  v" H" o" x# Mreturn ;; w( @% m- E9 B
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy" B* K! r! a+ S' N+ @
{3 x3 k) M. F) O) P! W( {
negocios.Buy(info.LotsMin(), _Symbol);3 _) t9 U7 X' ~2 k
}
3 @2 q. y% h3 t) c; |1 ]/ aelse // Draw rule 1.3 -- odd number - Sell
) @' e$ @+ a3 G0 Z# t5 J{
7 m$ H8 Y2 b! |: ^: V* fnegocios.Sell(info.LotsMin(), _Symbol);
* }: s+ B& y9 @9 v}
; f# l2 B# \! X& r/ r}, H( R4 e' o" S: j
//--- Check if we have a new candlestick...; E7 A6 q  M' `- c! W0 P1 M
bool tem_vela_nova(const MqlRates &rate)
+ L2 v# i0 D! e% a$ j8 h{
# L( H# }7 h, y' b$ M4 ]5 J{
7 u6 J9 \, D: T2 t; v. Q) [8 I0 ]$ Qret = true;
; K* p. A6 u% p) V9 R  M$ ?close_positions = false;
3 l- y: Q1 a! w8 U. ?, e7 K}4 J, Y, m% j1 j% b, e
else
& ?5 ?4 A% y0 _$ q. }, S+ [{
" B: K$ K2 Y* a, z8 mif(mdt.hour == 16)( O6 N6 H, k, W' S/ j
close_positions = (mdt.min >= 30);
! S- a7 X* I: e: _: R}" Q5 D8 ^' L7 @* [- s2 p+ j
}. N  e$ s7 {1 K
return ret;
& }& b1 ^+ J1 V9 H) V' \& n}1 U4 N, y/ m9 G. w' M
//---! b+ X+ T8 T; f, G
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])( u5 t4 A* i- V! U/ _+ }
{( J& g* v5 k0 u1 ^6 ~3 t
if(PositionsTotal()) // Is there a position?) x1 _) G5 ?4 C* _6 B* h
{  I$ E! q8 t. q* d6 e# ~
double offset[1] = { 0 };, w6 f* i/ Y; G- Y8 L! n8 @
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?! {9 E0 X* E, _- Q
&& PositionSelect(_Symbol))  // Select the existing position!
& ]8 y" h8 `, G; P! S" m{/ D: f. l- ^# _
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
' B1 r  N' f: y4 p9 Adouble SL = PositionGetDouble(POSITION_SL);4 O0 z$ U( W# r& A; |$ X% I3 `) x
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));$ _6 o; z" U" Q: o
if(tipo == POSITION_TYPE_BUY)
9 m5 d$ v7 }% i0 ^% Y: d{$ x, y+ B9 I3 u1 R3 _
if (cotacoes[1].high > cotacoes[0].high)0 M% w7 }/ W3 R/ \2 F2 n
{
  ?/ F2 J2 i7 H8 {" L2 D( M" R" Y9 |double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
: L* }+ i! j' k9 a$ M! Y2 \' minfo.NormalizePrice(sl);
  z3 b5 @+ D6 @if (sl > SL)8 d9 S! S' @' c9 v
{
6 v8 P0 k# E1 ^, Y. @negocios.PositionModify(_Symbol, sl, TP);
6 P/ D/ |) N3 q2 l7 c! T}( j$ r3 ]. s: Y$ |; K$ l3 x0 B% f
}! F% N  \5 `( y6 `
}4 o& _* [. I7 `& c
else // tipo == POSITION_TYPE_SELL
# Y5 `. p1 E; j9 N# m+ ]" a- _{/ `) D5 t4 \  z# E5 [6 O) }3 x
if (cotacoes[1].low < cotacoes[0].low)+ U3 O: U& o: q4 e; i5 ]
{4 P+ J" `5 q6 h% d8 N% y) l5 A! k
return true;$ {$ w3 q9 Z2 {! z0 J2 \$ o+ R
}% |) q- L; D# [0 {
// there was no position
/ N/ N: ~) a: U. l3 x" ereturn false;
# [/ ^2 {, j+ P. P2 O}
. `/ d" M) X3 I) a5 c- b- ~2 I我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。$ }" M/ N/ W; m" R) ?7 d
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-11 10:25 , Processed in 0.396271 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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