在Julia中绘制数据帧上的简单移动平均值

在Julia中绘制数据帧上的简单移动平均值,julia,julia-dataframe,julia-plots,Julia,Julia Dataframe,Julia Plots,我有一个excel文件,上面有日期和股票价格。我使用DataFrames.jl将这些数据读入一个dataframe using DataFrames, StatsPlots, Indicators df = DataFrame(XLSX.readtable("Demo-sv.xlsx", "Blad3")...) 这非常有效,我在这里打印了前6个条目 6×2 DataFrame │ Row │ Date │ Closeprice │ │

我有一个excel文件,上面有日期和股票价格。我使用DataFrames.jl将这些数据读入一个dataframe

using DataFrames, StatsPlots, Indicators

df = DataFrame(XLSX.readtable("Demo-sv.xlsx", "Blad3")...)
这非常有效,我在这里打印了前6个条目

6×2 DataFrame
│ Row │ Date       │ Closeprice │
│     │ Any        │ Any        │
├─────┼────────────┼────────────┤
│ 1   │ 2019-05-03 │ 169.96     │
│ 2   │ 2019-05-02 │ 168.06     │
│ 3   │ 2019-04-30 │ 165.58     │
│ 4   │ 2019-04-29 │ 166.4      │
│ 5   │ 2019-04-26 │ 167.76     │
│ 6   │ 2019-04-25 │ 167.46     │
然后,我用StatsPlots.jl
@df-df-plot(df.Date,df.Closeprice)
绘制这些数据,并得到一个漂亮的绘图图

问题是当我想用Indicators.jl绘制一个简单的移动平均线时

movingaverage = sma(df, n=200)
plot!(movingaverage, linewidth=2, color=:red)
我收到了这个错误消息

ERROR: LoadError: MethodError: no method matching sma(::DataFrame; n=200)
Closest candidates are:
sma(::Array{T,N} where N; n) where T<:Real at 
/Users/HBrovell/.julia/packages/Indicators/QGmEX/src/ma.jl:8
sma(::Temporal.TS{V,T}; args...) where {V, T} at 
/Users/HBrovell/.julia/packages/Indicators/QGmEX/src/temporal.jl:64
错误:LoadError:MethodError:没有与sma匹配的方法(::DataFrame;n=200)
最接近的候选人是:

sma(::数组{T,N}其中N;N)其中T我假设您需要的是:

sma(sort(df, :Date).ClosePrice, n=200)
还有一个问题是
ClosePrice
列的数据类型应该是数字,而不是
Any

您需要以某种方式对其进行转换,例如:

df[!, :ClosePrice] .= Float64.(df.ClosePrice)

如果我这样做,我似乎会得到同样的错误信息,我想你是说
df=DataFrame(XLSX.readtable(“Demo sv.XLSX”,“Blad3”)…)
@df-df-plot(df.Date,df.Closeprice)
!(movingaverage,linewidth=2,color=:red)
或更正我现在得到以下错误消息,“无方法匹配”现在是数组而不是数据帧。错误:LoadError:MethodError:没有与sma匹配的方法(::Array{Any,1};n=200)最接近的候选对象是:sma(::Array{T,n}其中n;n)where-Tyeah您在数据中还有一个问题-我编辑了答案。这很有效,现在我没有收到任何错误消息。但是我的日期在曲线图上错得很厉害。我得到的日期是-0059-01-01和0476-01-01。我是否还需要将其从任何格式转换为日期格式?这取决于您如何解析和格式化日期。如果你对此有疑问,我建议你单独问一个问题。