craysiii / binance

API Wrapper for the Binance cryptocurrency exchange written in Ruby.
MIT License
97 stars 79 forks source link

How to quick buy a specific coin? #19

Open tannguyeneastagile opened 6 years ago

tannguyeneastagile commented 6 years ago

I would like to buy a coin with my all current btc as fast as possible, I would like to know if we can do it with only 1 API call. The approach I'm using it we request to get the current price first, then calculate the quantity, then create an order based on that. Btw it will takes 2 API calls.

How can I achieve it by using only 1 API call, (the API to get current price will take about > 1sec).

craysiii commented 6 years ago

Hey there.

It seems the only way this could be accomplished is if you use websockets to get pricing updates and then use that last price to make the calculation. Binance doesn't give us a way to just say "buy BTC with 100% of X."

tannguyeneastagile commented 6 years ago

How can I extract data from client.agg_trade when using eventmachine? I would like to update the price of a symbol into my database everytime the client.agg_trade returns a new hash to make my db updated.

craysiii commented 6 years ago

Hey @tannguyen sorry for the late response.

EM.run do
  # Create event handler to update DB
  # e.data should be a string json response, you will need to parse using JSON.parse or similar
  updateAggTradeXRPETH = proc { |e| do_something_with JSON.parse(e)['some_value'] }  

  # Only pass message, and for each message received, called updateAggTradeXRPETH
  aggTradeMethods = { message: updateAggTradeXRPETH }

  # Pass a symbol and event handler Hash to connect and process events
  client.agg_trade symbol: 'XRPETH', methods: aggTradeMethods
end

Let me know if you need me to elaborate any more.

tannguyeneastagile commented 6 years ago

Thanks for your response, one more question: How can I get the average price after my order is filled fully? For example if I use:

client.create_order! symbol: 'XRPETH', side: 'BUY', type: 'LIMIT', 
  time_in_force: 'GTC', quantity: '100.00000000', price: '0.00055000'
# => {"symbol"=>"XRPETH", "orderId"=>918248, "clientOrderId"=>"kmUU0i6cMWzq1NElE6ZTdu", 
# "transactTime"=>1511685028420, "price"=>"0.00055000", "origQty"=>"100.00000000", 
# "executedQty"=>"100.00000000", "status"=>"FILLED", "timeInForce"=>"GTC", "type"=>"LIMIT", 
# "side"=>"BUY"}

It will display 0.00055000 instead of the exactly price.