Author: Orangetree
Price Data Components
Orders Execution
Checks for the total of open ordersIt can change open orders parameters, due to possible stepping strategy
Miscellaneous
It plays sound alerts
0 Views
0 Downloads
0 Favorites
TSL
//+------------------------------------------------------------------+ 
//|                                TSL.mql4                          | 
//|                              OrangeTree(c)                       | 
//+------------------------------------------------------------------+
#property copyright "Orangetree"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

input int TSL;                                  // trailing in Point()
input int BE      =0;                         // break-even in Point()
input double DIV  =1;                                // profit divisor
input bool Sound  =false;                           // Trailing Signal
input int Magic   =-1;                                 // Magic Number

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
  
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {

   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   /* frequnecy limitation ------------+
   static datetime time=0;
   datetime current_time =TimeCurrent();
   if(current_time-time<3) return;
   time =current_time;
   */ 
   TS(TSL,DIV,BE,Magic);
   
  }
//+------------------------------------------------------------------+
void TS(int tsl,double delitel=1,int offset=0,int magic=-1)
{

if(tsl==0) return;
bool   ch     =0;
double profit =0;
double sl     =0;
   for(int i =0;i<OrdersTotal();i++)
   {

      if (OrderSelect(i,SELECT_BY_POS)== false)   continue;         
      if (OrderSymbol()!=Symbol())                continue;
      if (OrderType()>1)                          continue;
      if (OrderMagicNumber()!=magic && magic!=-1) continue;
      
      if(OrderType()==OP_BUY)
       {
       profit =Bid-OrderOpenPrice()-tsl*Point();
       if(profit<0) continue;
       if(delitel==0)
           {sl =NormalizeDouble(OrderOpenPrice()+offset*Point(),Digits());}
       else
           {sl =NormalizeDouble(OrderOpenPrice()+profit/delitel+offset*Point(),Digits());}
       if(sl>OrderStopLoss())
           {
           ch =OrderModify(OrderTicket(),OrderOpenPrice(),sl,OrderTakeProfit(),0);
              if(ch)           
              {
               if(Sound) PlaySound("stops.wav");            
              // one modification in a tick
              return;
              }
           }   
       }
 
       if(OrderType()==OP_SELL)
       {
       profit =OrderOpenPrice()-Ask-tsl*Point();
       if(profit<0) continue;
       if(delitel==0)
           {sl =NormalizeDouble(OrderOpenPrice()-offset*Point(),Digits());}
       else
           {sl =NormalizeDouble(OrderOpenPrice()-profit/delitel-offset*Point(),Digits());}
       if(sl<OrderStopLoss()||OrderStopLoss()==0)
           {
           ch =OrderModify(OrderTicket(),OrderOpenPrice(),sl,OrderTakeProfit(),0);
              if(ch)           
              {
               if(Sound) PlaySound("stops.wav");            
              // one modification in a tick
              return;
              }           
           }  
       }
      
   }
    
}

Comments