Apply Same Take Profit

Author: Copyright 2020, MetaQuotes Software Corp.
0 Views
0 Downloads
0 Favorites
Apply Same Take Profit
//+------------------------------------------------------------------+
//|                                       Apply Same Take Profit.mq5 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property script_show_inputs
/*
   barabashkakvn Trading engine 3.136
*/
#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
//---
CPositionInfo  m_position;                   // object of CPositionInfo class
CTrade         m_trade;                      // object of CTrade class
//+------------------------------------------------------------------+
//| Enum Pips Or Points                                              |
//+------------------------------------------------------------------+
enum ENUM_PIPS_OR_POINTS
  {
   pips=0,     // Pips (1.00045-1.00055=1 pips)
   points=1,   // Points (1.00045-1.00055=10 points)
  };
//+------------------------------------------------------------------+
//| Enum Positions Type                                              |
//+------------------------------------------------------------------+
enum ENUM_POSITION_TYPE_APPLY
  {
   buy=0,      // BUY positions
   sell=1,     // SELL positions
   buy_sell=2, // BUY and SELL positions
  };
//--- input parameters
input group             "Trading settings"
input ENUM_PIPS_OR_POINTS  InpPipsOrPoints      = pips;     // Pips Or Points:
input ENUM_POSITION_TYPE_APPLY   InpPosType     = buy;      // Apply to:
input uint                 InpTakeProfit        = 46;       // Take Profit
input bool                 InpIgnorCurrentTP    = true;     // Ignore current TP
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   if(InpTakeProfit==0)
      return;
//---
   for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of open positions
      if(m_position.SelectByIndex(i))
        {
         if((m_position.PositionType()==POSITION_TYPE_BUY && InpPosType==sell) ||
            (m_position.PositionType()==POSITION_TYPE_SELL && InpPosType==buy))
            continue;
         if(!InpIgnorCurrentTP && m_position.TakeProfit()!=0.0)
            continue;
         //---
         double m_take_profit = 0.0;      // Take Profit                -> double
         double m_point       = 0.0;
         if(!SymbolInfoDouble(m_position.Symbol(),SYMBOL_POINT,m_point))
            continue;
         if(InpPipsOrPoints==pips) // Pips (1.00045-1.00055=1 pips)
           {
            //--- tuning for 3 or 5 digits
            double m_adjusted_point;      // point value adjusted for 3 or 5 points
            long tmp_long=0;
            if(!SymbolInfoInteger(m_position.Symbol(),SYMBOL_ORDER_MODE,tmp_long))
               continue;
            int m_digits=(int)tmp_long;
            int digits_adjust=1;
            if(m_digits==3 || m_digits==5)
               digits_adjust=10;
            m_adjusted_point=m_point*digits_adjust;
            //---
            m_take_profit              = InpTakeProfit               * m_adjusted_point;
           }
         else // Points (1.00045-1.00055=10 points)
           {
            m_take_profit              = InpTakeProfit               * m_point;
           }
         //---
         MqlTick  tick;
         if(!SymbolInfoTick(m_position.Symbol(),tick))
            continue;
         //---
         double price_current = m_position.PriceCurrent();
         double price_open    = m_position.PriceOpen();
         double stop_loss     = m_position.StopLoss();
         double ask           = tick.ask;
         double bid           = tick.bid;
         //---
         if(m_position.PositionType()==POSITION_TYPE_BUY)
           {
            double tp=ask+m_take_profit;
            if(price_current<tp)
              {
               if(!m_trade.PositionModify(m_position.Ticket(),stop_loss,tp))
                  Print(__FILE__," ",__FUNCTION__,", ERROR: ","Modify BUY ",m_position.Ticket(),
                        " Position -> false. Result Retcode: ",m_trade.ResultRetcode(),
                        ", description of result: ",m_trade.ResultRetcodeDescription());
               continue;
              }
           }
         else
           {
            double tp=bid-m_take_profit;
            if(price_current>tp)
              {
               if(!m_trade.PositionModify(m_position.Ticket(),stop_loss,tp))
                  Print(__FILE__," ",__FUNCTION__,", ERROR: ","Modify SELL ",m_position.Ticket(),
                        " Position -> false. Result Retcode: ",m_trade.ResultRetcode(),
                        ", description of result: ",m_trade.ResultRetcodeDescription());
              }
           }
        }
  }
//+------------------------------------------------------------------+

Comments