IG外汇平台MQL4教程:三阴三阳交易信号
以下是示例代码:
int MAGIC=35207814;
extern double lots=1;
//+------------------------------------------------------------------+
//| 下面定义start函数,该函数会在每次价格波动时自动执行。
//+------------------------------------------------------------------+
int start()
{
if(CalculateCurrentOrders(Symbol())==0) //当当前账户订单数为0时,继续执行;若有持仓,则不再重复开单。
{
double StopLoss;
double TakeProft;
if(Signal()==-1)//出现卖出信号
{
StopLoss=Open[3];//将止损设于第三根K线的开盘价
TakeProft=2*(Open[1]-Close[1]+Open[2]-Close[2]+Open[3]-Close[3])/Point;//止盈取前三根K线实体总和的2倍
if(OrderSend(Symbol(),OP_SELL,lots,Bid,3,StopLoss,Bid-TakeProft*Point,0,MAGIC,0,Blue)<0)//若卖出指令失败
Alert("auto sell is failed!---"+GetLastError());//弹出报警,并显示错误代码
else return(0);//成交后返回,不再执行后续代码
}
if(Signal()==1)//出现买入信号
{
StopLoss=Open[3];//止损设在第三根K线的开盘价
TakeProft=2*(Close[1]-Open[1]+Close[2]-Open[2]+Close[3]-Open[3])/Point;//止盈取前三根阳线实体总和的2倍
if(OrderSend(Symbol(),OP_BUY,lots,Ask,3,StopLoss,Ask+TakeProft*Point,0,MAGIC,0,Red)<0)//若买入指令失败
Alert("auto buy is failed!---"+GetLastError());//弹窗提醒并显示错误编号
else return(0);//开单成功则返回
}
}
//----
return(0);
}
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol) //此函数用于统计当前EA挂单且未平仓的订单数量
{
int ors=0;
for(int i=0;i { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break; if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGIC) { if(OrderType()==OP_BUY) ors++; if(OrderType()==OP_SELL) ors++; } } return(ors); } //*************交易信号函数,返回1表示买入,-1表示卖出,0表示无信号 int Signal() { int res; if(Close[1]Open[1] && Close[2]Open[2] && Close[3]Open[3]) //连续三根阳线产生买入信号(此策略适用于IG外汇平台等支持MQL4的环境) res=1; if(Close[1] res=-1; return(res); }