Skip to content

Instantly share code, notes, and snippets.

@mmueller-xyz
Last active January 6, 2022 21:52
Show Gist options
  • Save mmueller-xyz/08edb2557f35f2c77ae4f6b4dc4415ab to your computer and use it in GitHub Desktop.
Save mmueller-xyz/08edb2557f35f2c77ae4f6b4dc4415ab to your computer and use it in GitHub Desktop.
#!venv/bin/python3.5
from multiprocessing import Process
import requests
import sys
DAYS = [7, 14, 28, 56, 84]
WEEKS = [x / 7 for x in DAYS]
YEARS = [x / 365 for x in DAYS]
MERIT_WEIGHT = 0.05
BANKING_CAP = 2000000000
RATIO = [x / DAYS[0] for x in DAYS]
KEY = input("Api Key:")
WEEKLY_INCOME = int(input("(net) Weekly income (amount of money that can be put into the bank account):"))
WORKING_CAPITAL = int(input("working capital (money on hand and not to be invested):"))
def get_user():
print("Get User")
r = requests.get('http://api.torn.com/user/', params={'selections': 'merits,stocks,money', 'key': KEY})
check_api_error(r.json())
return r.json()
def check_api_error(json):
if "error" in json:
print(json['error'], file=sys.stderr)
exit(1)
def get_apr():
print("Get APR")
apr = []
r = requests.get('http://api.torn.com/torn/', params={'selections': 'bank', 'key': KEY})
check_api_error(r.json())
apr.append(float(r.json()["bank"]["1w"]) / 100)
apr.append(float(r.json()["bank"]["2w"]) / 100)
apr.append(float(r.json()["bank"]["1m"]) / 100)
apr.append(float(r.json()["bank"]["2m"]) / 100)
apr.append(float(r.json()["bank"]["3m"]) / 100)
return apr
def get_merits(json):
print("Get Merits")
return json["merits"]["Bank Interest"]
def get_tcb_benefit(json):
print("Get bb-benefit")
r = requests.get('http://api.torn.com/torn/', params={'selections': 'stocks', 'key': KEY})
check_api_error(r.json())
has_bb = False
for i, j in json["stocks"].items():
if j["stock_id"] == 2:
if r.json()["stocks"]["2"]["benefit"]["requirement"] <= j["stock_id"]["shares"]:
has_bb = True
return 0.1 if has_bb else 0
def get_capital(json):
return json["vault_amount"] + json["money_onhand"] + json["cayman_bank"] + json["city_bank"][
"amount"] - WORKING_CAPITAL
def sim_step(capital, real_ret, r_weeks=24.0, stack=None, runtime=0):
if stack is None:
stack = []
res = [(0, runtime, stack) for i in range(5)]
gain = calculate_gain(capital, real_ret)
if r_weeks <= 0 or capital > BANKING_CAP:
return capital, runtime, stack
for i, j in enumerate(WEEKS):
if r_weeks-j >= 0:
end_capital = round(gain[i] + WEEKLY_INCOME * j)
new_stack = stack.copy()
new_stack.append({"weeks": int(j), "start": capital, "end": end_capital})
res[i] = sim_step(end_capital, real_ret, r_weeks - j, new_stack, runtime + int(j))
max_i = 0
for i in range(len(res) - 1):
if res[max_i][0] / res[max_i][1] < res[i + 1][0] / res[i + 1][1]:
max_i = i + 1
return res[max_i]
def m_sim_step(capital, real_ret, r_weeks, stack, runtime, i, j, gain, ret):
if r_weeks - j >= 0:
end_capital = round(gain[i] + WEEKLY_INCOME * j)
new_stack = stack.copy()
new_stack.append({"weeks": int(j), "start": capital, "end": end_capital})
ret = sim_step(end_capital, real_ret, r_weeks - j, new_stack, runtime + int(j))
ret = (0, runtime, stack)
def calculate_real_apr(base_apr, merits=0, tcb_benefit=0.0):
return [x + x * MERIT_WEIGHT * merits + x * tcb_benefit for i, x in enumerate(base_apr)]
def calculate_real_return(real_apr):
return [(x * YEARS[i]) for i, x in enumerate(real_apr)]
def calculate_gain(capital, real_ret):
return [capital + x * min(capital, BANKING_CAP) - min(0, BANKING_CAP - capital) for x in real_ret]
def main():
base_apr = get_apr()
user = get_user()
merits = get_merits(user)
bb = get_tcb_benefit(user)
capital = get_capital(user)
real_apr = calculate_real_apr(base_apr, merits, bb)
real_return = calculate_real_return(real_apr)
print("--------------------------------------")
print("starting capital: {:,}$".format(capital))
print("weekly income: {:,}$".format(WEEKLY_INCOME))
print("merits: {} (+{:.0%} apr increase)".format(merits, MERIT_WEIGHT * merits))
print("bb-benefit: +{:.0%} apr increase".format(bb))
print("base returns: ", end="")
for i, str in enumerate(WEEKS):
print("{}w:{:.2%} ".format(int(str), base_apr[i]), end="")
print()
print("real returns: ", end="")
for i, str in enumerate(WEEKS):
print("{}w:{:.2%} ".format(int(str), real_apr[i]), end="")
print()
print("--------------------------------------")
# print(real_apr)
# print(real_return)
#
# print([x/365 for x in real_apr])
print("calculaing")
x = sim_step(capital, real_return, r_weeks=24)
while x[0] < BANKING_CAP:
# print(x)
x = sim_step(x[0], real_return, stack=x[2], r_weeks=24, runtime=x[1])
print("{} weeks calculated".format(x[1]))
income_amount = WEEKLY_INCOME * x[1]
increase = x[0] - get_capital(user)
print("--------------------------------------")
print("from {:,}$ to {:,}$ ({:,}$ increase)".format(get_capital(user), x[0], increase))
print("{:,}$ from weekly income, {:,}$ from investment returns".format(income_amount, increase - income_amount))
print("after {} weeks".format(x[1]))
print_investment(x[2])
print("--------------------------------------")
def print_investment(investment_plan):
print("detailed investment plan:")
for n,i in enumerate(investment_plan):
weeks = i["weeks"]
start = i["start"]
end = i["end"]
print(str(n+1) + ": {}w".format(weeks), end="")
print("\t{:,}$\t->\t{:,}$\t(+{:,}$ | +{:.2%}/week)".format(start, end, end - start, (end - start) / start / weeks))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment