Skip to content

Instantly share code, notes, and snippets.

@donpdonp
Last active October 9, 2015 02:17
Show Gist options
  • Save donpdonp/3422847 to your computer and use it in GitHub Desktop.
Save donpdonp/3422847 to your computer and use it in GitHub Desktop.
neuronbot btcwatch bitcoin price watch
function(msg) {
var override = false;
if(/^btcwatch/.test(msg.message)) {
override = true;
}
if(msg.type == "ticktock" || override ) {
var clockdate = override ? (Date.now()) : (Date.parse(msg.message))
var clock = new Date(clockdate)
/* every 5 minutes */
if(clock.getMinutes() % 5 == 0 || override) {
var hval = db.get('bitcoin_last')
var hdate = db.get('bitcoin_last_date');
var gl_data = http.get('https://data.mtgox.com/api/1/generic/order/lag');
var gox_lag = JSON.parse(gl_data).return;
var data = http.get('https://data.mtgox.com/api/2/BTCUSD/money/ticker');
var report = JSON.parse(data).data;
var tdiff = (clockdate - hdate)/1000;
var bdiff = report.last.value - hval;
var bratio = Math.abs(bdiff) / hval
if (tdiff > 60*60) {
db.set('bitcoin_last', report.last.value);
db.set('bitcoin_last_date', clockdate);
}
var btrigger = 0.20
if(bratio > btrigger || override) {
db.set('bitcoin_last', report.last.value);
db.set('bitcoin_last_date', clockdate);
var rounded = Math.floor(bdiff*1000)/1000
var sign = rounded > 0 ? "+" : ""
var timerounded = Math.floor(tdiff*10/60)/10
var basedate = new Date(hdate)
var lag_msg = gox_lag.lag_secs == 0 ? "no lag." : gox_lag.lag_secs.toFixed(1)+" sec."
var delay = (new Date() - new Date(report.now/1000))/1000
var msg = "BTC/USD changed "+sign+"$"+rounded+" in "+timerounded+
" mins. Mtgox "+report.last.display+
" goxlag is "+lag_msg
var email_msg = "MTGOX BTC "+sign+"$"+rounded+" "+report.last.display+" in "+timerounded+" mins. "+
"lag is "+lag_msg
if(override){
return msg
} else {
bot.say("#pdxtech-btc", msg)
bot.emit({type:'beacon',sender:'btcwatch',message: msg})
}
bot.emit({type:'btcwatch', value: report.last.value, lag: gox_lag, message: "new mtgox price"})
if(!override) {
http.post(db.get('cointhink_mailer'),
JSON.stringify({to:db.get('cointhink_to'),
from:db.get('cointhink_from'),
"subject":email_msg,
"text":"trigger is set at "+(btrigger*100)+"% inside 60 minutes."}))
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment