Technical Analysis

You may hear about Technical Analyst. That’s the basis of quantitative analysis of the stock market. There is a package in Python that makes the calculation of all the technical indicator much easier. I chose some of my favorite indicators and integrate into my code library.

Applying the talib is a little bit tricky, but take a look at this one line of code.

price.loc[price.Ticker==i,"ADXR"]= ta.ADXR(price.loc[price.Ticker==i].High.values, price.loc[price.Ticker==i].Low.values, price.loc[price.Ticker==i].Close.values, timeperiod=14)

The variable price is a DataFrame that return from our data getting object. Since we only want to analyze one stock’s time series feature, we want to filter the one ticker at one calculation, using price.Ticker == i. Then, the ADXR,  Average Directional Movement Index Rating indicator takes in High, Low and close. Basically, put them in the function of ta.ADXR()

For more information about the talib, check TA-Lib : Technical Analysis Library

Here’s the full picture of the function.







In [16]:
import talib as ta
import data_source_lib as da
In [15]:
get_data = da.get_stock_data(["AAPL"],freq = "daily")
price = get_data.get_ondemand_data()
Finished AAPL
100%|████████████████████████████████████████████████████████████████████████████████████| 1/1 [00:00<00:00,  1.56it/s]
In [40]:
 def get_technicals(price) :
    import pandas as pd
    import tqdm
    from IPython.display import clear_output
    if not isinstance(price, pd.DataFrame):
        raise "Please feed a DataFrame object"
    for i in tqdm.tqdm(range(len(set(price.Ticker)))):
       
       
        i = list(set(price.Ticker))
        #print price .loc[price.Ticker==i]

        #price.groupby('Ticker').get_group(list(set(price.Ticker))[i])
        #price.loc[price.Ticker==i,"ADX"]= ta.ADX(price.loc[price.Ticker==i].High.values, price.loc[price.Ticker==i].Low.values, price.loc[price.Ticker==i].Close.values, timeperiod=14)
        price.loc[price.Ticker==i,"ADXR"]= ta.ADXR(price.loc[price.Ticker==i].High.values, price.loc[price.Ticker==i].Low.values,\
                                                   price.loc[price.Ticker==i].Close.values, timeperiod=14)
        price.loc[price.Ticker==i,"APO"]= ta.APO(price.loc[price.Ticker==i].Close.values, fastperiod=12, slowperiod=26, matype=0)
        price.loc[price.Ticker==i,"AROONOSC"]= ta.AROONOSC(price.loc[price.Ticker==i].High.values,price.loc[price.Ticker==i].Close.values, timeperiod=14)
        price.loc[price.Ticker==i,"CCI"]= ta.CCI(price.loc[price.Ticker==i].High.values,price.loc[price.Ticker==i].Low.values,price.loc[price.Ticker==i].Close.values, timeperiod=14)
        price.loc[price.Ticker==i,"MFI"]= ta.MFI(price.loc[price.Ticker==i].High.values, price.loc[price.Ticker==i].Low.values, price.loc[price.Ticker==i].Close.values,\
                                                 price.loc[price.Ticker==i].loc[price.Ticker==i].Volume.values.astype(float),timeperiod=14)
        price.loc[price.Ticker==i,"MACD"], price.loc[price.Ticker==i,"MACD_signal"], price.loc[price.Ticker==i,"MACD_hist"] = ta.MACD(price.loc[price.Ticker==i].Close.values, fastperiod=12, slowperiod=26, signalperiod=9)
        price.loc[price.Ticker==i,"ROCP"]= ta.ROCP(price.loc[price.Ticker==i].Close.values, timeperiod=10)
        #price.loc[price.Ticker==i,"ROCR100"]= ta.ROCR100(price.loc[price.Ticker==i].Close.values, timeperiod=10)
        price.loc[price.Ticker==i,"RSI"]= ta.RSI(price.loc[price.Ticker==i].Close.values, timeperiod=14)
        price.loc[price.Ticker==i,"MA_fast"] = price.Close.rolling(10).mean()
        price.loc[price.Ticker==i,"MA_slow"] = price.Close.rolling(30).mean()
        clear_output()
        print "\nDone:", i