Navigation:Home > Content >

SuperTrap_R51.mq4

Time: 2015-03-11 | Download file:SuperTrap_R51.mq4

//+------------------------------------------------------------------+
//|                                                 SuperTrap_R5.mq4 |
//|                        Copyright 2012, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2012, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
#define  NL    "\n"


extern int     Magic.No   = 12345;
extern bool    Continuous.Trading = false;
extern int     Start.Hour = 8;
extern int     Start.Min  = 0;
extern bool    Entry.On.Bar.Signal=false;
extern double  Bar.Pip.Size = 10;
extern bool    Stop.Trade = false;
extern bool    Close.All  = false;
extern bool    Manual.Buy = false;
extern bool    Manual.Sell= false;
extern int     Grid.Dist = 10;
extern double  Lots      = 0.3;
extern double  Min.Lots  = 0.01;
extern double  Max.Lots  = 5;
extern int     Lots.Shift.Level=4;
extern double  TP        = 40;
extern double  SL        = 25;
extern double  Spread    = 3;
extern int     Max.Level = 10;
extern double  Multiplier1= 2;
extern double  Multiplier2= 1.8;



double Cur.Buy.Price,Grid1b.Price,Grid2b.Price,Grid3b.Price;
double Cur.Sell.Price,Grid1s.Price,Grid2s.Price,Grid3s.Price;
double Prof_b, Prof_bLast,Prof_s,Prof_sLast,iLots,dist_x,sLots,bLots,Multx,TPx,SLx,Poin;
int retry, last.ticket,level_buy,level_sell,level_pbuy,level_psell,iGrid,iDel,iDel1,iDel2,SL.Gap=4,NewStart;
int mbuy,msell,LSL,trade,dLast;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
  CheckPos();
  NewStart=1;
  
  // Check Digit  

   if (StringFind(Symbol(), "JPY") >= 0)
      Poin = 0.01;
   else
      Poin = 0.0001;
  
  
  if (level_buy+level_sell+level_pbuy+level_psell == 0)
      {
        Cur.Buy.Price   = Ask;
        Cur.Sell.Price  = Ask;
        Grid1b.Price = Cur.Buy.Price  + Grid.Dist * Poin;
        Grid1s.Price = Cur.Sell.Price - Grid.Dist * Poin;
       
      }
  
  if (level_buy+level_sell+level_pbuy+level_psell > 0) {iGrid=1;NewStart=0;}
 
  
  
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
  // if (Volume[0]>1) return;
  
//   if (Hour()<8) return;
//   if (Hour()==23 && Minute()== 59) CloseAll();
   
  // if (Hour()>=23 && Minute()>=50) {CloseAll(); return;}
 
 
 // comments   
 CheckPos();
 
  ObjectCreate("MgcNo", OBJ_LABEL,0,0,0);
  ObjectSetText("MgcNo", " Magic No : "+ Magic.No, 8, "Verdana", Yellow);
  ObjectSet("MgcNo", OBJPROP_CORNER, 1);
  ObjectSet("MgcNo", OBJPROP_XDISTANCE, 10);
  ObjectSet("MgcNo", OBJPROP_YDISTANCE, 20);
  
  

string Message =   " "+NL+
                   " P/L             : " +DoubleToStr(Prof_s+Prof_b,2)+NL+
                   " Sell P/L       : "  +DoubleToStr(Prof_s,2)+NL+
                   " Buy P/L      : "    +DoubleToStr(Prof_b,2)+NL+
                   " Open Buy    : "     +level_buy+NL+
                   " Pend Buy    : "     +level_pbuy+NL+
                   " Open Sell    : "    +level_sell+NL+
                   " Pend Sell    : "    +level_psell+NL+
                   " Start Time   : "    +Start.Hour+":"+Start.Min+NL+
                   " Time           : "    + TimeToStr(TimeCurrent());
                 
                  
 Comment (Message);        
 
 
 // Manual Close All && Stop Trade
   if (Close.All) {Close_pOrder();CloseAll(); trade=0;dLast=0;return;}
   if (Stop.Trade && level_buy==0 && level_sell==0 ) return;
  
 
 
 
 
 // Start Time
 
 if (Continuous.Trading) {trade=1;dLast=1;}
 
 
 if (!Continuous.Trading)
 { //
    if (Hour()==Start.Hour && Minute()>= Start.Min)// {trade=1;dLast=1;}
    {
         if (Entry.On.Bar.Signal) 
            {
              if (MathAbs(Open[0]-Close[0])/Poin >=Bar.Pip.Size ) {trade=1;dLast=1;}
            }
            
          if (!Entry.On.Bar.Signal)   {trade=1;dLast=1;}  
       }
    
    
  
    if (trade==0)
     { 
      if (dLast==1) {Close_pOrder();dLast=0;}
       return;
     }
     
  
  CheckPos();
 // NewStart=1;
  
 if (level_buy+level_sell+level_pbuy+level_psell == 0)
     {
        Cur.Buy.Price   = Ask;
        Cur.Sell.Price  = Ask;
        Grid1b.Price = Cur.Buy.Price  + Grid.Dist * Poin;
        Grid1s.Price = Cur.Sell.Price - Grid.Dist * Poin;
       
     }
  
 // if (level_buy+level_sell+level_pbuy+level_psell > 0) {iGrid=1;NewStart=0;}
 
  } //
  

 
 
 // Manual Close All && Stop Trade
 //  if (Close.All) {Close_pOrder();CloseAll(); trade=0;dLast=0;return;}
 //  if (Stop.Trade && level_buy==0 && level_sell==0 ) return;
  
   CheckPos();
   if (iGrid==0 && level_buy==0 && level_sell==0 && trade==1)
      {
      // Multx=1;
      // iLots=Lots;
       iLots=Min.Lots;
       Close_pOrder();
       PlaceBuyGrid();
       PlaceSellGrid();
       iGrid=1;
       NewStart=0;
       iDel=0;
       LSL=0;
       }
       
   CheckPos();
   if (level_sell > 0 || level_buy > 0) iGrid=2;

       
    if (iGrid==2 && Ask > Grid1b.Price+(TP+SL.Gap+Spread)*Poin || Bid < Grid1s.Price-(TP+SL.Gap+Spread)*Poin && Prof_b+Prof_s >0)
       {
        Close_pOrder();
        CloseAll();
        trade=0;
        dLast=0;
       }  
       
// Deleting First Pending Order    
   if (level_sell==1 && level_pbuy==1 && iDel ==0 && NewStart==0)
     {
       while (level_pbuy==1)
        {
        Close_pOrder();
        Sleep (1000);
        CheckPos();
        }
        iDel=1;
     }
  if (level_buy ==1 && level_psell==1 && iDel ==0 && NewStart==0)
     {
      while (level_psell==1)
       {
        Close_pOrder();
        Sleep (1000);
        CheckPos();
        }
        iDel=1;
     }
 
   if (iGrid==2 && level_sell==0 && level_buy==0)
      {
       Close_pOrder();
       iGrid=0; 
       iDel=0;
       LSL=0;  
       trade=0;
       dLast=0;
       Cur.Buy.Price   = Ask;
       Cur.Sell.Price  = Ask;
  
       Grid1b.Price = Cur.Buy.Price  + Grid.Dist * Poin;
       Grid1s.Price = Cur.Sell.Price - Grid.Dist * Poin;
      
      }
  
       
       
 if (iGrid!=0)
 {   
 
   CheckPos();
 
     Grid1b.Price = Cur.Buy.Price  + Grid.Dist * Poin;
     Grid1s.Price = Cur.Sell.Price - Grid.Dist * Poin;
  
 
     
     TPx =  TP/Grid.Dist;     
     TP  =  TPx*Grid.Dist;

     SLx =  (SL-SL.Gap-Spread)/Grid.Dist;
     SL  =  SLx*Grid.Dist+ SL.Gap + Spread;
    
// Place Opposite Order     
  
   if (level_pbuy==0 && Ask >= Cur.Buy.Price && level_psell==0)
      {
        if (level_sell  < Max.Level)   PlaceSellGrid();
      
      } 
      
   
   if (level_psell==0  && Bid <= Cur.Sell.Price && level_pbuy==0)
      {
        if (level_buy  < Max.Level)  PlaceBuyGrid();
      
      }  
      
// Manual Order

   if (Manual.Buy  && mbuy ==0)  {CheckPos();PlaceBuyGrid(); mbuy=1;}
   if (Manual.Sell && msell==0)  {CheckPos();PlaceSellGrid();msell=1;}
   if (!Manual.Buy)  mbuy=0;
   if (!Manual.Sell) msell=0;
   
    
// Modifying Open Order   
//     if (level_buy >= 2 && level_sell >= 2 && level_pbuy==0 && level_psell==0) {ModifyOpenOrder();iDel=0;}     
 //    if (level_buy >= 3 && level_sell >= 3 && level_pbuy==0 && level_psell==0) {ModifyOpenOrder();iDel=0;}
 //    if (level_buy >= 4 && level_sell >= 4 && level_pbuy==0 && level_psell==0) {ModifyOpenOrder();iDel=0;} 
         
      
      
       
 }
 
 
   
//----
   return(0);
  }
//+------------------------------------------------------------------+

//+ User Function--------------------------+

void PlaceBuyGrid()
{//start
   retry=0; //1st Grid
    last.ticket=0;
    while (last.ticket ==0 && retry <=10) 
     {
      RefreshRates();
      last.ticket=OrderSend(Symbol(),OP_BUYSTOP,iLots,Grid1b.Price,5,Grid1b.Price-SL*Poin,Grid1b.Price+TP*Poin,"sTrap_Buy",Magic.No,0,Blue);
      Sleep (2000);
      retry++;
     } 
  
 }//end function   
     
     
     
