2

I have been trying to get information on how to use local files to create candlecharts but most of them refer to yahoo and google files.

I have a CSV file

i<-"A"
library(quantmod)
A<-read.csv("D:\\DATA.csv",header=T)
        A$Close<-as.numeric(A$Close)
        A$High<-as.numeric(A$High)
        A$Low<-as.numeric(A$Low)
        A$Open<-as.numeric(A$Open)
        A$Volume<-as.numeric(A$Volume)

        #Select appropriate dataset
        A<-A[which(A$CODE==i),]
        A<-A[,(5:10)]
        A$DATE<-as.Date(A$DATE,"%Y/%d/%Y")
        A<-xts(A,order.by=as.POSIXct(A$DATE))
        A<-A[,(1:5)]

My original DATASET is as below:

head(A)
      CODE NAME YR2 YR2 Low High Close Open  Volume  DATE
49620   A A 10.25     21 112  120   116      101,500 9/11/2006
49621   A A 10.25     21 112  120   118  116 790,700 9/12/2006
49622   A A 10.25     21 117  124   119  118 445,300 9/13/2006
49623   A A 10.25     21 119  127   123  119 120,200 9/14/2006
49624   A A 10.25     21 120  127   124  123 448,700 9/15/2006
49625   A A 10.25     21 120  130   128  124 494,600 9/18/2006
 ##NOTE THAT THE FIRST COLUMNS DOES NOT HAVE A NAME(IT IS THE OBS NO.)

After running the above commands the dataset is as shown below.

head(A)
                    Low    High   Close  Open   Volume
2006-12-01 03:00:00 " 206" " 231" " 228" " 232" "1159"
2006-12-01 03:00:00 " 204" " 230" " 206" " 229" "5711"
2006-12-02 03:00:00 " 259" " 261" " 259" " 260" "1072"
2006-12-02 03:00:00 " 200" " 229" " 207" " 229" "1505"
2006-12-03 03:00:00 " 262" " 264" " 262" " 260" "5416"
2006-12-03 03:00:00 " 204" " 227" " 206" " 208" " 676"

I then created modified the colnames to the required formats My dataset after all modification is as shown below.

                    A.Low A.High A.Close A.Open A.Volume
2006-12-01 03:00:00 " 206" " 231"  " 228"   " 232"  "1159"
2006-12-01 03:00:00 " 204" " 230"  " 206"   " 229"  "5711"
2006-12-02 03:00:00 " 259" " 261"  " 259"   " 260"  "1072"
2006-12-02 03:00:00 " 200" " 229"  " 207"   " 229"  "1505"
2006-12-03 03:00:00 " 262" " 264"  " 262"   " 260"  "5416"
2006-12-03 03:00:00 " 204" " 227"  " 206"   " 208"  " 676"

I am stuck here because when I save it the dataset changes completely such that when I try to read it I get:

   Index.A.Low.A.High.A.Close.A.Open.A.Volume
1    2006-12-01 03:00:00  206  231  228  232 1159
2    2006-12-01 03:00:00  204  230  206  229 5711
3    2006-12-02 03:00:00  259  261  259  260 1072
4    2006-12-02 03:00:00  200  229  207  229 1505
5    2006-12-03 03:00:00  262  264  262  260 5416
6    2006-12-03 03:00:00  204  227  206  208  676

My problems are:

  1. Dates are not original dates in the dataset
  2. I am unable to get what I needed to do (draw the candlecharts)
Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
abraham
  • 31
  • 4
  • Welcome to StackOverflow. We can help you with this, but first you should help us a little. Please read [this post](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) explaining how to ask a good question by providing a reproducible example. Why not look at the `dput` function by typing `?dput` in your R terminal for a start. Note that your example data given above is incorrect: the header has 6 fields but the data only has 5 fields. Using `dput` reduces such errors. – SlowLearner Dec 25 '12 at 09:18
  • ...and format your question correctly, avoid using HTML tags... – Paul Hiemstra Jan 10 '13 at 12:19

1 Answers1

0

I agree with SlowLearner on requiring a more reproducible example.
A couple of observations though:

  1. You can convert to xts using order.by = as.Date itself.
  2. Check the format in as.Date. If your data is mm/dd/yyyy (as it seems from the data in the question, then the command should have format= '%m/%d/%Y' instead of '%Y/%d/%Y'.

Also, please ensure that your data is comma-separated and not tab separated (has slipped me up many times).

Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
NN1983
  • 13
  • 3