//+------------------------------------------------------------------+
//| 5分钟动量系统.mq4 |
//| Copyright 2016, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
#define MAGICMA 2016817
//--- input parameters
input double Maximum_riske=0.05;//资金最大风险系数
input int MovingPeriod=21;
input int ENEPeriod=55;
input double ENEdeviation=0.03;//ENE偏值
input int Maximum_TP=500;//最大止损点数
input int L_ma1=200;//离均线差值
bool woke=true;//允许运行
//+------------------------------------------------------------------+
//|Caculate open positions //检查开仓是否 |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)//计算当前持仓数量函数
{
int buys=0,sells=0;//定义两个临时变量
//---
for(int i=0;i<OrdersTotal();i++)//循环当前订单队列
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;//挑选出每一个订单的位置,如果没有就中断。
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)//根据订单位置,比较是否是当前K线商品 以及订单唯一标识号是否和本程序设置的一致
{
if(OrderType()==OP_BUY) buys++;//找到符合条件的持仓单后,如果是多单,则临时变量buys增加1
if(OrderType()==OP_SELL) sells++;//找到符合条件的持仓单后,如果是空单,则临时变量sells增加1
}
}
//--- return orders volume
if(buys>0) return(buys); //如果买>0,则返回值为(buys)
else return(-sells);//否则为带符号的sells个数值 本函数返回查询计算结束时的持仓单的个数。
}
//+------------------------------------------------------------------+
//| Calculate optimal lot size //计算最佳仓位大小 |
//+------------------------------------------------------------------+
double LotsOptimized()
{
double One_lot=MarketInfo(Symbol(),MODE_LOTSIZE);//一标准手手需要的保证金。
double lot=NormalizeDouble(AccountFreeMargin()*Maximum_riske/One_lot,2);//开仓数量。
return(lot);
}
//+------------------------------------------------------------------+
//| Check for open order conditions //检查开仓条件。 |
//+------------------------------------------------------------------+
void CheckForOpen()
{
double ma1,ene1_up,ene1_dn,macd,ma2,ene2_dn,ene2_up;
double bid=MarketInfo(Symbol(),MODE_BID);//市场实时价格
double vspread = MarketInfo(Symbol(),MODE_SPREAD); //市场点差
double Vpoint = MarketInfo(Symbol(),MODE_POINT);//市场最小波动单位
int res;
ma1=iMA(NULL,0,MovingPeriod,0,MODE_EMA,PRICE_CLOSE,1);//前次EMA21定义
ene1_up=iEnvelopes( NULL, 0, ENEPeriod , MODE_EMA, 0,PRICE_CLOSE, ENEdeviation, MODE_UPPER,1) ;//前次ENE上轨定义
ene1_dn=iEnvelopes( NULL, 0, ENEPeriod , MODE_EMA, 0,PRICE_CLOSE, ENEdeviation, MODE_LOWER, 1) ;//前次ENE下轨定义
ma2=iMA(NULL,0,MovingPeriod,0,MODE_EMA,PRICE_CLOSE,0);//当前EMA21定义
ene2_up=iEnvelopes( NULL, 0, ENEPeriod , MODE_EMA, 0,PRICE_CLOSE, ENEdeviation, MODE_UPPER,0) ;//当次ENE上轨定义
ene2_dn=iEnvelopes( NULL, 0, ENEPeriod , MODE_EMA, 0,PRICE_CLOSE, ENEdeviation, MODE_LOWER, 0) ;//当次ENE下轨定义
macd=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1);
double stoploss_s=ma2-bid+vspread+L_ma1*Vpoint;
double stoploss_b=bid-ma2+vspread+L_ma1*Vpoint;
//--- sell conditions//做空条件。
if(ma1<ene1_dn&&macd<0&&bid<ma2<ene2_dn&&stoploss_s<Maximum_TP*Vpoint)
{
res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,100,ma2-bid+vspread+L_ma1*Vpoint,0,"",MAGICMA,0,Red);
return;
}
//--- buy conditions//做多条件
if(ma1>ene1_dn&&macd>0&&bid>ma2>ene2_dn&& stoploss_b<Maximum_TP*Vpoint)
{
res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,100,bid-ma2+vspread+L_ma1*Vpoint,0,"",MAGICMA,0,Blue);
return;
}
}
//+-------------------------------------------------------------------+
//|Check for close oder conditions.//检查平单条件。 |
//+--------------------------------------------------------------------+
void CheckForClose()
{double v1spread = MarketInfo(Symbol(),MODE_SPREAD); //市场点差
double V1point = MarketInfo(Symbol(),MODE_POINT);//市场最小波动单位
double ma2=iMA(NULL,0,MovingPeriod,0,MODE_EMA,PRICE_CLOSE,0);//当前EMA21定义
double bid=MarketInfo(Symbol(),MODE_BID);//市场实时价格
double Vpoint = MarketInfo(Symbol(),MODE_POINT);//市场最小波动单位
double ma1=iMA(NULL,0,MovingPeriod,0,MODE_EMA,PRICE_CLOSE,1);//前次EMA21定义
for(int i=0;i<OrdersTotal();i++)
{ if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
//--- check order type
double tl=OrderStopLoss();//选出的定单的止损点位
double op=OrderOpenPrice();//选出的订单的开盘价位
double lot=OrderLots();//选出的手数
if(OrderType()==OP_BUY)
{
if(bid-op==op-tl)
if (OrderClose(OrderTicket(),NormalizeDouble (0.5*lot,2),Bid,100,White)
)
continue;
for(i = 0; i < OrdersTotal(); i++)
{if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
{
if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=MAGICMA)continue;// 不是要找的定单
if(OrderModify(OrderTicket(),0.5*lot,OrderClosePrice() ,OrderTakeProfit(),Orchid)==true)
continue;;
if(OrderType()==OP_BUY)
{if(OrderOpenPrice()<ma2-L_ma1||ma2>ma1)
{if(OrderModify(OrderTicket(),0.5*lot,ma2-L_ma1*Vpoint ,OrderTakeProfit(),Orchid)==true)
continue;
}
}
}
}
return;
}
}
for(int i=0;i<OrdersTotal();i++)
{ if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
//--- check order type
double tl=OrderStopLoss();//选出的定单的止损点位
double op=OrderOpenPrice();//选出的订单的开盘价位
double lot=OrderLots();//选出的手数
if(OrderType()==OP_SELL)
{
if(bid-op==op-tl)
if (OrderClose(OrderTicket(),NormalizeDouble (0.5*lot,2),Bid,100,Red)
)
continue;
for(i = 0; i < OrdersTotal(); i++)
{if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
{
if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=MAGICMA)continue;// 不是要找的定单
if(OrderModify(OrderTicket(),0.5*lot,OrderClosePrice() ,OrderTakeProfit(),Orchid)==true)
continue;;
if(OrderType()==OP_SELL)
{if(OrderOpenPrice()>ma2+L_ma1||ma2<ma1)
{if(OrderModify(OrderTicket(),0.5*lot,ma2-L_ma1*Vpoint ,OrderTakeProfit(),Orchid)==true)
continue;
}
}
}
}
return;
}
}
}
//+-----------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//--- check for history and trading
if(Bars<200 || IsTradeAllowed()==false)
return;
//--- calculate open orders by current symbol
if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
else CheckForClose();
}
int Fun_Error(int Error) // 处理运行错误的函数
{
switch(Error)
{ // 非致命错误
case 4: Alert("Trade server is busy. Trying once again..");
Sleep(3000); // 简单处理
return(1); // 退出函数
case 135:Alert("Price changed. Trying once again..");
RefreshRates(); // 更新数据
return(1); // 退出函数
case 136:Alert("No prices. Waiting for a new tick..");
while(RefreshRates()==false) // 等待新报价
Sleep(1); // 暂停
return(1); // 退出函数
case 137:Alert("Broker is busy. Trying once again..");
Sleep(3000); // 简单处理Simple solution
return(1); // 退出函数
case 146:Alert("Trading subsystem is busy. Trying once again..");
Sleep(500); // 简单处理
return(1); // 退出函数
// Critical errors
case 2: Alert("Common error.");
return(0); // 退出函数
case 133:Alert("Trading forbidden.");
return(0); // 退出函数
case 134:Alert("Not enough money to execute operation.");
return(0); // 退出函数
default: Alert("Error occurred: ",Error); // 其他错误
return(0); // 退出函数
}
}
//+------------------------------------------------------------------+
- 哪位高人可以把这个EA的每一句代表什么意思做个翻译!你会有意想不到的收获!
- 关于MT4自带的EA--Moving Average问题
- EA不会在多对上运行,需要帮助