Last active
February 14, 2022 07:42
-
-
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.
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
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 |
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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Python version: https://gist.github.com/rpkyle/b69a4c70bb35b53c1902587a7c3e52dc
R version: https://gist.github.com/rpkyle/e01e67b1b395291f65b7a584ebe6a93a
Julia version: https://gist.github.com/rpkyle/cedf2b34043362d303ed677f139c8375