
extern int MAGICMA = 64389222;
extern double LotsToBuySell = 0.05;
extern bool                UseTrail                      = true;
extern double                 TrailingLevel                 = 2;
extern double                 TrailingStep                  = 2;
input double Stoploss = 11;

bool b1a1, b1a2, b1b1, b1b2, b1c1, b1c2, b3a1, b3a2, b3b1, b3b2, b3c1, bextra1B, bextra2B, bextra1S, bextra2S, bV42b, bV42s;
bool bBBHMA1, bBBHMA2;
double dRatioArnyGy;
double val1, val2;
double D1, D2, D30_1_2,D1_2;
bool bBought, bSold;
double d91_92b,d91_92s,d651_652b,d651_652s;

int totalNrOfOrders;

double PrevEquity, rsi1, rsi2;
double TakeWinPrice;
int SwingStopLoss;
double PointValue;
double BaseRate;
int stopbuys;
int stopsells;
int buys;
int sells;
int BarCount;
int res;
int i, Longticket, Shortticket;
bool NewBar;
int pointx=1;
int StopLevel;

//+------------------------------------------------------------------+
//| Init function --- Initializing                                   |
//+------------------------------------------------------------------+
void init()
    {

    PointValue = .0001;
    if(MarketInfo(NULL,MODE_DIGITS)==3 || MarketInfo(NULL,MODE_DIGITS)==5) pointx=10;
    if(Symbol() == "AUS200") PointValue = 1;
    if(Symbol() == "DE30") PointValue = 1;
    if(Symbol() == "SPX200") PointValue = .1;
    if(StringFind(Symbol(),"JPY") > 0) PointValue = .01;
    if(StringFind(Symbol(),"XAU") > 0) PointValue = .1;
    if(StringFind(Symbol(),"GOLD") > 0) PointValue = .1;
    if(StringFind(Symbol(),"XAG") > 0) PointValue = .01;
    if(StringFind(Symbol(),"SILVER") > 0) PointValue = .01;
    }
    
    
    
//+------------------------------------------------------------------+
//| Start function                                                   |
//+------------------------------------------------------------------+
void start()
    {


//
for(int j=OrdersTotal()-1; j>=0; j--) {
      if(OrderSelect(j,SELECT_BY_POS)) {
         if(OrderMagicNumber()==MAGICMA && OrderSymbol()==Symbol()) {
            if(UseTrail) {
               if(OrderType()==0) {
                  if(OrderStopLoss()<OrderOpenPrice() && Bid>=OrderOpenPrice()+TrailingLevel*PointValue) 
                     bool res1=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0);
               } else if(OrderType()==1) {
                  if((OrderStopLoss()==0 || OrderStopLoss()>OrderOpenPrice()) && Ask<=OrderOpenPrice()-TrailingLevel*PointValue) 
                     bool res2=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0);
               }               
               TrailingPositions(Symbol(),true,(TrailingLevel+TrailingStep)*pointx,TrailingStep*pointx);
            }
         }
      }
   }

         
      int    vspread = (int)MarketInfo(Symbol(),MODE_SPREAD);  
      CalculateCurrentOrders();
      CheckNewBar();
      if (NewBar == true && vspread<=15)
         { 

           totalNrOfOrders=OrdersTotal();
           
                  // ##########################################          BUY         ##########################################                   
                  
                    //1a1 
                    if(iRSI(NULL, PERIOD_CURRENT, 7, PRICE_CLOSE, 2) < 30 && iRSI(NULL, PERIOD_CURRENT, 7, PRICE_CLOSE, 1) > 30)
                           {  
                           TrailingStep=2; TrailingLevel=2;
                            double priceb=Ask;
                            double slb=0;double tpp = 50;
                           if(Stoploss>0) slb=priceb-Stoploss*PointValue;
                              Longticket=OrderSend(Symbol(),OP_BUY,LotsToBuySell,priceb,30,slb,0,"Buy order placed",MAGICMA,0,Green);
                           }

                // ##########################################          SELL         ########################################## 
                      if(iRSI(NULL, PERIOD_CURRENT, 7, PRICE_CLOSE, 2) > 70 && iRSI(NULL, PERIOD_CURRENT, 7, PRICE_CLOSE, 1) < 70)
                              {  
                              TrailingStep=2; TrailingLevel=2;
                            double prices=Bid;
                            double sls=0;
                           if(Stoploss>0) sls=prices+Stoploss*PointValue;
                           Shortticket=OrderSend(Symbol(),OP_SELL,LotsToBuySell,prices,30,sls,0,"Sell order placed",MAGICMA,0,Red); 
                                
                              }

               DeleteStopOrders();
          
    }
}


