-2

So I have written a program that works fine. It pulls stock data from a website based off of a ticker then puts it into an excel spreadsheet based off of what I want the rows and columns to be. I would like to be able to do this with multiple stocks, however I do not know how to write a function. So, what I have been doing is copying and pasting the code over and over again. The only parameters that change in my code is the ticker and the row numbers labeled r, x, and y. Can someone please help me write a function for my program?

fd = gm.FinancialsDownloader()
fd_frames = fd.download('AAPL')
wb = UpdateWorkbook(r'C:\Users\vince\Project\Stock Python Project\Spreadsheet.xlsx', worksheet=1)
i_s = fd_frames['income_statement']
i_s.set_index('title', inplace=True)
i_s = i_s.drop('parent_index', axis=1)
i_s = i_s.loc[['Revenue','Operating expenses']] #Add all the names you want from income statement
i_s = i_s/(10**9)

b_s = fd_frames['balance_sheet']
b_s.set_index('title', inplace=True)
b_s = b_s.drop('parent_index', axis=1)
b_s = b_s.loc[['Assets','Current assets']]
b_s = b_s/(10**9)

c_f = fd_frames['cash_flow']
c_f.set_index('title', inplace=True)
c_f = c_f.drop('parent_index', axis=1)
c_f = c_f.loc[['Free Cash Flow','Operating cash flow']]
c_f = c_f/(10**9)

r = 6  # starts at whatever row
c = 13 # column 'M'
for l in i_s.values.tolist():
    for item in l:
        wb.ws.cell(row=r, column=c).value = item
        c += 1 # Column 'N'
    c = 13
    r += 1

x = 21   
for l in b_s.values.tolist():
    for item in l:
        wb.ws.cell(row=x, column=c).value = item
        c += 1 # Column 'N'
    c = 13
    x += 1

y = 41
for l in c_f.values.tolist():
    for item in l:
        wb.ws.cell(row=y, column=c).value = item
        c += 1 # Column 'N'
    c = 13
    y += 1
wb.save()
vdub32
  • 193
  • 1
  • 5
  • 12

1 Answers1

0

You can make a function in pure javascript, like

function demo(var1,var2,var3,var4){

    // custom code and calculation here 

}

    //In your case you can use above function like this

function getFinancialData(ticker,r,x,y){
// your above calculation here
}

  // call the getFinancialData and pass the dynamic value of ticker, r,x and y

   getFinancialData(ticker,r,x,y);