Exemplo n.º 1
0
 def LoadData(self):
     
     ds = YahooStockDataSource(cachedFolderName=cachedFolderName,
                         dataSetId=dataSetId,
                         instrumentIds=self.instrumentIds,
                         startDateStr=startDateStr,
                         endDateStr=endDateStr,
                         event='history')
    
     self.stkData = [ds.getBookDataByFeature()['open'],
             ds.getBookDataByFeature()['high'],
             ds.getBookDataByFeature()['low'],
             ds.getBookDataByFeature()['close'],
             ds.getBookDataByFeature()['adjClose'],
             ds.getBookDataByFeature()['volume']]
     tbRSI = tb.RSI(self.stkData[4][self.instrumentIds[0]],14)
     
     tbChaikin = tb.ADOSC(self.stkData[1][self.instrumentIds[0]],
                         self.stkData[2][self.instrumentIds[0]],
                         self.stkData[3][self.instrumentIds[0]],
                         self.stkData[5][self.instrumentIds[0]])
     
 
     '''
     Task1
     Create new dataframe here with 5 columns 
     column  1 : Date of stock price 
     column  2 : Day of the week (Monday Tuesday etc)
     Columnn 3 :  tbRSI
     Column 4 : tbChaikin
     column 5 :  Stock's adjusted price.
     Index : column1 
     '''
     dfdata = {'Adj_Close':self.stkData[4][self.instrumentIds[0]],
              'RSI':tbRSI,
              'Chaikin':tbChaikin}
     df = pd.DataFrame(dfdata)
     df['Trade_Date'] = pd.to_datetime(df.index)
     df['day_of_week'] = df['Trade_Date'].dt.day_name()
     
     dfFinal = df[["Trade_Date", "day_of_week", "RSI", "Chaikin", "Adj_Close"]]
     # dfFinal2 = df[["Trade_Date", "day_of_week", "RSI", "Chaikin", "Adj_Close"]]
     #df_rsi = pd.DataFrame(dfFinal['RSI'])
     #print(df_rsi)
     #df_rsi.to_csv('yahooData/weeklyTradeTest/RSI_Value.csv')
     
     
     
     '''
     Task 2
     filter above dataframe using column  2 value (e.g. 'Wednesday') and store in other dataframe 
     '''
     # dfFinal2 = dfFinal.loc[df['day_of_week'] == 'Wednesday']
     # dfFinal.to_csv(dir_path)    
     # print(dfFinal.head(50)
     # dfFinal = dfFinal.loc[df['day_of_week'] == 'Friday']
     dfFinal.to_csv(dir_path)    
     print(dfFinal.head(50))
Exemplo n.º 2
0
startDateStr = '2007/12/01'
endDateStr = '2017/12/01'
cachedFolderName = 'yahooData/'
dataSetId = 'testPairsTrading'
instrumentIds = [
    'SPY', 'AAPL', 'ADBE', 'T', 'EBAY', 'MSFT', 'QCOM', 'HPQ', 'JNPR', 'AMD',
    'IBM'
]
ds = YahooStockDataSource(cachedFolderName=cachedFolderName,
                          dataSetId=dataSetId,
                          instrumentIds=instrumentIds,
                          startDateStr=startDateStr,
                          endDateStr=endDateStr,
                          event='history')
data = ds.getBookDataByFeature()['adjClose']

# Heatmap to show the p-values of the cointegration test
# between each pair of stocks

scores, pvalues, pairs = find_cointegrated_pairs(data)
import seaborn

m = [0, 0.2, 0.4, 0.6, 0.8, 1]
seaborn.heatmap(pvalues,
                xticklabels=instrumentIds,
                yticklabels=instrumentIds,
                cmap='RdYlGn_r',
                mask=(pvalues >= 0.98))
plt.show()
print(pairs)