Calculate Max DrawDown - python

I am using pyfolio to calcuate the maxdrawdown and other risk indicator. What should be adjusted to get the correct value?
Near 27% should be the right maxdrawdown, I don't why some negative value is returned. And it seems the whole drawdown table is not corrected or as expected.
Thanks in advance
benchmark files
results files
import pandas as pd
import pyfolio as pf
import os
import matplotlib.pyplot as plt
from pandas import read_csv
from pyfolio.utils import (to_utc, to_series)
from pyfolio.tears import (create_full_tear_sheet,
create_simple_tear_sheet,
create_returns_tear_sheet,
create_position_tear_sheet,
create_txn_tear_sheet,
create_round_trip_tear_sheet,
create_interesting_times_tear_sheet,)
test_returns = read_csv("C://temp//test_return.csv", index_col=0, parse_dates=True,header=None, squeeze=True)
print(test_returns)
benchmark_returns = read_csv("C://temp//benchmark.csv", index_col=0, parse_dates=True,header=None, squeeze=True)
print(benchmark_returns)
fig = pf.create_returns_tear_sheet(test_returns,benchmark_rets=benchmark_returns,return_fig=True)
fig.savefig("risk.png")
maxdrawdown = pf.timeseries.max_drawdown(test_returns)
print(maxdrawdown)
table = pf.timeseries.gen_drawdown_table(test_returns)
print(table)

Related

Python time series: omitted values after using statsmodels.tsa.seasonal.seasonal_decompose?

Could anyone tell me what I did wrong that my first and last six observations are omitted in the final outcome?
I used the statsmode.tsa.seasonal_decompose to do seasonal adjustment.
Thanks.
import os
import statsmodels.api as sm
import pandas as pd
import numpy as np
#pd.options.display.mpl_style = 'default'
%matplotlib inline
#Load csv data#
cpi = pd.read_csv('/home/pythonwd/thai cpi.csv')
cpi = cpi.dropna()
#Create date and time series#
cpi['date'] = pd.to_datetime(cpi['date'], dayfirst=True)
cpi = cpi.set_index('date')
#Seasonal adjustment#
dec = sm.tsa.seasonal_decompose(cpi["cpi"],model='multiplicative')
dec.plot()
Data before the #Seasonal adjustment# line:
enter image description here
Data afterwards:
enter image description here

Plotly Heatmap - Giving your header/index names

Following the tutorial at - https://plotly.com/python/heatmaps/
If you do the lines
import plotly.express as px
df = px.data.medals_wide(indexed=True)
You can see that the "header" row is named "medals" allowing it to be used as an id later. Similar for nations.
When I create my own pandas dataframe
df = pd.DataFrame(model_data, columns=model_names, index=test_names)
What would I have to add to get the equivalent of "medals" and "nations" from the previous example into my dataframe?
assuming you have a 2D array / list of data
as simple as building data frame in way you note
import plotly.express as px
import pandas as pd
import numpy as np
models = [f"model {n}" for n in range(4)]
tests = [f"test {n}" for n in range(10)]
px.imshow(pd.DataFrame(index=tests, data=np.random.uniform(1,3,(len(tests),len(models))), columns=models))

Plot csv file with min/max/avg using python

I have a csv file with measuring points every 100 seconds.
5 different measurements have been made.
I am looking for a simple solution how to create a line plot with the average value for each measuring point with the min, max values as a bar with Python.
The CSV file looks like this:
0,0.000622,0.000027,0.000033,0.000149,0.000170
100,0.014208,0.017168,0.017271,0.015541,0.027972
200,0.042873,0.067629,0.035837,0.033160,0.018006
300,0.030700,0.018563,0.016640,0.020294,0.020338
400,0.018906,0.016507,0.015445,0.018734,0.017593
500,0.027344,0.045668,0.015214,0.016045,0.015520
600,0.021233,0.098135,0.016511,0.015892,0.018342
First column is in seconds.
Maybe someone can help me with a quick idea.
thanks in advance
--------------------added
What i have so far:
import pandas as pd
input_df = pd.read_csv(input.csv")
input_df['max_value'] = input_df.iloc[:,1:6].max(axis=1)
input_df['min_value'] = input_df.iloc[:,1:6].min(axis=1)
input_df['avg_value'] = input_df.iloc[:,1:6].mean(axis=1)
input_df.plot(x=input_df["0"], y='avg_value')
How can i add error bars (min_value,max_value)
You can use matplotlib. For your problem:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
data=np.array([ [0,0.000622,0.000027,0.000033,0.000149,0.000170],
[100,0.014208,0.017168,0.017271,0.015541,0.027972],
[200,0.042873,0.067629,0.035837,0.033160,0.018006],
[300,0.030700,0.018563,0.016640,0.020294,0.020338],
[400,0.018906,0.016507,0.015445,0.018734,0.017593],
[500,0.027344,0.045668,0.015214,0.016045,0.015520],
[600,0.021233,0.098135,0.016511,0.015892,0.018342] ])
mean = np.mean(data[:,1:], axis=1)
min = np.min(data[:,1:], axis=1)
max = np.max(data[:,1:], axis=1)
errs = np.concatenate((mean.reshape(1,-1)-min.reshape(1,-1), max.reshape(1,-1)-
mean.reshape(1,-1)),axis=0)
plt.figure()
plt.errorbar(data[:,0], mean, yerr=errs)
plt.show()

ValueError: Length of values does not match length of index (PYTHON)

I'm trying to implement the stochastic indicator in TA-Lib but I'm getting the error above. The error is on the last line. Please see code below:
import pandas_datareader as pdr
import datetime
import pandas as pd
import numpy as np
import talib as ta
#Download Data
aapl = pdr.get_data_yahoo('AAPL', start=datetime.datetime(2006, 10, 1), end=datetime.datetime(2012, 1, 1))
#Saves Data as CSV on desktop
aapl.to_csv('C:\\Users\\JDOG\\Desktop\\aapl_ohlc.csv', encoding='utf-8')
#Save to dataframe
df = pd.read_csv('C:\\Users\JDOG\\Desktop\\aapl_ohlc.csv', header=0, index_col='Date', parse_dates=True)
#Initialize the `signals` DataFrame with the `signal` column
signals = pd.DataFrame(index=aapl.index)
signals['signal'] = 0.0
#Create slow stochastics //**Broken**
signals['Slow Stochastics'] = ta.STOCH(aapl.High.values,aapl.Low.values,aapl.Close.values,fastk_period=5,slowk_period=3,slowk_matype=0,slowd_period=3,slowd_matype=0)
Your error is that the STOCH function returns a tuple and you are trying to add a tuple value to your dataframe. Try this:
thirtyyear['StochSlowk'],thirtyyear['StochSlowD'] = ta.STOCH(thirtyyear['High'].values, thirtyyear['Low'].values, thirtyyear['Close'].values, fastk_period=5, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0)

pandas.DataFrame returns Series not a Dataframe

I am working with a series of images. I read them first and store in the list then I convert them to dataframe and finally I would like to implement Isomap. When I read images (I have 84 of them) I get 84x2303 dataframe of objects. Now each object by itself also looks like a dataframe. I am wondering how to convert all of it to_numeric so I can use Isomap on it and then plot it.
Here is my code:
import pandas as pd
from scipy import misc
from mpl_toolkits.mplot3d import Axes3D
import matplotlib
import matplotlib.pyplot as plt
import glob
from sklearn import manifold
samples = []
path = 'Datasets/ALOI/32/*.png'
files = glob.glob(path)
for name in files:
img = misc.imread(name)
img = img[::2, ::2]
x = (img/255.0).reshape(-1,3)
samples.append(x)
df = pd.DataFrame.from_records(samples)
print df.dtypes
print df.shape
Thanks!

Categories