Skip to content

Instantly share code, notes, and snippets.

@vkocjancic
Created April 12, 2019 04:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vkocjancic/6e59db0fcf38b00e699f7feb370e317e to your computer and use it in GitHub Desktop.
Save vkocjancic/6e59db0fcf38b00e699f7feb370e317e to your computer and use it in GitHub Desktop.
A way to get OHLC data from Cryptowatch API in scala
import org.json4s._
import org.json4s.native.JsonMethods._
case class MarketCandle(
unixCloseTime: Int,
openPrice: Double,
highPrice: Double,
lowPrice: Double,
closePrice: Double,
change: Double,
volume: Double
)
object CryptowatchData{
implicit val formats = DefaultFormats
def main(args: Array[String]): Unit = {
if (args.length != 3) {
println("Usage:\n\tCryptowatchData <exchange> <market> <period>")
System.exit(1)
}
val exchange = args(0)
val market = args(1)
val period = args(2)
println(s"CryptowatchData started with parameters [$exchange] [$market] [$period]")
var data = getApiData(s"https://api.cryptowat.ch/markets/$exchange/$market/ohlc?periods=$period")
data = data.slice(data.indexOf("[["), data.indexOf("]]") + 2) //remove whatever crap cryptowatch put in front and after value arrays
var candles = List[MarketCandle]()
parse(data).asInstanceOf[JArray].arr.foreach(a => {
candles = MarketCandle(
a(0).extract[Int],
a(1).extract[Double],
a(2).extract[Double],
a(3).extract[Double],
a(4).extract[Double],
a(5).extract[Double],
a(6).extract[Double]) :: candles
})
println(candles)
}
@throws(classOf[java.io.IOException])
def getApiData(url: String) = scala.io.Source.fromURL(url).mkString
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment