Skip to content

Commit

Permalink
Merge pull request #127 from sfjep/master
Browse files Browse the repository at this point in the history
Update ytd and mtd calculation
  • Loading branch information
timkpaine authored Mar 24, 2021
2 parents 538cb08 + c5fb6ea commit a019b34
Show file tree
Hide file tree
Showing 2 changed files with 392 additions and 373 deletions.
38 changes: 30 additions & 8 deletions ffn/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ def _calculate(self, obj):
if len(dp) == 1:
return

self.mtd = calc_mtd(dp, mp)
self.ytd = calc_ytd(dp, yp)

# stats using daily data
self.returns = dp.to_returns()
self.log_returns = dp.to_log_returns()
Expand Down Expand Up @@ -241,9 +244,7 @@ def _calculate(self, obj):
self.worst_day = r.min()

self.total_return = obj[-1] / obj[0] - 1
# save ytd as total_return for now - if we get to real ytd
# then it will get updated
self.ytd = self.total_return

self.cagr = calc_cagr(dp)
self.incep = self.cagr

Expand Down Expand Up @@ -294,9 +295,6 @@ def _calculate(self, obj):
self.best_month = mr.max()
self.worst_month = mr.min()

# -2 because p[-1] will be mp[-1]
self.mtd = dp[-1] / mp[-2] - 1

# -1 here to account for first return that will be nan
self.pos_month_perc = len(mr[mr > 0]) / float(len(mr) - 1)
self.avg_up_month = mr[mr > 0].mean()
Expand Down Expand Up @@ -355,6 +353,7 @@ def _calculate(self, obj):
return

denom = dp[: dp.index[-1] - pd.DateOffset(months=6)]

if len(denom) > 0:
self.six_month = dp[-1] / denom[-1] - 1

Expand All @@ -367,9 +366,8 @@ def _calculate(self, obj):
if len(yr) < 2:
return

self.ytd = dp[-1] / yp[-2] - 1

denom = dp[: dp.index[-1] - pd.DateOffset(years=1)]

if len(denom) > 0:
self.one_year = dp[-1] / denom[-1] - 1

Expand Down Expand Up @@ -1271,6 +1269,30 @@ def to_drawdown_series(prices):
return drawdown


def calc_mtd(daily_prices, monthly_prices):
"""
Calculates mtd return of a price series.
Use daily_prices if prices are only available from same month
else use monthly_prices
"""
if len(monthly_prices) == 1:
return daily_prices[-1] / daily_prices[0] - 1
else:
return daily_prices[-1] / monthly_prices[-2] - 1


def calc_ytd(daily_prices, yearly_prices):
"""
Calculates ytd return of a price series.
Use daily_prices if prices are only available from same year
else use yearly_prices
"""
if len(yearly_prices) == 1:
return daily_prices[-1] / daily_prices[0] - 1
else:
return daily_prices[-1] / yearly_prices[-2] - 1


def calc_max_drawdown(prices):
"""
Calculates the max drawdown of a price series. If you want the
Expand Down
Loading

0 comments on commit a019b34

Please sign in to comment.