krijnsent / crypto_vba

An Excel/VBA project to communicate with various cryptocurrency exchanges APIs
MIT License
157 stars 54 forks source link

How to place a buy order to CoinbasePro? #51

Closed LMFigu closed 5 years ago

LMFigu commented 5 years ago

I'm having a hard time to understand how to place "buy" and "sell" orders. Could you guide me on that?

Thanks!

krijnsent commented 5 years ago

Hi @LMFigu

A quick attempt with some code you can copy-paste (not tested):

'From here: https://docs.pro.coinbase.com/#place-a-new-order 'A simple buy order Dim Params As New Dictionary Params.Add "size", 0.01 'this probably works, could be that you need to put the amount like a string: "0.01" Params.Add "price", 0.100 'same here Params.Add "side", "buy" Params.Add "product_id", "BTC-USD" OrderResult = PrivateCoinbasePro("orders", "POST", Cred, Params) Debug.print OrderResult 'Should give back an order ID

'Next, delete all orders CancelResult = PrivateCoinbasePro("orders", "DELETE", Cred) Debug.print CancelResult 'Should give back a list of order_ids that are cancelled

Does that work?

Koen

LMFigu commented 5 years ago

Thanks for the quick reply, unfortunately didn't manage to make it work. Created this code and couldn't find what's wrong. error

LMFigu commented 5 years ago

{"error_nr":400,"error_txt":"HTTP-Bad Request","response_txt":{"message":"Requires product_id"}}

krijnsent commented 5 years ago

Good news, I found it: apparently I completely forgot to build code that would take parameters (like buy price etc) into CoinbasePro... I added it and added an example. Could you test and close this issue if that works for you? Cheers!

LMFigu commented 5 years ago

Sorry, still can't make it work. it returns a Run-time error '424': Object required

krijnsent commented 5 years ago

When/where does that happen? If you run the tests? On what line does it happen?

LMFigu commented 5 years ago

I can't see where in the code this happen because there's no debug option. This is the code i have: Sub Buy()

Dim JsonResponse As String Dim json As Object '-> normally JSON is returned like {}, a dictionary in VBA, '-> some functions return JSON is returned like [], a collection in VBA '-> so to catch both, Dim Json as Object

Dim JsonRes As Dictionary Dim apiKey As String Dim secretKey As String

Set Sht = Worksheets("CoinbasePro")

Set Rng = Sht.Range("C10")

'Put the credentials in a dictionary apiKey = Sht.Range("apikey_coinbase_pro").Value secretKey = Sht.Range("secretkey_coinbase_pro").Value passphrase = Sht.Range("passphrase_coinbase_pro").Value

'Put the credentials in a dictionary Dim Cred As New Dictionary Cred.Add "apiKey", apiKey Cred.Add "secretKey", secretKey Cred.Add "Passphrase", passphrase

Dim Params8 As New Dictionary Params8.Add "size", 0.01 Params8.Add "price", 100.1 Params8.Add "side", "buy" Params8.Add "product_id", "BTC-EUR" TestResult = PrivateCoinbasePro("orders", "POST", Cred, Params8) If InStr(TestResult, "error_txt") > 0 Then 'Error result, assume insufficient funds, but could also be Product not found '{"error_nr":400,"error_txt":"HTTP-Bad Request","response_txt":{"message":"Insufficient funds"}} Test.IsOk InStr(TestResult, "response_txt") > 0 Set JsonResult = JsonConverter.ParseJson(TestResult) Test.IsEqual JsonResult("response_txt")("message"), "Insufficient funds" Else 'Normal result '{"id": "d0c5340b-6d6c-49d9-b567-48c4bfca13d2","price": "100.10000000","size": "0.01000000","product_id": "BTC-EUR","side": "buy","stp": "dc","type": "limit","time_in_force": "GTC","post_only": false,"created_at": "2016-12-08T20:02:28.53864Z","fill_fees": "0.0000000000000000","filled_size": "0.00000000","executed_value": "0.0000000000000000","status": "pending","settled": false} Test.IsOk InStr(TestResult, "created_at") > 0 Set JsonResult = JsonConverter.ParseJson(TestResult) Test.IsOk Len(JsonResult("id")) > 10 Test.IsEqual JsonResult("product_id"), "BTC-EUR" End If

'STEP 4 'Use private call to get personal information JsonResponse = PrivateCoinbasePro("orders", "POST", Cred, Params8) Set json = JsonConverter.ParseJson(JsonResponse)

Rng.Value = JsonResponse

End Sub

LMFigu commented 5 years ago

just realized that the orders were correctly placed, even that the error occurr

krijnsent commented 5 years ago

Hi @LMFigu , you forgot to include the initiation of the tests, so your code crashes at the first Test.IsOK(). You can find out by going step-by-step through the code:

A solution is to remove the Test module in your code, e.g.:

If InStr(TestResult, "error_txt") > 0 Then
    Debug.Print "ERROR " & TestResult
Else
    'Normal result
    Debug.Print "OK " & TestResult
End If

Cheers, Koen

LMFigu commented 5 years ago

it works perfect! Just realized that if its used the passwords from the spreadsheet it works if its used the passwords from the code it doesnt work.

image

LMFigu commented 5 years ago

one more thing how can i get the feedback from the site that the order was placed.

krijnsent commented 5 years ago

Hi there, after the order has been placed, the "return value" is in the variable named JsonResponse.

JsonResponse = PrivateCoinbasePro("orders", "POST", Cred, Params8) The value of JsonResponse is a Text/string and can e.g. be {"id":"just-another-id","price":"10.10" etc...

What you could do is Set json = JsonConverter.ParseJson(JsonResponse) In that way, the JSON text string is transformed into a Dictionary and you could e.g. do:

Sht.Range("A10").Value = JsonResponse  '(Raw returned data)
Sht.Range("B10").Value = json("id") '(just the id of the generated order)
Sht.Range("C10").Value = json("status") 'etc...

Alternatively, you could check the open orders, see the example in the CoinbasePro module. One addition: if your order fails (insufficient funds, another reason), the line Sht.Range("B10").Value = json("id") will fail/crash, so you'd need to check JsonResponse for errors like in the example I added some posts back (and treat error returns differently than OK returns).

LMFigu commented 5 years ago

The JsonResponse returns {"error_nr":400,"error_txt":"HTTP-Bad Request","response_txt":{"message":"Requires product_id"}}