from nsepy import get_history
from datetime import date
import csv
company=['TCS','RELIANCE','INFY','HDFCBANK','ITC','SUNPHARMA','HINDUNILVR','COALINDIA','ONGC','HDFC','SBIN','BHARTIARTL','WIPRO','ICICIBANK','KOTAKBANK','HCLTECH','LT','MARUTI','TATAMOTORS','NTPC','AXISBANK','IOC','ULTRACEMCO','ASIANPAINT','HINDZINC','M&M','POWERGRID','INFRATEL','BAJAJ-AUTO','LUPIN','BPCL','BOSCHLTD','HEROMOTOCO','INDUSINDBK','EICHERMOT','DRREDDY','ADANIPORTS','GODREJCP','TECHM','GAIL','DABUR','AUROPHARMA','SHREECEM','CIPLA','IDEA','SIEMENS','NMDC','BAJFINANCE','ZEEL','YESBANK','AMBUJACEM','GRASIM','MOTHERSUMI','BANKBARODA','MCDOWELL-N','CADILAHC','GLAXO','BRITANNIA','MARICO','INDIGO','TATASTEEL','JSWSTEEL','ASHOKLEY','PIDILITIND','TITAN','OFSS','BEL','CAIRN','BHEL','IBULHSGFIN','BAJAJFINSV','ABB','NHPC','HINDPETRO','VEDL','DIVISLAB','ACC','GSKCONS','LICHSGFIN','CONCOR','CUMMINSIND','TORNTPHARM','PFC','COLPAL','GLENMARK','UBL','SRTRANSFIN','EMAMILTD','UPL','DLF','PGHH','BHARATFORG','HAVELLS','OIL','PETRONET','CASTROLIND','APOLLOHOSP','RAJESHEXPO','HINDALCO','PEL','SAIL','TATAPOWER','BERGEPAINT','PNB','RECLTD','IDFCBANK','BAJAJHLDNG','MRF','ALKEM','TVSMOTOR','KANSAINER','AMARAJABAT','SUNTV','SUNDARMFIN','GILLETTE','BLUEDART','RELINFRA','M&MFIN','RPOWER','PAGEIND','IDBI','CRISIL','RCOM','AJANTPHARM','CENTRALBK','3MINDIA','NEYVELILIG','GODREJIND','SJVN','WABCOINDIA','EXIDEIND','MRPL','JSWENERGY','NBCC','APLLTD','CHOLAFIN','ABFRL','ALSTOMT&D','L&TFH','TORNTPOWER','MINDTREE','WOCKPHARMA','TATACOMM','ABIRLANUVO','VAKRANGEE','MPHASIS','CANBK','NATIONALUM','ADANIPOWER','WELSPUNIND','SHRIRAMCIT','STAR','BIOCON','TATACHEM','RAMCOCEM','SUPREMEIND','RELCAPITAL','NAUKRI','DISHTV','SANOFI','VOLTAS','MFSL','THERMAX','UNIONBANK','WHIRLPOOL','APOLLOTYRE','AIAENG','GRUH','GPPL','JUBLFOOD','SYMPHONY','PFIZER','OBEROIRLTY','IRB','FORTIS','HEXAWARE','ADANIENT','INDHOTEL','FEDERALBNK','GSPL','IGL','BANKINDIA','PIIND','SYNGENE','TATAGLOBAL','LALPATHLAB','KAJARIACER','GUJGASLTD','HONAUT','SRF','IPCALAB','TUBEINVEST','NATCOPHARM','SUZLON','MUTHOOTFIN','SPARC','ARVIND','GMRINFRA','SKSMICRO','TV18BRDCST','THOMASCOOK','SKFINDIA','JUBILANT','BATAINDIA','PCJEWELLER','IIFL','DALMIABHA','PRESTIGE','IDFC','AKZOINDIA','MAHINDCIE','FAGBEARING','CESC','SOLARINDS','JETAIRWAYS','GODFRYPHLP','BALKRISIND','PERSISTENT','EIHOTEL','CENTURYTEX','GODREJPROP','TATAELXSI','NH','INOXWIND','ENGINERSIN','DHFL','DBCORP','BAJAJCORP','GUJFLUORO','CUB','COROMANDEL','JINDALSTEL','JUSTDIAL','KARURVYSYA','ECLERX','TRENT','JYOTHYLAB','KRBL','JAGRAN','IOB','SADBHAV','RELIGARE','ADVANTA','TTKPRESTIG','RDEL','FRL','INDIANB','ASTRAL','VTL','CYIENT','JKCEMENT','GESHIP','HINDCOPPER','COFFEEDAY','NETWORK18','PHOENIXLTD','REDINGTON','EDELWEISS','ATUL','FINPIPE','HATSUN','SYNDIBANK','CEATLTD','RELAXO','BEML','SUNCLAYLTD','FINCABLES','AARTIIND','NCC','UCOBANK','ZENSARTECH','IFCI','AIL','PRISMCEM','JKLAKSHMI','CORPBANK','CAPF','ICRA','MOTILALOFS','ALLCARGO','ICIL','EIDPARRY','BASF','CENTURYPLY','ADANITRANS','LAXMIMACH','ENIL','MMTC','GRINDWELL','ASAHIINDIA','MOIL','REPCOHOME','SADBHIN','SUNDRMFAST','VIDEOIND','STRTECH','SEQUENT','BLUESTARCO','SBBJ','SCHNEIDER','SINTEX','MHRIL','PVR','ANDHRABANK','FCEL','MAHABANK','VRLLOG','LAOPALA','RALLIS','CARBORUNIV','HITACHIHOM','SHILPAMED','ALBK','FDC','SHK','AEGISCHEM','HATHWAY','ASHOKA','TRITURBINE','KALPATPOWR','KEC','KPRMILL','ORIENTCEM','TECHNO','CANFINHOME','HDIL','CROMPGREAV','COX&KINGS','KIRLOSENG','GDL','NIITTECH','SHOPERSTOP','SCI','GREAVESCOT','JMFINANCIL','TIMKEN','CHENNPETRO','DHANUKA','MANAPPURAM','RTNPOWER','J&KBANK','KPIT','ASTRAZEN','MONSANTO','ZYDUSWELL','BIRLACORPN','WABAG','SREINFRA','CARERATING','OCL','ORIENTBANK','SBT','PNCINFRA','DISHMAN','VIJAYABANK','SOBHA','INDOCO','JISLJALEQS','INDIACEM','VGUARD','OMAXE','BALRAMCHIN','WELCORP','TRIDENT','KSCL','TATAINVEST','NAVKARCORP','IBREALEST','SFCL','CCL','GSFC','BBTC','VSTIND','GULFOILLUB','GRANULES','ESSELPACK','RAYMOND','KWALITY','SUVEN','SITICABLE','SOUTHBANK','CERA','IL&FSTRANS','MANPASAND','CHAMBLFERT','TIDEWATER','LINDEINDIA','INTELLECT','FSL','DCMSHRIRAM','DCBBANK','KSBPUMPS','TEXRAIL','WONDERLA','MINDACORP','8KMILES','RUPA','KKCL','RESPONIND','RATNAMANI','TCI','RCF','NESCO','RAMCOSYS','BFUTILITIE','SHARDACROP','JBCHEPHARM','GREENPLY','BAJAJHIND','GMDCLTD','JKIL','HMT','KITEX','ELGIEQUIP','INGERRAND','AHLUCONT','MCLEODRUSS','HSIL','NAVNETEDUL','HFCL','UNICHEMLAB','VINATIORGA','BRFL','KTKBANK','HIMATSEIDE','BAJAJELEC','JKTYRE','PFS','POLARIS','MARKSANS','PTC','JPASSOCIAT','DENABANK','GAYAPROJ','INOXLEISUR','TVTODAY','HEIDELBERG','HMVL','AVANTIFEED','FMGOETZE','MAYURUNIQ','HTMEDIA','JINDALPOLY','APARINDS','MAGMA','TVSSRICHAK','TAKE','MYSOREBANK','MAHLIFE','SUPRAJIT','ESCORTS','DPSCLTD','ITDCEM','EVEREADY','IGARASHI','TATACOFFEE','LUXIND','SOMANYCERA','NAVINFLUOR','NILKAMAL','BALMLAWRIE','BRIGADE','BANARISUG','UNITEDBNK','MINDAIND','PRAJIND','CLNINDIA','EROSMEDIA','APLAPOLLO','PFOCUS','SONATSOFTW','TEAMLEASE','CAPLIPOINT','QUICKHEAL','PAPERPROD','DELTACORP','HCC','HCG','TNPL','FLFL','VESUVIUS','JBFIND','VIPIND','LAKSHVILAS','NBVENTURES','KNRCON','SUNTECK','PRECAM','GALLISPAT','GRAPHITE','MAHSCOOTER','KSK','NFL','PSB','LITL','JPPOWER','SKIPPER','RENUKA','GENUSPOWER','ONMOBILE'] #complete thee
import xlsxwriter
workbook = xlsxwriter.Workbook('Stock.xlsx')
worksheet = workbook.add_worksheet()
k=0
for i in company:
for j in range(len(company)):
data = get_history(symbol=j, start=date(2019,12,10), end=date(2020,12,10))
data.keys()
SUM=sum(data['Close'])
TOTAL=len(data['Close'])
SMA=SUM/TOTAL
print(SMA)
worksheet.write(0, k, i)
for row_num, di in enumerate(data[i]):
worksheet.write(row_num+1, k, di)
k+=1
workbook.close()
from openpyxl import load_workbook
wb2 = load_workbook('Stock.xlsx')
wb2.create_sheet('SBIN')
wb2.save('Stock.xlsx')