Navigation:Home > Content >

!.mq4

Time: 2017-07-12 | Download file:!.mq4

//+-----------------------------------------------------------------------------+
//|                                         Manual Trailing for Mauro Bianco    |
//|                                                    Copyright © 2006, Yannis |
//|                                                         All rights reserved |
//|                                                                             |
//+-----------------------------------------------------------------------------+
#property copyright "Copyright © 2006, Yannis"
#property link      "[email protected]"
//+-----------------------------------------------------------------------------+
//|                       May 1st 2006                                          |
//+-----------------------------------------------------------------------------+

//+----------------------User interface-----------------------------------------+
extern string  SL_TP_Trail_Options="--------------------------";
extern int     StopLoss_Pips=100;                                 // static, initial s/l. Unused if Use_ADR_for_sl_Pips = true
extern int     TakeProfit_Pips=100;                               // static, initial take profit
extern int     Trail_Pips=9999;                                    // trail_Pips_ Unused if Use_ADR_for_sl_Pips=true or if value=0
extern bool    Trail_Starts_After_BreakEven=false;               // if true trailing will start after a profit of "Move_To_BreakEven_at_Pips" is made
extern int     Move_To_BreakEven_at_Pips=9999;                      // trades in profit will move to entry price + Move_To_BreakEven_Lock_Pips as soon as trade is at entry price + Move_To_BreakEven_at_Pips
extern int     Move_To_BreakEven_Lock_Pips=9999;
extern int     Move_Trail_Every_xx_Pips=9999;                       // If > 0 then ALL other s/l are dropped and trail will only move by Trail_Pips amount for every "Move_Trail_Every_Pips" in profit
extern bool    Use_ADR_for_SL_Pips=false;                        // if true s/l and trail according to average daily range and tsl_divisor
extern double  tsl_divisor=0.40;

//+---------------------- Global Variables Definition --------------------------------------+
int b_ticket, s_ticket,slip, TodaysRange;
string DR, DR1, comment="",ScreenComment="";
double avg_rng, rng, sum_rng, x;
bool LongTradeShouldClose;
double TPPrice;

int init()
{  //HideTestIndicators(true);
   slip=(Ask-Bid)/Point;
   return(0);
}

int deinit()
{  return(0);
}

int start()
{  if (Use_ADR_for_SL_Pips) {StopLoss_Pips=NormalizeDouble(Daily_Range()/Point,Digits);}
   x=NormalizeDouble(Daily_Range()*tsl_divisor,Digits);
   TodaysRange=MathAbs(iHigh(Symbol(),PERIOD_D1,0)-iLow(Symbol(),PERIOD_D1,0))/Point;
   PosCounter          ();                      // check for open positions_ Sets b_ticket, s_ticket
   CheckInitialSLTP    ();
   if (Move_To_BreakEven_at_Pips!=0 && (s_ticket>0 || b_ticket>0)) {MoveToBreakEven();}
   if (s_ticket>0 || b_ticket>0) {Trail_Stop();}
   comments();
   return(0);
} 

