私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA/ d6 ~) I; |3 [
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
  L! [% g& V5 S; o为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。0 \: M8 Y. @' a
以下是制定这些规则的代码。. {7 \, V2 C3 x, G- j5 l
//--- Indicator ATR(1) with EMA(8) used for the stop level...
9 N, z4 A) |# B8 [  \- ^( Qint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);/ |( f  @" r3 B" ?' d
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
# r7 i3 m6 k" X2 T7 w6 O//--- Define a variable that indicates that we have a deal...7 u- \/ l; V- v0 G' K
bool tem_tick = false;
5 M, N  \! K& l# [//--- An auxiliary variable for opening a position
. w9 p4 E3 o: w#include<Trade/Trade.mqh>
0 Q4 _1 w2 H: I) E9 n#include<Trade/SymbolInfo.mqh>2 D' r5 C" {9 o' ~$ f( F. L  A2 U
CTrade negocios;
- X  x. D1 D2 O% O; F2 }1 t3 uCSymbolInfo info;1 r( {" ~' P) b- ]; e6 e& L
//--- Define in OnInit() the use of the timer every second
/ K! Y; c; G4 r' a" P# \//--- and start CTrade
3 M6 ]! ]$ F! J% r. q& ?int OnInit()
% g3 D5 _* n" ]{* i/ ~6 `$ g* N/ v" [- h: N' V
//--- Set the fill type to keep a pending order% y( o- h; O! t! q$ A
//--- until it is fully filled, F2 `1 F# G8 C9 S, Z1 }
negocios.SetTypeFilling(ORDER_FILLING_RETURN);0 U  T3 N/ D0 B5 b, R$ _
//--- Leave the fixed deviation at it is not used on B3 exchange! H% A" }1 q0 c) |+ i) `! F/ ~* Q
negocios.SetDeviationInPoints(5);9 p; Y# j/ c$ u' E# h
//--- Define the symbol in CSymbolInfo...
) ?' U6 ]; J3 X+ S* a" M8 z1 r! Ninfo.Name(_Symbol);
4 t% v, I. o# |* w1 H& m+ _$ @: b//--- Set the timer...
9 u2 n! p1 u5 f- d2 REventSetTimer(1);2 b- I% ]$ I5 U0 L
//--- Set the base of the random number to have equal tests...6 _! H1 M" M' _
MathSrand(0xDEAD);/ Y- ?. P( R0 S
return(INIT_SUCCEEDED);3 [" B  ?/ `! }" s7 Q
}
* i# \2 D3 t) c. X2 u! z//--- Since we set a timer, we need to destroy it in OnDeInit().
4 I0 q( O/ V# L/ [" y+ svoid OnDeinit(const int reason)
* A% G0 ~+ x8 A: s& F1 s* n: g{5 v# }9 q  V( T+ v9 g
EventKillTimer();
2 _, `$ `- M5 S0 A}
+ `1 ]1 T0 ?& Q5 Y2 A& Y% z, d//--- The OnTick function only informs us that we have a new deal6 b4 c* {( r  O0 B5 p, [3 L, h- b
void OnTick()
. L5 Y$ B; x8 S. h7 V7 Q9 I{+ W) e7 R2 @6 p! y1 p
tem_tick = true;" N  L; E+ r: d5 M% G- `- s8 q8 e
}
$ v: v0 C1 ]3 J7 }7 d//+------------------------------------------------------------------+8 I+ i0 {: `! S8 g; X
//| Expert Advisor main function                                     |
# u! ~' |+ ~8 j+ t6 O//+------------------------------------------------------------------+  g: Y' }' R) z# {. X2 @" Y6 u
void OnTimer()! `# ^( e. n) x1 ^$ q/ _& t5 Z' z0 k
{
( q9 A: y2 }2 _MqlRates cotacao[];
* o1 I/ Q7 D4 p) Y) X. v! ?return ;
( P8 q  O2 q" A, b" M8 e: `) E: }if (negocios_autorizados == false) // are we outside the trading window?- ^6 t9 G5 d: f
return ;
9 p/ a6 B8 ]0 y7 |! J//--- We are in the trading window, try to open a new position!+ p1 c% d# j5 `9 B- N
int sorteio = MathRand();. z7 Y) q+ V$ Y1 [% S3 b+ c
//--- Entry rule 1.1
; N% Y0 [, y5 z4 A4 Q3 v$ dif(sorteio == 0 || sorteio == 32767)( ^, T& r1 ~* R% v( a, O
return ;
& {' i2 s5 U% p7 e) Eif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
  I+ d' E: g! S5 n0 J{) P: O/ o& n; O6 V$ r0 X
negocios.Buy(info.LotsMin(), _Symbol);+ H+ J& T! `! u0 c3 ^
}+ A; z# c& M* [) f  n5 v; b. F
else // Draw rule 1.3 -- odd number - Sell9 R5 h( h% N: A% F
{8 K0 d0 O  g; ]$ w
negocios.Sell(info.LotsMin(), _Symbol);4 a2 R1 Q0 ^! Q3 @$ z
}( U$ a! B8 d. e6 D4 Z/ L' v
}
* S  D/ d$ u  A- h//--- Check if we have a new candlestick...  c! c. j' p/ X$ H% d
bool tem_vela_nova(const MqlRates &rate)
* b) C) G$ a+ V{
; ^* r' [/ c! w: O8 B- g{
! g, V+ f# o+ ~6 V' H0 {* Y7 Z5 t) pret = true;
  o1 \7 Y, z% W; [# \% R9 dclose_positions = false;' r. @* U. f0 t0 z
}
5 E- m2 U2 C7 Kelse
4 B6 U, \9 d  z4 \6 @: X0 |{0 R4 h9 ]5 v1 \- D5 ], [
if(mdt.hour == 16)$ s8 X# c4 m% o  T4 l; T2 V" y
close_positions = (mdt.min >= 30);+ N5 G  A  c# N' _4 `
}. p+ k; N3 s3 Z5 b/ R# k8 Z
}2 P7 L$ y& L) r
return ret;: E$ R6 S+ y: m4 N/ z) X, G& q
}
1 [; a/ u5 Y0 ^4 j3 T, v6 L//---
: O: P; Z) z6 L* j! Pbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
- ~: u5 V6 j8 w{
  _( n% ]2 y+ A8 x& t) Tif(PositionsTotal()) // Is there a position?
) q9 X0 b" P( s{& t! c1 |/ B: o/ A# i+ C
double offset[1] = { 0 };
0 t% r" S8 v2 Gif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
  g# S8 F9 g; d$ z( [0 F' ~&& PositionSelect(_Symbol))  // Select the existing position!
