Posted by: Sourav | June 22, 2017

None output from python solved

https://stackoverflow.com/questions/7053652/random-none-output-from-basic-python-function

from nsepy import get_history, get_index_pe_history
from datetime import date

sbin = get_history(symbol=’SBIN’,
start=date(2015,1,1),
end=date(2015,1,10))
sbin[[ ‘VWAP’, ‘Turnover’]].plot(secondary_y=’Turnover’)

nifty = get_history(symbol=”NIFTY”,
start=date(2015,1,1),
end=date(2015,1,10),
index=True)
nifty[[‘Close’, ‘Turnover’]].plot(secondary_y=’Turnover’)

 

#Futures and Options historical data
nifty_fut = get_history(symbol=”NIFTY”,
start=date(2015,1,1),
end=date(2015,1,10),
index=True,
futures=True, expiry_date=date(2015,1,29))

stock_opt = get_history(symbol=”SBIN”,
start=date(2015,1,1),
end=date(2015,1,10),
option_type=”CE”,
strike_price=300,
expiry_date=date(2015,1,29))

#Index P/E ratio history
nifty_pe = get_index_pe_history(symbol=”NIFTY”,
start=date(2015,1,1),
end=date(2015,1,10))

 

print nifty_fut

import pandas_datareader.data as web
import datetime
start = datetime.datetime(2010, 1, 1)
end = datetime.datetime(2013, 1, 27)
f = web.DataReader(‘F’,’google’, start, end)
#print (f)
f.to_csv(“/home/sourav/google_finance_data.csv”)

import yahoo_finance
import sys
import csv
import xlsxwriter
import openpyxl
import xlrd
import pandas as pd
from openpyxl.utils import coordinate_from_string, column_index_from_string

symbol = yahoo_finance.Share(“GOOG”)
google_data = symbol.get_historical(“2015-06-25”, “2016-06-30”)
google_df = pd.DataFrame(google_data)

# Output data into CSV
google_df.to_csv(“/home/sourav/google_stock_data.csv”)
#wb=xlsxwriter.Workbook(“google_stock_data.csv”.replace(“csv”,”xlsx”))
wb = openpyxl.Workbook()
ws = wb.active
ws.title=’google_stock_data_experiment’
f = open(‘google_stock_data.csv’)
reader = csv.reader(f, delimiter=’,’)
for row in reader:
ws.append(row)
f.close()

wb.save(‘google_stock_data.xlsx’)
ws=wb.active
ws.cell(row=1, column=10,value=’Trend’)
ws.cell(row=1, column=11,value=’Return’)
ws.cell(row=1, column=14,value=’10-day SMA’)
ws.cell(row=1, column=15,value=’Smoothing Constant 2/(10 + 1)’)
ws.cell(row=1, column=16,value=’10-day EMA’)
wb.save(‘google_stock_data.xlsx’)
cell = ws[“B2”]

for row in ws.iter_rows(min_row=2, min_col=3, max_col=3):
for cell in row:

xy = coordinate_from_string(cell.coordinate) # returns (‘A’,4)
colnum = column_index_from_string(xy[0]) # returns 1
rownum = xy[1]
tempval2=float(ws.cell(row=rownum, column=colnum).value)
ws.cell(row=rownum, column=colnum,value=tempval2).number_format = ‘%s%s’

tempval=0.0
values=[]
returnval=0.00
count=0
cell = ws[“A2″]
xycell = coordinate_from_string(cell.coordinate)
rowstorage=int(xycell[1])
#print (rowstorage)

tempcount=2

for row in ws.iter_rows(min_row=1, min_col=3, max_col=3):

for cell in row:
count+=1
#print(cell.coordinate)
xy = coordinate_from_string(cell.coordinate) # returns (‘A’,4)
colnum = column_index_from_string(xy[0]) # returns 1
rownum = xy[1]
#print(rownum)

#print(str(colnum) +’ ‘+str(rownum)+’ has a value ‘+ws.cell(row=rownum, column=colnum).value)

if ws.cell(row=rownum, column=colnum).value==”:

break
if (int(rownum)>=rowstorage+9):
#print(‘inside’)
straddress=’C’ +str(tempcount)+’:C’ + str(rownum)
#print(straddress)

ws.cell(row=rownum, column=14,value=”=AVERAGE(“+straddress+”)”).number_format = ‘0.00’
if (int(rownum)==rowstorage+9):
tempu=ws.cell(row=rownum, column=14).value
ws.cell(row=rownum, column=16,value=tempu).number_format = ‘0.00’

tempcount+=1
if (int(rownum)>=rowstorage+10):
ws.cell(row=rownum, column=15,value=(2/(10+1))).number_format = ‘0.00’

## if (int(rownum)>=rowstorage+10):
## tempu2=(ws.cell(row=rownum-1, column=16).value)
##
## tempu3=(ws.cell(row=rownum, column=3).value)
## tempu4=(ws.cell(row=rownum, column=15).value)
## print(tempu2)
## print(tempu3)
## print(tempu4)
## ws.cell(row=rownum, column=16,value=tempu4*(tempu3-tempu2)+tempu2).number_format = ‘0.00’
cell = ws[“A2″]
try:

