FREE TRADE MANAGER MT4 EXPERT WITH SOURCE CODES


https://www.earnforex.com/forum/threads/trade-manager-tools-experts.30111/#post-150053

This expert is from ,https://www.profitabletraders.co.uk



/+------------------------------------------------------------------+
//|                                                                 trademanager.mq4 |
//|                        Copyright 2015, MetaQuotes Software Corp. |
//|                                                           https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015,https://www.profitabletraders.co.uk."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
extern int StopLoss=600;
extern int TakeProfit=800;
extern int c_TP_1=40;
extern int c_TP_2=60;
extern int trailingStart    = 12;
extern int TrailingDistance = 20;
extern string CloseTime="19:00";
extern int BreakEven=20;
int slippage=3;
double point;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create timer
   point=1/MathPow(10,(Digits-1));
//  EventSetTimer(60);

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
//  set_tp_sl();
   if (CloseTime!="" && (TimeCurrent()-TimeCurrent()%60)==StrToTime(TimeToStr(TimeCurrent(),TIME_DATE)+"  "+CloseTime))CloseAll();
   if (BreakEven>0)MoveBreakEven();
   if (trailingStart>0) Trailing();
   PartialClose();
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---

  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---

  }
//+------------------------------------------------------------------+
void set_tp_sl()
  {
   double SL,TP;
   int i,Total;
   int Dig=Digits();
   bool res;

//+------------------------------------------------------------------+

   Total=OrdersTotal();
   if(Total>0)
     {
      for(i=Total-1; i>=0; i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
           {
            if(OrderSymbol()==Symbol() && OrderType()==OP_SELL && OrderStopLoss()==0 && OrderTakeProfit()==0)
              {
               if(StopLoss>0)SL=OrderOpenPrice()+StopLoss*Point;else SL=0;
               if(TakeProfit>0)TP=OrderOpenPrice()-TakeProfit*Point;else TP=0;
               res=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(SL,Dig),NormalizeDouble(TP,Dig),OrderExpiration(),CLR_NONE);
               if(!res) Print("Error in OrderModify. Error code=",GetLastError());
              }
            if(OrderSymbol()==Symbol() && OrderType()==OP_BUY && OrderStopLoss()==0 && OrderTakeProfit()==0)
              {
               if(StopLoss>0)SL=OrderOpenPrice()-StopLoss*Point;else SL=0;
               if(TakeProfit>0)TP=OrderOpenPrice()+TakeProfit*Point;else TP=0;
               res=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(SL,Dig),NormalizeDouble(TP,Dig),OrderExpiration(),CLR_NONE);
               if(!res) Print("Error in OrderModify. Error code=",GetLastError());
              }
           }
        }
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CloseAll()
  {
   bool result=false;
   int count=0,total=OrdersTotal();
   for(count=total-1; count>=0; count--)
     {
      if(OrderSelect(count,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol())
           {
            result=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage,CLR_NONE);
           }
        }
     }
  }
//+------------------------------------------------------------------+
void MoveBreakEven()
  {
   bool modify;
   int count=0,total=OrdersTotal();
   for(count=total-1; count>=0; count--)
     {
      bool select=OrderSelect(count,SELECT_BY_POS,MODE_TRADES);
      if(OrderType()==OP_BUY&&Bid>(OrderOpenPrice()+BreakEven*point)&&OrderStopLoss()!=OrderOpenPrice())  modify= OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Red);
      if(OrderType()==OP_SELL&&Ask<(OrderOpenPrice()-BreakEven*point)&&OrderStopLoss()!=OrderOpenPrice())  modify= OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Red);
     }
  }
//+------------------------------------------------------------------+
void Trailing()
  {
   bool mod;
   int count=0,total=OrdersTotal();
   for(count=total-1; count>=0; count--)
     {
      bool select=OrderSelect(count,SELECT_BY_POS,MODE_TRADES);
      if(OrderType()==OP_BUY && Bid>(OrderOpenPrice()+trailingStart*point))
        {
         if(NormalizeDouble(Bid-TrailingDistance*point,Digits)>OrderStopLoss())
           {
            mod=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Bid-TrailingDistance*point,Digits),OrderTakeProfit(),
                            OrderExpiration());
           }
        }
      if(OrderType()==OP_SELL && Ask<(OrderOpenPrice()-trailingStart*point))
        {
         if(NormalizeDouble(Ask+TrailingDistance*point,Digits)<OrderStopLoss()||OrderStopLoss()==0)
           {
            mod=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Ask+TrailingDistance*point,Digits),OrderTakeProfit(),
                            OrderExpiration());
           }
        }
     }
  }
//+------------------------------------------------------------------+
void PartialClose()
{
   bool result=false;
   int count=0,total=OrdersTotal();
   for(count=total-1; count>=0; count--)
     {
      bool select=OrderSelect(count,SELECT_BY_POS,MODE_TRADES);
      if(OrderType()==OP_BUY && Bid>(OrderOpenPrice()+c_TP_1*point)&&StringFind(OrderComment(),"from",0)==-1)
        {
        result = OrderClose(OrderTicket(),NormalizeDouble(OrderLots()/2,2),OrderClosePrice(),slippage);   
        }
           if(OrderType()==OP_BUY && Bid>(OrderOpenPrice()+c_TP_2*point)&&StringFind(OrderComment(),"from",0)!=-1)
        {
        result = OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage);   
        }
      if(OrderType()==OP_SELL && Ask<(OrderOpenPrice()-c_TP_1*point)&&StringFind(OrderComment(),"from",0)==-1)
        {
        result = OrderClose(OrderTicket(),NormalizeDouble(OrderLots()/2,2),OrderClosePrice(),slippage);   
        }
      if(OrderType()==OP_SELL && Ask<(OrderOpenPrice()-c_TP_2*point)&&StringFind(OrderComment(),"from",0)!=-1)
        {
        result = OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage);   
        }
     }
}

Comments

Popular posts from this blog

freelance mql5.com EAS can be hacked and eas stolen = unsafe.

Why do or most intraday robots (EAS) fail on mql5.com site ?