Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add detailed monthly returns heatmap with drawdown information #45

Merged
merged 11 commits into from
Jul 22, 2024

Conversation

twkim112
Copy link
Contributor

KakaoTalk_Photo_2024-06-26-16-56-50

Key changes:

  • Implement new monthly_returns_detailedview function in wrappers.py
  • Add corresponding monthly_heatmap_detailedview function in core.py

This new visualization provides a comprehensive view of strategy performance, allowing users to quickly identify patterns, strong/weak periods, and the relationship between returns and drawdowns over time.

twkim112 and others added 2 commits June 26, 2024 16:59
…early return & drawdown information.

Example: https://imgur.com/a/PsKjhWy

- Implement new monthly_returns_detailedview function in wrappers.py
- Add corresponding monthly_heatmap_detailedview function in core.py
Add detailed monthly returns heatmap with drawdown information
Copy link

korbit-ai bot commented Jun 26, 2024

My review is in progress 📖 - I will have feedback for you in a few minutes!

Copy link

@korbit-ai korbit-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have reviewed your code and found 1 potential issue. To discuss my individual comments that I have added, tag me in replies using @korbit-ai.


Please react with a 👍 to my comments that you find helpful and a 👎 to those you find unhelpful - this will help me learn and improve as we collaborate.

Comment on lines 1203 to 1213
def monthly_heatmap_detailedview(
returns,
benchmark=None,
grayscale=False,
figsize=(14, 6),
annot_size=10,
returns_label="Strategy",
fontname="Arial",
savefig=None,
show=True,
):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

category Readability and Maintainability

The monthly_heatmap_detailedview function is quite complex and lengthy, which can impact readability and maintainability. To improve this, consider breaking it down into smaller, more focused functions. This could involve separating the calculations of returns and drawdowns, as well as the heatmap generation, into their own functions. This will make the code easier to understand and maintain over time.

@grzesir
Copy link
Contributor

grzesir commented Jun 26, 2024 via email

@twkim112
Copy link
Contributor Author

Thank you for your feedback. I'll adjust the MDD to show the maximum drawdown for the entire year, matching the annual return figure.
Regarding the monthly maxDD calculation I am going to fix code especially for October 2023 to correct the discrepancy you noted.

@grzesir
Copy link
Contributor

grzesir commented Jun 27, 2024 via email

…onth-end basis

- Ensures consistency between monthly returns and drawdowns calculations
- Addressed feedback:
  - Removed "Year" and "Month" labels from axes
  - Updated title format
  - Adjusted layout
  - Added 'YTD' label to clarify year-to-date metrics on left margin
@twkim112
Copy link
Contributor Author

I've addressed most of the comments in the feedback. When I checked the returns file and looked at the period from the end of September to October 2023, I found that the return calculated from the last trading day of September to the last trading day of October is -7.67%, which is why it's displayed that way. However, the monthly drawdown was shown as -6.37% because it was based on October 1st to 31st. I've modified the function to calculate the monthly drawdown on a month-end to month-end basis, consistent with the returns calculation. I've also implemented the other suggested changes from the feedback.

image

@twkim112
Copy link
Contributor Author

I agree. I don't think this feature is particularly necessary for a basic HTML report. This code was created to view monthly drawdowns separately when generating specific plots.

@twkim112
Copy link
Contributor Author

twkim112 commented Jul 1, 2024

image

I've been making several attempts and have made some progress, but I'm still encountering a couple of issues:

  • When I reduce the font size of the monthly drawdown, the font color doesn't match the return value color as intended. Instead, all drawdown values appear in the same color.
  • Regarding the YTD section, I'm wondering if it's possible to apply multiple font sizes to yticklabels in seaborn. Specifically, I want to make the drawdown percentage smaller than the year and return percentage. I'm not sure if these limitations are inherent to seaborn or if there's a workaround I'm missing.

I've moved the YTD label to the right-aligned position, which matches the reference image.

@twkim112
Copy link
Contributor Author

twkim112 commented Jul 2, 2024

for i in range(pivot_returns.shape[0]):
    for j in range(pivot_returns.shape[1]):
        cell = ax.get_children()[i * pivot_returns.shape[1] + j + 1] 
        return_color = cell.get_color()
        monthly_dd_color = 'white' if return_color == 'w' else 'black'
        ax.text(j + 0.5, i + 0.55, annot_drawdowns.iloc[i, j],
                ha='center', va='top', fontsize=annot_size * 0.8, color=monthly_dd_color)

When I added this code, it produced a plot that looks correct as shown below. I'm still unsure how to apply different font sizes to yticklabels in seaborn, particularly making the drawdown percentage smaller than the year and return percentage in the YTD section.

image

- Modify YTD label positioning
- Set both monthly and yearly drawdown font sizes to 80% of return font size
- Change basic annot_size to 11
@twkim112
Copy link
Contributor Author

twkim112 commented Jul 2, 2024

image

  1. Y-axis label and drawdown display have been adjusted for better alignment and clarity.
  2. The YTD (Year-to-Date) label position has been modified to ensure it's properly aligned with other elements.
  3. Both monthly and yearly drawdown font sizes are now set to 80% of the return font size.
  4. The base annotation size annot_size has been increased to 11, providing a better default size for the heatmap's text elements.

@twkim112
Copy link
Contributor Author

twkim112 commented Jul 3, 2024

image

For the existing white font tile, I set it to the gainsboro color because if I changed this colors dimgray, it would look bad on certain tiles. The following plot shows an example with all monthly dd set to dimgray.

image

@twkim112
Copy link
Contributor Author

twkim112 commented Jul 3, 2024

image

It seems like () is needed for negative numbers like in financial statements, but I don't think it's necessary since there is an explicit - sign. The version without the () looks a bit cleaner, though.

- add monthly_dd_font_rate, annual_dd_font_rate argument, use can edit both
- increase default annual_dd_font_rate to 0.9
@twkim112
Copy link
Contributor Author

twkim112 commented Jul 3, 2024

  • remove parentheses for annual and monthly dd
  • add monthly_dd_font_rate, annual_dd_font_rate argument, a user can edit both
  • increase default annual_dd_font_rate to 0.9

@twkim112
Copy link
Contributor Author

twkim112 commented Jul 3, 2024

image

The cumulative returns of the strategy I used show many empty spaces without % because the period starts from 2019, making it look sparse. However, as you mentioned, if the period gets longer, it look strange with too many %.

…n auto-detected size and a user-specified rate.

- Add `return_font_rate` argument.
- Improvements to the heatmap rendering process, including better handling of null values and grid removal.
- Change gainsboro drawdown color to white for better visibility on orange tiles
@twkim112
Copy link
Contributor Author

twkim112 commented Jul 4, 2024

image

  • Dynamic font size adjustment for returns has been implemented. Now, sns.heatmap automatically determines the optimal font size to fill the tiles, similar to the original layout.
  • A new return_font_rate argument has been added. This allows users to fine-tune the font size if they find it too large or small for their specific needs.
  • The heatmap rendering process has been improved, including better handling of null values and removal of the grid. This addresses the thin vertical grey bars issue for the remainder of 2024.
  • The drawdown color has been changed from gainsboro to white for better visibility on orange tiles.

Regarding the issue of overlapping returns and drawdowns in the YTD section for long-term data like BTC, I experimented with reducing the font size when overlaps occur. However, this approach led to two problems: the font became too small compared to the returns in the central heatmap, and overall readability was significantly compromised. Instead of reducing font size of it, I think it's better that users adjust the figsize argument to accommodate longer time periods. This approach maintains readability while allowing for more data to be displayed clearly. At this point, I believe this is the most suitable solution, as it gives users the flexibility to optimize the display for their specific data range.

figsize = (14, 6), return_font_rate = 1.0
image

figsize = (16, 9), return_font_rate = 1.0
image

- Adjust annual_dd_font_rate to 0.8
- Adjust vertical position of yearly return & dd by 0.05 up.
@twkim112
Copy link
Contributor Author

twkim112 commented Jul 4, 2024

I realized that sns.heatmap doesn't automatically adjust the font size, but simply uses rcParams['font.size']. So I rolled back to the previous format. I kept the feature where users can either use annot_size as is or adjust it with return_font_rate.

@twkim112
Copy link
Contributor Author

twkim112 commented Jul 4, 2024

  • Sample: btc["Close"], figsize=(14, 10), annot_size=11, return_font_rate=1.2
    image

@twkim112
Copy link
Contributor Author

twkim112 commented Jul 4, 2024

  • Fix annual_returns calculation
    image

@twkim112
Copy link
Contributor Author

twkim112 commented Jul 5, 2024

Thanks for the suggestions. I propose merging this PR for now. I'll create a new pull request for these improvements for future implementation when I have more bandwidth, including your suggestions and an update of the original heatmap.

@BlackArbsCEO
Copy link

this is a great addition. any ideas when this can get merged?

@grzesir
Copy link
Contributor

grzesir commented Jul 10, 2024 via email

@grzesir
Copy link
Contributor

grzesir commented Jul 22, 2024

this is awesome @twkim112 ! great work! merging now

@grzesir grzesir merged commit 2e22cff into Lumiwealth:main Jul 22, 2024
@grzesir
Copy link
Contributor

grzesir commented Jul 22, 2024

This is now deployed in v0.3.2! 🎉

@twkim112
Copy link
Contributor Author

I'll create a PR in the next few days!

@twkim112
Copy link
Contributor Author

Okay I'll try adding the cbar option to the original heatmap and see if that affects the width

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants