Plotting the highest day

Hi,
How to plot the highest day as shown in Section 3 Unit 7?

Course Name: Crypto Trading Strategies: Intermediate, Section No: 3, Unit No: 9, Unit type: Notebook

I am assuming you are referring to bar chart showing Sharpe of each day. If you want to just plot the sharpe ratio in the calculate day wise sharpe ratio part, try this code. I think the values are already printed out.

# Calculate day-wise Sharpe ratios for the full dataset
day_wise_sharpe = calc_sharpe(df)

# Define the standard order of days for the x-axis
days_order = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']

# Reindex the Sharpe values to ensure consistent ordering
day_wise_sharpe = day_wise_sharpe.reindex(days_order)

# Create a simple bar plot
plt.figure(figsize=(10, 6))
bars = plt.bar(day_wise_sharpe.index, day_wise_sharpe.values)

# Add a horizontal line at y=0
plt.axhline(y=0, color='black', linestyle='-', alpha=0.3)

# Customize the plot
plt.title('Day-wise Sharpe Ratio', fontsize=14)
plt.ylabel('Sharpe Ratio', fontsize=12)
plt.grid(axis='y', linestyle='--', alpha=0.3)

# Add data values on top of each bar
for bar in bars:
   height = bar.get_height()
   plt.text(bar.get_x() + bar.get_width()/2., 
            height + (0.05 if height >= 0 else -0.1),
            f'{height:.2f}',
            ha='center', va='bottom' if height >= 0 else 'top')

plt.tight_layout()
plt.show()

But if you want to see Sharpe bar charts for each step of your strategy calculation, you would need to add plotting code inside your backtest loop. Here is the code. Run in the last cell of the notebook. But please do keep in mind there are going to be a lot of plots. It will take some time.

# Initialize the signal column to NaN (Not a number)
df['signal'] = np.nan

# Initialize the highest day variable
highest_day = ''

# Set the iterating variable of the while loop to 61 as we need at least 60 days of data 
# to determine the day on which the Sharpe ratio is highest.
i = 61
while i < len(df):

   # After every 14 days (i%14==0), calculate the day-wise Sharpe ratio for the past 60 days
   if i % 14 == 0:
       sharpe = calc_sharpe(df[i-61:i-1])

       # Determine the day on which the Sharpe ratio is the highest
       highest_day = sharpe.idxmax()
       
       # Create a simple bar chart for the current Sharpe calculation
       plt.figure(figsize=(10, 6))
       
       # Define the standard order of days
       days_order = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
       
       # Reindex to ensure consistent day ordering
       sharpe_plot = sharpe.reindex(days_order)
       
       # Create bars
       bars = plt.bar(sharpe_plot.index, sharpe_plot.values)
       
       # Highlight the highest day in green
       highest_idx = sharpe_plot.index.get_loc(highest_day)
       bars[highest_idx].set_color('green')
       
       # Add a line at y=0
       plt.axhline(y=0, color='black', linestyle='-', alpha=0.3)
       
       # Add title with date
       plt.title(f'Day-wise Sharpe Ratio (as of {df.index[i].strftime("%Y-%m-%d")})', fontsize=14)
       plt.ylabel('Sharpe Ratio', fontsize=12)
       
       plt.show()

   # Set the signal to 1 when the day is equal to highest_day and 0 otherwise
   df.loc[df.index[i], 'signal'] = np.where(
       df.day.iloc[i] == highest_day, 1, 0)

   # Increment the iterating variable (i)
   i = i+1

# Print the last 5 rows of the dataframe df
df.tail()

Let me know if something is not clear.

Thanks for the reply