Modify Take profit for all positions with a specific magic index

To add comments, please log in or register
Onailuig80
48
Onailuig80  

Hi All,

i would need an help in the program i'm developing.

I have an ea that open a position, buy, with a specified magic index (example id:111)

After some pips if it is triggered the strategy, it opens a new position always buy and always id:111... and so on anytime the strategy is triggered.

Anytime a new position is opened i would need to recalculate for all the buy position with id:111 the take profit, made by this formula : (OpenPriceOrder1*Lotsize1+OpenpriceOrder2*Lotsize2+...+OpenpriceOrderX*LotSizeX)/(Lotsize1+Lotsize2+....+LotsizeX)

Thank you to all will help me!!!!!

Marco vd Heijden
Moderator
12714
Marco vd Heijden  
How about just adding up OrderProfit() ?
Onailuig80
48
Onailuig80  
Thanks for the answer! Can you explain me with a code example? Many many thanks... 
Mehmet Bastem
1672
Mehmet Bastem  
Onailuig80:

Hi All,

i would need an help in the program i'm developing.

I have an ea that open a position, buy, with a specified magic index (example id:111)

After some pips if it is triggered the strategy, it opens a new position always buy and always id:111... and so on anytime the strategy is triggered.

Anytime a new position is opened i would need to recalculate for all the buy position with id:111 the take profit, made by this formula : (OpenPriceOrder1*Lotsize1+OpenpriceOrder2*Lotsize2+...+OpenpriceOrderX*LotSizeX)/(Lotsize1+Lotsize2+....+LotsizeX)

Thank you to all will help me!!!!!

 example,

//+------------------------------------------------------------------+
//|                                     HaskayafxTakeProfitEAV01.mq4 |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
input int MagicNumber=1111;
double TakeProfirLevel=0.0;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   TakeProfitUtil(MagicNumber);
   TakeProfitUpdate(MagicNumber,TakeProfirLevel);
  }
//+------------------------------------------------------------------+

int TakeProfitUtil(int GlnMagicNumber)
{
  int total = OrdersTotal();
  TakeProfirLevel=0.0;
  bool tck;
  for(int i=total-1;i>=0;i--)
  {
   tck= OrderSelect(i, SELECT_BY_POS);
    int type   = OrderType();
   
    bool result = false;
    
    if(OrderMagicNumber()==GlnMagicNumber)
    {
     TakeProfirLevel=TakeProfirLevel+(OrderOpenPrice()*OrderLots());
    
    }
    
  }
  return(0);
}

int TakeProfitUpdate(int GlnMagicNumber,double TPlevel)
{
  int total = OrdersTotal();
  TakeProfirLevel=0.0;
  bool tck;
  double TP=0;
  for(int i=total-1;i>=0;i--)
  {
   tck= OrderSelect(i, SELECT_BY_POS);
    int type   = OrderType();
   
    bool result = false;
    double Dj=MarketInfo(OrderSymbol(),MODE_DIGITS);
    TP=NormalizeDouble(TPlevel,Dj);
    if(OrderMagicNumber()==GlnMagicNumber)  tck=OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),TP,0,CLR_NONE);
 
    
    
  }
  return(0);
}
Onailuig80
48
Onailuig80  
Mehmet Bastem:

 example,

Now i will try and update the program. If it works, i'll be greatfull ever to you!!!
Onailuig80
48
Onailuig80  

It works! Really wonderfull! Just a last help if you can :

The formula TakeProfirLevel=TakeProfirLevel+(OrderOpenPrice()*OrderLots());    should be implemented before update the take profits dividing the TotalOpenLots for the MagicIndex considered :-)  I don't know how to calculate the TotalOpenLots...

If you could implement it and let me see where update the mql i have finished! Thanks

Mehmet Bastem
1672
Mehmet Bastem  
Onailuig80:

It works! Really wonderfull! Just a last help if you can :

The formula TakeProfirLevel=TakeProfirLevel+(OrderOpenPrice()*OrderLots());    should be implemented before update the take profits dividing the TotalOpenLots for the MagicIndex considered :-)  I don't know how to calculate the TotalOpenLots...

If you could implement it and let me see where update the mql i have finished! Thanks

ok.

//+------------------------------------------------------------------+
//|                                     HaskayafxTakeProfitEAV01.mq4 |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
input int MagicNumber=1111;
double TakeProfirLevel=0.0;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   TakeProfitUtil(MagicNumber);
   TakeProfitUpdate(MagicNumber,TakeProfirLevel);
  }
//+------------------------------------------------------------------+

int TakeProfitUtil(int GlnMagicNumber)
{
  int total = OrdersTotal();
  TakeProfirLevel=0.0;
  
  double TotalLots=0.0;
  bool tck;
  for(int i=total-1;i>=0;i--)
  {
   tck= OrderSelect(i, SELECT_BY_POS);
    int type   = OrderType();
   
    bool result = false;
    
    if(OrderMagicNumber()==GlnMagicNumber)
    {
     TakeProfirLevel=TakeProfirLevel+(OrderOpenPrice()*OrderLots());
     TotalLots=TotalLots+OrderLots();
    
    
    }
    
  }
  TakeProfirLevel=NormalizeDouble((TakeProfirLevel/TotalLots),Digits);
  return(0);
}

int TakeProfitUpdate(int GlnMagicNumber,double TPlevel)
{
  int total = OrdersTotal();
  TakeProfirLevel=0.0;
  bool tck;
  double TP=0;
  for(int i=total-1;i>=0;i--)
  {
   tck= OrderSelect(i, SELECT_BY_POS);
    int type   = OrderType();
   
    bool result = false;
    double Dj=MarketInfo(OrderSymbol(),MODE_DIGITS);
    TP=NormalizeDouble(TPlevel,Dj);
    if(OrderMagicNumber()==GlnMagicNumber)  tck=OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),TP,0,CLR_NONE);
 
    
    
  }
  return(0);
}
Onailuig80
48
Onailuig80  
Mehmet Bastem:

ok.

Super thank you, you helped me and it works exactly as i want. Thank you, god bless you.
To add comments, please log in or register