Created
April 12, 2019 04:47
-
-
Save vkocjancic/6e59db0fcf38b00e699f7feb370e317e to your computer and use it in GitHub Desktop.
A way to get OHLC data from Cryptowatch API in scala
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
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