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

Wrong implementation of units - "rejectReason":"UNITS_INVALID" #195

Closed trueToastedCode closed 1 year ago

trueToastedCode commented 1 year ago

This error occurs e.g. when trading Bitcoin.

Steps to reproduce

MarketOrderRequest(instrument="BTC_USD", units=0.02,
                   takeProfitOnFill=TakeProfitDetails(price=tp1).data,
                   stopLossOnFill=StopLossDetails(price=sl1).data)

The Error

... "rejectReason":"UNITS_INVALID","instrument":"BTC_USD","units":"0" ... File: types.py

class Units(OAType):
    """representation Units, string value of an integer."""

    def __init__(self, units):
        self._v = "{:d}".format(int(units))

The library formats the price as integer - e.g. 0.02 gets 0. Unfortunately one Bitcoin is currently worth thousands of Dollars ... this doesn't make any sense.

Solution

This library should support decimal points... So I changed self._v = "{:d}".format(int(units)) to self._v = str(units) ... seems to work ok.

hootnot commented 1 year ago

looks like you don't use 0.7.2 ?

>>> from oandapyV20.types.units import Units
>>> Units(0.1).value
'0.1'
>>> Units(0.01).value
'0.01'