I need to make a DataFrame (df_max_res) with the 15 best performances from my stock strategies combined with company tickers (AAPL for Apple Computers etc.). I have a list of more than 500 stock tickers that I fetch and on which I analyze using four of my own strategies.
In the for eachP in perf_array
nested inner iteration I obtain performance results from all combinations of the strategy and ticker. I want to save these results to a DataFrame and to a csv file using this code (or a better suggestion):
#==============================================================================
# Saving results in pandas and to a csv-file
#==============================================================================
def saving_res_pandas():
global df_res, df_max_res
df_res = pd.DataFrame(columns=('Strategy', 'Ticker', 'Strat',
'ROI', 'Sharpe R', 'VaR'))
for eachP in perf_array:
df_res.loc[len(df_res) + 1] = [strategy, ticker, strat, stratROI]
# Select the top 15 of all results (ticker/strategy combo) into new df.
df_max_res = df_res[:15]
# Saving to a csv.
df_max_res.to_csv('df_performance_data_sp500ish.csv')
print('After analysing %1.1f Years ~ %d workdays - %d strategies and %d tickers' '\n'
'The following matrix of tickers and strategies show highest ROI: '
% (years, days, len(strategies), len(stock_list))
)
return df_res
#==============================================================================
# Chose which of below methods to save perf-data to disk with
#==============================================================================
saving_res_pandas()
# Reading in df_max_res with best ticker/strategy results
df_max_res = pd.read_csv('df_performance_data_sp500ish.csv')
print(df_max_res)
The code above creates my DataFrame just fine, but it does not save the iteration performance result as I expect.
I am getting this output:
=======================================================
aa === <function strategy1 at 0x00000000159A0BF8> ==
=======================================================
Holdings: 0
Funds: 14659
Starting Valuation: USD 15000.00 ~ DKK: 100000.50
Current Valuation: USD 14659.05 ~ DKK: 97727.49
=== aa == <function strategy1 at 0x00000000159A0BF8> ==
ROI: -1.9 perc. & Annual Profit -1894 DKK ==
######################################################################
cannot set a row with mismatched columns
== ALL Tickers Done for == <function strategy1 at 0x00000000159A0BF8> ==================
Strategy analysis pr ticker - COMPLETE !
Empty DataFrame
Columns: [Unnamed: 0, Strategy, Ticker, ROI, SharpeR, VaR]
Index: []