I have a for-loop that computes DataFrames and outputs them to an excel sheet. Right now, I am able to export one full DF to one excel sheet.
As shown below, I am trying to add code inside the for-loop so that it can do this computation for many DFs and export each DF to a separate excel worksheet within the same workbook, delineated by the sheet_name.
list = ["AUD_JPY", "AUD_USD"]
granularity = "D"
bar_count = 5000
MA_list = [20, 50]
writer = pd.ExcelWriter('BT.xlsx')
resultsDF = pd.DataFrame(columns = ['instrument', 'Start date', 'End date']
for MA in MA_list:
for instrument in list:
...
data = {'instrument': [instrument], 'Start date': [startDate], 'End date': [endDate]}
resultsDF = resultsDF.append(data, ignore_index=True)
instrument=+1
resultsDF.to_excel(writer, sheet_name='Price_Over_SMA{0}_{1}'.format(MA,
granularity))
writer.save()
MA=+1
This only exports the first DF from the 'instrument' loop to the excel sheet. I get this worksheet:
But I also want to export another worksheet next to that one that should be called 'Price_Over_SMA50_D' in this case.
Not sure what I am doing wrong.
Thanks