# -*- coding: utf-8 -*-
"""
Created on June 14, 2023

@author: CTING
"""

import pandas as pd
import matplotlib.pyplot as plt

ticker = 'TSLA'

df = pd.read_csv(ticker + '.csv')
df = df.rename(columns={'Unnamed: 0':'Date'})
dateX = pd.to_datetime(df.Date, format='%Y-%m-%d')
dateX.index = range(len(dateX))
df.index = dateX

a = df.adjclose.pct_change()
df['R'] = a

font = {'family':'sans-serif', 'weight':'normal', 'size':16}
plt.rc('font', **font)

fig = plt.figure(figsize=(12,8))
ax = fig.add_subplot(111)
ax.plot(df.R * 100 , c= '#A17511',  lw=1.0)

ax.autoscale(enable=True, axis='x', tight=True)
plt.ylabel('Return in %')
plt.xlabel('')
ax.grid(c='#008000', lw=0.5, ls=':' , which='major')

plt.savefig('plot_return_' + ticker + '.png'  ,dpi=300)
plt.show()


### Statastical Analysis
n = len(df)-1

sample_mean = sum(df.R[1:])/n
print(f'Average daily return = {sample_mean:.3%}')

sample_variance = sum((df.R[1:]-sample_mean)**2)/(n-1)
volatility = pow(sample_variance, 0.5 )
print(f'Daily volatility = {volatility:.3%}')

t_stat = pow(n,0.5)*(sample_mean-0)/volatility
print(f't statistic = {t_stat:.4}')









