Created
March 2, 2017 18:54
-
-
Save ratzo/8b8c2f37b4aca5a9a2043e674865613d to your computer and use it in GitHub Desktop.
A bot to buy low and sell high bitcoins through Coinbase
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Based on the code from Martijn Frazer | |
* http://pastebin.com/eZ25L9yw | |
*/ | |
var fs = require('fs'); | |
var Client = require('coinbase').Client; | |
var client = new Client({ | |
'apiKey': '', | |
'apiSecret': '' | |
}); | |
// BTC: Bitcoin Trading Contraption | |
var btc = { | |
config: { | |
verbose: true, | |
sleep: 15, // time in minutes between runs | |
profit: 1.2, // threshold factor to sell | |
buyAmount: 15, // amount of EUR to buy per transaction | |
samples: 500, // size of the sample set | |
btcWallet: { | |
id: '', | |
address: '' | |
}, | |
eurWallet: { | |
id: '', | |
address: '' | |
}, | |
data: { | |
buyRates: 'data/buyrates.json', | |
btc: 'data/btc.json' | |
} | |
}, | |
go: function() { | |
if(btc.config.verbose) { | |
console.log("\r\n\n"+Date()); | |
} | |
// first do some buying | |
btc.buy(); | |
// then do some selling | |
setTimeout(btc.sell, 10 * 1000); | |
setTimeout(btc.go, btc.config.sleep * 60 * 1000); | |
}, | |
buy: function() { | |
client.getBuyPrice({currencyPair: "BTC-EUR"}, function(err, price) { | |
if(!err) { | |
var buyPrice = price.data.amount; | |
if(btc.config.verbose) { | |
console.log('BUY - current price is: '+buyPrice); | |
} | |
fs.readFile(btc.config.data.buyRates, function(err, data) { | |
if(!err) { | |
var buyRates = JSON.parse(data); | |
var low = Infinity; | |
// add current buy price to the list | |
buyRates.push(buyPrice); | |
// take action if we have enough samples | |
if(buyRates.length >= btc.config.samples) { | |
// determine low | |
buyRates.forEach(function(value) { | |
low = Math.min(parseFloat(value), low); | |
}); | |
// price is low, try to buy some bitcoin | |
if(buyPrice <= (low + 0.5) ) { | |
if(btc.config.verbose) { | |
console.log('BUY - current price is LOW!'); | |
} | |
// check euro wallet | |
client.getAccount(btc.config.eurWallet.id, function(err, euro) { | |
if(!err) { | |
// determine amount to buy | |
var amountToBuy = Math.min( | |
btc.config.buyAmount, euro.balance.amount | |
); | |
// buy bitcoins, minimum = 1 euro | |
if(amountToBuy >= 1) { | |
if(btc.config.verbose) { | |
console.log('BUY - placing order for '+amountToBuy+' euro'); | |
} | |
euro.sendMoney({ | |
to: btc.config.btcWallet.address, | |
amount: amountToBuy, | |
currency: "EUR" | |
}, function(err, tx) { | |
if(!err) { | |
// get the buy resource for this transaction | |
euro.getBuy(tx.buy.id, function(err, buy) { | |
if(!err) { | |
// get current btc purchases | |
fs.readFile(btc.config.data.btc, function(err, data) { | |
if(!err) { | |
var btcPurchases = JSON.parse(data); | |
// add this purchase | |
btcPurchases.push({ | |
id: buy.id, | |
timestamp: buy.created_at, | |
eurValue: buy.total.amount, | |
btcValue: buy.amount.amount, | |
rate: buyPrice | |
}); | |
// update purchase file | |
fs.writeFile(btc.config.data.btc, JSON.stringify(btcPurchases)); | |
} | |
}); | |
} | |
}); | |
} | |
}); | |
} | |
} | |
}); | |
} | |
buyRates.shift(); | |
} | |
// store new list of buy rates | |
fs.writeFile(btc.config.data.buyRates, JSON.stringify(buyRates)); | |
} | |
}); | |
} | |
}); | |
}, | |
sell: function() { | |
// get list of purchases | |
fs.readFile(btc.config.data.btc, function(err, data) { | |
if(!err) { | |
var btcPurchases = JSON.parse(data); | |
var tmp = []; | |
if(btcPurchases.length) { | |
// get sell price | |
client.getSellPrice({currencyPair: "BTC-EUR"}, function(err, price) { | |
if(!err) { | |
var sellPrice = price.data.amount; | |
if(btc.config.verbose) { | |
console.log('SELL - current price is: '+sellPrice); | |
} | |
btcPurchases.forEach(function(purchase) { | |
// sell price is higher than desired profit factor --> SELL! | |
if(sellPrice >= (purchase.rate * btc.config.profit)) { | |
client.getAccount(btc.config.btcWallet.id, function(err, bitcoin) { | |
if(!err) { | |
var sellAmount = Math.min(purchase.btcValue, bitcoin.balance.amount); | |
if(sellAmount >= 0.0001) { | |
if(btc.config.verbose) { | |
console.log('SELL - selling '+sellAmount+' btc, bought at: '+purchase.rate); | |
} | |
bitcoin.sendMoney({ | |
to: btc.config.eurWallet.address, | |
amount: sellAmount, | |
currency: "BTC" | |
}, function(err, tx) { | |
if(!err) { | |
if(btc.config.verbose) { | |
console.log( | |
'SELL - profit made: ' | |
+(tx.native_amount.amount - purchase.eurValue) | |
); | |
} | |
} | |
}); | |
} | |
} | |
}); | |
} else { | |
tmp.push(purchase); | |
} | |
}); | |
// store remaining purchases in its file | |
fs.writeFile(btc.config.data.btc, JSON.stringify(tmp)); | |
} | |
}); | |
} | |
} | |
}); | |
} | |
}; | |
btc.go(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment