GT_BB_SINWIN

Profit factor:
0.00
7 Views
0 Downloads
0 Favorites
GT_BB_SINWIN
//+------------------------------------------------------------------+
//|                                                 GT BB SINWIN.mq4 |
//|                      Copyright © 2010, MetaQuotes Software Corp. |
//|                             http://www.mql4.com/ru/users/rustein |
//+------------------------------------------------------------------+
#define MAGIC  131313
//-------------------------------------------------------------------+
extern int    StopLoss          = 377;
extern int    Slippage          = 34;
//-------------------------------------------------------------------+
extern double Lots              = 0.01;
extern double MaximumRisk       = 0.2;
extern double DecreaseFactor    = 5.0;
//-------------------------------------------------------------------+
extern bool   Trailing          = true;
extern int    MinProfit         = 377;
extern int    TrailingStop      = 233;
extern int    TrailingStep      = 144;
//-------------------------------------------------------------------+
extern bool   NoLoss            = false;
extern int    MinProfitB        = 233;
extern int    NoLossLevel       = 55;
//+------------------------------------------------------------------+
extern int    Shift             = 0;
//+------------------------------------------------------------------+
extern int    BBPeriod    = 20;
extern int    BBDeviation = 2;
extern int    BBPrice     = 0;  // 0=Close,1=Open,2=High,3=Low,4=Median,5=Typical,6=Weighted
//-------------------------------------------------------------------+
extern int    MA          = 20; //
extern int    MAMode      = 0;  // 0=SMA,1=EMA,2=SSMA,3=LWMA
extern int    MAPrice     = 0;  // 0=Close,1=Open,2=High,3=Low,4=Median,5=Typical,6=Weighted
//+------------------------------------------------------------------+
//| Calculate open positions                                         |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
  {
   int buys=0,sells=0;
//----
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGIC)
        {
         if(OrderType()==OP_BUY)  buys++;
         if(OrderType()==OP_SELL) sells++;
        }
     }
//---- return orders volume
   if(buys>0) return(buys);
   else       return(-sells);
  }
//+------------------------------------------------------------------+
//| Calculate optimal lot size                                       |
//+------------------------------------------------------------------+
double LotsOptimized()
  {
   double lot=Lots;
   int    orders=HistoryTotal();     // history orders total
   int    losses=0;                  // number of losses orders without a break
//---- select lot size
   lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,2);
//---- calcuulate number of losses orders without a break
   if(DecreaseFactor>0)
     {
      for(int i=orders-1;i>=0;i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print("Error in history!"); break; }
         if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue;
         //----
         if(OrderProfit()>0) break;
         if(OrderProfit()<0) losses++;
        }
      if(losses>1) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,2);
     }
//---- return lot size
   if(lot<0.1) lot=0.1;
   return(lot);
  }
//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+
void CheckForOpen()
  {
   double ma;
   int    res;
//---- go trading only for first tiks of new bar
   if(Volume[0]>1) return;
//--------------------------------------------------------------------+
      double BBup = iBands(NULL,0,BBPeriod,BBDeviation,0,BBPrice, MODE_UPPER,Shift+1);
      double BBlo = iBands(NULL,0,BBPeriod,BBDeviation,0,BBPrice, MODE_LOWER,Shift+1);
      double MAnow = iMA(NULL,0,MA,0,MAMode,MAPrice,Shift);
      double MApre = iMA(NULL,0,MA,0,MAMode,MAPrice,Shift+1);    
//--------------------------------------------------------------------+
//---- buy conditions
     if(Low[Shift+1] < BBlo && MAnow > MApre)
     {
      res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,Slippage,Ask-(StopLoss*Point),0,"GT BB SINWIN",MAGIC,0,Green);
      return;
     }     
//---- sell conditions
     if(High[Shift+1] > BBup && MAnow < MApre)  
     {
      res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,Slippage,Bid+(StopLoss*Point),0,"GT BB SINWIN",MAGIC,0,Red);
      return;
     }
//----
  }
