返回列表 发帖

MT4自带的EA:Moving Average 详解

MT4自带的EA:Moving Average 详解
) Z! _& c5 ~/ r, M" X) a3 z! Y2 e, H! A: u; I
//+------------------------------------------------------------------+
2 |% w2 c  N: b; P5 ]- z3 P//| Moving Average.mq4 |
; i( W+ y+ L; g4 O//| Copyright ?2005, MetaQuotes Software Corp. |
2 s( [" a' ]9 _% w3 L( W9 Q//+------------------------------------------------------------------++ }2 F! j6 L, U, y7 {* [
#define MAGICMA 20050610 //定义本EA操作的订单的唯一标识号码
8 e% y* I6 l5 b+ }) m
; o: m: Z; F5 X- W8 textern double Lots = 0.1;//每单的交易量4 M4 N8 a  o8 Q; F1 x( p9 j9 [
extern double MaximumRisk = 0.02;//作者定义的最大风险参数. T) `/ l/ y$ x* I
extern double DecreaseFactor = 3;//作者定义的参数,作用要看程序中的用法6 F1 D7 }+ S3 M! b+ U2 q
extern double MovingPeriod = 10;//EA中使用的均线的周期! J) \+ V3 \5 M" Z; ^' z% G5 M+ y
extern double MovingShift =3;//EA中使用的均线向左的K线偏移量
; D2 _' ^$ j+ j+ a3 r3 J//+------------------------------------------------------------------+
! F* U9 V: t8 C7 W//| Calculate open positions |
  O: E- c. A3 I/ p: _9 _//+------------------------------------------------------------------+
2 e% r9 D2 r% l1 P( q& o' N0 ^int CalculateCurrentOrders(string symbol)//函数作用,计算当前持仓订单的数量5 K& u2 {- D! S& F. o
{
; ^  ^* p% Z% V) `  ]$ Q4 ^int buys=0,sells=0;//定义两个临时变量,准备用于后面的多空订单的个数计算, z0 J2 Y7 e7 _  ?# t
//----
6 _# F' q4 m. bfor(int i=0;i( v0 p9 {- B1 P9 a* \6 y
{+ Q' ]1 ]7 q# b" J8 h0 N
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;//挑出持仓单的每一个订单位置4 G4 K3 F. j! ^& `0 Y' |
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)//根据订单位置,比较是否是当前K线商品以及订单唯一标识号是否和本程序设置的一致(用于避免EA误操作其他程序控制的持仓单)
+ u9 A& c; W( |4 Z7 z  b4 _{
/ ~8 B. j7 H2 q# D9 i3 Wif(OrderType()==OP_BUY) buys++;//找到符合条件的持仓单后,如果是多单,则临时变量buys增加1
* {' W9 S% V5 x% `4 lif(OrderType()==OP_SELL) sells++;//找到符合条件的持仓单后,如果是空单,则临时变量sells增加1
3 w1 ^: u/ \% S}6 F6 @8 }) E7 ]4 v
}
1 u" [9 G0 u; G, _//---- return orders volume9 ^6 Z4 \4 v: g! H
if(buys>0) return(buys);/ A2 b, @" D" \
else return(-sells);//本函数返回查询计算结束时的持仓单的个数。
) r. f, e: V/ n& q3 u$ j}5 D8 d  S9 }( |7 V8 W( |' S, p: c
//+------------------------------------------------------------------+
9 V+ y7 W8 @$ Y" E+ J# m; r# w" L& c. a//| Calculate optimal lot size |
. _8 K7 J2 S) }/ i! ]* @" e1 ?1 f//+------------------------------------------------------------------+
' H' O& s$ C7 Edouble LotsOptimized()//函数目的,根据要求 计算出订单交易量
2 n7 x5 m5 T+ F  ^{+ v- L0 u: f8 i- X
double lot=Lots;: k* _1 a; B& G% Z
int orders=HistoryTotal(); // history orders total 历史出场订单的个数2 V% r+ F: h6 U6 w
int losses=0; // number of losses orders without a break2 M8 _  G( ~! m0 L( N
//---- select lot size
. d' ]& G* D$ b! [lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1);//通过风险系数的计算获得当前入场单应该采用的交易量9 ^! c6 @. m4 q; b5 f$ o
//---- calcuulate number of losses orders without a break! m- r. u% i3 o, t7 O
if(DecreaseFactor>0)# ^, h+ E4 ^/ [  I, D# b7 y( l
{
4 F8 d$ H  k4 Xfor(int i=orders-1;i>=0;i--)
# ?8 e" j0 g5 c& S- X{
+ D6 h: P; T, k2 i& a& Fif(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print("Error in history!"); break; }//循环查询出场单队列4 h% t% {, G6 @3 o' r) u
if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue;//4 u0 w7 t1 Q- q8 z& B8 E
//----
/ k. Y% x' K2 n- A9 k$ |if(OrderProfit()>0) break;
& Z3 z, P6 B$ P- V- C. \" {; h( g$ n+ Vif(OrderProfit()<0) losses++;//循环计算所有出场亏损单的亏损总和9 X7 Z# Y& Z( s0 v' E. f" \
}7 O. W# x7 V) w2 ?
if(losses>1) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);//如果亏损额大于1,则下一入场单的交易量修正为新的计算结果。, F# N& G; D: {6 I# \# L& [
}
: C8 Y! F( n' Y8 a+ \9 C7 N//---- return lot size
$ _% X- V! [& [1 a* Aif(lot<0.1) lot=0.1;//如果计算出的交易量小于帐户最小手数0.1,则下一入场单的交易手数使用0.1作为交易量  [4 d+ m# x9 {3 H$ }8 _
return(lot);
! ], a6 t* X# R2 p}
2 u) ~7 x: L+ g% F( ^//+------------------------------------------------------------------+" o: y0 Z' R: _; P# O
//| Check for open order conditions |2 S0 q9 d  [& C" E% p# A1 @
//+------------------------------------------------------------------+6 M1 o. i5 ?) a! S2 j3 R
void CheckForOpen()//检查入场条件的情况并作处理
1 c7 j& y0 {9 \! x- j{
" w# q" i  t5 z0 a  Ddouble ma;
- {& W( P+ x& K& o  P5 ]int res;
" v- r9 T( x2 S7 P; \/ l# @//---- go trading only for first tiks of new bar
$ y; g9 |+ t0 {if(Volume[0]>1) return;//如果当前K线持仓量大于1,说明不是K线的开盘时间点,则直接返回 否则是K线第一个价格,则继续下面的过程
  C* M! o8 D# W0 P4 f( Q//---- get Moving Average
) F; K" O: j. e! z! Vma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);//获得当前的均线数值
6 ]4 W  A0 i2 S. l% p% ~//---- sell conditions
! C$ @, B2 q7 O9 n1 p6 @if(Open[1]>ma && Close[1]
* A+ h$ T) R4 k+ ^{
- @( H/ S  A" W$ N! D" T9 Y4 hres=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,0,"",MAGICMA,0,Red);
3 t( v! W# p& K0 P; [' K' K* y* zreturn;5 i/ r1 U" w  U0 A# u7 [/ ]- |  J  p
}$ ~6 r+ V, ]9 W6 C1 ~5 E3 |9 {
//---- buy conditions- v' _) j  O3 \
if(Open[1]ma) //如当前K开盘价小于均线,而前一K收盘价大于均线,则发出入场空单
- x7 e1 h3 g3 R; p# P0 s- h; r! ]{# m" d9 H1 k8 O' M) Z% [: [+ ^0 V5 l
res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,0,0,"",MAGICMA,0,Blue);0 u: |* w! q& l/ W
return;
" U. c- k& `+ O) D- h; Z}
; z7 c& y7 ^7 x# |2 g' J: }$ @//----
& |0 {0 ^0 ?/ e}+ s( q! I. [4 u- M  V
//+------------------------------------------------------------------+
, K' `0 Q! u* w9 l) X4 |//| Check for close order conditions |
$ G9 c" r- N5 e- ~0 k4 z//+------------------------------------------------------------------+6 x! k+ t0 Y( a/ x( z0 z# C  N7 A% f
void CheckForClose()//检查出场条件的情况并作处理
9 a* ^* Y( G% e{& M( m: v/ b- a/ e
double ma;
) B& I; T# t) m//---- go trading only for first tiks of new bar
( F8 J0 Y$ [' r1 I) r/ d0 Hif(Volume[0]>1) return;2 C2 ]% y/ l0 ]
//---- get Moving Average: I& P. [3 Y. m& D0 B
ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);6 F) j- g9 _- [- G5 \7 X
//----
6 ~, O3 w! x7 K0 Yfor(int i=0;i
* R8 f9 n$ F# o5 ^  F7 I9 U7 T{- q# [) `2 g: `. r  N2 Z
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
) d0 u. g# A3 D# kif(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;- _, v% _9 k3 b
//---- check order type
3 z$ j0 A8 o, e. l3 F% K5 B/ Rif(OrderType()==OP_BUY)
# A( j- }8 Y$ _0 q4 t: o{; j/ K; Z/ y* [: S6 L
if(Open[1]>ma && Close[1]: K! Y; f/ h  j3 W0 P  R  Q
break;) r: H$ [7 |: G( R# N! A
}
9 h# Q" V# r. k$ xif(OrderType()==OP_SELL)/ r- \2 a1 ]1 f* h/ ]* n- w
{
4 Q, P$ X2 C% U# ]7 O' c+ jif(Open[1]ma) OrderClose(OrderTicket(),OrderLots(),Ask,3,White););//如果持仓是空单,则当当前K开盘价大于均线,而前一K收盘价小于均线,则发出平仓指令/ P' a* v3 @3 N' u

) W" [) N6 S7 n2 H7 b( @break;
% ?) _2 `8 p/ C! m}6 ?1 F+ j- t" H$ Q. e
}0 r2 Y" ?8 ?, G' E* a2 H( [6 x
//----
7 r* R* V. i: v: H- U$ d, B}+ V0 \+ p. f/ b- f. l# \
//+------------------------------------------------------------------+
) @' V4 G6 E  L$ o9 j//| Start function |
" `: ^- J* f2 n( W: t//+------------------------------------------------------------------+7 a! n3 M7 ~8 e% K% @  Z9 K
void start()//主循环过程
3 S. M0 X. b7 v; v$ Z( }{9 t; l3 Z7 d0 s: C" \* J
//---- check for history and trading) m; |8 r- h4 S9 v: q
if(Bars<100 || IsTradeAllowed()==false) return;
  |9 _1 \8 c2 B/ K//---- calculate open orders by current symbol
# i7 x& h+ W+ T8 ]1 Sif(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
: S) L+ B/ A/ ~else CheckForClose();  T) b$ N! [# Z' p. \" y
//----
5 p4 H2 d% d8 k+ a6 T* b}
# k% l- ]8 I/ N2 k3 l( N//+------------------------------------------------------------------+
; k  o, I, c8 Z. o& p5 c% Z# I5 l# l0 V0 q6 S# p2 F8 Y
* x6 i5 R4 F0 @* s* o5 W9 H9 h
汇茶外汇论坛
客服QQ:708853620;  
服务客户,诚信双赢;  汇茶外汇论坛

返回列表

站长推荐 关闭


NordFX外汇平台真实帐户开户流程

主要货币2点差起,MT4交易平台,多种账户类型,支持银联卡,信用卡,ALERTPAY,WebMoney,LR等存取款


查看