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

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

extern double Lots=0.1;
extern int    TralUp=11;
extern int    EnterFiltr=6;
extern int    InHistory=5;
extern double SL=45;
       int    StopLev;
       int    Tral;
       double MA, MAP;
       double Hich, Loch;
       int    i, CurTot, StopTot;      
        
int OpenOrders()
{
  Hich=High[Highest(Symbol(),NULL,MODE_HIGH,InHistory,0)]+(EnterFiltr+MarketInfo(Symbol(),MODE_SPREAD))*Point;
  Loch=Low[Lowest(Symbol(),NULL,MODE_LOW,InHistory,0)]-EnterFiltr*Point;
  OrderSend(Symbol(),OP_BUYSTOP,Lots,Hich,3,Hich-SL*Point,0,NULL,753,0,CLR_NONE);
  OrderSend(Symbol(),OP_SELLSTOP,Lots,Loch,3,Loch+SL*Point,0,NULL,753,0,CLR_NONE);
//  OrderSend(Symbol(),OP_SELLLIMIT,Lots,Bid+EnterFiltr*Point,3,Ask+2*EnterFiltr*Point,0,NULL,753,0,CLR_NONE);
//  OrderSend(Symbol(),OP_BUYLIMIT,Lots,Ask-EnterFiltr*Point,3,Bid-2*EnterFiltr*Point,0,NULL,753,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()==753)&&((OrderType()==OP_BUY)||(OrderType()==OP_SELL)))
       {
        CurTot++;
        if (OrderType()==OP_BUY)
          {
           if ((OrderOpenPrice()+Tral*Point)<Bid)
             {
              if ((OrderStopLoss()+Tral*Point)<Bid) {OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Tral*Point,Bid+Tral*Point,OrderExpiration(),CLR_NONE);}
             }
          }
        if (OrderType()==OP_SELL)
          {
           if (Ask<(OrderOpenPrice()-Tral*Point))
             {
              if (Ask<(OrderStopLoss()-Tral*Point)) {OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Tral*Point,Ask-Tral*Point,OrderExpiration(),CLR_NONE);}
             }
          }
       }
     if ((Symbol()==OrderSymbol())&&(OrderMagicNumber()==753)&&(OrderType()>1)) {StopTot++;}
    }
  for (i=0;i<OrdersTotal();i++)
    {
     OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
     if ((CurTot>0)&&(Symbol()==OrderSymbol())&&(OrderMagicNumber()==753)&&(OrderType()>1)) {OrderDelete(OrderTicket());}
    }  
  if ((CurTot==0)&&(StopTot==0)) {OpenOrders();}
  return(0);
}