//+------------------------------------------------------------------+
//| Check for close order conditions                                 |
//+------------------------------------------------------------------+
void CheckForClose()
  {
   double ma;
//---- go trading only for first tiks of new bar
   if(Volume[0]>1) return;
//--------------------------------------------------------------------+
      double BBup = iBands(NULL,0,BBPeriod,BBDeviation,0,BBPrice, MODE_UPPER,Shift);
      double BBlo = iBands(NULL,0,BBPeriod,BBDeviation,0,BBPrice, MODE_LOWER,Shift);
      double MAnow = iMA(NULL,0,MA,0,MAMode,MAPrice,Shift);
      double MApre = iMA(NULL,0,MA,0,MAMode,MAPrice,Shift+1);    
//--------------------------------------------------------------------+
//----
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)        break;
      if(OrderMagicNumber()!=MAGIC || OrderSymbol()!=Symbol()) continue;
      //---- check order type 
      if(OrderType()==OP_BUY)
        {
         if(High[Shift] > BBup && MAnow < MApre) OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Gold);
         break;
        }
      if(OrderType()==OP_SELL)
        {
         if(Low[Shift] < BBlo && MAnow > MApre) OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Gold);
         break;
        }
     }
//----
  }  
//+------------------------------------------------------------------+
//| Start function                                                   |
//+------------------------------------------------------------------+
void start()
  {
   if(Trailing) TrailPositions();
   if(NoLoss) CreateNoLoss();     
//---- check for history and trading
   if(Bars<100 || IsTradeAllowed()==false) return;
//---- calculate open orders by current symbol
   if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
   else                                    CheckForClose();
  }   
//+------------------------------------------------------------------+
//|-----------------------// Trail positions //----------------------+
//+------------------------------------------------------------------+
void TrailPositions()
{
  int Orders = OrdersTotal();
  for (int i=0; i<Orders; i++)
  {
    if (!(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))) continue;
    if (OrderSymbol() != Symbol()) continue;
    {    
      if (OrderType() == OP_BUY)  
      {
        if (Bid-OrderOpenPrice() > MinProfit*Point) 
        {
          if (OrderStopLoss() < Bid-(TrailingStop+TrailingStep-1)*Point) 
          {
          OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*Point,OrderTakeProfit(),0,Gold);          
          }
        }
      }   
      if (OrderType() == OP_SELL)  
      {
        if (OrderOpenPrice()-Ask > MinProfit*Point) 
        {
          if (OrderStopLoss() > Ask+(TrailingStop+TrailingStep-1)*Point) 
          {
          OrderModify(OrderTicket(),OrderOpenPrice(),Ask+TrailingStop*Point,OrderTakeProfit(),0,Gold);         
          }
        }
      }   
    }   
  }  
}
//+------------------------------------------------------------------+
//|---------------------------// No Losee //-------------------------+
//+------------------------------------------------------------------+
void CreateNoLoss()
{
  int Orders = OrdersTotal();
  for (int i=0; i<Orders; i++)
  {
    if (!(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))) continue;
    if (OrderSymbol() != Symbol()) continue;
    {    
      if (OrderType() == OP_BUY && OrderStopLoss() < OrderOpenPrice())  
      {
        if (Bid-OrderOpenPrice() > MinProfitB*Point) 
        {
          if (OrderStopLoss() < Bid-(NoLossLevel-1)*Point) 
          {
          OrderModify(OrderTicket(), OrderOpenPrice(),OrderOpenPrice()+NoLossLevel*Point,OrderTakeProfit(),0,Gold);           
          }
        }
      }
      if (OrderType() == OP_BUY && OrderStopLoss() == 0)  
      {
        if (Bid-OrderOpenPrice() > MinProfitB*Point) 
        {
          if (OrderStopLoss() < Bid-(NoLossLevel-1)*Point) 
          {
          OrderModify(OrderTicket(), OrderOpenPrice(),OrderOpenPrice()+NoLossLevel*Point,OrderTakeProfit(),0,Gold);          
          }
        }
      }         
      if (OrderType() == OP_SELL && OrderStopLoss() > OrderOpenPrice())  
      {
        if (OrderOpenPrice()-Ask > MinProfitB*Point) 
        {
          if (OrderStopLoss() > Ask+(NoLossLevel-1)*Point) 
          {
          OrderModify(OrderTicket(), OrderOpenPrice(),OrderOpenPrice()-NoLossLevel*Point,OrderTakeProfit(),0,Gold);           
          }
        }
      }  
      if (OrderType() == OP_SELL && OrderStopLoss() == 0)  
      {
        if (OrderOpenPrice()-Ask > MinProfitB*Point) 
        {
          if (OrderStopLoss() > Ask+(NoLossLevel-1)*Point) 
          {
          OrderModify(OrderTicket(), OrderOpenPrice(),OrderOpenPrice()-NoLossLevel*Point,OrderTakeProfit(),0,Gold);           
          }
        }        
      }   
    }   
  }  
}
//+------------------------------------------------------------------+
//|---------------------------// END //------------------------------|
//+------------------------------------------------------------------+

Comments