Skip to content

Instantly share code, notes, and snippets.

@mlusiak
Created December 11, 2013 01:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mlusiak/7903762 to your computer and use it in GitHub Desktop.
Save mlusiak/7903762 to your computer and use it in GitHub Desktop.
Putting it all together - get data, save to redis, read back, chart
#if INTERACTIVE
#r "bin/Debug/FSharp.Data.dll"
#r "bin/Debug/FSharp.Charting.dll"
#r "bin/Debug/ServiceStack.Common.dll"
#r "bin/Debug/ServiceStack.Interfaces.dll"
#r "bin/Debug/ServiceStack.Text.dll"
#r "bin/Debug/ServiceStack.Redis.dll"
#load "../packages/FSharp.Charting.0.90.5/FSharp.Charting.fsx"
#endif
open System.IO
open System.Net
open System.Threading
open System.Collections.Generic
open System.Drawing
open FSharp.Data
open FSharp.Charting
open ServiceStack.Common
open ServiceStack.Text
open ServiceStack.Redis
let getHttp (url: string) =
let req = System.Net.WebRequest.Create(url)
let resp = req.GetResponse()
let stream = resp.GetResponseStream()
let reader = new StreamReader(stream)
let html = reader.ReadToEnd()
resp.Close()
html
type Bitstamp = JsonProvider<""" {"high": "844.00", "last": "714.33", "timestamp": "1386459953", "bid": "713.10", "volume": "81293.11654151", "low": "542.38", "ask": "713.88"} """>
let currentPrice() =
let tickerJson = getHttp "https://www.bitstamp.net/api/ticker/"
let ticker = Bitstamp.Parse(tickerJson)
(ticker.Timestamp, ticker.Last)
[<CLIMutable>]
type BitstampLast = {Id:int64; Timestamp:int; Value:decimal}
let redis = new RedisClient("localhost")
redis.FlushAll()
let redisBitstamp = redis.As<BitstampLast>()
for i = 1 to 20 do
let cp = currentPrice()
let last = {Id = redisBitstamp.GetNextSequence(); Timestamp = fst cp; Value = snd cp}
let stored = redisBitstamp.Store(last)
printfn "Stored %d %f at %d" stored.Timestamp stored.Value stored.Id
Thread.Sleep(31000)
let allValues = redisBitstamp.GetAll()
[for v in allValues -> (v.Timestamp, v.Value)]
|> Chart.Line
|> Chart.WithYAxis(Min = 900.0, Max = 1000.0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment