# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
# %matplotlib inline

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import scipy.interpolate
import requests
import datetime

# %load_ext autoreload
# %autoreload 2

import ptvsd
ptvsd.enable_attach()
# -

from access_treasurydirectgov import *
import bond_analytics as ba
from db_manager_UST import *

date_range = pd.date_range(start='2020/08/06', end='2020/08/16', freq='D')

dn = db_manager_UST()

df = dn.retrieve_as_of(datetime.datetime(2020,8,6))

df_p = df[df['type'].isin(['Note', 'Bond'])]

ttm = (df_p['maturityDate'] - df_p['date']).dt.days / 365.0

plt.figure()
plt.plot(ttm, df_p['endOfDay'], '.')
plt.show()

df['securityTerm'].value_counts()

df[df['securityTerm'] == '20-Year 6-Month']

get_cusip_info('912810FR4')[1]

df = read_hist_data_from_treasurydirectgov(datetime.datetime(2020,8,20))
df

any([True, True])

datetime.date.today() 

# + tags=[]
adf = []
for dt in date_range:
    print(dt)
    try:
        df = read_hist_data_from_treasurydirectgov(dt)
    except Exception as ex:
        print(ex)
        continue
    df['Date'] = dt
    adf.append(df)
df_a = pd.concat(adf)        
# -

df_note = df_a[df_a['SECURITY TYPE'].isin(['MARKET BASED NOTE', 'MARKET BASED BOND'])].copy()



df[['CUSIP','SECURITY TYPE','RATE', 'MATURITY DATE']].T.to_dict()[33]['MATURITY DATE']


# +
def calc_yield(row):
    dt_start = datetime.datetime(2019,1,1)
    dt_end = row['MATURITY DATE']
    coupon = row['RATE']
    today = row['Date']
    price = row['END OF DAY']
    if price < 0.01:
        price = 0.5*(row['BUY'] + row['SELL'])
    
    b = ba.USConventional(row['SECURITY TYPE'], dt_start, dt_end, coupon)
    y = b.get_yield(today, price)
    
    return y

df_note['Yield'] = df_note.apply(calc_yield, axis = 1)
# -

df_note_dt = df_note[df_note['Date'] == '2020-07-30']

df_p = df_note_dt
plt.figure()
x = df_p['MATURITY DATE']
y = df_p['Yield']
plt.plot(x, y, 'o')
plt.show()

import pymongo

myclient = pymongo.MongoClient('mongodb://localhost:27017/')

mydb = myclient['mydatabase']

myclient.list_database_names()

mycol = mydb['customers']

x = mycol.find_one()

print(x)

# + tags=[]
print(mydb.list_collection_names())

# + tags=[]
collist = mydb.list_collection_names()
if "customers" in collist:
  print("The collection exists.")
# -

mydict = {'name':'john', 'address':'highway 37'}
x = mycol.insert_one(mydict)

mycol.find_one()

collist = mydb.list_collection_names()
if "customers" in collist:
  print("The collection exists.")

x.inserted_id

mycol.insert_many(df_note.to_dict(orient = 'records'))

pd.DataFrame(mycol.find({'BUY':{'$lt':0.1}}, {'_id':0}))



StarcoderdataJupyterScriptsDedupFiltered