如何通过R下Binance订单

时间:2021-01-10 22:52:29

标签: r binance

我正在尝试通过 R studio 下一个新订单,打包 Binancer,我正确连接到 Binance Api(我在 Binancer::credentials 中添加了密钥和秘密)但是当我输入时:

binancer::binance_new_order(symbol="WAVESBTC",side="BUY",type="MARKET",quantity = 1)

我收到此错误:binancer 中的错误::binance_new_order(symbol = "WAVESBTC", side = "BUY", : abs(quot - round(quot)) < 1e-10 不正确

我尝试更改数量并输入我想购买的波数或我想花费的 BTC 数量,但我总是收到此错误。我想按照这个顺序在 Waves 上花费 100% 的比特币,我哪里错了?有没有其他方式可以通过R在Binance下订单?

2 个答案:

答案 0 :(得分:1)

您使用 binanceR 下订单的方式存在一些问题。与 Binance 本身不同,您无法指定要在加密货币上花费的钱包百分比,而必须计算加密货币的确切金额。这非常简单,如下所示:

#Fetching the current price of crypto of interest
curr_price <- binance_ticker_price("WAVEBTC")
curr_price <- curr_price$price
#Fetch your wallet balances
avail_btc <- binance_balances()
avail_btc <- filter(avail_btc, asset == "BTC")
avail_btc <- as.numeric(avail_usdt$free)
#Calculate possible coin quantity
buy_quantity<- avail_btc/curr_price

在这种情况下,我们使用了所有可用的钱包加密货币 (100%+/-) 来计算我们想要购买的加密货币数量,即数量。您可以通过除以所需的部分(即钱包 avail_btc/2 的 50%)来选择更少的钱包。

#Reduce quantity of coins by 0.5% to avoid insufficient balance error NBNB!!
calc_price <- function(price, growth) {
  new_price <- (price*growth)+price
  return(new_price)
}
buy_quantity <- calc_price(buy_quantity, -0.005)

有很多方法可以压缩这个过程,但在我看来,分步进行更适合进行故障排除。

现在我们已经确定了正确的数量,我们可以下订单了:

binance_new_order("WAVEBTC", 
    side = "BUY", 
    type = "LIMIT", 
    quantity = buy_quantity, 
    price = b_price, 
    time_in_force = "GTC", 
    test = FALSE) 

但是我们得到了非常烦人的错误:Error in binance_new_order("WAVEBTC", side = "BUY", type = "LIMIT", quantity = buy_quant, : abs(quot - round(quot)) < 1e-10 is not TRUE

发生这种情况是因为所有代币对您要传递给 Binance 的数量或价格都有限制。我不确定,但币安会为您解决这个问题,这就是为什么您从未在那里遇到过这种情况。例如:WAVEBTC 将接受 3.2234 的数量但不接受 3.223456,您会看到额外的两位小数...您可以手动尝试猜测数量或价格的正确格式,但对于自动化来说是一种痛苦。

解决方案很简单,但并不明显。我必须转到 binanceR 的源代码并找到生成错误的部分。这里分别是数量和价格:

#Price
quot <- (price - filters[filterType == 'PRICE_FILTER', minPrice]) / filters[filterType == 'PRICE_FILTER', tickSize]
            stopifnot(abs(quot - round(quot)) < 1e-10)

#Quantity
quot <- (buy_quantity - filters[filterType == 'LOT_SIZE', minQty]) / filters[filterType == 'LOT_SIZE', stepSize]
    stopifnot(abs(quot - round(quot)) < 1e-10)

这些代码基本上会检查您的数量或价格是否有任何不需要的小数。如果我们在使用“ONTUSDT”的示例上运行此程序,您将看到,如果我们使用 buy_quantity332.44543 并计算 quot,我们会得到 33243.54 的值。如果我们运行 stopifnot(abs(quot - round(quot)) < 1e-10) 我们会得到错误。但为什么? abs(quot - round(quot): 0.457 的值是什么,它不符合错误代码标准,这就是我们得到错误的原因。如果我们将 buy_quantity 更改为 332.44,我们不会收到错误消息,因为我们的 abs(quot - round(quot) 值为 0。这是解决问题的关键!

我们需要做的是确定我们所讨论的硬币的“正确”小数位数是多少。为此,我们将使用一个简单的函数来计算数字和/或字符的小数位(你会明白为什么):

decimalplaces <- function(x) {
  if (class(x)=="character") {
    x<-gsub("(.*)(\\.)|([0]*$)","",x)
    nchar(x)
  } else if (abs(x - round(x)) > .Machine$double.eps^0.5) {
    nchar(strsplit(sub('0+$', '', as.character(x)), ".", fixed = TRUE)[[1]][[2]])
  } else {
    return(0)
  }
}

接下来,我们需要获取这些代码段使用的过滤器,以确定我们的数量或价格是否正确:

filters <- binance_filters("WAVEBTC")

现在我们需要重新格式化 quot 的方程,以便我们计算未知量,看起来像这样(记住我们想要一个 quot 的值 0):

quantity <- (quot*filters[filterType == 'LOT_SIZE', stepSize])+filters[filterType == 'LOT_SIZE', minQty])
#Remember to change quot to 0
quantity <- (0*filters[filterType == 'LOT_SIZE', stepSize])+filters[filterType == 'LOT_SIZE', minQty])
#Since anything * by 0 = 0, our final equation is: 
quantity <- filters[filterType == 'LOT_SIZE', minQty]

再次以“ONTUSDT”为例,quantity 值为 0.01。这是您的 buy_quantity 需要的小数位数。很简单:

#Sometimes the value will be in scientific notation, so we convert it which creates a character string and this is why the decimalplaces function accommodates numbers and character strings.  
t <- format(quantity, scientific = FALSE)
dec <- decimalplaces(t)
buy_quantity<- round(buy_quantity, digits = dec)

注意!对于您的价格,请遵循相同的流程!

如果我们在错误代码中使用 buy_quantity,您将看到我们没有收到错误消息并且下订单有效!

binance_new_order("WAVEBTC", 
                  side = "BUY", 
                  type = "LIMIT", 
                  quantity = buy_quantity, 
                  price = b_price, 
                  time_in_force = "GTC", 
                  test = TRUE)

祝您好运,安全交易!

答案 1 :(得分:0)

在这种情况下,我认为问题出在 type="MARKET" 过滤器中,在大多数情况下 quot 最终值 Inf 所以它不会通过 {{1} } 该类型的条件。