val = float(cell.value)
#print(‘val is ‘+str(val)+ ‘ and tempval is ‘+str(tempval))
if tempval!=0.0:

if (val>tempval) and (rownum!=1):

ws.cell(row=rownum, column=colnum+7,value=’UP’)
tempval=val
elif tempval>val and (rownum!=1):
#print(tempval)
ws.cell(row=rownum, column=colnum+7,value=’DOWN’)
tempval=val
elif (tempval==val) and (rownum!=1):

ws.cell(row=rownum, column=colnum+7,value = ‘same’)
tempval=val
else:
pass

else:
tempval=val

#print (val)
if ws.cell(row=rownum, column=colnum).value!=” and ws.cell(row=rownum+1, column=colnum).value!=”:
try:
val1=float(ws.cell(row=rownum, column=colnum).value)
val2=float(ws.cell(row=rownum+1, column=colnum).value)
returnval=(val1/val2)-1

except:
returnval=0.0
#ws.cell(row=rownum, column=colnum+8,value=returnval)
ws.cell(row=rownum, column=colnum+8,value=returnval).number_format = ‘0.00%’

else:
pass
 

except ValueError:
pass
#print (count)
straddress=’K2:K’ + str(count)
#print (straddress)
ws[“M2”] = “=AVERAGE(“+straddress+”)”
ws[“M3”] = “=VARP(“+straddress+”)”
ws[“M4”] = “=STDEV(“+straddress+”)”
ws.column_dimensions[‘N’].width = 15

ws.column_dimensions[‘O’].width = 20

ws.column_dimensions[‘P’].width = 15
wb.save(‘google_stock_data.xlsx’)

wb.close()

import yahoo_finance
import sys
import csv
import xlsxwriter
import openpyxl
import xlrd
import pandas as pd
from openpyxl.utils import coordinate_from_string, column_index_from_string

symbol = yahoo_finance.Share(“GOOG”)
google_data = symbol.get_historical(“2015-06-25”, “2016-06-30”)
google_df = pd.DataFrame(google_data)

# Output data into CSV
google_df.to_csv(“/home/sourav/google_stock_data.csv”)
#wb=xlsxwriter.Workbook(“google_stock_data.csv”.replace(“csv”,”xlsx”))
wb = openpyxl.Workbook()
ws = wb.active
ws.title=’google_stock_data_experiment’
f = open(‘google_stock_data.csv’)
reader = csv.reader(f, delimiter=’,’)
for row in reader:
ws.append(row)
f.close()

wb.save(‘google_stock_data.xlsx’)
ws=wb.active
ws.cell(row=1, column=10,value=’Trend’)
ws.cell(row=1, column=11,value=’Return’)
wb.save(‘google_stock_data.xlsx’)
tempval=0.0
values=[]
returnval=0.00
count=0
for row in ws.iter_rows(min_row=1, min_col=3, max_col=3):

for cell in row:
count+=1
#print(cell.coordinate)
xy = coordinate_from_string(cell.coordinate) # returns (‘A’,4)
colnum = column_index_from_string(xy[0]) # returns 1
rownum = xy[1]
#print(str(colnum) +’ ‘+str(rownum)+’ has a value ‘+ws.cell(row=rownum, column=colnum).value)

if ws.cell(row=rownum, column=colnum).value==”:

break
try:

val = float(cell.value)
#print(‘val is ‘+str(val)+ ‘ and tempval is ‘+str(tempval))
if tempval!=0.0:

if (val>tempval) and (rownum!=1):

ws.cell(row=rownum, column=colnum+7,value=’UP’)
tempval=val
elif tempval>val and (rownum!=1):
#print(tempval)
ws.cell(row=rownum, column=colnum+7,value=’DOWN’)
tempval=val
elif (tempval==val) and (rownum!=1):

ws.cell(row=rownum, column=colnum+7,value = ‘same’)
tempval=val
else:
pass

else:
tempval=val

#print (val)
if ws.cell(row=rownum, column=colnum).value!=” and ws.cell(row=rownum+1, column=colnum).value!=”:
try:
val1=float(ws.cell(row=rownum, column=colnum).value)
val2=float(ws.cell(row=rownum+1, column=colnum).value)
returnval=(val1/val2)-1

except:
returnval=0.0
#ws.cell(row=rownum, column=colnum+8,value=returnval)
ws.cell(row=rownum, column=colnum+8,value=returnval).number_format = ‘0.00%’
else:
pass

except ValueError:
pass
#print (count)
straddress=’K2:K’ + str(count)
#print (straddress)
ws[“M2”] = “=AVERAGE(“+straddress+”)”
ws[“M3”] = “=VARP(“+straddress+”)”
ws[“M4”] = “=STDEV(“+straddress+”)”
wb.save(‘google_stock_data.xlsx’)

wb.close()

import pandas_datareader as pdr
from datetime import datetime

