//+------------------------------------------------------------------+
//| 01.mq4 |
//| Copyright 2021, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
#define MAGICMA 20211220
//--- input parameters
input double Input1=0.01;//交易手数
input int shortMaPeriod=12; //短期均线
input int LongMaPeriod=48;//长期均线
input ENUM_MA_METHOD method = MODE_SMA;//均线方式
//+------------------------------------------------------------------+
//主函数入口:OnTick 函数每一个报价执行一次 |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnTick()
{
//---检查是否有单子
if(IsOrderExist()==false)
CheckForOpen();//没有单子就检查是否能开单
else
CheckForClose();//有单子就检查是否应该平仓
}
//+------------------------------------------------------------------+
//|检查是否有单子,true=有单子,false=没单子
//+------------------------------------------------------------------+
bool IsOrderExist()
{
for(int i=0;i<OrdersTotal();i++)//扫描所有单子
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)break;//挨个选中
if(OrderSymbol()!=Symbol())continue;//货币名称不符,跳过
if(OrderMagicNumber()!=MAGICMA)continue;//魔术号不符,跳过
return(true);//找到单子了,返回true
}
return(false);//没有找到单子,返回false
}
//+------------------------------------------------------------------+
//|判断开仓条件
//+------------------------------------------------------------------+
void CheckForOpen()
{
if(Volume[0]>1)
return;//每次K线收线才开单
double maShort=iMA(NULL,0,shortMaPeriod,0,method,PRICE_CLOSE,0);//短期移动平均线1
double maLong=iMA(NULL,0,LongMaPeriod,0,method,PRICE_CLOSE,0);//长期移动平均线2
//做多条件:价格在两条均线的上方,并且价格向上穿越短期均线
if(Open[1]<maShort && Close[1]>maShort && Close[1]>maLong)
{
if(OrderSend(Symbol(),OP_BUY,Lots(),Ask,3,0,0,"",MAGICMA,0,Blue)<0)
Print("开仓失败:",GetLastError());
}
//做空条件:价格在两条均线的下方,并且价格向下穿越短期均线
if(Open[1]>maShort && Close[1]<maShort && Close[1]<maLong)
{
if(OrderSend(Symbol(),OP_SELL,Lots(),Bid,3,0,0,"",MAGICMA,0,clrRed)<0)
Print("开仓失败:",GetLastError());
}
}
//+------------------------------------------------------------------+
//|判断平仓条件
//+------------------------------------------------------------------+
void CheckForClose()
{
if(Volume[0]>1)
return;//每次K线收线才平仓
double maShort=iMA(NULL,0,shortMaPeriod,0,method,PRICE_CLOSE,0);//短期移动平均线
for(int i=0; i<OrdersTotal(); i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)continue;
if(OrderSymbol()!=Symbol())continue;//货币名称不符,跳过
if(OrderMagicNumber()!=MAGICMA)continue;//魔术号不符,跳过
if(OrderType()==OP_BUY)//多单
{
if(Open[1]>shortMaPeriod && Close[1]<shortMaPeriod)//价格向下穿越短期均线,平多单
{
if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,White))//平仓
Print("平仓失败:",GetLastError());
}
}
if(OrderType()==OP_SELL)//空单
{
if(Open[1]<shortMaPeriod && Close[1]>shortMaPeriod)//价格向上穿越短期均线,平空单
{
(!OrderClose(OrderTicket(),OrderLots(),Ask,3,White))//平仓
Print("平仓失败:",GetLastError());
}
}
}
}
//+------------------------------------------------------------------+
|