ตั้งค่าการซื้อขายของ Api ให้ create oder แบบมีจุดทศนิยมที่ต่ำกว่าหนึ่งได้
Posted: Sat Apr 11, 2020 10:26 am
ตอนนี้ผมได้ทำการเขียนโค้ดให้สามารถซื้อขายโดยใช้ Binance Api ผมสามารถ create order ได้แล้วแต่ติดที่ว่าผมไม่สามารถตั้ง price หรือ quantity ให้น้อยกว่า 1 ได้ เช่น 0.001
ซึ่งมันจำเป็นมากๆในการซื้อขายอัตราเเลกเปลี่ยน อยากได้วิธีการให้มันสามารถตั้ง price หรือ quantity ให้เป็นทศนิยมที่น้อยกว่า 1 เช่น 0.001,0.0234 ครับ
อันนี้คือที่ติดอยู่ครับตรง price กับ quantity ถ้าเปลี่ยนเป็นทศนิยมน้อยกว่า 1 จะขึ้น error
:Expected "{"error_nr":400,"error_txt":"HTTP-Bad Request","response_txt":{"code":-1100,"msg":"Illegal characters found in parameter 'quantity'; legal range is '^([0-9]{1,20})(\\.[0-9]{1,20})?$'."}}" to equal "{}"
โค้ดทั้งหมดครับ
อันนี้คือที่ติดอยู่ครับตรง price กับ quantity ถ้าเปลี่ยนเป็นทศนิยมน้อยกว่า 1 จะขึ้น error
:Expected "{"error_nr":400,"error_txt":"HTTP-Bad Request","response_txt":{"code":-1100,"msg":"Illegal characters found in parameter 'quantity'; legal range is '^([0-9]{1,20})(\\.[0-9]{1,20})?$'."}}" to equal "{}"
Code: Select all
'Test a test order
Dim Params3 As New Dictionary
Params3.Add "symbol", "BTCUSDT"
Params3.Add "side", "BUY"
Params3.Add "type", "LIMIT"
Params3.Add "price", 6000
Params3.Add "quantity", CDec(0.002)
Params3.Add "timeInForce", "GTC"
Params3.Add "timestamp", GetBinanceTime()
TestResult = PrivateBinance("order", "POST", Cred, Params3)
Test.IsEqual TestResult, "{}"
โค้ดทั้งหมดครับ
Code: Select all
'Remove these 2 lines, unless you define 2 constants somewhere ( Public Const secretkey_btce = "the key to use everywhere" etc )
Apikey = apikey_binance
secretKey = secretkey_binance
'Put the credentials in a dictionary
' Create a new test suite
Dim Suite As New TestSuite
Suite.Description = "ModExchBinance"
' Create reporter and attach it to these specs
Dim Reporter As New ImmediateReporter
Reporter.ListenTo Suite
' Create a new test
Dim Test As TestCase
Set Test = Suite.Test("TestBinancePublic")
'Error, unknown command
TestResult = PublicBinance("AnUnknownCommand", "GET")
Test.IsOk InStr(TestResult, "error") > 0
Set JsonResult = JsonConverter.ParseJson(TestResult)
Test.IsEqual JsonResult("error_nr"), 404
'Error, command without parameters
TestResult = PublicBinance("depth", "GET")
Test.IsOk InStr(TestResult, "error") > 0
Set JsonResult = JsonConverter.ParseJson(TestResult)
Test.IsEqual JsonResult("error_nr"), 400
'OK request
TestResult = PublicBinance("time", "GET")
'{"serverTime":1513605418615}
Test.IsOk InStr(TestResult, "serverTime") > 0
Set JsonResult = JsonConverter.ParseJson(TestResult)
Test.IsOk JsonResult("serverTime") > 1510000000000#
'Put parameters/options in a dictionary
Dim Params As New Dictionary
Params.Add "symbol", "BNBUSDT"
TestResult = PublicBinance("ticker/24hr", "GET", Params)
'{"symbol":"BNBUSDT","priceChange":"-0.00022700","priceChangePercent":"-0.633","weightedAvgPrice":"0.03538261","prevClosePrice":"0.03586800","lastPrice":"0.03564100","lastQty":"0.14000000","bidPrice":"0.03564100","bidQty":"0.22300000","askPrice":"0.03564800","askQty":"0.43200000","openPrice":"0.03586800","highPrice":"0.03600300","lowPrice":"0.03410000","volume":"380396.97600000","quoteVolume":"13459.43958266","openTime":1551288592637,"closeTime":1551374992637,"firstId":109505628,"lastId":109773015,"count":267388}
Test.IsOk InStr(TestResult, "priceChange") > 0
Test.IsOk InStr(TestResult, "closeTime") > 0
Set JsonResult = JsonConverter.ParseJson(TestResult)
Test.IsEqual JsonResult("symbol"), "BNBUSDT"
Test.IsOk JsonResult("lastPrice") > 0
TestResult = GetBinanceTime()
'e.g. 1516565004894
Test.IsOk TestResult > 1510000000000#
'Unix time period:
t1 = DateToUnixTime("1/1/2014")
t2 = DateToUnixTime("1/1/2018")
Set Test = Suite.Test("TestBinancePrivate GET")
'Binance always requires a timestamp parameter, first test without
TestResult = PrivateBinance("account", "GET", Cred)
'{"code":-1102,"msg":"Mandatory parameter 'timestamp' was not sent, was empty/null, or malformed."}
Test.IsOk InStr(TestResult, "code") > 0
Test.IsOk InStr(TestResult, "Mandatory parameter") > 0
Set JsonResult = JsonConverter.ParseJson(TestResult)
Test.IsEqual JsonResult("response_txt")("code"), -1102
'Add timestamp to the parameters and try again
Dim Params2 As New Dictionary
Params2.Add "timestamp", GetBinanceTime()
TestResult = PrivateBinance("account", "GET", Cred, Params2)
'{"makerCommission":10,"takerCommission":10,"buyerCommission":0,"sellerCommission":0,"canTrade":true,"canWithdraw":true,"canDeposit":true,"updateTime":1512476238993,"balances":[{"asset":"BTC","free":"0.00000000","locked":"0.00000000"},{"asset":"LTC","free":"0.00000000","locked":"0.00000000"},{"asset":"ETH","free":"0.00000000","locked":"0.00000000"},{"asset":"NEO","free":"0.00000000","locked":"0.00000000"},{"asset":"BNB","free":"0.00000000","locked":"0.00000000"},{"asset":"QTUM","free":"0.00000000","locked":"0.00000000"},{"asset":"EOS","free":"0.00000000","locked":"0.00000000"},{"asset":"SNT","free":"0.00000000","locked":"0.00000000"},{"asset":"BNT","free":"0.00000000","locked":"0.00000000"},{"asset":"GAS","free":"0.00000000","locked":"0.00000000"},{"asset":"BCC","free":"0.00000000","locked":"0.00000000"},{"asset":"USDT","free":"0.00000000","locked":"0.00000000"},{"asset":"HSR","free":"0.00000000","locked":"0.00000000"},{"asset":"OAX","free":"0.00000000","locked":"0.00000000"},{...
Test.IsOk InStr(TestResult, "takerCommission") > 0
Test.IsOk InStr(TestResult, "locked") > 0
Set JsonResult = JsonConverter.ParseJson(TestResult)
Test.IsOk JsonResult("takerCommission") > 0
Test.IsOk JsonResult("balances").Count > 10
Dim quantity As Double
Set Test = Suite.Test("TestBinancePrivate POST/DELETE")
'Test a test order
Dim Params3 As New Dictionary
Params3.Add "symbol", "BTCUSDT"
Params3.Add "side", "BUY"
Params3.Add "type", "LIMIT"
Params3.Add "price", 6000
Params3.Add "quantity", CDec(0.002)
Params3.Add "timeInForce", "GTC"
Params3.Add "timestamp", GetBinanceTime()
TestResult = PrivateBinance("order", "POST", Cred, Params3)
Test.IsEqual TestResult, "{}"