ibm = pdr.get_data_yahoo(symbols=’IBM’, start=datetime(2000, 1, 1), end=datetime(2012, 1, 1))
print(ibm[‘Adj Close’])

import yahoo_finance
import sys
import csv
import xlsxwriter
import openpyxl
import pandas as pd
from openpyxl.utils import coordinate_from_string, column_index_from_string

symbol = yahoo_finance.Share(“GOOG”)
google_data = symbol.get_historical(“2015-06-25”, “2016-06-30”)
google_df = pd.DataFrame(google_data)

# Output data into CSV
google_df.to_csv(“/home/sourav/google_stock_data.csv”)
#wb=xlsxwriter.Workbook(“google_stock_data.csv”.replace(“csv”,”xlsx”))
wb = openpyxl.Workbook()
ws = wb.active
ws.title=’google_stock_data_experiment’
f = open(‘google_stock_data.csv’)
reader = csv.reader(f, delimiter=’,’)
for row in reader:
ws.append(row)
f.close()

wb.save(‘google_stock_data.xlsx’)
ws=wb.active
ws.cell(row=1, column=10,value=’Trend’)
wb.save(‘google_stock_data.xlsx’)
tempval=0.0
values=[]
for row in ws.iter_rows(min_row=2, min_col=3, max_col=3):

for cell in row:
#print(cell.coordinate)
xy = coordinate_from_string(cell.coordinate) # returns (‘A’,4)
colnum = column_index_from_string(xy[0]) # returns 1
rownum = xy[1]
#print(str(colnum) +’ ‘+str(rownum)+’ has a value ‘+ws.cell(row=rownum, column=colnum).value)

if ws.cell(row=rownum, column=colnum).value==”:
break
try:

val = float(cell.value)
#print(‘val is ‘+str(val)+ ‘ and tempval is ‘+str(tempval))
if tempval!=0.0:

if val>tempval:

ws.cell(row=rownum, column=colnum+7,value=’UP’)
tempval=val
elif tempval>val:
#print(tempval)
ws.cell(row=rownum, column=colnum+7,value=’DOWN’)
tempval=val
else:
ws.cell(row=rownum, column=colnum+7,value = ‘same’)
tempval=val
else:
tempval=val

#print (val)
except ValueError:
pass
wb.save(‘google_stock_data.xlsx’)

wb.close()

import yahoo_finance
import pandas as pd

symbol = yahoo_finance.Share(“GOOG”)
google_data = symbol.get_historical(“2014-01-01”, “2016-06-30”)
google_df = pd.DataFrame(google_data)

# Output data into CSV
google_df.to_csv(“/home/sourav/google_stock_data.csv”)

 

Source:http://stackoverflow.com/questions/12433076/download-history-stock-prices-automatically-from-yahoo-finance-in-python

#trivia game by Sourav Bhattacharya
try:
game_file=open(“trivia.txt”,”r”)
except IOError as e:
print ‘unable to open the file trivia.txt ending program \n’,e
raw_input(“press any key to exit \n”)
sys.exit()
else:
pass
line=game_file.readline()
line=line.replace(“/”,”\n”)
line=line[0:len(line)-2]
title=line

print “\t\tWelcome to Trivia Challenge!\n”
print “\t\t”, title, “\n”
score=0
count=0
while 1:

line=game_file.readline()
line=line.replace(“/”,”\n”)
line=line[0:len(line)-2]
category=line
if category == ”:
break
count+=1
print category
line=game_file.readline()
line=line.replace(“/”,”\n”)
line=line[0:len(line)-2]
question=line
print question
answer=[]
for i in range(0,4):
line=game_file.readline()
line=line.replace(“/”,”\n”)
line=line[0:len(line)-2]
answer.append(line)
for i in range(0,4):
print “\t”,i+1,”-“,answer[i]

correct=game_file.readline()
correct=correct[0]
#print correct
answer=raw_input(“please enter correct answer 1-4”)
line=game_file.readline()
line=line.replace(“/”,”\n”)
line=line[0:len(line)-2]
if answer==correct:
score+=1
print ‘right!= ‘,line
print ‘score= ‘,score

else:
print ‘wrong!= ‘,line
print ‘score= ‘,score

print ‘\n\n’
print ‘that was the final question’
print ‘your score is ‘,score, ‘ out of ‘,count

game_file.close

import pickle, shelve
print(“Pickling lists.”)
variety = [“sweet”, “hot”, “dill”]
shape = [“whole”, “spear”, “chip”]
brand = [“Claussen”, “Heinz”, “Vlassic”]
f = open(“pickles1.dat”, “wb”)
pickle.dump(variety, f)
pickle.dump(shape, f)
pickle.dump(brand, f)
f.close()
print(“\nUnpickling lists.”)
f = open(“pickles1.dat”, “rb”)
variety = pickle.load(f)
shape = pickle.load(f)
brand = pickle.load(f)

print(variety)
print(shape)
print(brand)
f.close()

Older Posts »

Categories

%d bloggers like this: