Skip to content

Instantly share code, notes, and snippets.

@kMutagene
Last active February 14, 2022 07:42
Show Gist options
  • Save kMutagene/b1af6e4d388dbd04a47a1014ad450593 to your computer and use it in GitHub Desktop.
Save kMutagene/b1af6e4d388dbd04a47a1014ad450593 to your computer and use it in GitHub Desktop.
Dash stonks ticker App to pluck into an asp.netcore app via DashApp.toHttpHandler.
open Dash.NET; open Plotly.NET; open FSharp.Data; open Deedle; open System; open System.IO; open System.Text; open System.Text.RegularExpressions
let req = Http.Request("https://finance.yahoo.com/quote/AMZN/history?p=AMZN",httpMethod=HttpMethod.Get)
let body = match req.Body with | HttpResponseBody.Text b -> b
let crumb = Regex("CrumbStore\":{\"crumb\":\"(?<crumb>.+?)\"}").Match(body).Groups.["crumb"].Value
let cookie = req.Cookies.["B"]
let getDf ticker : Frame<System.DateTime,string> =
let response =
Http.RequestString(
$"https://query1.finance.yahoo.com/v7/finance/download/{ticker}",
query= ["period1","1167609600"; "period2",(string System.DateTime.Now.Ticks); "crumb",crumb],
cookies=["B",cookie]
)
use stream = new MemoryStream(Encoding.UTF8.GetBytes(response))
Frame.ReadCsv(stream,true,separators=",") |> Frame.indexRows "Date"
open Dash.NET.Html; open Dash.NET.DCC; open ComponentPropTypes
let layout =
Html.div [
Attr.children [
Dropdown.dropdown "stock-dropdown" [
Dropdown.Options [
DropdownOption.create "Coke" "COKE" false "Coke"
DropdownOption.create "Tesla" "TSLA" false "Tesla"
DropdownOption.create "Apple" "AAPL" false "Apple"
]
Dropdown.Value (Dropdown.DropdownValue.SingleValue "COKE")
] []
Graph.graph "stock-graph" [] []
]
]
open Dash.NET.Operators
let callback =
Callback.singleOut(
"stock-dropdown" @. Value,
"stock-graph" @. (CustomProperty "figure"),
(fun (ticker:string) ->
"stock-graph" @. (CustomProperty "figure") => (Chart.Line((getDf ticker).["Close"] |> Series.observations)|> GenericChart.toFigure)
),
PreventInitialCall = false
)
let myDashApp =
DashApp.initDefault()
|> DashApp.withLayout layout
|> DashApp.addCallback callback
open Dash.NET
open Plotly.NET
open FSharp.Data
open Deedle
open System
open System.IO
open System.Text
open System.Text.RegularExpressions
let req = Http.Request("https://finance.yahoo.com/quote/AMZN/history?p=AMZN",httpMethod=HttpMethod.Get)
let body = match req.Body with | HttpResponseBody.Text b -> b
let crumb = Regex("CrumbStore\":{\"crumb\":\"(?<crumb>.+?)\"}").Match(body).Groups.["crumb"].Value
let cookie = req.Cookies.["B"]
let getDf ticker : Frame<System.DateTime,string> =
let response =
Http.RequestString(
$"https://query1.finance.yahoo.com/v7/finance/download/{ticker}",
query= [
"period1","1167609600"
"period2",(string System.DateTime.Now.Ticks)
"crumb",crumb
],
cookies=["B",cookie]
)
use stream = new MemoryStream(Encoding.UTF8.GetBytes(response))
Frame.ReadCsv(stream,true,separators=",") |> Frame.indexRows "Date"
open Dash.NET.Html
open Dash.NET.DCC
open ComponentPropTypes
let layout =
Html.div [
Attr.children [
Dropdown.dropdown "stock-dropdown" [
Dropdown.Options [
DropdownOption.create "Coke" "COKE" false "Coke"
DropdownOption.create "Tesla" "TSLA" false "Tesla"
DropdownOption.create "Apple" "AAPL" false "Apple"
]
Dropdown.Value (Dropdown.DropdownValue.SingleValue "COKE")
] []
Graph.graph "stock-graph" [] []
]
]
open Dash.NET.Operators
let callback =
Callback.singleOut(
"stock-dropdown" @. Value,
"stock-graph" @. (CustomProperty "figure"),
(fun (ticker:string) ->
"stock-graph" @. (CustomProperty "figure") => (
Chart.Line(
(getDf ticker).["Close"] |> Series.observations
)
|> GenericChart.toFigure
)
),
PreventInitialCall = false
)
let myDashApp =
DashApp.initDefault()
|> DashApp.withLayout layout
|> DashApp.addCallback callback