返回列表 发帖

MT4自带的EA:Moving Average 详解

MT4自带的EA:Moving Average 详解, @) l% W" J5 U* C7 K: u8 \3 ?

, _# U  U# R$ F9 A. M//+------------------------------------------------------------------+* r1 [0 H" R1 o. X6 h! i; e
//| Moving Average.mq4 |9 V0 Y* s2 _8 d# s+ z( k& \% m3 l# ^; q
//| Copyright ?2005, MetaQuotes Software Corp. |. J# {& \/ d+ u9 n+ g
//+------------------------------------------------------------------+5 P6 e$ V" ?4 b7 o+ \. d
#define MAGICMA 20050610 //定义本EA操作的订单的唯一标识号码
! B9 V1 x8 p0 k$ `. J" a: [8 d" ~- R& ~& U" [) Q7 E( M
extern double Lots = 0.1;//每单的交易量/ b5 t- W  D' U
extern double MaximumRisk = 0.02;//作者定义的最大风险参数0 L  Y3 m6 [9 a
extern double DecreaseFactor = 3;//作者定义的参数,作用要看程序中的用法
* C7 Z  y; c# ~4 X: H, yextern double MovingPeriod = 10;//EA中使用的均线的周期
+ f8 i  X, j- d4 b# r+ Dextern double MovingShift =3;//EA中使用的均线向左的K线偏移量% L4 S; w" M7 b+ D2 Q+ H; I
//+------------------------------------------------------------------+, x' e( D0 T2 O9 Z* r( t2 I
//| Calculate open positions |) x5 X: V# i7 ]1 u
//+------------------------------------------------------------------+
8 V" l4 a! L8 e: F0 A% F, Wint CalculateCurrentOrders(string symbol)//函数作用,计算当前持仓订单的数量) {, @* P/ S  t. _) g
{" k) a- N1 Q0 G7 S( q2 K
int buys=0,sells=0;//定义两个临时变量,准备用于后面的多空订单的个数计算7 t6 H- l8 U3 S
//----
' U7 I+ C7 E* Ifor(int i=0;i
' q. q. @4 S6 Y, `4 @, y$ l# F{2 g* d( j$ y0 i) @( t8 H$ M
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;//挑出持仓单的每一个订单位置
- G! y6 p( k: r# g' Tif(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)//根据订单位置,比较是否是当前K线商品以及订单唯一标识号是否和本程序设置的一致(用于避免EA误操作其他程序控制的持仓单)7 a: D, v7 m* x: F* {
{; T% \  Q4 ]' H0 r+ L. A2 B; v
if(OrderType()==OP_BUY) buys++;//找到符合条件的持仓单后,如果是多单,则临时变量buys增加1/ V: l4 }( X* `% l( X
if(OrderType()==OP_SELL) sells++;//找到符合条件的持仓单后,如果是空单,则临时变量sells增加1
9 A0 J; F7 P1 b# c, d}8 e/ [, @& g+ e
}
: C* D) x* A( b0 k//---- return orders volume" g5 H2 H# O* F4 A5 @" Z+ m
if(buys>0) return(buys);
" C( x. W2 e6 _1 }( Kelse return(-sells);//本函数返回查询计算结束时的持仓单的个数。6 I+ V* P" I+ Q1 G1 I
}: L2 o: C7 V! l, l: k
//+------------------------------------------------------------------++ n9 L& U3 F* V/ {
//| Calculate optimal lot size |
& S- h0 C. p- X: K# u//+------------------------------------------------------------------+
9 K5 o+ Z; ?" h2 Odouble LotsOptimized()//函数目的,根据要求 计算出订单交易量- F1 h% o$ L. C6 C
{
: A  C" K7 p" g5 K$ u8 u$ U$ Mdouble lot=Lots;4 O, `4 i/ c- ~( d$ b+ \
int orders=HistoryTotal(); // history orders total 历史出场订单的个数; a+ J$ H& G6 P! a0 u0 F; {
int losses=0; // number of losses orders without a break# L- a2 M8 ^, ?% q) V
//---- select lot size
- O  o, D$ t# }' ylot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1);//通过风险系数的计算获得当前入场单应该采用的交易量' F. ^# b' v- A5 V
//---- calcuulate number of losses orders without a break. J, b2 e* O& A* u6 G1 c
if(DecreaseFactor>0): w, @  b4 N/ o
{
2 s" l* G& \# a. _! W3 c8 i2 ?for(int i=orders-1;i>=0;i--)/ c% _9 ~  G; c& R
{! Q! U) `! X! N- S/ X  H9 U( Q
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print("Error in history!"); break; }//循环查询出场单队列
: N9 C( R0 e& Y9 fif(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue;//
$ r" R7 v3 ~: Z: \) h//----# E/ Q  C% r( b7 A" Q* t: l
if(OrderProfit()>0) break;
' @% z0 {+ B+ F: Q* ?if(OrderProfit()<0) losses++;//循环计算所有出场亏损单的亏损总和8 |) Q" O1 l8 b( O6 W& u
}
% w" \! k7 f  P. j) d4 Z  Z% @4 @if(losses>1) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);//如果亏损额大于1,则下一入场单的交易量修正为新的计算结果。. ?% N7 p* X/ `$ c* L9 C
}& [  f* f; K/ ?3 ^
//---- return lot size) |4 h4 h/ y' l' _4 z
if(lot<0.1) lot=0.1;//如果计算出的交易量小于帐户最小手数0.1,则下一入场单的交易手数使用0.1作为交易量2 x% T# g: i2 i: }, L; D
return(lot);. Z( G" f2 t, I) ]0 s6 j
}
' _0 A8 A, x1 N' U4 f5 r" M# i//+------------------------------------------------------------------+
9 r6 \1 t/ f0 n9 e//| Check for open order conditions |
7 @' U- \2 h6 J$ J3 U//+------------------------------------------------------------------+
0 C) B/ d: Q/ u5 R% j0 Fvoid CheckForOpen()//检查入场条件的情况并作处理) s3 G* N. A6 k" s
{: z% Q# X5 }! d, H  m4 T
double ma;
9 \8 I, q3 r. rint res;
; s" l, z& K* L: C- [//---- go trading only for first tiks of new bar' Z/ X  x$ G% X# G7 g  G- S1 J/ L9 ^
if(Volume[0]>1) return;//如果当前K线持仓量大于1,说明不是K线的开盘时间点,则直接返回 否则是K线第一个价格,则继续下面的过程" p1 E. K' r2 J$ S  g1 D
//---- get Moving Average
" N5 M/ b; |9 d3 ~1 G. Vma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);//获得当前的均线数值
7 z8 y8 {+ E' y' N' @' A' k//---- sell conditions$ h' b: C3 k+ `
if(Open[1]>ma && Close[1]( V) w' ~2 Y' \7 G5 m9 R7 b
{  b! d& _9 W3 d  v& D
res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,0,"",MAGICMA,0,Red);
( x5 [. x- O2 q1 a3 H$ ireturn;
; H, e% e. \! K9 S. g}- n$ ~5 O( a* _! e* ^: y' c
//---- buy conditions6 C% C" O8 H9 d1 J
if(Open[1]ma) //如当前K开盘价小于均线,而前一K收盘价大于均线,则发出入场空单& S# R4 O6 |- `" p
{; f8 I8 a* J6 ]- p( T' @" ^
res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,0,0,"",MAGICMA,0,Blue);
+ }2 z( r9 S4 V, }( v, e% ureturn;
; \6 R* c7 Y" `; w! {; V}
% `/ Y6 Z2 O" q+ S2 E+ E//----% Y& F5 h6 _( z& i/ i7 ~
}
$ A0 R% l3 A7 ?: f8 V! r/ g//+------------------------------------------------------------------+' d4 x0 \7 L3 R3 q* L; [
//| Check for close order conditions |: }0 n% X3 H0 W2 r3 U) d
//+------------------------------------------------------------------+
; R- X8 b  K$ f- D6 w7 hvoid CheckForClose()//检查出场条件的情况并作处理
. `  z" i$ T* D7 E  Z4 A" z{
! W" A' C7 u& e9 k9 G0 Bdouble ma;
( D4 k) k* {% d! E1 X! A$ `//---- go trading only for first tiks of new bar
7 F: v  m2 [/ k  b% lif(Volume[0]>1) return;  `: J' c) L( ~
//---- get Moving Average9 z* {  Q5 [# G% Z
ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);: m$ U4 Z( }! F% q  g3 K8 D
//----
0 B  P2 s$ q$ @8 l1 `5 S$ C1 r( Pfor(int i=0;i
" k% g, g: w1 h6 r4 w" x% f{
" F% _, u; [8 _% _0 E  {if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;1 R$ n+ z3 G6 ]8 ~
if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;. N, K" ~" e, C# L  }: q) V
//---- check order type' S7 }4 v: f% }& W1 H
if(OrderType()==OP_BUY)5 u1 Q, T7 r. {/ ^& {" F: j
{
% K8 W/ L/ |. L0 M( @7 lif(Open[1]>ma && Close[1]
0 ?/ P3 g: i6 x5 p& ibreak;
8 b8 d0 I/ E, [}& [4 p. w3 U% C- k# j8 `
if(OrderType()==OP_SELL)/ c3 J: j9 G7 H% [
{
$ s! e5 N; D9 C: @: cif(Open[1]ma) OrderClose(OrderTicket(),OrderLots(),Ask,3,White););//如果持仓是空单,则当当前K开盘价大于均线,而前一K收盘价小于均线,则发出平仓指令: {. t6 M# H/ _
4 L0 y+ J# ?4 H. W' y5 X
break;( M5 J3 t, I4 }: w  I0 c
}
! f! O3 C3 r2 F# X) u# {}: w0 t5 Y: w3 K7 N* B* y7 B
//----5 K! Y9 r/ ]% R9 {( y1 f" c
}1 s$ o' S1 a7 B3 k; b: t% W/ w
//+------------------------------------------------------------------+9 u0 R; z0 F; ^8 ~/ R
//| Start function |
% F. g' f2 g, F. {) q//+------------------------------------------------------------------+. j4 m0 l6 O6 ~! X/ F; c
void start()//主循环过程0 H' _! _9 K- P+ X5 i' u
{) @- l7 q- E8 N
//---- check for history and trading5 H' f& u2 m( z0 z) V
if(Bars<100 || IsTradeAllowed()==false) return;+ o/ g3 E3 f( f  [6 x) c
//---- calculate open orders by current symbol  p: D! ?4 `/ F2 Z0 I/ \8 m2 H
if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();. o; q( _, i0 @/ H- O; w
else CheckForClose();
# [& `, e* V5 c" w- H. R/ z* e//----
0 H3 \3 f2 F6 b% u}
" [1 I/ H1 M/ ~- y% `3 T//+------------------------------------------------------------------+
( X3 Y) c& p) i' U: @% G. u, j/ v# i2 P  ^3 G

- K, y/ d9 w1 ^4 A汇茶外汇论坛
客服QQ:708853620;  
服务客户,诚信双赢;  汇茶外汇论坛

返回列表

站长推荐 关闭


EXNESS外汇平台开户流程

EXNESS外汇平台开户流程 EXNESS(杠杆高达1:1000,可交易外汇、期货、股票等,点差0.4点起,没有任何交易限制)。取款瞬间到帐。 ...


查看