Buy and Sell BTC
Level: Basic
Prerequisites: Local development environment
If you are building a bitcoin application, chances are, you want your users to have some bitcoin to be able to use your app. You can use Coinbase API to help your users buy bitcoin. In this short guide you'll learn how to buy and sell bitcoin programmatically. We will set up very simple price thresholds to buy or sell bitcoin automatically.
Payment Methods
First, you need to make sure you have a verified payment method associated
with your Coinbase account.
require 'coinbase/wallet'
client = Coinbase::Wallet::Client.new(api_key: <api key>,
api_secret: <api secret>,
CB_VERSION: 'YYYY-MM-DD')
payment_methods = client.payment_methods
puts payment_methods
from coinbase.wallet.client import Client
client = Client(<api_key>,
<api_secret>,
api_version='YYYY-MM-DD')
payment_methods = client.get_payment_methods()
var Client = require('coinbase').Client;
var client = new Client({
'apiKey': 'API KEY',
'apiSecret': 'API SECRET',
'version':'YYYY-MM-DD'
});
client.getPaymentMethods(function(err, paymentMethods) {
console.log(paymentMethods);
});
Buy and Sell Bitcoin
Now that you've confirmed that you can buy and sell bitcoin, let's go ahead and set up simple price checks that will trigger either a buy or a sell when the price of bitcoin crosses the corresponding threshold:
account = client.primary_account
payment_method = client.payment_methods.first
buy_price_threshold = 200
sell_price_threshold = 500
buy_price = client.buy_price({currency: 'USD'})
sell_price = client.sell_price({currency: 'USD'})
if sell_price.amount.to_f <= sell_price_threshold
sell = account.sell({"amount" => "1",
"currency" => "BTC",
"payment_method" => payment_method.id})
end
if buy_price.amount.to_f <= buy_price_threshold
buy = account.buy({"amount" => "1",
"currency" => "BTC",
"payment_method" => payment_method.id})
end
account = client.get_primary_account()
payment_method = client.get_payment_methods()[0]
buy_price_threshold = 200
sell_price_threshold = 500
buy_price = client.get_buy_price(currency='USD')
sell_price = client.get_sell_price(currency='USD')
if float(sell_price.amount) <= sell_price_threshold:
sell = account.sell(amount='1',
currency="BTC",
payment_method=payment_method.id)
if float(buy_price.amount) <= buy_price_threshold:
buy = account.buy(amount='1',
currency="BTC",
payment_method=payment_method.id)
var buyPriceThreshold = 200;
var sellPriceThreshold = 500;
client.getAccount('primary', function(err, account) {
client.getSellPrice({'currency': 'USD'}, function(err, sellPrice) {
if (parseFloat(sellPrice['amount']) <= sellPriceThreshold) {
account.sell({'amount': '1',
'currency': 'BTC'}, function(err, sell) {
console.log(sell);
});
}
});
client.getBuyPrice({'currency': 'USD'}, function(err, buyPrice) {
if (parseFloat(buyPrice['amount']) <= buyPriceThreshold) {
account.buy({'amount': '1',
'currency': 'BTC'}, function(err, buy) {
console.log(buy);
});
}
});
});
In this guide you learned how to set up very simple price monitoring to automate buying and selling bitcoin. You can now come up with more sophisticated ways to track price changes and automate your trading.
Updated 9 months ago