Skip to content

Commit

Permalink
update example docs, move from Plotly.js to Plots.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
eohne committed Aug 8, 2024
1 parent 35d238d commit f5dfd6a
Show file tree
Hide file tree
Showing 8 changed files with 702 additions and 207 deletions.
206 changes: 205 additions & 1 deletion docs/src/DataFramesetc.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,38 @@ using DataFrames
prices = get_prices("AAPL")

DataFrame(prices)
```

### Broadcasting to get one DataFrame (stacked)
```julia
tickers=["AAPL","TSLA","F"]
prices = get_prices.(tickers,startdt="2024-01-01",enddt="2024-08-01") |> (x-> DataFrame.(x)) |> (x->vcat(x...))
```

```julia
438×8 DataFrame
Row │ ticker timestamp open high low close adjclose vol
│ String DateTime Float64 Float64 Float64 Float64 Float64 Float64
─────┼─────────────────────────────────────────────────────────────────────────────────────────
1 │ AAPL 2024-01-02T14:30:00 186.658 187.945 183.407 185.64 185.152 8.2272e7
2 │ AAPL 2024-01-03T14:30:00 183.736 185.392 182.948 184.25 183.766 5.8261e7
3 │ AAPL 2024-01-04T14:30:00 181.671 182.609 180.405 181.91 181.432 7.17945e7
4 │ AAPL 2024-01-05T14:30:00 181.512 182.28 179.697 181.18 180.704 6.21396e7
436 │ F 2024-07-29T13:30:00 11.0085 11.0085 10.6344 11.01 10.8411 9.02616e7
437 │ F 2024-07-30T13:30:00 10.8608 10.9396 10.6147 10.84 10.6737 6.82735e7
438 │ F 2024-07-31T13:30:00 10.6934 10.8411 10.5556 10.82 10.654 7.43204e7
431 rows omitted
```

## TimeArray from TimeSeries

If you use Julia 1.9 or newer you can just use a sink argument in `get_prices` instead.
```julia
using TimeSeries, YFinance
get_prices(TimeArray,"AAPL")
```
(runtime for this is about 80% faster than `yahoo(:INTC)` from MarketData.jl (median run time: 0.09s vs 0.41s) )
(runtime for this is about 80% faster than `yahoo(:INTC)` from MarketData.jl)

or alternatively

Expand Down Expand Up @@ -48,6 +71,117 @@ end
stock_price_to_time_array(prices)
```

### Broadcast and create one TimeArray for the adjclose prices


```julia
tickers = ["AAPL","TSLA","F"]
prices = get_prices.((TimeArray,),tickers)
```

```julia
3-element Vector{TimeArray{Float64, 2, DateTime, Matrix{Float64}}}:
5×6 TimeArray{Float64, 2, DateTime, Matrix{Float64}} 2024-08-01T13:30:00 to 2024-08-07T13:30:00
5×6 TimeArray{Float64, 2, DateTime, Matrix{Float64}} 2024-08-01T13:30:00 to 2024-08-07T13:30:00
5×6 TimeArray{Float64, 2, DateTime, Matrix{Float64}} 2024-08-01T13:30:00 to 2024-08-07T13:30:00
```

Combine into one TimeArray:
```julia
prices=hcat(prices...)
```

