Created
December 15, 2013 11:53
-
-
Save anonymous/7972124 to your computer and use it in GitHub Desktop.
Hedging client for Bitstamp
This file contains hidden or 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
var Bitstamp = require("bitstamp"); | |
var id = process.env.BITHEDGE_ID; | |
var key = process.env.BITHEDGE_KEY; | |
var secret = process.env.BITHEDGE_SECRET; | |
var client = new Bitstamp(key, secret, id); | |
var stderr = process.stderr; | |
var argv = process.argv; | |
var op; | |
argv.shift(); | |
argv.shift(); | |
op = argv.shift(); | |
if ("old" == op) { | |
function pick(error, orders) | |
{ | |
if (error) { | |
stderr.write("Failed to get orders\n"); | |
process.exit(1); | |
} | |
if (orders.length > 1) { | |
stderr.write("No obsolete orders\n"); | |
process.exit(1); | |
} | |
if (1 == orders.length) | |
console.log(orders[0].id); | |
} | |
client.open_orders(pick); | |
} else if ("cancel" == op) { | |
var order = argv.shift(); | |
function check(error) | |
{ | |
if (error) { | |
stderr.write("Failed to cancel\n"); | |
process.exit(1); | |
} | |
} | |
if (order) | |
client.cancel_order(order, check); | |
} else if ("up" == op) { | |
var fee = argv.shift(); | |
var usd = argv.shift(); | |
var btc = argv.shift(); | |
var delta, amount, price; | |
fee = parseFloat(fee); | |
usd = parseFloat(usd); | |
btc = parseFloat(btc); | |
delta = Math.ceil(100 / fee) / 100; | |
delta -= 0.01; | |
usd -= 0.01; | |
price = (usd + 2 * delta) / btc; | |
amount = delta / price; | |
price = price.toFixed(2); | |
amount = amount.toFixed(8); | |
client.sell(amount, price, console.log); | |
} else if ("down" == op) { | |
var fee = argv.shift(); | |
var usd = argv.shift(); | |
var btc = argv.shift(); | |
var delta, amount, price; | |
fee = parseFloat(fee); | |
usd = parseFloat(usd); | |
btc = parseFloat(btc); | |
delta = Math.ceil(100 / fee) / 100; | |
delta -= 0.01; | |
usd -= 0.01; | |
price = (usd - 2 * delta) / btc; | |
amount = delta / price; | |
price = price.toFixed(2); | |
amount = amount.toFixed(8); | |
client.buy(amount, price, console.log); | |
} else if ("balance" == op) { | |
function show(error, balance) | |
{ | |
var usd, btc; | |
if (error) { | |
stderr.write("Failed to get balance\n"); | |
process.exit(1); | |
} | |
fee = balance.fee; | |
usd = balance.usd_balance; | |
btc = balance.btc_balance; | |
console.log(fee, usd, btc); | |
} | |
client.balance(show); | |
} else { | |
stderr.write("Unknown operation\n"); | |
process.exit(1); | |
} |
This file contains hidden or 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
HEDGE = node bithedge | |
all: | |
npm install bitstamp | |
hedge: | |
$(HEDGE) old >|old.txt | |
cat old.txt | |
$(HEDGE) cancel `cat old.txt` | |
$(HEDGE) balance >|balance.txt | |
cat balance.txt | |
$(HEDGE) up `cat balance.txt` | |
$(HEDGE) down `cat balance.txt` | |
loop: | |
-while date; do \ | |
$(MAKE) hedge; \ | |
sleep 60; \ | |
done | |
clean: | |
-rm -fr old.txt balance.txt node_modules |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment