You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<matplotlib.axes._subplots.AxesSubplot at 0x7ff98aa50358>
highs= (nifty["52 week high"] ==nifty.High).value_counts()
print("Fraction of times market is at highs:", highs[True]/(highs[True] +highs[False]))
lows= (nifty["52 week low"] ==nifty.Low).value_counts()
print("Fraction of times market is at lows:", lows[True]/(lows[True] +lows[False]))
Fraction of times market is at highs: 0.11054456455231491
Fraction of times market is at lows: 0.016724454415663878
# Variables almost remaining constantsdebt_rate=0.074# percent
# DebtCorpus with both a deposit() and a withdraw() function classDebtCorpus:
def__init__(self):
self.balance=0self.date=datetime(1990, 1, 1)
defCheck(self, date):
if (date<self.date):
print("Date cannot be less than last account operation date")
returnFalsereturnTruedefDeposit(self, date, amount):
if (self.Check(date) ==False):
returnFalsedelta= (date-self.date).days/365.25self.balance=amount+self.balance*pow(1+debt_rate, delta)
self.date=datereturnTruedefWithdraw(self, date, amount):
if (self.Check(date) ==False):
returnFalsedelta= (date-self.date).days/365.25self.balance=self.balance*pow(1+debt_rate, delta)
self.date=datefulfilled=min(self.balance, amount)
ifself.balance<=amount:
print('Balance is lesser than requested', self.balance, amount)
self.balance-=fulfilledreturnfulfilleddefGet(self, date):
delta= (date-self.date).days/365.25self.balance=self.balance*pow(1+debt_rate, delta)
self.date=datereturnself.balance
Open 896.40
High 905.45
Low 895.75
Close 897.80
Shares Traded 32224833.00
Turnover (Rs. Cr) 811.39
P/E 11.72
P/B 2.08
Div Yield 1.81
Name: 1999-01-04 00:00:00, dtype: float64
# debt = DebtCorpus()# print(debt.Deposit(datetime(2000, 1, 1), 100))# print(debt.Withdraw(datetime(2010, 1, 1), 100))# debt.Get(datetime(2020, 1, 1))# vanilla strategy with params# monthly_sip = 100# default_exposure = 0.5# green_pe = 15# red_pe = 28# Every month, invest monthly_sip * default_exposure in index and invest monthly_sip * (1 - default_exposure) in debt.# If nifty pe > red_pe, pull out all money from index to debt.# if nifty pe < green_pe, pull out all money from debt to index.
nifty.index[-1].to_pydatetime()
datetime.datetime(2019, 12, 31, 0, 0)
defEvaluateStrategy(df, params):
print('params:', json.dumps(params.__dict__, indent=2))
push_num_installments=int(params.push_num_installments)
pull_num_installments=int(params.pull_num_installments)
# strategycurr_month=-1e=drivers.EquityCorpus(df)
d=drivers.DebtCorpus()
total_invested=0num_installments=0size_installment=0;
forindindf.index:
ifind.month!=curr_month:
curr_month=ind.monthindex_sip=params.monthly_sip*params.default_exposuredebt_sip=params.monthly_sip* (1-params.default_exposure)
current_pe=df['P/E'][ind]
if (current_pe<params.green_pe):
# we are in bear market.if (0==num_installments):
debt_funds=d.Get(ind)
# print('debt_funds', debt_funds, ind)size_installment=debt_funds/params.push_num_installmentsto_invest=size_installmentifnum_installments<params.push_num_installmentselsed.Get(ind)
# print('to_invest', to_invest, size_installment, d.Get(ind))debt_sip-=to_investindex_sip+=to_investnum_installments+=1elif (current_pe>params.red_pe):
# we are in bull marketequity_funds=e.Get(ind)
if (0==num_installments):
# print('equity_funds', equity_funds, ind)size_installment=equity_funds/params.pull_num_installmentsto_redeem=min(size_installment, equity_funds)
# print('to_redeem', to_redeem, size_installment, e.Get(ind), ind)index_sip-=to_redeemdebt_sip+=to_redeemnum_installments+=1else:
num_installments=0assertabs(index_sip+debt_sip-params.monthly_sip) <0.01,\
'index_sip:'+str(index_sip) +', debt_sip: '+str(debt_sip) +', monthly_sip:'+str(params.monthly_sip)
if (index_sip>0):
# print('deposit in equity', index_sip, ind)e.Deposit(ind, index_sip)
elif (index_sip<0):
# print('withdraw from equity', index_sip, ind)e.Withdraw(ind, -index_sip)
if (debt_sip>0):
d.Deposit(ind, debt_sip)
elif (debt_sip<0):
d.Withdraw(ind, -debt_sip)
total_invested+=params.monthly_sipstart_date=df.index[0].to_pydatetime()
end_date=df.index[-1].to_pydatetime()
# print('start-end', start_date, end_date)# print('total_invested', total_invested)# print('e.Get()', e.Get(end_date))# print('d.Get()', d.Get(end_date))returns= (e.Get(end_date) +d.Get(end_date)) /total_investedprint('returns', returns)
returnreturns