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
197 stars 82 forks source link

Limit Order Bad Request #90

Closed Schariss closed 3 years ago

Schariss commented 3 years ago
Order limitOrderTSLA = alpacaAPI.requestNewLimitOrder(
                    "TSLA",
                    100,
                    OrderSide.BUY,
                    OrderTimeInForce.DAY,
                    600.00,
                    false);

- Exception : net.jacobpeterson.alpaca.rest.exception.AlpacaAPIRequestException: Alpaca API Request Exception! : Status Code = 400, Status Text = "Bad Request", API Response Code = 40010000, API Response Message = "request body format is invalid" at net.jacobpeterson.alpaca.AlpacaAPI.requestNewOrder(AlpacaAPI.java:500)

Petersoj commented 3 years ago

What version are you using? That exact code is working for me on version 7.0. What Logger implementation are you using? Can you show me what the debug log outputs?

Schariss commented 3 years ago

First of all, thank you for your fast response. Yes, I am using version 7, and logger SLF4J library from Lombok on a spring boot application. I tried the requestNewOrder, requestNewMarketOrder and a few more methods, they are all working fine with the exception of requestNewLimitOrder.

net.jacobpeterson.alpaca.rest.exception.AlpacaAPIRequestException: Alpaca API Request Exception! : Status Code = 400, Status Text = "Bad Request", API Response Code = 40010000, API Response Message = "request body format is invalid" at net.jacobpeterson.alpaca.AlpacaAPI.requestNewOrder(AlpacaAPI.java:503) at net.jacobpeterson.alpaca.AlpacaAPI.requestNewLimitOrder(AlpacaAPI.java:554) at app.AlpacaApiTestApplication.run(AlpacaApiTestApplication.java:30) at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:804) at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:788) at org.springframework.boot.SpringApplication.run(SpringApplication.java:333) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1311) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1300) at app.AlpacaApiTestApplication.main(AlpacaApiTestApplication.java:21)

Le dim. 7 mars 2021 à 23:21, Jacob Peterson notifications@github.com a écrit :

What version are you using? That exact code is working for me on version 7.0. What Logger implementation are you using? Can you show me what the debug log outputs?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Petersoj/alpaca-java/issues/90#issuecomment-792364208, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALGE5EWUCRWTB2GT3YWOP43TCP36LANCNFSM4YYJ5GQA .

Petersoj commented 3 years ago

Could you try that code snippet on a fresh project just to ensure that the problem is isolated to your Spring boot application? It's still working fine for me. Edit: Also, could you set the log level of your logger implementation to TRACE before you instantiate the AlpacaAPI object. And then give me the output on the console (remove any sensitive information such as your API keys).

Schariss commented 3 years ago

I tried the same code as yours again on a maven project, but the problem persists. The other methods are working fine, I even tried the requestNewOrder method and specified the necessary fields for limit order but it didn't work.

net.jacobpeterson.alpaca.rest.exception.AlpacaAPIRequestException: Alpaca API Request Exception! : Status Code = 400, Status Text = "Bad Request", API Response Code = 40010000, API Response Message = "request body format is invalid"

at net.jacobpeterson.alpaca.AlpacaAPI.requestNewOrder(AlpacaAPI.java:503)

at net.jacobpeterson.alpaca.AlpacaAPI.requestNewLimitOrder( AlpacaAPI.java:554)

at com.adnane.alpaca_test.App.main(App.java:33)

Le lun. 8 mars 2021 à 05:16, Jacob Peterson notifications@github.com a écrit :

Could you try that code snippet on a fresh project just to ensure that the problem is isolated to your Spring boot application? It's still working fine for me. Also, could you add the following to your code before you instantiate the AlpacaAPI object: System.setProperty("org.slf4j.simpleLogger.log.net.jacobpeterson", "trace"); And then give me the output on the console (remove any sensitive information such as your API keys).

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Petersoj/alpaca-java/issues/90#issuecomment-792449481, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALGE5ETKPRFRWVRBLXIY66LTCRFSZANCNFSM4YYJ5GQA .

Petersoj commented 3 years ago

Is this Paper or Live? Can you show me your pom.xml? Could you also make a Zip archive of your maven project and upload it here? Perhaps you could also re-download the artifact from Maven Central by deleting the ~/.m2/repository/net/jacobpeterson/alpaca-java/7.0 directory.

Caceresenzo commented 3 years ago

Hi I have the same problem, and i found what's causing it:

When you are doing a request with a limit price, you are sending:

{
  "symbol": "AAPL",
  "side": "buy",
  "type": "limit",
  "time_in_force": "day",
  "qty": "1.0",
  "limit_price": "0,08",     //  <- culprit
  "extended_hours": true
}

Which is formating the limit_price using: https://github.com/Petersoj/alpaca-java/blob/4e70b6004c92698e694241c24fbf0b7165e6fbd4/src/main/java/net/jacobpeterson/util/format/FormatUtil.java#L28-L30

This is using the coma (,) instead of the dot (.) that Alpaca is expecting.

Here is a minimalist main:

public static void main(String[] args) {
    System.out.println(Locale.getDefault());
    System.out.println(FormatUtil.toCurrencyFormat((Double) 134.5));
}
en_US
134,50

I am pretty sure that this is a kind of locale problem.

Petersoj commented 3 years ago

@Caceresenzo yes I believe you're correct. The NumberFormat instance doesn't specify the EN_US locale explicitly. I will fix this. Thank you!

Caceresenzo commented 3 years ago

Glad to hear that!

I tested with a simple HTTP client, and if you do replace the coma (,) with the dot (.), everything work fine.

Petersoj commented 3 years ago

Fixed in 019c617a0a2ed58c0b9e27a7933698e5b3b58f2e

Caceresenzo commented 3 years ago

When will this be in master?

Petersoj commented 3 years ago

@Caceresenzo I will make a patch release later today.

Petersoj commented 3 years ago

@Caceresenzo 7.1.1 is has been published. Should be accessible from Maven Central in an hour or so.

Caceresenzo commented 3 years ago

@Petersoj many thanks!