MQL4:双均线交易系统

一、交易策略

入场原则:

做多入场:30分钟图上10周期EMA向上交叉20周期EMA 做空入场:30分钟图上10周期EMA向下交叉20周期EMA

出场原则:

做多出场:30分钟图上10周期EMA向下交叉20周期EMA 做空出场:30分钟图上10周期EMA向上交叉20周期EMA![]

二、EA源码

//+------------------------------------------------------------------+
//|                                                 myEA20180104.mq4 |
//|                                             Copyright 2017,CuiYi |
//|                                                1010333562@qq.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017,CuiYi"
#property link      "1010333562@qq.com"
#property version   "1.00"
#property strict
#define MAGICMA 19880305

extern double Lots=1;

int CalculateCurrentOrder(string symbol)
	{
	int buys=0,sells=0;
	for(int i=0;i<OrdersTotal();i=i+1)
		{
		if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)	break;
		if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MAGICMA)
			{
			if(OrderType()==OP_BUY) 	buys++;
			if(OrderType()==OP_SELL)	sells++;
			}
		}
	if(buys>0)	return(buys);
	else		return(-sells);
	}

void CheckForOpen()
	{
	double ma1,ma2,ma3,ma4;
	int res;
	
	ma1=iMA(NULL,30,10,6,MODE_EMA,PRICE_CLOSE,0);
	ma2=iMA(NULL,30,20,6,MODE_EMA,PRICE_CLOSE,0);
	ma3=iMA(NULL,30,10,6,MODE_EMA,PRICE_CLOSE,1);
	ma4=iMA(NULL,30,20,6,MODE_EMA,PRICE_CLOSE,1);
	
	if(ma1>ma2 && ma3<ma4)
		{
		res=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"",MAGICMA,0,Blue);
		return;
		}
	
	if(ma1<ma2 && ma3>ma4)
		{
		res=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0,"",MAGICMA,0,Red);
		return;
		}
	}

void CheckForClose()
	{
	double ma1,ma2,ma3,ma4;
		
	ma1=iMA(NULL,30,10,6,MODE_EMA,PRICE_CLOSE,0);
	ma2=iMA(NULL,30,20,6,MODE_EMA,PRICE_CLOSE,0);
	ma3=iMA(NULL,30,10,6,MODE_EMA,PRICE_CLOSE,1);
	ma4=iMA(NULL,30,20,6,MODE_EMA,PRICE_CLOSE,1);
		
	for(int i=0;i<OrdersTotal();i++)
		{
		if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
		if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
		
		if(OrderType()==OP_BUY)
			{
			if(ma1<ma2 && ma3>ma4)	
			OrderClose(OrderTicket(),Lots,Bid,3,White);
			break;
			}
			
		if(OrderType()==OP_SELL)
			{
			if(ma1>ma2 && ma3<ma4)	
			OrderClose(OrderTicket(),Lots,Bid,3,White);
			break;
			}
		}
	}	

void start()
	{
	if(Bars<100 || IsTradeAllowed()==false) return;
	if(CalculateCurrentOrder(Symbol())==0) CheckForOpen();
	else CheckForClose();
	}

三、系统回测

1.EUR/USD

回测时间:2017/01/01 - 2018/01/01

时间周期:M30

净值图:

输入图片说明

回测报告:

输入图片说明

20180204更新:

//+------------------------------------------------------------------+
//|                                            5&15min双均线交易.mq4 |
//|                                             Copyright 2018,CuiYi |
//|                                                1010333562@qq.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018,CuiYi"
#property link      "1010333562@qq.com"
#property version   "1.00"
#property strict
#define MAGICMA 19880305

extern double Lots=0.08;

int CalculateCurrentOrder(string symbol)
	{
	int buys=0,sells=0;
	for(int i=0;i<OrdersTotal();i=i+1)
		{
		if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)	break;
		if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
			{
			if(OrderType()==OP_BUY) 	buys++;
			if(OrderType()==OP_SELL)	sells++;
			}
		}
	if(buys>0)	return(buys);
	else		return(-sells);
	}

