Why Nostr? What is Njump?
2024-08-31 14:26:06

Ape Mithrandir on Nostr: ```python import yfinance import pandas as pd import numpy as np import datetime as ...

```python
import yfinance
import pandas as pd
import numpy as np
import datetime as dt
import pytz
import argparse
from dateutil.relativedelta import relativedelta

def get_returns(sInformation, T):
# Calculate the reference date
years = int(T) # Get the whole number of years
months = int((T - years) * 12) # Convert the fractional part to months
ref_dt = dt.datetime.now(pytz.timezone('America/New_York')) - relativedelta(years=years, months=months)
hist = sInformation.history(period='10y')
hist = hist[hist.index >= ref_dt]
f = hist.Close.tolist()[0]
m = hist.Close.mean()
l = hist.Close.tolist()[-1]
tot_lr = np.log(l/f) * 100.0
avg_lr = np.log(l/f)/T * 100.0
tot_ret = (l/f - 1) * 100.0
avg_ret = ((l/f) ** (1.0/T) - 1) * 100.0
# Using T / 2 to represent the average holding time of the avg price
tot_lr_m = np.log(l/m) * 100.0
avg_lr_m = np.log(l/m)/(T/2.0) * 100.0
tot_ret_m = (l/m - 1) * 100.0
avg_ret_m = ((l/m) ** (2.0/T) - 1) * 100.0
return f, m, l, tot_lr, avg_lr, tot_ret, avg_ret, tot_lr_m, avg_lr_m, tot_ret_m, avg_ret_m

def format_number(value):
if value == 0:
return "0" # Handle zero case
elif abs(value) < 1:
return f"{value:.3f}" # 3 decimal places for small values
elif abs(value) < 100:
return f"{value:.2f}" # 2 decimal places for medium values
elif abs(value) < 1000:
return f"{value:.1f}" # 1 decimal place for larger values
else:
return f"{value:,.0f}" # No decimal places for very large values

def print_stats(s, T, f, m, l, tot_lr, avg_lr, tot_ret, avg_ret, tot_lr_m, avg_lr_m, tot_ret_m, avg_ret_m):
print("=============================================")
print(f"{s} Price Stats:")
print("=============================================")
print(f"{T} years ago Price: {format_number(f)}")
print(f"{T} years avg Price: {format_number(m)}")
print(f"Current price: {format_number(l)}\n")
print(f"{s} Returns vs the {T} years old Price:")
print("=============================================")
print(f"Total {T} years log-returns: {format_number(tot_lr)}%")
print(f"Avg {T} years ann. log-returns: {format_number(avg_lr)}%")
print(f"Total {T} years simple-returns: {format_number(tot_ret)}%")
print(f"Avg {T} years ann. simple-returns: {format_number(avg_ret)}%\n")
print(f"{s} Returns vs the {T} years avg Price:")
print("=============================================")
print(f"Total {T} years log-returns: {format_number(tot_lr_m)}%")
print(f"Avg {T} years ann. log-returns: {format_number(avg_lr_m)}%")
print(f"Total {T} years simple-returns: {format_number(tot_ret_m)}%")
print(f"Avg {T} years ann. simple-returns: {format_number(avg_ret_m)}%\n")

def main():
#Create the parser
parser = argparse.ArgumentParser(description='Process some integers.')

# Add arguments
parser.add_argument('--s1', type=str, required=True, help='First Yahoo Finance Instrument')
parser.add_argument('--s2', type=str, help='Second Yahoo Finance Instrument')
parser.add_argument('--T', type=float, required=True, help='Time period in years')

# Parse the arguments
try:
args = parser.parse_args()
except SystemExit as e:
print("Error: Missing required arguments.")
parser.print_help()
return

s1Information = yfinance.Ticker(args.s1)
f1, m1, l1, tot_lr1, avg_lr1, tot_ret1, avg_ret1, tot_lr_m1, avg_lr_m1, tot_ret_m1, avg_ret_m1 = get_returns(s1Information, args.T)
print_stats(args.s1, args.T, f1, m1, l1, tot_lr1, avg_lr1, tot_ret1, avg_ret1, tot_lr_m1, avg_lr_m1, tot_ret_m1, avg_ret_m1)
if args.s2 is not None:
s2Information = yfinance.Ticker(args.s2)
f2, m2, l2, tot_lr2, avg_lr2, tot_ret2, avg_ret2, tot_lr_m2, avg_lr_m2, tot_ret_m2, avg_ret_m2 = get_returns(s2Information, args.T)
print_stats(args.s2, args.T, f2, m2, l2, tot_lr2, avg_lr2, tot_ret2, avg_ret2, tot_lr_m2, avg_lr_m2, tot_ret_m2, avg_ret_m2)

if __name__ == '__main__':
main()
```
Author Public Key
npub16dsu2ghu9yd363utyw7dejqc8rgx5ps7r4hxddrvx2x64dh73p7qqkh6vy