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
398 stars 107 forks source link

Getting errors that JSON is not serializable for ClientExtensions, TakeProfitDetails & StopLossOnFill #154

Closed msarm closed 4 years ago

msarm commented 4 years ago

Getting errors that JSON is not serializable for ClientExtensions, TakeProfitDetails & StopLossOnFill

Here is the code used to create LimitOrderRequest:

    takeProfitOnFillOrder = TakeProfitDetails(price=takeProfit)
    stopLossOnFill = StopLossDetails(price=stopLoss)
    clientExtensions= ClientExtensions(clientID="123", clientTag="ABC", clientComment = "new stratagy" )
    client = oandapyV20.API(access_token=apikey, environment=environment)
    ordr = LimitOrderRequest(instrument=instrument, 
                            units=units, 
                            price= openPrice, 
                            takeProfitOnFill=takeProfitOnFillOrder, 
                            stopLossOnFill=stopLossOnFill,
                            clientExtensions=clientExtensions
                            )
    r = orders.OrderCreate(accountID = account, data=ordr.data)
    rv = client.request(r)
    print(r.response)
msarm commented 4 years ago

Finally, I figured out the issue with my code: passed the ClientExtensions, TakeProfitDetails & StopLossOnFill object as object and now converted to JSON string using "data" property.

    takeProfitOnFillOrder = TakeProfitDetails(price=takeProfit)
    stopLossOnFill = StopLossDetails(price=stopLoss)
    clientExtensions= ClientExtensions(clientID="123", clientTag="ABC", clientComment = "new stratagy" )
    client = oandapyV20.API(access_token=apikey, environment=environment)
    ordr = LimitOrderRequest(instrument=instrument, 
                            units=units, 
                            price= openPrice, 
                            takeProfitOnFill=takeProfitOnFillOrder.data, 
                            stopLossOnFill=stopLossOnFill.data,
                            clientExtensions=clientExtensions.data
                            )
    r = orders.OrderCreate(accountID = account, data=ordr.data)
    rv = client.request(r)
    print(r.response)