Last active
July 20, 2018 15:17
-
-
Save fsboehme/e71e83e741f51f36de6ee9aa91b5c414 to your computer and use it in GitHub Desktop.
Simple trade notification for Cryptopia.
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
// This script will monitor your trades on Cryptopia. | |
// You'll get alerted (in Terminal and via voice prompt) when a new trade is executed. | |
// Save locally, add your api key, and run with "node <script name>" in Terminal. | |
// If you don't have node installed | |
// - on Mac (assuming Homebrew installed): brew install node | |
// - on Windows: http://blog.teamtreehouse.com/install-node-js-npm-windows | |
// Install other packages used: | |
// npm i requirejs cryptopia-api say dateformat | |
const Cryptopia = require('cryptopia-api')(); | |
const options = { | |
API_KEY: 'your_api_key', | |
API_SECRET: 'your_secret_key', | |
HOST_URL: 'https://www.cryptopia.co.nz/api' | |
}; | |
Cryptopia.setOptions(options); | |
const say = require('say'); | |
const df = require('dateformat'); | |
var tradeHistory0 = []; | |
var frequency = 60*1000; | |
var openOrders = async function () { | |
try { | |
var response = await Cryptopia.getTradeHistory({Count: 3}); | |
var tradeHistory = response['Data']; | |
// console.log(tradeHistory); //Array of available Trade History for Market from API | |
console.log(df(new Date(), 'HH:MM:ss.l')); | |
var diff = tradeHistory.diff(tradeHistory0); | |
if (diff.length > 0) { | |
say.speak('crib toepia trade executed'); | |
console.log(diff); | |
tradeHistory0 = tradeHistory | |
} | |
} catch (err) { | |
console.error(err); | |
} | |
// call itself again with frequency delay | |
setTimeout(openOrders, frequency); | |
} | |
openOrders(); | |
// util function to get diff between 2 arrays | |
Array.prototype.diff = function(otherArray) { | |
function comparer(otherArray){ | |
return function(current){ | |
return otherArray.filter(function(other){ | |
return other.TradeId == current.TradeId | |
}).length == 0; | |
} | |
} | |
return this.filter(comparer(otherArray)); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment