Problem! I can't modify position.

Cao Wang  

I tried to modify the loss. But I got error code 10016.

How to call PositionModify correctly?

Thanks.

CTrade ExtTrade;

void ModifyPorfit()

  {

   double p = PositionGetDouble(POSITION_PROFIT);
   if(p < 0)
      return;

   double volume = PositionGetDouble(POSITION_VOLUME);
   double points = p / volume ;

   if(points > 100)
     {
      double sl = PositionGetDouble(POSITION_PRICE_OPEN);
      double tp = PositionGetDouble(POSITION_TP);
      if(!ExtTrade.PositionModify(PositionGetTicket(0),sl,tp))
        {
         int code = ExtTrade.ResultRetcode();
         printf("Modify failed:"+code);
        }
     }
  }


Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Position Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Position Properties
  • www.mql5.com
Position Properties - Trade Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Vladimir Karputov  

1. Please insert the code correctly: when editing a message, press the button  Codeand paste your code into the pop-up window. (The first time I corrected your message)

2. To access the properties of a position, this position must first be selected (for example, using  PositionGetTicket )

Documentation on MQL5: Trade Functions / PositionGetTicket
Documentation on MQL5: Trade Functions / PositionGetTicket
  • www.mql5.com
PositionGetTicket - Trade Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Khuman Bakhramirad  

i added an input for the index of the position you want to modify

void ModifyPorfit(int index)

  {
   if(!PositionSelectByTicket(PositionGetTicket(index)))
      return;
   double p = PositionGetDouble(POSITION_PROFIT);
   if(p < 0)
      return;
   double volume = PositionGetDouble(POSITION_VOLUME);
   double points = p / volume ;
   if(points > 100)
     {
      double sl = PositionGetDouble(POSITION_PRICE_OPEN);
      double tp = PositionGetDouble(POSITION_TP);
      if(!ExtTrade.PositionModify(PositionGetTicket(0), sl, tp))
        {
         int code = ExtTrade.ResultRetcode();
         printf("Modify failed:" + code);
        }
     }
  }
Cao Wang  

Wonderful! It works. Thanks a lot. 

Khuman Bakhramirad #:

i added an input for the index of the position you want to modify

Mr Anucha Maneeyotin  
Hi brianinchrist


Example code

#include <Trade\PositionInfo.mqh>

CPositionInfo        eaposition;


 if(eaposition.SelectByIndex(i))

{

// Get Value here

}

brianinchrist
brianinchrist
  • 2021.09.06
  • www.mql5.com
Trader's profile