void CheckInitialSLTP()
{  int sl,tp;
   if (b_ticket>0) 
   {  bool select1=OrderSelect(b_ticket,SELECT_BY_TICKET);
      if (OrderStopLoss()==0 || OrderTakeProfit()==0)
      {  if (OrderStopLoss  ()==0)  {sl=StopLoss_Pips;}
         if (OrderTakeProfit()==0)  {tp=TakeProfit_Pips;}
         if ((sl>0 && OrderStopLoss()==0) || (tp>0 && OrderTakeProfit()==0))  
         {  bool modify1=OrderModify(b_ticket, OrderOpenPrice(), OrderOpenPrice()-sl*Point,OrderOpenPrice()+tp*Point,OrderExpiration(),MediumSpringGreen);
            if (OrderSelect(b_ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("Initial SL or TP is Set for Long Entry");
            else Print("Error setting initial SL or TP for Long Entry");
         }
      }
   }
   if (s_ticket > 0)
   {  bool select2=OrderSelect(s_ticket,SELECT_BY_TICKET);
      if (OrderStopLoss()==0 || OrderTakeProfit()==0)
      {  if (OrderStopLoss  ()==0)  {sl=StopLoss_Pips;}
         if (OrderTakeProfit()==0)  {tp=TakeProfit_Pips;}
         if ((sl>0 && OrderStopLoss()==0) || (tp>0 && OrderTakeProfit()==0))  
         {  bool modify2=OrderModify(s_ticket, OrderOpenPrice(), OrderOpenPrice()+sl*Point,OrderOpenPrice()-tp*Point,OrderExpiration(),MediumVioletRed);
            if (OrderSelect(s_ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("Initial SL or TP is Set for Short Entry");
            else Print("Error setting initial SL or TP for Short Entry");
         }
      }
   }
}

double Daily_Range()
{  if (DR==TimeToStr(CurTime(),TIME_DATE))
   {  return(NormalizeDouble(avg_rng,Digits));
   }
   rng=0;sum_rng=0;avg_rng=0;
   for (int i=0;i0) 
   {  bool select4=OrderSelect(b_ticket,SELECT_BY_TICKET);
      if (OrderType()==OP_BUY)
      {
      PipsProfit=NormalizeDouble(((Bid - OrderOpenPrice())/Point),Digits);
      AmountProfit=OrderProfit();
      }
   }
   else if (s_ticket>0) 
   {  bool select5=OrderSelect(s_ticket,SELECT_BY_TICKET);
      if (OrderType()==OP_SELL)
      {
      PipsProfit=NormalizeDouble(((OrderOpenPrice()-Ask)/Point),Digits);
      AmountProfit=OrderProfit();
      }
   }
   if (Move_To_BreakEven_at_Pips>0) s1=""+Move_To_BreakEven_at_Pips+"."+Move_To_BreakEven_Lock_Pips+""+"";
   else                             s1="";
   }

void Trail_With_ADR(int AfterBE)
{  double bsl, b_tsl, ssl, s_tsl;
   PosCounter();
   // x=Minimum Wave Range of Average Daily Range Trailing Stop Calculation
   if (AfterBE==0) // Trail Starts immediately
   {  if(b_ticket>0)
      {  bsl=NormalizeDouble(x,Digits);
         b_tsl=0;
         bool select6=OrderSelect(b_ticket,SELECT_BY_TICKET);
         if (OrderType()==OP_BUY)
         {
         //if stoploss is less than minimum wave range, set bsl to current SL
         if (OrderStopLoss()=x)
         {  bsl=NormalizeDouble(x,Digits);
         }
         //determine if stoploss should be modified
         if (Bid>(OrderOpenPrice()+bsl) && OrderStopLoss()<(OrderOpenPrice()+(Bid-(OrderOpenPrice()+bsl))))
         {  b_tsl=NormalizeDouble(OrderOpenPrice()+(Bid-(OrderOpenPrice()+bsl)),Digits);
            Print("b_tsl ",b_tsl);
            if (OrderStopLoss()0)
      {  ssl=NormalizeDouble(x,Digits);
         s_tsl=0;
         bool select7=OrderSelect(s_ticket,SELECT_BY_TICKET);
         if (OrderType()==OP_SELL)
         {
         //if stoploss is less than minimum wave range, set ssl to current SL
         if (OrderStopLoss()>OrderOpenPrice() && OrderStopLoss()-OrderOpenPrice()OrderOpenPrice() && OrderStopLoss()-OrderOpenPrice()>=x)
         {  ssl=NormalizeDouble(x,Digits);
         }
         //determine if stoploss should be modified
         if (Ask<(OrderOpenPrice()-ssl) && OrderStopLoss()>(OrderOpenPrice()-(OrderOpenPrice()-ssl)-Ask))
         {  s_tsl=NormalizeDouble(OrderOpenPrice()-((OrderOpenPrice()-ssl)-Ask),Digits);
            Print("s_tsl ",s_tsl);
            if(OrderStopLoss()>s_tsl)
            {  bool modify4=OrderModify(s_ticket,OrderOpenPrice(),s_tsl,OrderTakeProfit(),OrderExpiration(),MediumVioletRed);
            }
         }
         }
      }
   }
   else // If Trail_Starts_After_BreakEven
   {  if (b_ticket>0)
      {  bsl=NormalizeDouble(x,Digits);
         b_tsl=0;
         bool select8=OrderSelect(b_ticket,SELECT_BY_TICKET);
         if (OrderType()==OP_BUY)
         {
         if (Bid>=(OrderOpenPrice()+(Move_To_BreakEven_at_Pips*Point)))
         {  //if stoploss is less than minimum wave range, set bsl to current SL
            if (OrderStopLoss()=x)
            {  bsl=NormalizeDouble(x,Digits);
            }
            //determine if stoploss should be modified
            if (Bid>(OrderOpenPrice()+bsl) && OrderStopLoss()<(OrderOpenPrice()+(Bid-(OrderOpenPrice()+bsl))))
            {  b_tsl=NormalizeDouble(OrderOpenPrice()+(Bid-(OrderOpenPrice()+bsl)),Digits);
               Print("b_tsl ",b_tsl);
               if (OrderStopLoss()0)
      {  ssl=NormalizeDouble(x,Digits);
         s_tsl=0;
         bool select9=OrderSelect(s_ticket,SELECT_BY_TICKET);
         if (OrderType()==OP_SELL)
         {
         if (Ask<=(OrderOpenPrice()-(Move_To_BreakEven_at_Pips*Point)))
         {  //if stoploss is less than minimum wave range, set ssl to current SL
            if(OrderStopLoss()>OrderOpenPrice() && OrderStopLoss()-OrderOpenPrice()OrderOpenPrice() && OrderStopLoss()-OrderOpenPrice()>=x)
            {  ssl=NormalizeDouble(x,Digits);
            }
            //determine if stoploss should be modified
            if(Ask<(OrderOpenPrice()-ssl) && OrderStopLoss()>(OrderOpenPrice()-(OrderOpenPrice()-ssl)-Ask))
            {  s_tsl=NormalizeDouble(OrderOpenPrice()-((OrderOpenPrice()-ssl)-Ask),Digits);
               Print("s_tsl ",s_tsl);
               if(OrderStopLoss()>s_tsl)
               {  bool modify6=OrderModify(s_ticket,OrderOpenPrice(),s_tsl,OrderTakeProfit(),OrderExpiration(),MediumVioletRed);
               }
            }
         }
         }
      }
   }
}

void Trail_With_Standard_Trailing(int AfterBE)
{  double bsl, b_tsl, ssl, s_tsl;
   PosCounter();
   if (AfterBE==0)
   {  if (b_ticket>0)
      {  bsl=Trail_Pips*Point;
         bool select10=OrderSelect(b_ticket,SELECT_BY_TICKET);
         if (OrderType()==OP_BUY)
         {
         //determine if stoploss should be modified
         if(Bid>(OrderOpenPrice()+bsl) && OrderStopLoss()<(OrderOpenPrice()+(Bid-(OrderOpenPrice()+bsl))))
         {  b_tsl=NormalizeDouble(OrderOpenPrice()+(Bid-(OrderOpenPrice()+bsl)),Digits);
            Print("b_tsl ",b_tsl);
            if (OrderStopLoss()0)
      {  ssl=Trail_Pips*Point;
         //determine if stoploss should be modified
         bool select11=OrderSelect(s_ticket,SELECT_BY_TICKET);
         if (OrderType()==OP_SELL)
         {
         if (Ask<(OrderOpenPrice()-ssl) && OrderStopLoss()>(OrderOpenPrice()-(OrderOpenPrice()-ssl)-Ask))
         {  s_tsl=NormalizeDouble(OrderOpenPrice()-((OrderOpenPrice()-ssl)-Ask),Digits);
            Print("s_tsl ",s_tsl);
            if (OrderStopLoss()>s_tsl)
            {  bool modify8=OrderModify(s_ticket,OrderOpenPrice(),s_tsl,OrderTakeProfit(),OrderExpiration(),MediumVioletRed);
            }
         }
         }
      }
   }
   else // If Trail_Starts_After_BreakEven
   {  if (b_ticket>0)
      {  bool select12=OrderSelect(b_ticket,SELECT_BY_TICKET);
         if (OrderType()==OP_BUY)
         {
         if (Bid>=(OrderOpenPrice()+(Move_To_BreakEven_at_Pips*Point)))
         {  bsl=Trail_Pips*Point;
            if (Bid>(OrderOpenPrice()+bsl) && OrderStopLoss()<(OrderOpenPrice()+(Bid-(OrderOpenPrice()+bsl))))
            {  b_tsl=NormalizeDouble(OrderOpenPrice()+(Bid-(OrderOpenPrice()+bsl)),Digits);
               Print("b_tsl ",b_tsl);
               if (OrderStopLoss()0)
      {  bool select13=OrderSelect(s_ticket,SELECT_BY_TICKET);
         if (OrderType()==OP_SELL)
         {
         if (Ask<=(OrderOpenPrice()-(Move_To_BreakEven_at_Pips*Point)))
         {  ssl=Trail_Pips*Point;
            //determine if stoploss should be modified
            if(Ask<(OrderOpenPrice()-ssl) && OrderStopLoss()>(OrderOpenPrice()-(OrderOpenPrice()-ssl)-Ask))
            {  s_tsl=NormalizeDouble(OrderOpenPrice()-((OrderOpenPrice()-ssl)-Ask),Digits);
               Print("s_tsl ",s_tsl);
               if(OrderStopLoss()>s_tsl)
               {  bool modify10=OrderModify(s_ticket,OrderOpenPrice(),s_tsl,OrderTakeProfit(),OrderExpiration(),MediumVioletRed);
               }
            }
         }
         }
      }
   }
}


void Trail_With_Every_xx_Pips()
{  double bsl, b_tsl, ssl, s_tsl, CurrProfit;
   int Factor;
   PosCounter();
   if (b_ticket>0)
   {  bool select14=OrderSelect(b_ticket,SELECT_BY_TICKET);
      if (OrderType()==OP_BUY)
      {
      CurrProfit=((Bid-OrderOpenPrice())/Point);
      if (CurrProfit>=Move_Trail_Every_xx_Pips)
      {  Factor=MathFloor(CurrProfit/Move_Trail_Every_xx_Pips);
         bsl=Factor*Trail_Pips*Point;
         //determine if stoploss should be modified
         if(Bid>(OrderOpenPrice()+bsl) && OrderStopLoss()<(OrderOpenPrice()+(Bid-(OrderOpenPrice()+bsl))))
         {  b_tsl=NormalizeDouble(OrderOpenPrice()+(Bid-(OrderOpenPrice()+bsl)),Digits);
            Print("b_tsl ",b_tsl);
            if (OrderStopLoss()0)
   {  bool select15=OrderSelect(s_ticket,SELECT_BY_TICKET);
      if (OrderType()==OP_SELL)
      {
      CurrProfit=((OrderOpenPrice()-Ask)/Point);
      if (CurrProfit>=Move_Trail_Every_xx_Pips)
      {  Factor=MathFloor(CurrProfit/Move_Trail_Every_xx_Pips);
         ssl=Factor*Trail_Pips*Point;
         //determine if stoploss should be modified
         if (Ask<(OrderOpenPrice()-ssl) && OrderStopLoss()>(OrderOpenPrice()-(OrderOpenPrice()-ssl)-Ask))
         {  s_tsl=NormalizeDouble(OrderOpenPrice()-((OrderOpenPrice()-ssl)-Ask),Digits);
            Print("s_tsl ",s_tsl);
            if (OrderStopLoss()>s_tsl)
            {  bool modify12=OrderModify(s_ticket,OrderOpenPrice(),s_tsl,OrderTakeProfit(),OrderExpiration(),MediumVioletRed);
            }
         }
      }
      }
   }
}

void Trail_Stop()
{  if (Move_Trail_Every_xx_Pips>0 && Trail_Pips>0)
   {  Trail_With_Every_xx_Pips();
   }
   else
   {  if (Use_ADR_for_SL_Pips)
      {  if (Trail_Starts_After_BreakEven)   Trail_With_ADR(1);
         else                                Trail_With_ADR(0);
      }
      else if (Trail_Pips>0)
      {  if (Trail_Starts_After_BreakEven)   Trail_With_Standard_Trailing(1);
         else                                Trail_With_Standard_Trailing(0);
      }
   }
}

void MoveToBreakEven()
{  PosCounter();
   if (b_ticket > 0)
   {  bool select16=OrderSelect(b_ticket,SELECT_BY_TICKET);
      if (OrderType()==OP_BUY)
      {
      if (OrderStopLoss()((Move_To_BreakEven_at_Pips*Point) +OrderOpenPrice()))
         {  bool modify13=OrderModify(b_ticket, OrderOpenPrice(), (OrderOpenPrice()+(Move_To_BreakEven_Lock_Pips*Point)),OrderTakeProfit(),OrderExpiration(),MediumSpringGreen);
            if (OrderSelect(b_ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("Long StopLoss Moved to BE at : ",OrderStopLoss());
            else Print("Error moving Long StopLoss to BE: ",GetLastError());
         }
      }
      }
   }
   if (s_ticket > 0)
   {  bool select17=OrderSelect(s_ticket,SELECT_BY_TICKET);
      if (OrderType()==OP_SELL)
      {
      if (OrderStopLoss()>OrderOpenPrice())
      {  if ( Ask < (OrderOpenPrice()-(Move_To_BreakEven_at_Pips*Point)))
         {  bool modify14=OrderModify(OrderTicket(), OrderOpenPrice(), (OrderOpenPrice()-(Move_To_BreakEven_Lock_Pips*Point)),OrderTakeProfit(),OrderExpiration(),MediumVioletRed);
            if(OrderSelect(s_ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("Short StopLoss Moved to BE at : ",OrderStopLoss());
            else Print("Error moving Short StopLoss to BE: ",GetLastError());
         }
      }
      }
   }
}

Recommend