EA in mt4 to dynamically set tp of all trade to specific target.

 
help me write a script for ea in mql 4

input target = $20;
double input lotSize = 0.01;
double iniTPb = 0; // the price where target is acheived.
double iniTPs = 0; // the price where target is acheived.
double buypl = 0; // sum of all buy trades p/l
double sellpl = 0; // sum of all sell trades p/l
double currentPl = 0;
double pipValue = 0;
double buyLots = 0;
double sellLots = 0;
double futurePrice = 0;


if there is no open trade 

message me to trade wisely.

when i open first new trade 

if trade type is buy
pipValue = MarketInfo(Symbol(), MODE_TICKVALUE) * lotSize

iniTPb = OrderOpenPrice() + (target/pipValue) * Points

set TP as iniTPb.

if trade type is sell
pipValue = MarketInfo(Symbol(), MODE_TICKVALUE) * lotSize

iniTPs = OrderOpenPrice() - (target/pipValue) * Points

set TP as iniTPs.


when i open next new trade // this code should run whenever i open a new trade only

calculate buypl = sum of profit or loss of buy trades  
calculate sellpl = sum of profit or loss of sell trades 
calculate buyLots = sum of lots of buy trades only
calculate sellLots = sum of lots of sell trades only 

calculate currentPl = (buypl) + (sellpl) 

// to find value of futurePrice 

make a loop to increment last orders Open price by adding 0.01 till currentPl's value equals target
// for example- if last orders open price is 2000.00 then is should increment as 2000.01, 2000.02, 2000.03.
by incrementing 0.01 when currentPl's value equals target then that price would be futurePrice.

return futurePrice
// set tp of all open trade

set futurePrice as tp of all open trades.
 

When you post code please use the CODE button (Alt-S)!

Use the CODE button

Thank you.

 
Kadar Khan: help me write a script for ea in mql 4

You have only four choices:

  1. Search for it (CodeBase or Market). Do you expect us to do your research for you?

  2. Try asking at:

  3. MT4: Learn to code it.
    MT5: Begin learning to code it.

    If you don't learn MQL4/5, there is no common language for us to communicate. If we tell you what you need, you can't code it. If we give you the code, you don't know how to integrate it into your code.
              I need HEEEELP, please, it's URGENT...really ! - General - MQL5 programming forum (2017)

  4. Or pay (Freelance) someone to code it. Top of every page is the link Freelance.
              Hiring to write script - General - MQL5 programming forum (2019)

We're not going to code it for you (although it could happen if you are lucky or the problem is interesting.) We are willing to help you when you post your attempt (using CODE button) and state the nature of your problem.
          No free help (2017)

 

I HAVE TRIED BELOW CODE, IT IS NOT GIVING ME RESULT THATS WHY I AKSED HERE.

//+------------------------------------------------------------------+
//|                                                        MyEA.mq4  |
//|                                        |
//+------------------------------------------------------------------+
input double target = 20.0;
input double lotSize = 0.01;
double iniTPb = 0; // the price where target is achieved.
double iniTPs = 0; // the price where target is achieved.
double buypl = 0; // sum of all buy trades p/l
double sellpl = 0; // sum of all sell trades p/l
double currentPl = 0;
double pipValue = 0;
double buyLots = 0;
double sellLots = 0;
double futurePrice = 0;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   EventSetTimer(1); // Set a timer to check trades every second
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   EventKillTimer();
  }
//+------------------------------------------------------------------+
//| Expert timer function                                            |
//+------------------------------------------------------------------+
void OnTimer()
  {
   if(OrdersTotal() == 0)
     {
      Alert("Trade wisely.");
      return;
     }
  }
//+------------------------------------------------------------------+
//| Expert trade function                                            |
//+------------------------------------------------------------------+
void OnTrade()
  {
   // Check if there are open trades
   if(OrdersTotal() > 0)
     {
      // Update P/L and Lots
      buypl = 0;
      sellpl = 0;
      buyLots = 0;
      sellLots = 0;

      for(int i = 0; i < OrdersTotal(); i++)
        {
         if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
           {
            if(OrderType() == OP_BUY)
              {
               buypl += OrderProfit();
               buyLots += OrderLots();
              }
            else if(OrderType() == OP_SELL)
              {
               sellpl += OrderProfit();
               sellLots += OrderLots();
              }
           }
        }
      currentPl = buypl + sellpl;

      // Determine pip value
      pipValue = MarketInfo(Symbol(), MODE_TICKVALUE) * lotSize;

      // Calculate futurePrice for buy trades
      if(OrderType() == OP_BUY)
        {
         futurePrice = OrderOpenPrice();
         while(currentPl < target)
           {
            futurePrice += 0.01 * Point;
            currentPl = buypl + (futurePrice - OrderOpenPrice()) * pipValue;
           }
        }

      // Calculate futurePrice for sell trades
      else if(OrderType() == OP_SELL)
        {
         futurePrice = OrderOpenPrice();
         while(currentPl < target)
           {
            futurePrice -= 0.01 * Point;
            currentPl = sellpl + (OrderOpenPrice() - futurePrice) * pipValue;
           }
        }

      // Set TP for all trades
      for(int j = 0; j < OrdersTotal(); j++)
        {
         if(OrderSelect(j, SELECT_BY_POS, MODE_TRADES))
           {
            if(OrderType() == OP_BUY)
              {
               OrderModify(OrderTicket(), OrderOpenPrice(), 0, futurePrice, 0, clrNONE);
              }
            else if(OrderType() == OP_SELL)
              {
               OrderModify(OrderTicket(), OrderOpenPrice(), 0, futurePrice, 0, clrNONE);
              }
           }
        }
     }
   else
     {
      Alert("Trade wisely.");
     }
  }
//+------------------------------------------------------------------+ 

 
Kadar Khan #:

I HAVE TRIED BELOW CODE, IT IS NOT GIVING ME RESULT THATS WHY I AKSED HERE.


When you post code please use the CODE button (Alt-S)!

Use the CODE button