hootnot / oanda-api-v20

OANDA REST-V20 API wrapper. Easy access to OANDA's REST v20 API with oandapyV20 package. Checkout the Jupyter notebooks!
MIT License
397 stars 107 forks source link

Couldnot modify Trades' Extensions #137

Closed 5harever closed 5 years ago

5harever commented 5 years ago

As you can see below, I have one trade of EUR_USD.

[IN]

instrument = "EUR_USD"

params = {"instrument" : instrument}
r = trades.TradesList(accountID=accountID, params=params)
client.request(r)

[OUT]

{'lastTransactionID': '191',
 'trades': [{'currentUnits': '300',
   'financing': '-0.0034',
   'id': '160',
   'initialMarginRequired': '3.3922',
   'initialUnits': '300',
   'instrument': 'EUR_USD',
   'marginUsed': '3.3946',
   'openTime': '2018-11-30T18:25:27.729105653Z',
   'price': '1.13079',
   'realizedPL': '0.0000',
   'state': 'OPEN',
   'stopLossOrder': {'createTime': '2018-11-30T18:32:46.972901259Z',
    'guaranteed': False,
    'id': '166',
    'price': '1.10000',
    'state': 'PENDING',
    'timeInForce': 'GTC',
    'tradeID': '160',
    'triggerCondition': 'DEFAULT',
    'type': 'STOP_LOSS'},
   'takeProfitOrder': {'createTime': '2018-11-30T18:32:46.972901259Z',
    'id': '165',
    'price': '1.15000',
    'state': 'PENDING',
    'timeInForce': 'GTC',
    'tradeID': '160',
    'triggerCondition': 'DEFAULT',
    'type': 'TAKE_PROFIT'},
   'unrealizedPL': '0.0750'}]}

Then, I would like to modify above trades' extension with trade id : 160, right?

[IN]

data =  {"clientExtensions" : {
                               "comment": "myComment",
                               "id": "myxxxxx"
                              }
        }

r = trades.TradeClientExtensions(accountID=accountID,tradeID=160,data=data)
client.request(r)

And what I got was:

[OUT]

V20Error: {"tradeClientExtensionsModifyRejectTransaction":{"type":"TRADE_CLIENT_EXTENSIONS_MODIFY_REJECT","rejectReason":"CLIENT_TRADE_ID_ALREADY_EXISTS","tradeID":"160","tradeClientExtensionsModify":{"id":"myxxxxx","comment":"myComment"},"id":"192","accountID":"101-011-8015835-002","userID":8015835,"batchID":"192","requestID":"78532062367077888","time":"2018-12-01T03:05:39.625695065Z"},"relatedTransactionIDs":["192"],"lastTransactionID":"192","errorMessage":"The client Trade ID specifed is already assigned to another open Trade","errorCode":"CLIENT_TRADE_ID_ALREADY_EXISTS"}

It said "CLIENT_TRADE_ID_ALREADY_EXISTS".

I understood it but if I would like to modify some trades' extensions, the trade should be exist, right?

I think I must lose in some point.

Please kindly advise me.

Thank you very much.

hootnot commented 5 years ago

Hi, seen the error message it sounds like you already assigned client extensions with id myxxxx to another trade.

I guess that if you request a listing of your (open) trades, you will find one having the tradeextensions you want to set. Change that data to something unique and try again.

data =  {"clientExtensions" : {
                               "comment": "myComment",
                               "id": "myT160"
                              }
        }

r = trades.TradeClientExtensions(accountID=accountID,tradeID=160,data=data)
client.request(r)

Dec-2

I just checked this on one of my open trades. There is NO issue at all. I bet that if you use:

r = trades.OpenTrade(accountID=accountID)

instead of:

r = trades.TradesList(accountID=accountID, params=params)

you will see another trade having the extension already that you want to set.

This is the kind of question that you can find the answer for yourself. It is not my task to provide free support on coding issues. If you find a bug that is different. So please invest more time yourself next time.