Skip to content
This repository has been archived by the owner on Feb 23, 2022. It is now read-only.

How to quick buy a specific coin? #19

Open
tannguyeneastagile opened this issue Apr 2, 2018 · 4 comments
Open

How to quick buy a specific coin? #19

tannguyeneastagile opened this issue Apr 2, 2018 · 4 comments

Comments

@tannguyeneastagile
Copy link

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
Copy link
Owner

craysiii commented Apr 2, 2018

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
Copy link
Author

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
Copy link
Owner

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
Copy link
Author

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants