October 22, 2024
Chicago 12, Melborne City, USA
python

Algo trading Drawdown calculations SettingWithCopyWarning:


I am trying to calculate maximum drawdown using closing price of a stock.

STEP1: Imported the data and sliced that data in a new data frame df1 with date and closing price.

  df = pd.read_csv('BHEL.NS.csv')
  df['Date'] = pd.to_datetime(df['Date'])
  df1 = df[['Date', 'Close']]
  print(df1.head(n=15))

STEP2: Used the data to calculate rolling maxima (maxrollr) and calculate Drawdowns (local maxima-actual) (absolute dollar drawdown)

for x, y in df1.iteritems():
 rollr=[]
 maxrollr=[]
 for i in y:
   rollr.append(i)
   maxrollr.append(max(rollr))
 Drawdown=[]
 for j in range(0,len(rollr)):
  differ=maxrollr[j]-rollr[j]
  Drawdown.append(differ)

STEP3: Added local maxima (maxrollr) and drawdown as a columns in dataframe df1. and used these values to calculate and add two new columns Drawdown in percentage (Max_Drawdown_per) and Drawdown in log terms (Max_Drawdown_Log)

df1['maxrollr']=maxrollr
df1['Drawdown']=Drawdown
df1['Max_Drawdown_per']=-1*((df1['Close']/df1['maxrollr'])-1)
df1['Max_Drawdown_Log']=np.log(df1['maxrollr'])-np.log(df1['Close'])

STEP4: Plotted graph :

date=df1["Date"]
value1=df1["Drawdown"] 
value2=df1["maxrollr"]
value3=df1["Close"]
filt=df1[df1['Drawdown']==df1.Drawdown.max()].index
Dot=df1.loc[filt,'Date']
Dotp=df1.loc[filt,'Close']
fig, ax = plt.subplots(figsize=(11, 7))
plt.plot(Dot,Dotp,'X')
ax.plot(date, value1,label="max drawdowns")
ax.plot(date, value2,label="max roll prices")
ax.plot(date, value3,label="Close price")
plt.legend(loc="best")
plt.show()

The code runs fine but keep getting the following warning!

ERROR OR WARNING:

*Warning (from warnings module):
  File "C:/python37/RMAX ROLLING.py", line 79
    df1['maxrollr']=maxrollr
SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
*

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy


Warning (from warnings module):
  File "C:/python37/RMAX ROLLING.py", line 80
    df1['Drawdown']=Drawdown
SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

Warning (from warnings module):
  File "C:/python37/RMAX ROLLING.py", line 81
    df1['Max_Drawdown_per']=-1*((df1['Close']/df1['maxrollr'])-1)
SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

Warning (from warnings module):
  File "C:/python37/RMAX ROLLING.py", line 82
    df1['Max_Drawdown_Log']=np.log(df1['maxrollr'])-np.log(df1['Close'])
SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

Question: 1. What are the ramifications if I ignore the warning?
2. How to tackle the problem? Suggest solutions

Code ran fine but got the below warning:
ERROR OR WARNING
SettingWithCopyWarning:

Question: 1. What are the ramifications if I ignore the warning?
2. How to tackle the problem? Suggest solutions



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video