2 g2 O' H0 A, U. V& U% ~{  ~/ R2 s: U8 l% a( i( ?0 f/ e
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
3 U' s* b( T* s! adouble SL = PositionGetDouble(POSITION_SL);
9 F3 }& i) d: Z. i$ fdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
$ N; Q8 X( v' \* h5 x( sif(tipo == POSITION_TYPE_BUY)
: N$ Q7 T, n3 n  [  s) H: |$ L* q{
2 ^. f" p& ~& N* ~0 R3 O( b4 _if (cotacoes[1].high > cotacoes[0].high)
. E4 W; k+ n( M- H2 z) B( l6 o{
/ f% M5 L3 i. \; ~0 Fdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
$ H  \) N& m3 c5 {$ o! ]info.NormalizePrice(sl);
# Q: X* a: R# _+ k) j* J+ P8 [if (sl > SL)
/ r6 O3 p! T; y1 T$ @, V{3 W0 a4 O; d0 H/ j# p% i, {
negocios.PositionModify(_Symbol, sl, TP);* s: A. a  B1 G0 U* a
}
8 C1 d* k# r( L0 l! _9 A}
7 K4 g3 e1 @! x. _}
" M, G6 ~& ?' ~7 O8 b) A9 F9 Telse // tipo == POSITION_TYPE_SELL8 e. D" [% E( ?" K7 f' ^2 N+ L
{
5 T$ c& w* ^1 S) l% ?4 Y/ u# T! s  aif (cotacoes[1].low < cotacoes[0].low)
8 M6 g5 s" a% Y2 F{
1 \4 C, U+ b% |! [1 F" Freturn true;
9 d4 E6 p( r& U: Y  R}0 K) v2 A7 X8 ?* B2 B/ ~
// there was no position; G" M" u- A. t- T6 d
return false;7 _0 k* v# N/ k$ s& t
}
* ?- r$ o* [- P& d我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。; N2 D- O0 e- a. j. E- C, o
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-14 01:13 , Processed in 0.470867 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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