//+------------------------------------------------------------------+
//| Check for new bar                                  |
//+------------------------------------------------------------------+
void CheckNewBar()                              
  {      
                                           
   static datetime NewTime=0;                  
   NewBar=false;                               
   if(NewTime!=Time[0])                        
     {
      NewTime=Time[0];                         
      NewBar=true;    
      
                               
     }
  }



//+------------------------------------------------------------------+
//| Close Positions   --- MODIFY!!! -> if dir changed!!!             |
//+------------------------------------------------------------------+
void ClosePositions()
    {
       for(i=0;i<OrdersTotal();i++)
       {
       if (OrderType()==OP_BUY && OrderMagicNumber()==MAGICMA)
          {
          res = OrderClose(OrderTicket(),OrderLots(),Bid,1,clrMaroon);
          if (res > -1) return;
          }
       if (OrderType()==OP_SELL && OrderMagicNumber()==MAGICMA)
          {
          res = OrderClose(OrderTicket(),OrderLots(),Ask,1,clrMaroon);
          if (res > -1) return;
          }
       }
    }

    
        
//+------------------------------------------------------------------+
//| Calculate open positions                                         |
//+------------------------------------------------------------------+
void CalculateCurrentOrders()
    {
    stopbuys = 0;
    stopsells = 0;
    buys = 0;
    sells = 0;
    for(i=0;i<OrdersTotal();i++)
       {
       if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
       if (OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
          {
          if(OrderType()==OP_BUYSTOP) stopbuys++;
          if(OrderType()==OP_SELLSTOP) stopsells++;
          if(OrderType()==OP_BUY) buys++;
          if(OrderType()==OP_SELL) sells++;
          }
       }
    }
    
 
//+------------------------------------------------------------------+
//| Delete Stop Orders                               |
//+------------------------------------------------------------------+
void DeleteStopOrders()
    {
       for(i=0;i<OrdersTotal();i++)
       {
       if (OrderType()==OP_BUYSTOP && OrderMagicNumber()==MAGICMA)
          {
          res = OrderDelete(OrderTicket());
          if (res > -1) return;
          }
       if (OrderType()==OP_SELLSTOP  && OrderMagicNumber()==MAGICMA)
          {
          res = OrderDelete(OrderTicket());
          if (res > -1) return;
          }
       }
    }
    

        
    
    
double TrailingPositions(string sym, bool ProfitTrail, int Level, int Step, bool hidden=false) {
   double pBid, pAsk, pp;
   //----
   pp=MarketInfo(sym, MODE_POINT);
   if (OrderType()==OP_BUY) {
      pBid=MarketInfo(sym, MODE_BID);
      if (!ProfitTrail || (pBid-OrderOpenPrice())>Level*pp) {
         if(pBid-OrderStopLoss()>Level*pp) {
            if(!hidden) bool fmb=OrderModify(OrderTicket(),OrderOpenPrice(),pBid-(Level-Step)*pp,OrderTakeProfit(),0,White);
            return pBid-(Level-Step)*pp;
         }
      }
   }
   if (OrderType()==OP_SELL) {
      pAsk=MarketInfo(sym, MODE_ASK);
      if (!ProfitTrail || OrderOpenPrice()-pAsk>Level*pp) {
         if(OrderStopLoss()-pAsk>Level*pp || OrderStopLoss()==0) {
            if(!hidden) bool fms=OrderModify(OrderTicket(),OrderOpenPrice(),pAsk+(Level-Step)*pp,OrderTakeProfit(),0,White);
            return pAsk+(Level-Step)*pp;
         }
      }
   }
   return 0;
}

//+------------------------------------------------------------------+
   