//+------------------------------------------------------------------+
//|                                                 kolbas by Maloma |
//+------------------------------------------------------------------+

#include <stdlib.mqh>
#include <stderror.mqh>

extern double Lots=0.1;
extern int    TralUp=0;
extern int    EnterFiltr=5;

       int    StopLev;
       int    Tral;
       double MA, MAP;
       double Hich, Loch;
       int    i, CurTot, StopTot;      
        
int OpenOrders()
{
  OrderSend(Symbol(),OP_BUYSTOP,Lots,Ask+EnterFiltr*Point,3,Bid-EnterFiltr*Point,0,NULL,159,0,CLR_NONE);
  OrderSend(Symbol(),OP_SELLSTOP,Lots,Bid-EnterFiltr*Point,3,Ask+EnterFiltr*Point,0,NULL,159,0,CLR_NONE);
//  OrderSend(Symbol(),OP_SELLLIMIT,Lots,Bid+EnterFiltr*Point,3,Ask+2*EnterFiltr*Point,0,NULL,159,0,CLR_NONE);
//  OrderSend(Symbol(),OP_BUYLIMIT,Lots,Ask-EnterFiltr*Point,3,Bid-2*EnterFiltr*Point,0,NULL,159,0,CLR_NONE);
  return(0);
}

int start()
{ 
  StopLev=MarketInfo(Symbol(),MODE_STOPLEVEL);
  Tral=StopLev+TralUp;
  CurTot=0;
  StopTot=0;      
  for (i=0;i<OrdersTotal();i++)
    {
     OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
     if ((Symbol()==OrderSymbol())&&(OrderMagicNumber()==159)&&((OrderType()==OP_BUY)||(OrderType()==OP_SELL)))
       {
        CurTot++;
        if (OrderType()==OP_BUY)
          {
//           if ((OrderOpenPrice()+Tral*Point)<Bid)
             {
              if (((OrderStopLoss()+(Tral+EnterFiltr)*Point)<Bid)||(OrderStopLoss()==0)) {OrderModify(OrderTicket(),OrderOpenPrice(),Bid-(Tral+EnterFiltr)*Point,Bid+Tral*Point,OrderExpiration(),CLR_NONE);}
             }
          }
        if (OrderType()==OP_SELL)
          {
//           if (Ask<(OrderOpenPrice()-Tral*Point))
             {
              if ((Ask<(OrderStopLoss()-(Tral+EnterFiltr)*Point)||(OrderStopLoss()==0))) {OrderModify(OrderTicket(),OrderOpenPrice(),Ask+(Tral+EnterFiltr)*Point,Ask-Tral*Point,OrderExpiration(),CLR_NONE);}
             }
          }
       }
     if ((Symbol()==OrderSymbol())&&(OrderMagicNumber()==159)&&(OrderType()>1)) {StopTot++;}
    }
  for (i=0;i<OrdersTotal();i++)
    {
     OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
     if ((CurTot>0)&&(Symbol()==OrderSymbol())&&(OrderMagicNumber()==159)&&(OrderType()>1)) {OrderDelete(OrderTicket());}
    }  
  if ((CurTot==0)&&(StopTot==0)) {OpenOrders();}
  return(0);
}