xKDR / TSFrames.jl

Timeseries in Julia
MIT License
89 stars 22 forks source link

Get last row of a TSFrame and one value of this last row #200

Open femtotrader opened 2 months ago

femtotrader commented 2 months ago

Hello,

I have some sample data like.

julia> using TSFrames
julia> using MarketData
julia> ts = random_ohlcv() |> TSFrame
500×5 TSFrame with DateTime Index
 Index                Open     High     Low      Close    Volume
 DateTime             Float64  Float64  Float64  Float64  Float64
──────────────────────────────────────────────────────────────────
 2020-01-01T00:00:00   826.17   832.42   825.61   829.02     81.7
 2020-01-01T01:00:00   828.91   830.73   823.34   823.54      9.8
 2020-01-01T02:00:00   823.03   825.27   819.16   822.9      56.2
 2020-01-01T03:00:00   823.38   824.78   820.55   824.78     82.5
 2020-01-01T04:00:00   825.56   829.12   824.66   829.12     42.1
 2020-01-01T05:00:00   829.92   832.46   824.97   826.81     11.4
 2020-01-01T06:00:00   827.07   834.45   827.07   832.56     13.0
 2020-01-01T07:00:00   833.46   834.97   830.1    832.5      31.4
 2020-01-01T08:00:00   832.06   836.01   828.83   829.42     23.1
 2020-01-01T09:00:00   829.9    831.92   829.64   831.83     39.9
 2020-01-01T10:00:00   830.87   831.6    824.07   825.75     62.8
 2020-01-01T11:00:00   826.61   832.88   825.93   831.28      1.2
          ⋮              ⋮        ⋮        ⋮        ⋮        ⋮
 2020-01-21T08:00:00   707.24   707.57   701.69   703.09     42.3
 2020-01-21T09:00:00   703.17   703.17   691.93   692.25     85.0
 2020-01-21T10:00:00   691.94   691.94   681.72   683.03     26.5
 2020-01-21T11:00:00   683.61   683.61   671.0    671.45     27.9
 2020-01-21T12:00:00   671.14   672.0    668.2    669.61     97.1
 2020-01-21T13:00:00   668.99   669.6    665.31   665.82     85.3
 2020-01-21T14:00:00   665.51   670.26   665.51   668.44     27.3
 2020-01-21T15:00:00   668.75   669.56   663.97   666.67     19.3
 2020-01-21T16:00:00   666.39   671.97   665.61   670.89     32.8
 2020-01-21T17:00:00   670.72   670.86   667.37   667.37      4.9
 2020-01-21T18:00:00   667.18   670.17   666.05   670.17     32.1
 2020-01-21T19:00:00   669.95   677.97   669.95   673.04     28.6
                                                  476 rows omitted

I would like to get last Volume of this TSFrame (ie here 28.6)

I tried getting last row like this

julia> ts[end, [:Volume]]
ERROR: MethodError: no method matching axes(::TSFrame, ::Int64)

Closest candidates are:
  axes(::Core.SimpleVector, ::Integer)
   @ Base essentials.jl:784
  axes(::Base.Broadcast.Broadcasted{<:Any, <:Tuple{Vararg{T, N}} where T}, ::Integer) where N
   @ Base broadcast.jl:239
  axes(::DataFrames.GroupKey, ::Integer)
   @ DataFrames C:\Users\femto\.julia\packages\DataFrames\58MUJ\src\groupeddataframe\groupeddataframe.jl:653
  ...

Stacktrace:
 [1] lastindex(a::TSFrame, d::Int64)
   @ Base .\abstractarray.jl:427
 [2] top-level scope
   @ REPL[81]:1

that's odd because

julia> ts.coredata[end, [:Volume]]
DataFrameRow
 Row │ Volume
     │ Float64
─────┼─────────
 500 │    28.6

works

Isn't there simpler API call (ie directly from TSFrames... not using Dataframes API) than

julia> ts.coredata[end, [:Volume]][1]
28.6

Kind regards

chiraganand commented 2 months ago

Isn't there simpler API call (ie directly from TSFrames... not using Dataframes API) than

Yes, this should be fixed. Currently, one can do (again, not very convenient):

julia> ts[end][:, :Volume]][1]

ts[end] returns the last row of a TSFrame but indexing a column isn't working. I think lastindex(ts) will need to change:

https://github.com/xKDR/TSFrames.jl/blob/1cb47467a3351199ecef4d39f30ee08ea2ee9c22/src/utils.jl#L110-L112