# -*- coding: utf-8 -*- """ Created on Thu Oct 3 15:57:44 2024 @author: turinici """ """ This program uses historical data in the format in : https://turinici.com/wp-content/uploads/cours/common/close_cac40_historical.csv It can also be downloaded form yahoo finance in daily prices (at least the "close") if possible at lest 5 years Idea: use yahoo e.g., yfinance package "pip install yfinance" Then the code does : 1'/ order by increasing date 2/ plot price histogram and returns (with "log" and/or "actuarial") 3/ test normality of : prices, log returns, actuarial returns for instance can use scipy.stats.normaltest 4/ shows the random versus optimal results 5/ show what a backtest would obtain """ import pandas as pd import numpy as np import matplotlib.pyplot as plt from scipy.stats import normaltest,kstest #from scipy.special import softmax # take csv from www course :we suppose it is available locally data = pd.read_csv('close_cac40_historical.csv',sep=';', index_col = 'Date') data.head() # order by increasing date, keep variable 'data' data.sort_index(ascending=True,inplace=True) #data.sort(by='Date',inplace=True) data.head() data.tail() # plot histogram of prices _=data.hist(bins=30, figsize = (15,15)) def normality_test(data,kolmogorov_smirnov=False,level=0.01,print_results=True): """ Tests normality of each column of dataframe "data". Inputs: kolmogorov_smirnov= false: use "normaltest", otherswise use kstest, both from scipy.stats level = p-value threshold level for the conclusions Outputs: the number of yes/no in the results """ pvalues=[] for cols in data.keys(): # normality test if(kolmogorov_smirnov): # put in pv the p-value of the Kolmogorov-Smirnov test pv=kstest(data[cols].dropna(),'norm').pvalue else: # put in pv the p-value of the default scipy.stats normality test pv=normaltest(data[cols].dropna()).pvalue pvalues.append(pv) if(pv