PositionModify Issues - Please help.

 

Hello, 

This is for MQL5.

I've made an EA and everything is working but my Position Modify, its all the right values but it simply doesn't go through. 

void CheckTradeAndMoveBreakEven()
   {
      bool selected=PositionSelect(_Symbol);
                 if(selected) // if the position is selected
                 {
                    long pos_id            =PositionGetInteger(POSITION_IDENTIFIER);
                    double pricess           =PositionGetDouble(POSITION_PRICE_OPEN);
                    ENUM_POSITION_TYPE type=(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
                    long pos_magic         =PositionGetInteger(POSITION_MAGIC);
                    string comment         =PositionGetString(POSITION_COMMENT);
                    double takeprofit = PositionGetDouble(POSITION_TP);
                    Print("SL PRICE:" + pricess + " : Take Profit Price : " + takeprofit);
                    
                 if(!trade.PositionModify(1234567, pricess, takeprofit))
                 {
                 //--- failure message
                 Print("OrderModify() method failed. Return code=",trade.ResultRetcode(),". Code description: ",trade.ResultRetcodeDescription());
                 }
                 else
                 {
                 Print("OrderModify() method executed successfully. Return code=",trade.ResultRetcode()," (",trade.ResultRetcodeDescription(),")");
                 }
           }
   }

Here's some pictures to show that the values are correct.

Info


I'm simply trying to breakeven my trade when an area on the chart is hit -> when the area is hit i call the function 

CheckTradeAndMoveBreakEven()

But it gives me the errorcode above. "method failed" but still gives me a success code? - the Symbol Market Watch Stop loss is at = 0 so i should be able to do whatever with the stoploss, but it doesn't let me do anything.

- I also tried using OrderModify, exact same result.


Some help would be really appreciated, i searched the forum for same error but didnt find anything really.

 

Did you lookup the meaning of code 10009? —

Documentation on MQL5: Constants, Enumerations and Structures / Codes of Errors and Warnings / Trade Server Return Codes

10009

TRADE_RETCODE_DONE

Request completed

Documentation on MQL5: Constants, Enumerations and Structures / Codes of Errors and Warnings / Trade Server Return Codes
Documentation on MQL5: Constants, Enumerations and Structures / Codes of Errors and Warnings / Trade Server Return Codes
  • www.mql5.com
Trade Server Return Codes - Codes of Errors and Warnings - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Fernando Carreiro #:

Did you lookup the meaning of code 10009? —

Documentation on MQL5: Constants, Enumerations and Structures / Codes of Errors and Warnings / Trade Server Return Codes

10009

TRADE_RETCODE_DONE

Request completed


Well "Request Completed" but it doesn't move the stop, if you look at the images i sent, nothing happens.

 
kramrow #: Well "Request Completed" but it doesn't move the stop, if you look at the images i sent, nothing happens.
You have shown the output for the Experts log, but what is printed in the Journal log for the transaction?
 
kramrow:
bool selected=PositionSelect(_Symbol);

By the way, is this a "netting" account or a "hedging" account?

I hope it is a "netting" account, because if it is for a "hedging" account, then using this method of selecting a position is not ideal.

Note

For the "netting" interpretation of positions (ACCOUNT_MARGIN_MODE_RETAIL_NETTING and ACCOUNT_MARGIN_MODE_EXCHANGE), only one position can exist for a symbol at any moment of time. This position is a result of one or more deals. Do not confuse positions with valid pending orders, which are also displayed on the Trading tab of the Toolbox window.

If individual positions are allowed (ACCOUNT_MARGIN_MODE_RETAIL_HEDGING), multiple positions can be open for one symbol. In this case, PositionSelect will select a position with the lowest ticket.

 
Fernando Carreiro #:

By the way, is this a "netting" account or a "hedging" account?

I hope it is a "netting" account, because if it is for a "hedging" account, then using this method of selecting a position is not ideal.

Note

For the "netting" interpretation of positions (ACCOUNT_MARGIN_MODE_RETAIL_NETTING and ACCOUNT_MARGIN_MODE_EXCHANGE), only one position can exist for a symbol at any moment of time. This position is a result of one or more deals. Do not confuse positions with valid pending orders, which are also displayed on the Trading tab of the Toolbox window.

If individual positions are allowed (ACCOUNT_MARGIN_MODE_RETAIL_HEDGING), multiple positions can be open for one symbol. In this case, PositionSelect will select a position with the lowest ticket.

Its Netting, and its the Journal log in the strategy tester.

- All in all it gives no further information, but when i breakpoint the function i got like 10 decimals after my SL, which should only be two i guess?

Tried rounding it 10 different ways it always gives the same 10 decimals.

 

Something is wrong here ...

if(!trade.PositionModify(1234567, pricess, takeprofit))

What is 1234567 supposed to be?

You should be using the position's ticket ...

ulong pos_ticket = PositionGetInteger( POSITION_TICKET );
if( !trade.PositionModify( pos_ticket, pricess, takeprofit ) )
 
Fernando Carreiro #:

Something is wrong here ...

What is 1234567 supposed to be?

You should be using the position's ticket ...

Because there's only ever one trade going at a time, i give it a forced ticket/Magic number, this has nothing to do with the issue, from my understanding.

- Already tried the solution you just gave but with no result, since its just giving the exact same output a generated ticket thats getting called in the PositionModify, exact same as my "1234567".

The ticket is always 1234567.

      if(Position_Count == 1){
         if(Bid >= oneup){
            CheckTradeAndMoveBreakEven();       
         }
        }

This is the code running the function above.

Basicly just sees if bid goes over a certain price, if it does it runs the function - which works because the breakpoints in the function gets hit when the condition occurs

 
I fixed it by using MathRound, now it breaks even.
 
kramrow #:

Because there's only ever one trade going at a time, i give it a forced ticket/Magic number, this has nothing to do with the issue, from my understanding.

- Already tried the solution you just gave but with no result, since its just giving the exact same output a generated ticket thats getting called in the PositionModify, exact same as my "1234567".

The ticket is always 1234567.

This is the code running the function above.

Basicly just sees if bid goes over a certain price, if it does it runs the function - which works because the breakpoints in the function gets hit when the condition occurs

You can't force a ticket number. The ticket number is generated by the trade server, not you nor your code.

A magic number is not a ticket number. They are two different things.

Use the proper ticket number as I have shown.

Also, make sure to check the Stops Level and Freeze Level before moving stops — The checks a trading robot must pass before publication in the Market

The checks a trading robot must pass before publication in the Market
The checks a trading robot must pass before publication in the Market
  • www.mql5.com
Before any product is published in the Market, it must undergo compulsory preliminary checks in order to ensure a uniform quality standard. This article considers the most frequent errors made by developers in their technical indicators and trading robots. An also shows how to self-test a product before sending it to the Market.