Last active
August 29, 2023 12:17
-
-
Save kodejuice/fd60a1995d073056667a020faf082436 to your computer and use it in GitHub Desktop.
I use this script to know the amount of stocks i need to buy given a certain amount
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Sochima Biereagu (@kodejuice), 2023 | |
# I use this script to know the amount of stocks i need to buy given a certain amount | |
# I have weighted all the stocks listed here | |
# The stocks listed are mostly Nigeria, China, Japan, India stocks plus some other stocks in US | |
# Last updated: Aug 2023 | |
""" | |
Usage | |
python weighted_stocks.py [amt] [...categories] | |
_ | |
categories is any combination of 'NGN', 'JPN', 'CHN', 'IND', 'CRYP', 'X' | |
""" | |
import sys | |
# Order | |
""" | |
- NGN | |
- JPN | |
- X < | |
- IND << | |
- CRYP | |
- CHN | |
""" | |
assets = { | |
# Nigeria | |
'NGN': [ | |
(50, [ | |
'FLOURMILL', | |
'HONYFLOUR', | |
'UPDC', | |
'SFSREIT', | |
'PRESCO', | |
]), | |
(18, [ | |
'DANGSUGAR', | |
'NESTLE', | |
'NASCON', | |
'BUACEMENT', | |
'CADBURY', | |
'UNILEVER', | |
'UCAP', | |
]), | |
(17, [ | |
'NGXGROUP', | |
'MTNN', | |
'AIRTELAFRI', | |
'UBA', | |
'STANBIC_ETF_40', | |
'VETIVA_ETF', | |
'OKOMUOIL', | |
]), | |
(15, [ | |
'BUAFOODS', | |
'TRANSCOHOT', | |
'DANGCEM', | |
'ZENITHBANK', | |
'SEPLAT', | |
'JBERGER', | |
'UBN', | |
'EKOCORP', | |
'FBNH', | |
]) | |
], | |
# China | |
'CHN': [ | |
(50, [ | |
'BABA', | |
'BIDU', | |
'TCOM', | |
'CQQQ' | |
'YUMC', | |
]), | |
(30, [ | |
'GXC', | |
'NIO', | |
'PDD' | |
'CNYA', | |
]), | |
(20, [ | |
'WB', | |
'DQ' | |
]), | |
], | |
# India | |
'IND': [ | |
(55, [ | |
'INFY', | |
'IBN', | |
'IFN', | |
'INDA', | |
]), | |
(45, [ | |
'HDB', | |
'INDY', | |
'INCO', | |
'EPI', | |
'PIN', | |
]), | |
], | |
# cryptos | |
'CRYP': [ | |
(60, [ | |
'WKC', | |
'LTC', | |
'OLIVE' | |
]), | |
(30, [ | |
'BTC', | |
'SHIB', | |
'MANA', | |
]), | |
(10, [ | |
'ETH', | |
]), | |
], | |
# Japan | |
'JPN': [ | |
(100, [ | |
'EWJ', | |
'BBJP', | |
'DXJ', | |
'FLJP', | |
]), | |
], | |
# misc | |
'X': [ | |
(60, [ | |
'VEU', | |
'XLV', | |
'HYG', | |
'VSS', | |
'DOCN', | |
'TACK', | |
'PTNQ', | |
]), | |
(40, [ | |
'IONQ', | |
'Rigetti', | |
'SPDW', | |
'AAPL', | |
'MSFT', | |
'QQQ', | |
]), | |
] | |
} | |
# make sure all weights all sum up to 100 | |
for p in assets: | |
weighted = assets[p] | |
weights = [w for w, _ in weighted] | |
assert (sum(weights) == 100) | |
def get_amount_currency(amount): | |
amount = amount.strip() | |
if '0' <= amount[0] <= '9': | |
# no currency in ammount | |
return amount, "#" | |
return amount[1:], amount[0] | |
def apply_weights_given_amt(amount, category, currency_symbol='#'): | |
if category not in assets: | |
return [] | |
out = [category] | |
for weight, tickers in assets[category]: | |
amt = round((weight/100) * amount, 2) | |
individual_amt = round(amt / len(tickers), 2) | |
out += [f"{tker} {currency_symbol}{individual_amt:,}" for tker in tickers] | |
return out | |
def main(amount, categories=None): | |
amt, currency_symbol = get_amount_currency(amount) | |
if not categories: | |
categories = list(assets.keys()) | |
else: | |
categories = categories.split(",") | |
categories = [c.strip() for c in categories] | |
res = [] | |
categories_count = len(categories) | |
amount_for_each_category = round(float(amt) / categories_count, 2) | |
for c in categories: | |
if c == '': | |
continue | |
if c not in assets: | |
print(f"'{c}' not a valid category") | |
res += ["\n".join(apply_weights_given_amt(amount_for_each_category, c, currency_symbol))] | |
print("\n\n".join(res)) | |
if __name__ == '__main__': | |
args = sys.argv | |
if len(args) == 2: | |
main(args[1]) | |
elif len(args) > 2: | |
main(args[1], ",".join(args[2:])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment