File size: 1,399 Bytes
642c876
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import numpy as np
import pandas as pd
import streamlit as st

@st.cache(persist=True, show_spinner=False)
def absolute_return(prices):
  'a function to calculate the absolute return given a daily price series'
  abs_rtn = ((prices.iloc[-1]/prices[0])-1)
  return abs_rtn

@st.cache(persist=True, show_spinner=False)
def annual_return(prices):
  'a function to calculate the annualised return given a daily price series'
  abs_rtn = absolute_return(prices)
  annual_rnt = (pow((abs_rtn/100)+1, 365/len(prices))-1)*100
  return annual_rnt

@st.cache(persist=True, show_spinner=False)
def max_drawdown(prices):
  '''
  A function to calculate the max drawdown for a given price series "prices"
  as well as the index of the start of the max drawdown period, "start_idx"
  and the index of end of the max drawdwon period, "end index"
  '''
  if type(prices)==type(pd.Series(dtype='object')):
      prices = prices.values
  end_idx = np.argmax(np.maximum.accumulate(prices) - prices) # end of the period
  start_idx = np.argmax(prices[:end_idx]) # start of period
  max_dd = (prices[start_idx]-prices[end_idx])/prices[start_idx]
  return max_dd, start_idx, end_idx

@st.cache(persist=True, show_spinner=False)
def annual_vol(prices):
  '''
  A function to calculate the annuaised volatility of a price series assuming
  cryptos trade 365 days a year
  '''
  return prices.pct_change().std()*(365**0.5)