移动平均线似乎是使用 geom_ma 从最近的日期向后计算的
Moving averages appear to be calculating from the most recent date backwards using geom_ma
在我的时间序列上,移动平均线似乎是从右到左绘制的。这是我的代码:
library(Quandl)
library(ggplot2)
# Get Historical Futures Prices: Crude Oil Futures from Quandl. Contract Code is CL
CME_CL_Data <- Quandl('CHRIS/CME_CL1', start_date = '2021-01-01')
# Plot the OHLC Barchart of the Crude Oil Futures Historical Prices
ggplot(CME_CL_Data, aes(x = Date, y = Last)) +
geom_barchart(aes(open = Open, high = High, low = Low, close = Last)) +
xlab('') + ylab('Daily Prices') +
ggtitle('Future Daily Prices: WTI Crude') +
geom_ma(ma_fun = SMA, n=20, linetype = 1, size = .75) +
geom_ma(ma_fun = SMA, n=50, color = 'red', size = 1.25) +
theme_light()
这是输出图表:Chart of CL with Moving Averages
我似乎无法确定是我在做什么导致的。
Quandl returns 数据按递减日期排序:
> head(CME_CL_Data)
Date Open High Low Last Change Settle Volume
1 2021-06-28 73.99 74.45 72.62 72.77 -1.14 72.91 351101
2 2021-06-25 73.32 74.18 72.85 74.00 0.75 74.05 357898
3 2021-06-24 73.28 73.61 72.32 73.33 0.22 73.30 331826
4 2021-06-23 72.91 74.25 72.82 73.28 0.23 73.08 422226
5 2021-06-22 73.41 73.95 72.94 73.09 -0.60 73.06 18977
6 2021-06-21 71.52 73.96 71.15 73.53 2.02 73.66 113253
Previous Day Open Interest
1 416743
2 427257
3 426977
4 426766
5 21423
6 59274
因此,geom_ma
正在反向移动平均线。您需要重新排序您的数据:
library(Quandl)
library(ggplot2)
library(tidyquant)
# Get Historical Futures Prices: Crude Oil Futures from Quandl. Contract Code is CL
CME_CL_Data <- Quandl('CHRIS/CME_CL1', start_date = '2021-01-01')
# Reoder data
CME_CL_Data <- CME_CL_Data[order(CME_CL_Data$Date),]
# Plot the OHLC Barchart of the Crude Oil Futures Historical Prices
ggplot(CME_CL_Data, aes(x = Date, y = Last)) +
geom_barchart(aes(open = Open, high = High, low = Low, close = Last)) +
xlab('') + ylab('Daily Prices') +
ggtitle('Future Daily Prices: WTI Crude') +
geom_ma(ma_fun = SMA, n=20, linetype = 1, size = .75) +
geom_ma(ma_fun = SMA, n=50, color = 'red', size = 1.25) +
theme_light()
默认情况下,Quandl
按降序生成 data.frame。您可以指定升序 order
,例如:
CME_CL_Data <- Quandl('CHRIS/CME_CL1', start_date = '2021-01-01', order = c("asc"))
否则,您的代码在我的机器上运行良好。
在我的时间序列上,移动平均线似乎是从右到左绘制的。这是我的代码:
library(Quandl)
library(ggplot2)
# Get Historical Futures Prices: Crude Oil Futures from Quandl. Contract Code is CL
CME_CL_Data <- Quandl('CHRIS/CME_CL1', start_date = '2021-01-01')
# Plot the OHLC Barchart of the Crude Oil Futures Historical Prices
ggplot(CME_CL_Data, aes(x = Date, y = Last)) +
geom_barchart(aes(open = Open, high = High, low = Low, close = Last)) +
xlab('') + ylab('Daily Prices') +
ggtitle('Future Daily Prices: WTI Crude') +
geom_ma(ma_fun = SMA, n=20, linetype = 1, size = .75) +
geom_ma(ma_fun = SMA, n=50, color = 'red', size = 1.25) +
theme_light()
这是输出图表:Chart of CL with Moving Averages
我似乎无法确定是我在做什么导致的。
Quandl returns 数据按递减日期排序:
> head(CME_CL_Data)
Date Open High Low Last Change Settle Volume
1 2021-06-28 73.99 74.45 72.62 72.77 -1.14 72.91 351101
2 2021-06-25 73.32 74.18 72.85 74.00 0.75 74.05 357898
3 2021-06-24 73.28 73.61 72.32 73.33 0.22 73.30 331826
4 2021-06-23 72.91 74.25 72.82 73.28 0.23 73.08 422226
5 2021-06-22 73.41 73.95 72.94 73.09 -0.60 73.06 18977
6 2021-06-21 71.52 73.96 71.15 73.53 2.02 73.66 113253
Previous Day Open Interest
1 416743
2 427257
3 426977
4 426766
5 21423
6 59274
因此,geom_ma
正在反向移动平均线。您需要重新排序您的数据:
library(Quandl)
library(ggplot2)
library(tidyquant)
# Get Historical Futures Prices: Crude Oil Futures from Quandl. Contract Code is CL
CME_CL_Data <- Quandl('CHRIS/CME_CL1', start_date = '2021-01-01')
# Reoder data
CME_CL_Data <- CME_CL_Data[order(CME_CL_Data$Date),]
# Plot the OHLC Barchart of the Crude Oil Futures Historical Prices
ggplot(CME_CL_Data, aes(x = Date, y = Last)) +
geom_barchart(aes(open = Open, high = High, low = Low, close = Last)) +
xlab('') + ylab('Daily Prices') +
ggtitle('Future Daily Prices: WTI Crude') +
geom_ma(ma_fun = SMA, n=20, linetype = 1, size = .75) +
geom_ma(ma_fun = SMA, n=50, color = 'red', size = 1.25) +
theme_light()
默认情况下,Quandl
按降序生成 data.frame。您可以指定升序 order
,例如:
CME_CL_Data <- Quandl('CHRIS/CME_CL1', start_date = '2021-01-01', order = c("asc"))
否则,您的代码在我的机器上运行良好。