void CheckForOpen()
	{
	double ma1,ma2,ma3,ma4;
	int res;
	
	ma1=iMA(NULL,30,9,1,MODE_EMA,PRICE_CLOSE,1);
	ma2=iMA(NULL,30,18,1,MODE_EMA,PRICE_CLOSE,1);
	ma3=iMA(NULL,15,9,1,MODE_EMA,PRICE_CLOSE,1);
	ma4=iMA(NULL,15,18,1,MODE_EMA,PRICE_CLOSE,1);
	
	
	if(ma1>ma2 && ma3>ma4)
		{
		res=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"",MAGICMA,0,Blue);
		return;
		}
	
	if(ma1<ma2 && ma3<ma4)
		{
		res=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0,"",MAGICMA,0,Red);
		return;
		}
	}

void CheckForClose()
	{
	double ma1,ma2,ma3,ma4;
		
	ma1=iMA(NULL,30,9,1,MODE_EMA,PRICE_CLOSE,1);
	ma2=iMA(NULL,30,18,1,MODE_EMA,PRICE_CLOSE,1);
	ma3=iMA(NULL,15,9,1,MODE_EMA,PRICE_CLOSE,1);
	ma4=iMA(NULL,15,18,1,MODE_EMA,PRICE_CLOSE,1);
		
	for(int i=0;i<OrdersTotal();i++)
		{
		if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)	break;
		if(OrderMagicNumber()==MAGICMA || OrderSymbol()==Symbol())
			{
			if(OrderType()==OP_BUY)
				{
				if(ma3<ma4)	
					{
					if(!OrderClose(OrderTicket(),Lots,Bid,3,White))
						Print("OrderClose error ",GetLastError());
					}
				break;
				}
			
			if(OrderType()==OP_SELL)
				{
				if(ma3>ma4)
					{
					if(OrderClose(OrderTicket(),Lots,Ask,3,White))
						Print("OrderClose error ",GetLastError());
					}
				break;
				}
			}
		}
	}

void start()
	{
	if(Bars<100 || IsTradeAllowed()==false) return;
	if(CalculateCurrentOrder(Symbol())==0) CheckForOpen();
	else CheckForClose();
	}

20180205更新:

//+------------------------------------------------------------------+
//|                                                15min均线交易.mq4 |
//|                                             Copyright 2018,CuiYi |
//|                                                1010333562@qq.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018,CuiYi"
#property link      "1010333562@qq.com"
#property version   "1.00"
#property strict
#define MAGICMA 19880305

extern double Lots=0.08;

int CalculateCurrentOrder(string symbol)
	{
	int buys=0,sells=0;
	for(int i=0;i<OrdersTotal();i=i+1)
		{
		if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)	break;
		if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
			{
			if(OrderType()==OP_BUY) 	buys++;
			if(OrderType()==OP_SELL)	sells++;
			}
		}
	if(buys>0)	return(buys);
	else		return(-sells);
	}

void CheckForOpen()
	{
	double ma1,ma2;
	int res;
	
	ma1=iMA(NULL,15,9,1,MODE_EMA,PRICE_CLOSE,1);
	ma2=iMA(NULL,15,18,1,MODE_EMA,PRICE_CLOSE,1);
	
	if(ma1>ma2)
		{
		res=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"",MAGICMA,0,Blue);
		return;
		}
	
	if(ma1<ma2)
		{
		res=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0,"",MAGICMA,0,Red);
		return;
		}
	}

void CheckForClose()
	{
	double ma3,ma4;
		
	ma3=iMA(NULL,15,9,1,MODE_EMA,PRICE_CLOSE,1);
	ma4=iMA(NULL,15,18,1,MODE_EMA,PRICE_CLOSE,1);
		
	for(int i=0;i<OrdersTotal();i++)
		{
		if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)	break;
		if(OrderMagicNumber()==MAGICMA || OrderSymbol()==Symbol())
			{
			if(OrderType()==OP_BUY)
				{
				if(ma3<ma4)	
					{
					if(!OrderClose(OrderTicket(),Lots,Bid,3,White))
						Print("OrderClose error ",GetLastError());
					}
				break;
				}
			
			if(OrderType()==OP_SELL)
				{
				if(ma3>ma4)
					{
					if(OrderClose(OrderTicket(),Lots,Ask,3,White))
						Print("OrderClose error ",GetLastError());
					}
				break;
				}
			}
		}
	}

void start()
	{
	if(Bars<100 || IsTradeAllowed()==false) return;
	if(CalculateCurrentOrder(Symbol())==0) CheckForOpen();
	else CheckForClose();
	}

转载于:https://my.oschina.net/u/3093769/blog/1602346

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值