Skip to content

Instantly share code, notes, and snippets.

@caioaao
Last active June 22, 2017 20:06
Show Gist options
  • Select an option

  • Save caioaao/ec04456a910604b7b342f414322ee390 to your computer and use it in GitHub Desktop.

Select an option

Save caioaao/ec04456a910604b7b342f414322ee390 to your computer and use it in GitHub Desktop.
Docs showing how to plot NMR buy prices

First some libraries needed:

library(jsonlite)
library(ggplot2)

Testing connection

resp <- fromJSON("https://cryptoderivatives.market/api/token/NMR/")

Well, it works :) The data is inside trades. We can see it here:

names(resp$trades)
 [1] "type"        "contract"    "name"        "owner"       "asset"
 [6] "symbol"      "tokensFrom"  "tokensTo"    "usedRate"    "buyRate"
[11] "sellRate"    "blockNumber" "timestamp"   "recid"       "tokens"
[16] "ethers"      "txHash"      "txFee"

We can now create a function that loads the data and extract the buys events.

getBuys <- function() {
    resp <- fromJSON("https://cryptoderivatives.market/api/token/NMR/");
    resp$trades[resp$trades$type == "Taker Bought Asset", ]
}

Now let's plot it :)

ggplot(getBuys(), aes(x=timestamp, y=sellRate)) + geom_line() + labs(x="Timestamp", y="ETH/NMR")

img

Cool, but lets try doing a boxplot. We'll create a function so we can experiment with the timestamp.

prepareBoxplotData <- function(buys, tsWindow) {
    buys$truncTimestamp <- buys$timestamp %/% tsWindow;
    buys
}

buysBoxplot <- function(tsWindow) {
    buys <- prepareBoxplotData(getBuys(), tsWindow)
    ggplot(buys, aes(x=timestamp, y=as.numeric(sellRate), group=truncTimestamp)) + labs(x="Timestamp", y="ETH/NMR") + geom_boxplot()
}

Now let's try it with a one-hour window.

buysBoxplot(1*60*60)

img

Uh… To scattered. Let's try 4 hours:

buysBoxplot(4*60*60)

img

Not that informative either… We should probably stick with the line plot.

First some libraries needed:

library(jsonlite)
library(ggplot2)

Testing connection

resp <- fromJSON("https://cryptoderivatives.market/api/token/NMR/")

Well, it works :) The data is inside trades. We can see it here:

names(resp$trades)

We can now create a function that loads the data and extract the buys events.

getBuys <- function() {
    resp <- fromJSON("https://cryptoderivatives.market/api/token/NMR/");
    resp$trades[resp$trades$type == "Taker Bought Asset", ]
}

Now let’s plot it :)

ggplot(getBuys(), aes(x=timestamp, y=sellRate)) + geom_line() + labs(x="Timestamp", y="ETH/NMR")

Cool, but lets try doing a boxplot. We’ll create a function so we can experiment with the timestamp.

prepareBoxplotData <- function(buys, tsWindow) {
    buys$truncTimestamp <- buys$timestamp %/% tsWindow;
    buys
}

buysBoxplot <- function(tsWindow) {
    buys <- prepareBoxplotData(getBuys(), tsWindow)
    ggplot(buys, aes(x=timestamp, y=as.numeric(sellRate), group=truncTimestamp)) + labs(x="Timestamp", y="ETH/NMR") + geom_boxplot()
}

Now let’s try it with a one-hour window.

buysBoxplot(1*60*60)

Uh… To scattered. Let’s try 4 hours:

buysBoxplot(4*60*60)

Not that informative either… We should probably stick with the line plot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment