如题,我把原5分钟动量系统做了些修改。做EA测试的时候是没有问题的(如下图),但这两天实战发现EA只会平半仓,另外半仓不会移动到盈亏平衡点或移动止损,求各位朋友 帮帮修改下。
EA测试结果
EA源代码
[C++] 纯文本查看 复制代码//+------------------------------------------------------------------+
//| 1234567890.mq4 |
//+------------------------------------------------------------------+
extern int _MagicNumber = 1123;
extern double MAPeriod=20; //指数均线周期
extern double StopLossSpred = 100; //初始止损离均线的点数
extern double CloseSpred = 150; //后半仓止损离均线的点数
extern double Lots=0.2; //持仓,必须为偶数
extern bool 是否开启多单交易=true;
extern bool 是否开启空单交易=true;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{
double MacdCurrent, MacdPrevious, Ma;
int cnt, ticket, total;
if(Bars0)
{
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==_MagicNumber)
{
total++;
}
}
}
}
//开单检查
if(total0 && MacdPreviousMa && Ask0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("开多单成功 : ",OrderOpenPrice());
}
else Print("开多单发生错误 : ",GetLastError());
return(0);
}
// 检查开空单的可能性
if(MacdCurrent0 && Bid=Ma-200*Point && 是否开启空单交易==true) //MACD首绿且汇价位于均线下20点内做空
{
ticket=OrderSend(Symbol(),OP_SELLSTOP,Lots,Ma-100*Point,3,Ma+StopLossSpred*Point,0,"lbs-sell", _MagicNumber,TimeCurrent()+Period()*1440,Green);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("开空单成功 : ",OrderOpenPrice());
}
else Print("开空单发生错误 : ",GetLastError());
return(0);
}
return(0);
}
//减仓及平仓检查
for(cnt=0;cnt=OrderOpenPrice()-OrderStopLoss() && OrderOpenPrice()>OrderStopLoss() && OrderLots()==Lots)
{
ticket=OrderClose(OrderTicket(),Lots/2,Bid,3,Violet); // 平掉一半仓位
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("多单减仓成功 : ",OrderOpenPrice());
}
else Print("多单减仓发生错误 : ",GetLastError());
return(0);
}
//修改后半仓位的止损价到盈亏平衡点
if(Ask-OrderOpenPrice()>OrderOpenPrice()-OrderStopLoss() && OrderOpenPrice()>OrderStopLoss() && OrderLots()==Lots/2)
{
ticket=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Red);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("多单盈亏平衡点修改成功 : ",OrderOpenPrice());
}
else Print("多单盈亏平衡点修改发生错误 : ",GetLastError());
return(0);
}
//根据行情发展修改剩余仓位的止损价到均线下15点
if(Ma-CloseSpred*Point>OrderStopLoss() && OrderOpenPrice()0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("多单止损修改成功 : ",OrderOpenPrice());
}
else Print("多单止损修改发生错误 : ",GetLastError());
return(0);
}
}
else // 如果有做空单存在
{
//空单减仓检查
if(OrderOpenPrice()-Bid>OrderStopLoss()-OrderOpenPrice() && OrderOpenPrice()0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("空单减仓成功 : ",OrderOpenPrice());
}
else Print("空单减仓发生错误 : ",GetLastError());
return(0);
}
//修改后半仓位的止损价到盈亏平衡点
if(OrderOpenPrice()-Bid>OrderStopLoss()-OrderOpenPrice() && OrderOpenPrice()0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("空单盈亏平衡点修改成功 : ",OrderOpenPrice());
}
else Print("空单盈亏平衡点修改发生错误 : ",GetLastError());
return(0);
}
//根据行情发展修改剩余仓位的止损价到均线下15点
if(Ma+CloseSpred*Point=OrderStopLoss() && OrderLots()==Lots/2)
{
ticket=OrderModify(OrderTicket(),OrderOpenPrice(),Ma+CloseSpred*Point,OrderTakeProfit(),0,Green);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("空单止损修改成功 : ",OrderOpenPrice());
}
else Print("空单止损修改发生错误 : ",GetLastError());
return(0);
}
}
}
}
return(0);
}
// the end.
附上EA
|