# 布林线交易总出问题

13

ball.mq4 4 kb
143

//| MACD Sample.mq4 |
//| Copyright ?2005, MetaQuotes Software Corp. |
//| http://www.metaquotes.net/ |
//+------------------------------------------------------------------+

extern double TakeProfit = 500;
extern double Lots = 0.1;
extern double TrailingStop = 300;
double B1,B2,B3,IB1,IB2;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{
int cnt, ticket, total;

if(Bars<100)
{Print("bars less than 100");
return(0);}
if(TakeProfit<100)
{Print("TakeProfit less than 100");
return(0);}
B1=iMA(NULL,0,20,0,MODE_SMA,PRICE_CLOSE,1);
B2=iMA(NULL,0,20,0,MODE_SMA,PRICE_CLOSE,5);
B3=iMA(NULL,0,20,0,MODE_SMA,PRICE_CLOSE,2);
IB1=iBands(NULL,0,20,2,0,PRICE_CLOSE,MODE_UPPER,0);
IB2=iBands(NULL,0,20,2,0,PRICE_CLOSE,MODE_LOWER,0);
total=OrdersTotal();//orderstotal为订单总数函数
if(total<1)
{
if(AccountFreeMargin()<(1000*Lots))
{
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0);
}
//当前20个周期的平均値大于前一个20个周期的平均值//当前20个周期的平均値大于向前第5个20个周期的平均值//以上判断现在金价是上升趋势//当前买价跌破20个周期（5分钟）的平均值；
if(B1<B2&&B1<B3&&Bid>B1)
{ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,7,Bid+300*Point,0,"",16348,0,Blue);return(0);}
return(0);
}
//下面是平仓条件

for(cnt=0;cnt<total;cnt++)
{
if(OrderType()<=OP_SELL && // check for opened position
OrderSymbol()==Symbol()) // check for symbol
{
if(OrderType()==OP_BUY) // long position is opened
{
// should it be closed?
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
return(0); // exit
}
// check for trailing stop
if(TrailingStop>0)
{
if(Bid-OrderOpenPrice()>Point*TrailingStop)
{
if(OrderStopLoss()<Bid-Point*TrailingStop)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
return(0);
}
}
}
}
else // go to short position
{
// should it be closed?
if(Bid<IB2)
{
return(0); // exit
}
// check for trailing stop
if(TrailingStop>0)
{
{
{
return(0);
}
}
}
}
}
}
return(0);
}
// the end.
143

143