```julia
5×18 TimeArray{Float64, 2, DateTime, Matrix{Float64}} 2024-08-01T13:30:00 to 2024-08-07T13:30:00
┌─────────────────────┬────────┬────────┬────────┬────────┬──────────┬───────────┬────────┬────────┬────────┬─────────┬────────────┬───────────┬─────────┬─────────┬─────────┬─────────
│ │ open │ high │ low │ close │ adjclose │ vol │ open_1 │ high_1 │ low_1 │ close_1 │ adjclose_1 │ vol_1 │ open_2 │ high_2 │ low_2 │ close_
├─────────────────────┼────────┼────────┼────────┼────────┼──────────┼───────────┼────────┼────────┼────────┼─────────┼────────────┼───────────┼─────────┼─────────┼─────────┼─────────
2024-08-01T13:30:00224.37224.48217.02218.36218.366.2501e7227.69231.87214.33216.86216.868.38619e710.693410.752510.447310.6
2024-08-02T13:30:00219.15225.6217.71219.86219.861.05569e8214.88216.13205.78207.67207.678.28801e710.388210.38829.8466310.0
2024-08-05T13:30:00199.09213.5196.0209.27209.271.19549e8185.22203.88182.0198.88198.881.00309e89.413389.748169.344459.7
2024-08-06T13:30:00205.3209.99201.07207.23207.236.96605e7200.75202.9192.67200.64200.647.37839e79.639859.708779.531539.7
2024-08-07T13:30:00206.9213.64206.39209.82209.826.34023e7200.77203.49191.48191.76191.767.07614e79.859.989.759.7
└─────────────────────┴────────┴────────┴────────┴────────┴──────────┴───────────┴────────┴────────┴────────┴─────────┴────────────┴───────────┴─────────┴─────────┴─────────┴─────────
3 columns omitted
```

Get the relevant column names (all columns that are called adjclose)
```julia
cidx = colnames(prices)[occursin.(r"adj",string.(colnames(prices)))]
```

```
3-element Vector{Symbol}:
:adjclose
:adjclose_1
:adjclose_2
```

Select these columns
```julia
prices = prices[cidx]
```

```julia
5×3 TimeArray{Float64, 2, DateTime, Matrix{Float64}} 2024-08-01T13:30:00 to 2024-08-07T13:30:00
┌─────────────────────┬──────────┬────────────┬────────────┐
│ │ adjclose │ adjclose_1 │ adjclose_2 │
├─────────────────────┼──────────┼────────────┼────────────┤
2024-08-01T13:30:00218.36216.8610.526
2024-08-02T13:30:00219.86207.679.87617
2024-08-05T13:30:00209.27198.889.56107
2024-08-06T13:30:00207.23200.649.63
2024-08-07T13:30:00209.82191.769.77
└─────────────────────┴──────────┴────────────┴────────────┘
```

Rename them back to the tickers:
```julia
TimeSeries.rename!(prices,Symbol.(tickers))
```

```julia
5×3 TimeArray{Float64, 2, DateTime, Matrix{Float64}} 2024-08-01T13:30:00 to 2024-08-07T13:30:00
┌─────────────────────┬────────┬────────┬─────────┐
│ │ AAPL │ TSLA │ F │
├─────────────────────┼────────┼────────┼─────────┤
2024-08-01T13:30:00218.36216.8610.526
2024-08-02T13:30:00219.86207.679.87617
2024-08-05T13:30:00209.27198.889.56107
2024-08-06T13:30:00207.23200.649.63
2024-08-07T13:30:00209.82191.769.77
└─────────────────────┴────────┴────────┴─────────┘
```

Or in one function:
```julia
function get_adj_close_TA(tickers,startdt,enddt,interval)
prices = get_prices.((TimeArray,),tickers,startdt=startdt,enddt=enddt,interval=interval) |> (x->hcat(x...))
prices = prices[colnames(prices)[occursin.(r"adj",string.(colnames(prices)))]]
TimeSeries.rename!(prices,Symbol.(tickers))
return prices
end;

get_adj_close_TA(["AAPL","TSLA","F","AMD","NFLX","WBA"],Date(2023-01-01),Date(2024-01-01),"1d")
```

