Thursday, July 25, 2013

Adding technical analysis indicator to stock chart

The quantmod support quite a number of stock technical analysis indicators. The list is shown in table below.
Below discribe the steps to add these indicators to the stock chart created by chartSeries:
1. Create the chart using chartSeries, details in previous post.
2. Use the quantmod name (listed in the table below) to add the indicator, eg:
               addVo()   # to add volume
               addCCI()  # to add Commodity Channel Index
               addBBands()  # to add Bollinger Bands
3. To add customizied indicator use the addTA function, eg:
               addTA(OpCl(AAPL),col='blue', type='h')  #add the Open to Close price change, quantmod OpCl function  

4. The code below show how to create a function to add customize indicator to stock chart using newTA:                addOpCl <- col="green" create="" function="" indicator="" nbsp="" newta="" pcl="" ta="" the="" to="" type="h" use="">               addOpCl()      # to add the custom TA to chart 5. Alternatively, the TA can be added directly in the chartSeries function when creating the chart, eg:                chartSeries(GS, theme="white", TA="addVo();addBBands();addCCI()")   

Wednesday, April 14, 2010

R with Interactive Brokers' TWS or TWS gateway

1. Start Interactive Brokers' TWS or TWS gateway.
2. Start R and load Interactive Brokers module.
    >require(IBrokers)       or   >library(IBrokers)
3. Connect to Interactive Brokers' TWS.
    >tws<-twsConnect()
4. Create the contract
    >cont<-twsSTK("IBM")   # this is for stock
    >cont<-twsFUT("YM","ECBOT","201006")
5. Stock or future data can be requested by any of the following data:
    a) Use the following code to request for historical data:
        >data<-reqHistoricalData(tws,cont)
    b) Use the below code to request for market data:
        >data<-reqMktData(tws,cont)
        * when requesting market data the code will appear hanging as it is waiting for the market data feed. Need to spawn separate thread to handle the market data.
    c) Use the below code to request for market depth:
        >data<-reqMktDepth(tws,cont)
6. The data can be charted using chartSeries, plot ... etc.

Monday, April 5, 2010

Getting started with R

1. Download R from http://www.r-project.org/ and install.
2. Start R and install quantmod package in R prompt.
    >install.packages(c('xts','Defaults','quantmod'))
3. Get IBM stock quote using Yahoo web site.
    >library("quantmod") or require(quantmod)
    >getSymbols("IBM") or
    >getSymbols("IBM",src="Yahoo")
4. Chart the IBM using any of the following command:
       >plot(IBM)







      >chartSeries(IBM)

5) To change to white scheme and only current year chart, use the following:
  chartSeries(IBM,theme='white',subset='2013-01::2013-07')


6)  reChart function can be used to change the chart setting, eg:
      reChart(major.ticks='months',subset='first 16 weeks')

7) To show the content of IBM, just type IBM in the prompt