EA模版分享 解决重复下单,开单不能下止损单等...
选择字号: 超大 标准 发布时间:2022-04-26 15:16 | 作者:外汇EA
//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);
}
【下载必读】如何下载本站资源:
*本站资源全部免费下载,请加QQ群:1128719745,验证消息【EAMT】,通过后在【群文件】搜索名称下载!群内禁发广告,每个QQ仅1次申请资格,请珍惜!
*群内资源采用最新的压缩方式,双击直接解压,解压缩时请关闭杀毒软件或允许。无毒请放心使用!