```julia
252×6 TimeArray{Float64, 2, DateTime, Matrix{Float64}} 2021-01-04T14:30:00 to 2021-12-31T14:30:00
┌─────────────────────┬─────────┬─────────┬─────────┬────────┬────────┬─────────┐
│ │ AAPL │ TSLA │ F │ AMD │ NFLX │ WBA │
├─────────────────────┼─────────┼─────────┼─────────┼────────┼────────┼─────────┤
2021-01-04T14:30:00126.83243.2577.0379292.3522.8634.6737
2021-01-05T14:30:00128.398245.0377.145392.77520.834.4727
2021-01-06T14:30:00124.076251.9937.3022590.33500.4936.0389
2021-01-07T14:30:00128.31272.0137.4839895.16508.8937.9066
2021-12-29T14:30:00176.888362.06317.069148.26610.5445.4322
2021-12-30T14:30:00175.724356.7816.9943145.15612.0945.2061
2021-12-31T14:30:00175.103352.2617.2434143.9602.4445.3539
└─────────────────────┴─────────┴─────────┴─────────┴────────┴────────┴─────────┘
245 rows omitted
```

## TSFrame from TSFrames.jl

If you use Julia 1.9 or newer you can just use a sink argument in `get_prices` instead.
Expand Down Expand Up @@ -83,4 +217,74 @@ function stock_price_to_TSFrames(x)
end

stock_price_to_TSFrames(prices)
```

### Broadcast and create one TSFrame for the adjclose prices
```julia
tickers = ["AAPL","TSLA","F"]
prices = get_prices.((TSFrame,),tickers);
```

We now want to combine them by taking the adjclose of each tsframe.

```julia
prices = join(getindex.(prices,:,([:Index,:adjclose],))...)
```

```julia
5×3 TSFrame with DateTime Index
Index adjclose adjclose_1 adjclose_2
DateTime Float64? Float64? Float64?
───────────────────────────────────────────────────────
2024-08-01T13:30:00 218.36 216.86 10.526
2024-08-02T13:30:00 219.86 207.67 9.87617
2024-08-05T13:30:00 209.27 198.88 9.56107
2024-08-06T13:30:00 207.23 200.64 9.63
2024-08-07T13:30:00 209.82 191.76 9.77
```

Rename:
```julia
TSFrames.rename!(prices, tickers)
```

```julia
5×3 TSFrame with DateTime Index
Index AAPL TSLA F
DateTime Float64? Float64? Float64?
───────────────────────────────────────────────────
2024-08-01T13:30:00 218.36 216.86 10.526
2024-08-02T13:30:00 219.86 207.67 9.87617
2024-08-05T13:30:00 209.27 198.88 9.56107
2024-08-06T13:30:00 207.23 200.64 9.63
2024-08-07T13:30:00 209.82 191.76 9.77
```

Or in one function:

```julia

function get_adj_close_TSF(tickers,startdt,enddt,interval)
prices = get_prices.((TSFrame,),tickers,startdt=startdt,enddt=enddt,interval=interval)
prices = join(getindex.(prices,:,([:Index,:adjclose],))...)
TSFrames.rename!(prices, tickers)
return prices
end;
get_adj_close_TSF(["AAPL","TSLA","F","AMD","NFLX","WBA"],Date(2023-01-01),Date(2024-01-01),"1d")
```

```julia
252×6 TSFrame with DateTime Index
Index AAPL TSLA F AMD NFLX WBA
DateTime Float64? Float64? Float64? Float64? Float64? Float64?
─────────────────────────────────────────────────────────────────────────────────
2021-01-04T14:30:00 126.83 243.257 7.03792 92.3 522.86 34.6737
2021-01-05T14:30:00 128.398 245.037 7.1453 92.77 520.8 34.4727
2021-01-06T14:30:00 124.076 251.993 7.30225 90.33 500.49 36.0389
2021-01-07T14:30:00 128.31 272.013 7.48398 95.16 508.89 37.9066
2021-12-29T14:30:00 176.888 362.063 17.069 148.26 610.54 45.4322
2021-12-30T14:30:00 175.724 356.78 16.9943 145.15 612.09 45.2061
2021-12-31T14:30:00 175.103 352.26 17.2434 143.9 602.44 45.3539
245 rows omitted
```
Loading

0 comments on commit f5dfd6a

Please sign in to comment.