/*[[
	Name := BB Squeeze
	Author := Kamen Nyagolov
	Lots := 0.01
	Stop Loss := 23
	Take Profit := 0
	Trailing Stop := 45
]]*/

Variable: BBUpCurr(0), BBDownCurr(0), BBMiddleCurr(0), BBWidthCurr(0), 
          BBUpPrev(0), BBDownPrev(0), BBMiddlePrev(0), BBWidthPrev(0),
          BBUpPrev2(0), BBDownPrev2(0), BBMiddlePrev2(0), BBWidthPrev2(0);
Variable: cnt(0), mode(0), ArrowFlag(0), BBWAlert(0);
Define: Percent(3);
Define: ExitPercent(15);
Define: FirstExitPercent(5);
Define: SecExitPercent(10);
Define: Alarm(0);

BBUpCurr = iBands(20,2,MODE_HIGH,0);
BBDownCurr = iBands(20,2,MODE_LOW,0);
BBMiddleCurr = iBands(20,2,MODE_MAIN,0);
BBUpPrev = iBands(20,2,MODE_HIGH,1);
BBDownPrev = iBands(20,2,MODE_LOW,1);
BBMiddlePrev = iBands(20,2,MODE_MAIN,1);
BBUpPrev2 = iBands(20,2,MODE_HIGH,2);
BBDownPrev2 = iBands(20,2,MODE_LOW,2);
BBMiddlePrev2 = iBands(20,2,MODE_MAIN,2);
BBWidthCurr = ((BBUpCurr - BBDownCurr) / BBMiddleCurr) * 1000;
BBWidthPrev = ((BBUpPrev - BBDownPrev) / BBMiddlePrev) * 1000;
BBWidthPrev2 = ((BBUpPrev2 - BBDownPrev2) / BBMiddlePrev2) * 1000;

//If Bars > 4000 then Exit;
If TotalTrades = 0 then
  {
   ArrowFlag=0;
   BBWAlert=0;
  };

If BBWidthCurr > 2.6 and BBWidthCurr < 3.3 and BBWidthCurr > BBWAlert and Hour >= 8 and Alarm = 1 then 
  { 
   Alert("BBWidth is ",BBWidthCurr, ", Price is ",Bid,"/",Ask);
   BBWAlert = BBWidthCurr;
  };

If Hour >=8 and Hour < 19 and TotalTrades = 0 and BBWidthPrev < Percent and BBWidthCurr >= Percent 
            and (CurTime - LastTradeTime) >1800 then //and BBWidthCurr > BBWidthPrev
 {
  If Ask>=iMA(20,MODE_SMA,0) then //and Ask >= High[1] then
  {
   SetOrder(OP_BUY,0.01,Ask,3,Ask-StopLoss*Point,0,Green);
  };
  If Bid<=iMA(20,MODE_SMA,0) then //and Bid <= Low[1] then
  {
   SetOrder(OP_SELL,0.01,Bid,3,Bid+StopLoss*Point,0,Red);
  };
  Exit;
 };
 
for cnt=1 to TotalTrades
{

mode=Ord(cnt,VAL_TYPE);

If mode=OP_SELL and Ord(cnt,VAL_SYMBOL)=Symbol and (Ord(cnt,VAL_STOPLOSS)-Bid) > TrailingStop*Point then
  {
	ModifyOrder(Ord(cnt,VAL_TICKET),Ord(cnt,VAL_OPENPRICE),(Bid+TrailingStop*Point),Ord(cnt,VAL_TAKEPROFIT),Red);
	Exit;
  };

If mode=OP_BUY and Ord(cnt,VAL_SYMBOL)=Symbol and (Ask-Ord(cnt,VAL_STOPLOSS)) > TrailingStop*Point then
  {
	ModifyOrder(Ord(cnt,VAL_TICKET),Ord(cnt,VAL_OPENPRICE),(Ask-TrailingStop*Point),Ord(cnt,VAL_TAKEPROFIT),Green);
	Exit; 
  };
};


For cnt = 1 to TotalTrades Begin
  If OrderValue(cnt,VAL_TYPE) = OP_BUY and BBWidthCurr >= ExitPercent then
   {
    CloseOrder(OrderValue(cnt,VAL_TICKET),OrderValue(cnt,VAL_LOTS),Bid,3,Yellow);
   }
  If OrderValue(cnt,VAL_TYPE) = OP_SELL and BBWidthCurr >= ExitPercent then
   {
    CloseOrder(OrderValue(cnt,VAL_TICKET),OrderValue(cnt,VAL_LOTS),Ask,3,Yellow);
   };
End;


For cnt = 1 to TotalTrades Begin
  If OrderValue(cnt,VAL_TYPE) = OP_BUY and BBWidthCurr >= FirstExitPercent and ArrowFlag=0 then
   {
	Print("FirstExitPercent is ", FirstExitPercent, ", Time is ",  TimeToStr(CurTime), ", BidPrice is", Bid);
    SetArrow(CurTime,Bid+4*Point,231,Orange);
    SetArrow(CurTime,OrderValue(cnt,VAL_OPENPRICE)-7*Point,231,Red);
    ArrowFlag=1;
   }
  If OrderValue(cnt,VAL_TYPE) = OP_SELL and BBWidthCurr >= FirstExitPercent and ArrowFlag=0 then
   {
	Print("FirstExitPercent is ", FirstExitPercent, ", Time is ",  TimeToStr(CurTime), ", AskPrice is", Ask);
    SetArrow(CurTime,Ask+4*Point,231,Orange);
    SetArrow(CurTime,OrderValue(cnt,VAL_OPENPRICE)+13*Point,231,Red);
    ArrowFlag=1;
   };
End;

For cnt = 1 to TotalTrades Begin
  If OrderValue(cnt,VAL_TYPE) = OP_BUY and BBWidthCurr >= SecExitPercent and ArrowFlag=1 then
   {
	Print("SecExitPercent is ", SecExitPercent, ", Time is ",  TimeToStr(CurTime), ", BidPrice is", Bid);
    SetArrow(CurTime,Bid,231,Orange);
    SetArrow(CurTime,Bid-25*Point,231,Red);
    ArrowFlag=2;
   }
  If OrderValue(cnt,VAL_TYPE) = OP_SELL and BBWidthCurr >= SecExitPercent and ArrowFlag=1 then
   {
	Print("SecExitPercent is ", SecExitPercent, ", Time is ",  TimeToStr(CurTime), ", AskPrice is", Ask);
    SetArrow(CurTime,Ask,231,Orange);
    SetArrow(CurTime,Ask+25*Point,231,Red);
    ArrowFlag=2;
   };
End;
