-3

This is my assignment:

Read a time series data of any stock of your choice from online resources, plot the volume of trades ("Volume") over the range of price ("High" − "Low").

This is what I have:

#Set the working directory
setwd("C:/Examples")

mydata<- read.csv("C:/Examples/table.csv")
attach(mydata)

plot(Volume, High - Low)

The result I get is this:

enter image description here

This looks like the right way to do it but I'm not sure.

I read How to make a great R reproducible example and I think I need to include a vector or matrix to make this reproducible. I don't know how to do this for my specific problem however.

This is the CSV file with the stock info:

Date           Open      High        Low        Close     Volume      Adj Close
2/9/2016    26.639999   27.690001   26.51       26.82     13919100    26.82
2/8/2016    27.610001   27.969999   26.48       27.049999 24473600    27.049999
2/5/2016    29.059999   29.139999   27.73       27.969999 16077500    27.969999
2/4/2016    27.91       29.23       27.709999   29.15     28517000    29.15
2/3/2016    28.450001   28.610001   26.57       27.68     55527600    27.68
2/2/2016    29.32       30.23       28.129999   29.059999 34022500    29.059999
2/1/2016    29.27       29.790001   28.790001   29.57     12865800    29.57
1/29/2016   29.1        29.51       28.51       29.51     18718300    29.51
1/28/2016   30.59       30.629999   28.6        28.75     15420500    28.75
1/27/2016   29.9        30.530001   29.450001   29.690001 13269900    29.690001
1/26/2016   29.76       30.190001   29.620001   29.98     11422600    29.98
1/25/2016   29.959999   30.389999   29.66       29.780001 23095500    29.780001

I don't know how I can make this any clearer. Is the code I have right? If not why?

Community
  • 1
  • 1
template boy
  • 10,230
  • 8
  • 61
  • 97

1 Answers1

3

You can try something like this:

Read the dataframe

mydata <- read.table(text="Date,Open,High,Low,Close,Volume,Adj Close
2/9/2016,26.639999,27.690001,26.51,26.82,13919100,26.82
2/8/2016,27.610001,27.969999,26.48,27.049999,24473600,27.049999
2/5/2016,29.059999,29.139999,27.73,27.969999,16077500,27.969999
2/4/2016,27.91,29.23,27.709999,29.15,28517000,29.15
2/3/2016,28.450001,28.610001,26.57,27.68,55527600,27.68
2/2/2016,29.32,30.23,28.129999,29.059999,34022500,29.059999
2/1/2016,29.27,29.790001,28.790001,29.57,12865800,29.57
1/29/2016,29.1,29.51,28.51,29.51,18718300,29.51
1/28/2016,30.59,30.629999,28.6,28.75,15420500,28.75
1/27/2016,29.9,30.530001,29.450001,29.690001,13269900,29.690001
1/26/2016,29.76,30.190001,29.620001,29.98,11422600,29.98
1/25/2016,29.959999,30.389999,29.66,29.780001,23095500,29.780001", header=T, sep=',')

Make a new column with the difference of High and Low columns

mydata$`High-Low` <- mydata$High-mydata$Low

Make a Time Serie with 2 selected columns, ordered by dates

myts <- ts(mydata[order(strptime(mydata$Date, format="%m/%d/%Y")),c('Volume', 'High-Low')])

Plot it

plot(myts)

enter image description here

HubertL
  • 19,246
  • 3
  • 32
  • 51
  • I got `myts <- ts(mydata[,c('Volume', 'High-Low')])` Error in `[.data.frame(mydata, , c("Volume", "High-Low"))` : undefined columns selected – template boy Mar 15 '16 at 00:54