//EA 模版功能
//1.同一标识不会重复下单
//2.止损止盈可以设置为0
//3.兼容有些平台 开单不能下止损的问题
//
#property copyright "wangyu"
//定义全局变量
double ema10_15M;
double ema20_15M;
double ema10_30M;
double ema20_30M;
double k;
double d;
double prevK;
double prevD;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
int buyTicket=0;
int sellTicket=0;
getData();
if(needOpenBuy())
{
buyTicket = buy(0.01,250,250,"buy",0);
if(buyTicket>0)
{
Alert("多单下单成功,订单号码是:"+buyTicket);
}else if(buyTicket<0)
{
Alert("多单下单失败");
}
}
if(needOpenSell())
{
sellTicket = sell(0.01,300,200,"sell",0);
if(sellTicket>0)
{
Alert("空单下单成功,订单号码是:"+sellTicket);
}else if(buyTicket<0)
{
Alert("空单下单失败");
}
}
return(0);
}
//+------------------------------------------------------------------+
//get data
void getData()
{
//获取 均线 数据
ema10_15M = iMA(Symbol(),PERIOD_M15,10,0,MODE_EMA,PRICE_CLOSE,0);//最后一根k线
ema20_15M = iMA(Symbol(),PERIOD_M15,20,0,MODE_EMA,PRICE_CLOSE,0);//最后一根k线
ema10_30M = iMA(Symbol(),PERIOD_M30,10,0,MODE_EMA,PRICE_CLOSE,0);//最后一根k线
ema20_30M = iMA(Symbol(),PERIOD_M30,10,0,MODE_EMA,PRICE_CLOSE,0);//最后一根k线
//获取 kdj 数据
k = iStochastic(Symbol(),PERIOD_M15,8,5,5,MODE_SMA,0,MODE_MAIN,0);//最后一根k线 主线 蓝色
d = iStochastic(Symbol(),PERIOD_M15,8,5,5,MODE_SMA,0,MODE_SIGNAL,0);//最后一根k线 线号线 红色
prevK = iStochastic(Symbol(),PERIOD_M15,8,5,5,MODE_SMA,0,MODE_MAIN,1);//上一根k线 主线 蓝色
prevD = iStochastic(Symbol(),PERIOD_M15,8,5,5,MODE_SMA,0,MODE_SIGNAL,1);//上一根k线 线号线 红色
}
// open sell contidion
bool needOpenSell()
{
bool result = FALSE;
if((ema10_15M<ema20_15M)&&(ema10_30M<ema20_30M))
{
if((k<d)&&(prevK>prevD))
{
result = TRUE;
}
}
return (result);
}
// open buy contidion
bool needOpenBuy()
{
bool result = FALSE;
if((ema10_15M>ema20_15M)&&(ema10_30M>ema20_30M))
{
if((k>d)&&(prevK<prevD))
{
result = TRUE;
}
}
return (result);
}
int buy(double lots,int stopLoss,int stopProfit,string comment,int magic)
{
int ticket = 0;
int findBuyNum =0;
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
{
if(OrderComment()==comment)
{
findBuyNum++;
}
}
}
if(findBuyNum==0) //避免同一标识 重复下单
{
ticket = OrderSend(Symbol(),OP_BUY,lots,Ask,30,0,0,comment,magic,0,White); //适应开单不能下止盈止损的平台
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET)==true)
{
int stopLossValue=OrderOpenPrice()-stopLoss*Point;
int stopProfitValue=OrderOpenPrice()+stopProfit*Point;
if(stopLoss==0)
{
stopLossValue=0;
}
if(stopProfit==0)
{
stopProfitValue=0;
}
if(OrderModify(OrderTicket(),OrderOpenPrice(),stopLossValue,stopProfitValue,0,White)==true) //下单成功没问题,修改订单成功后在返回
{
return (ticket);
}
}
}
}
return (0);
}
int sell(double lots,int stopLoss,int stopProfit,string comment,int magic) //magic 暂时没有用到
{
int ticket = 0;
int findSellNum =0;
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
{
if(OrderComment()==comment)
{
findSellNum++;
}
}
}
if(findSellNum==0) //避免同一标识 重复下单
{
ticket=OrderSend(Symbol(),OP_SELL,lots,Bid,30,0,0,comment,magic,0,Red); //适应开单不能下止盈止损的平台
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET)==true)
{
int stopLossValue=OrderOpenPrice()+stopLoss*Point;
int stopProfitValue=OrderOpenPrice()-stopProfit*Point;
if(stopLoss==0)
{
stopLossValue=0;
}
if(stopProfit==0)
{
stopProfitValue=0;
}
if(OrderModify(OrderTicket(),OrderOpenPrice(),stopLossValue,stopProfitValue,0,Red)==true) //下单成功没问题,修改订单成功后在返回
{
return (ticket);
}
}
}
}
return (0);
}
EA模版分享 解决重复下单,开单不能下止损单等...
- · 本网所载的全部内容仅作参考之用,所载资料来源和观点客观可靠,但不担保其准确性或完整性,且内容不构成对任何人的投资建议。
- · 本网所提供的行情数据分析及观点不应取代个人的独立判断,不对因使用本网而造成的损失承担任何责任,据此入市,风险自担!