官术网_书友最值得收藏!

Real-time data through the Cryptocompare API

For real-time data, the Cryptocompare API is used (https://www.cryptocompare.com/api/#), more specifically HistoMinute (https://www.cryptocompare.com/api/#-api-data-histominute-), which gives us access to OHLC data for the past seven days at most. The details of the API will be discussed in a section devoted to implementation, but the API response is very similar to our historical dataset, and this data is retrieved using a regular HTTP request. For example, a simple JSON response from https://min-api.cryptocompare.com/data/histominute?fsym=BTC&tsym=USD&limit=23&aggregate=1&e=Bitstamp has the following structure:

{
"Response":"Success",
"Type":100,
"Aggregated":false,
"Data":
[{"time":1510774800,"close":7205,"high":7205,"low":7192.67,"open":7198, "volumefrom":81.73,"volumeto":588726.94},
{"time":1510774860,"close":7209.05,"high":7219.91,"low":7205,"open":7205, "volumefrom":16.39,"volumeto":118136.61},
... (other price data)
],
"TimeTo":1510776180,
"TimeFrom":1510774800,
"FirstValueInArray":true,
"ConversionType":{"type":"force_direct","conversionSymbol":""}
}

Through Cryptocompare HistoMinute, we can get open, high, low, close, volumefrom, and volumeto from each minute of historical data. This data is stored for 7 days only; if you need more, use the hourly or daily path. It uses BTC conversion if data is not available because the coin is not being traded in the specified currency:

Figure 6: Open, high, low, close, and volume values through Cryptocompare HistoMinute

Now, the following method fetches the correctly formed URL of the Cryptocompare API (https://www.cryptocompare.com/api/#-api-data-histominute-), which is a fully formed URL with all parameters, such as currency, limit, and aggregation specified. It finally returns the future that will have a response body parsed into the data model, with the price list to be processed at an upper level:

import javax.inject.Inject
import play.api.libs.json.{JsResult, Json}
import scala.concurrent.Future
import play.api.mvc._
import play.api.libs.ws._
import processing.model.CryptoCompareResponse

class RestClient @Inject() (ws: WSClient) {
def getPayload(url : String): Future[JsResult[CryptoCompareResponse]] = {
val request: WSRequest = ws.url(url)
val future = request.get()
implicit val context = play.api.libs.concurrent.Execution.Implicits.defaultContext
future.map {
response => response.json.validate[CryptoCompareResponse]
}
}
}

In the preceding code segment, the CryptoCompareResponse class is the model of API, which takes the following parameters:

  • Response
  • Type
  • Aggregated
  • Data
  • FirstValueInArray
  • TimeTo
  • TimeFrom

Now, it has the following signature:

case class CryptoCompareResponse(Response : String,
Type : Int,
Aggregated : Boolean,
Data : List[OHLC],
FirstValueInArray : Boolean,
TimeTo : Long,
TimeFrom: Long)

object CryptoCompareResponse {
implicit val cryptoCompareResponseReads = Json.reads[CryptoCompareResponse]
}

Again, the preceding two code segments the open-high-low-close (also known as OHLC), are a model class for mapping with CryptoAPI response data array internals. It takes these parameters:

  • Time: Timestamp in seconds, 1508818680, for instance.
  • Open: Open price at a given minute interval.
  • High: Highest price.
  • Low: Lowest price.
  • Close: Price at the closing of the interval.
  • Volumefrom: Trading volume in the from currency. It's BTC in our case.
  • Volumeto: The trading volume in the to currency, USD in our case.
  • Dividing Volumeto by Volumefrom gives us the weighted price of BTC.

Now, it has the following signature:

case class OHLC(time: Long,
open: Double,
high: Double,
low: Double,
close: Double,
volumefrom: Double,
volumeto: Double)

object OHLC {
implicit val implicitOHLCReads = Json.reads[OHLC]
}
主站蜘蛛池模板: 交口县| 宝山区| 离岛区| 五大连池市| 扶余县| 筠连县| 乌苏市| 靖西县| 盐池县| 石家庄市| 同心县| 万全县| 北流市| 宾川县| 会宁县| 永昌县| 曲阳县| 河曲县| 常宁市| 民勤县| 民丰县| 永丰县| 招远市| 和林格尔县| 威宁| 巴中市| 常熟市| 黄梅县| 双牌县| 五原县| 开阳县| 英山县| 四川省| 肥东县| 铜梁县| 宁德市| 宁安市| 关岭| 丹凤县| 沙田区| 眉山市|