void PlaceSellGrid()
{//start
   retry=0; //1st Grid
    last.ticket=0;
    while (last.ticket ==0 && retry <=10) 
     {
      RefreshRates();
      last.ticket=OrderSend(Symbol(),OP_SELLSTOP,iLots,Grid1s.Price,5,Grid1s.Price+ SL*Poin,Grid1s.Price-TP*Poin,"sTrap_Sell",Magic.No,0,Red);
      Sleep (2000);
      retry++;
     } 

 }//end function   
 
 
 void CheckPos()
 {
   Prof_b=0;Prof_bLast=0;Prof_s=0;Prof_sLast=0;sLots=0;bLots=0;iLots=Min.Lots;
   level_buy=0;level_pbuy=0;level_sell=0;level_psell=0;
   for (int c=0;cLots.Shift.Level*Min.Lots)   Multx=Multiplier2;
           iLots=OrderLots()*Multx;
           if (OrderLots()>=Min.Lots*MathPow(Multx,Lots.Shift.Level)&& LSL==0) {iLots=Lots;LSL=1;}
           
           if (iLots>=Max.Lots) iLots=Max.Lots;
           bLots=bLots+OrderLots();
           level_buy++;
        
         //  if (NewStart==1 && level_buy>0) {Grid1b.Price = OrderOpenPrice();}
           Grid1b.Price = OrderOpenPrice();
           
           ObjectCreate("iLots", OBJ_LABEL,0,0,0);
           ObjectSetText("iLots", " Buy Lots : "+ DoubleToStr(OrderLots(),2), 8, "Verdana", LawnGreen);
           ObjectSet("iLots", OBJPROP_CORNER, 1);
           ObjectSet("iLots", OBJPROP_XDISTANCE, 10);
           ObjectSet("iLots", OBJPROP_YDISTANCE, 35); 
         }
  
  
         
    
  
         if( Symbol() == OrderSymbol() &&
           OrderMagicNumber() == Magic.No && OrderType() == OP_SELL)
         {
           Prof_s = Prof_s + OrderProfit();
           Prof_sLast = OrderProfit();
               
           if (OrderLots()<=Lots.Shift.Level*Min.Lots) {Multx=Multiplier1;LSL=0;}
           if (OrderLots()>Lots.Shift.Level*Min.Lots) Multx=Multiplier2;
        
           iLots=OrderLots()*Multx;
           if (OrderLots()>=Min.Lots*MathPow(Multx,Lots.Shift.Level)&& LSL==0) {iLots=Lots;LSL=1;}
           
           if (iLots>=Max.Lots) iLots=Max.Lots;
           sLots=sLots+OrderLots();
           level_sell++;
      
          // if (NewStart==1 && level_sell>0) {Grid1s.Price = OrderOpenPrice();}
           Grid1s.Price = OrderOpenPrice();
           
          // ObjectsDeleteAll(0,"iLots");
           ObjectCreate("iLots", OBJ_LABEL,0,0,0);
           ObjectSetText("iLots", " Sell Lots : "+ DoubleToStr(OrderLots(),2), 8, "Verdana", LawnGreen);
           ObjectSet("iLots", OBJPROP_CORNER, 1);
           ObjectSet("iLots", OBJPROP_XDISTANCE, 10);
           ObjectSet("iLots", OBJPROP_YDISTANCE, 35); 
           
           
         }
  
    
      }//c
   }
   
 // ObjectCreate("iLots", OBJ_LABEL,0,0,0);
//  ObjectSetText("iLots", " Lots : "+ DoubleToStr(iLots,2), 8, "Verdana", LawnGreen);
 // ObjectSet("iLots", OBJPROP_CORNER, 1);
//  ObjectSet("iLots", OBJPROP_XDISTANCE, 10);
 // ObjectSet("iLots", OBJPROP_YDISTANCE, 35); 
   
}      
  
  
void Close_pOrder()
 {
  int i, ticket;
  for( i=OrdersTotal()-1;i>=0;i--)
        {
                          OrderSelect(i, SELECT_BY_POS);
                                                
                          ticket = OrderTicket();
                         
                          
                          
                          if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
                         
                          {
                            if( Symbol() == OrderSymbol()        &&
                                OrderMagicNumber() == Magic.No   &&
                                (OrderType() == OP_SELLSTOP      ||
                                 OrderType() == OP_BUYSTOP ))
              
                               {
                                 OrderDelete( ticket, CLR_NONE);
                               }
                           }    
         }
         
 }  
 
 
 
 void CloseAll()
{
     int total=OrdersTotal();
     
     for(int i=total-1;i>=0;i--)
     {
        OrderSelect(i, SELECT_BY_POS);                          
        int type   = OrderType();
        bool result = false;
        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
          if( Symbol() == OrderSymbol() &&
          OrderMagicNumber() == Magic.No )
          {
            switch(type)
            {
            //Close opened long positions
            case OP_BUY       : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
                    //            level_buy=0; 
                          
            //Close opened short positions
            case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
                    //            level_sell=0;
            }
           }
         }                   
      }                    
      
   return;
  }    
  
  
// void ModifyOpenOrder()
// {
 
 
//  for (int c=0; c        

Recommend