Petersoj / alpaca-java

A Java API for Alpaca, the commission free, algo friendly, stock trading broker. https://alpaca.markets
https://petersoj.github.io/alpaca-java/
MIT License
203 stars 83 forks source link

net.jacobpeterson.alpaca.rest.endpoint.orders.requestOrder() notional to String with 9 decimal truncation #139

Closed Sonny8Huang closed 8 months ago

Sonny8Huang commented 1 year ago

Hi Peter,

Is it possible to release an update for "net.jacobpeterson.alpaca.rest.endpoint.orders.requestOrder() notional to String with 9 decimal truncation".

In requestOrder(), notional is passed in Double, when converting into Restful call :

   if (quantity != null) {
        jsonBodyBuilder.appendJSONBodyProperty("qty", quantity.toString());
    } else {
        jsonBodyBuilder.appendJSONBodyProperty("notional", notional.toString());
    }

which may has up to 11 decimals in converting to String. In my application, I have passed notional like : msg.order.notional.setScale(2, RoundingMode.DOWN).doubleValue()

However in Alpaca Paper Trading, it has caused issue repeatedly and let to order rejections, since it became something like : 135.69 --> 135.68941440804 (Not sure why same code doesn't cause issue in Alpaca Live Trading environment ?!)

This issue is logged on Alpaca Community Forum : https://forum.alpaca.markets/t/notional-increment-does-not-fulfill-minimum-pricing-criteria/12844/2

It seems this is beyond my control unless overriding requestOrder() myself.

Hope you can help! And appreciate everything you have done and for all your contributions to the community! Thx

Petersoj commented 8 months ago

Should be fixed in 10.0.0. Re-open if not.