Computingparadigm.files.wordpress.com



import pandas as pd

import pandas.io.data as web

import numpy as np

import datetime

from sys import exit

company_names = []

company_symbols = []

stock_values = pd.DataFrame()

close_rates = pd.DataFrame()

def read_symbols(filename):

#Function to read the company names and company symbols from the file.

with open(filename,"r") as fp:

line = fp.readlines()

for text in line:

company_names.append(text.split()[0])

company_symbols.append(text.split()[1])

def read_stock_values(start_date,end_date):

#Function to read the stock data from yahoo exchange website

#

i = 0

global stock_values

for symbol in company_symbols:

print "Getting the data for %s"%company_names[i]

df = web.DataReader(symbol,"yahoo",start_date,end_date)[["Open","Close"]]

df["Company"] = company_names[i]

stock_values = stock_values.append(df)

i+=1

def recommend_stock():

#returns the recommended stock based on the opening and closing rates of stocks for the month of JAN2016

stock_values["Difference"] = stock_values["Open"] - stock_values["Close"]

stock_values["Rate"] = (stock_values["Difference"] / stock_values["Close"])*100

#print(stock_values)

pivoted_value = pd.pivot_table(stock_values,values=["Rate"],columns=["Company"],aggfunc=np.std)

#print pivoted_value

print (pivoted_value.idxmax(axis=1).values)

def write_data(filename):

#Writes the results into a file passed by the user(in this case output.csv'''

stock_values.to_csv(filename,encoding='utf-8')

def sell_shares():

close_rates = pd.read_csv("stock_data.csv",index_col=0,parse_dates=True)

def peak_date(column):

return(column.ix[column.idxmax()])

def main():

choice = 0

while(choice != 3):

choice = input("Enter your choice:\n1.Buy a share\n2.Sell a share\n3.Exit")

if choice == 1:

read_symbols("selected_companies.txt")

read_stock_values(datetime.datetime(2016,1,1),datetime.datetime(2016,1,31))

recommend_stock()

write_data("output.csv")

elif choice == 2:

close_rates = pd.read_csv("stock_data.csv",index_col=0,parse_dates=True)

#print close_rates

print close_rates.apply(peak_date).idxmax()

elif choice == 3:

exit()

else:

print ("Invalid choice..Try again")

if __name__ == '__main__':

main()

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download