Skip to content

Instantly share code, notes, and snippets.

@kodejuice
Last active July 23, 2024 09:29
Show Gist options
  • Save kodejuice/fd60a1995d073056667a020faf082436 to your computer and use it in GitHub Desktop.
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
# 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: July 2024
"""
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',
'TRANSCOHOT',
'FIDELITY',
'AXA',
]),
(18, [
'NASCON',
'BUACEMENT',
'CADBURY',
'NGXGROUP',
'STANBIC_ETF_40',
'AIRTELAFRI',
]),
(17, [
'MTNN',
'UBA',
'VETIVA_ETF',
'BUAFOODS',
'JBERGER',
'FBNH',
]),
(15, [
'DANGCEM',
'NESTLE',
'UCAP',
'UNILEVER',
'DANGSUGAR',
'ZENITHBANK',
'SEPLAT',
'EKOCORP',
])
],
# China
'CHN': [
(75, [
'DQ',
'TCOM',
'NIO',
'PDD',
]),
(25, [
'CNYA',
'GXC',
'YUMC',
'CQQQ'
'BIDU',
'BABA',
'WB',
]),
],
# India
'IND': [
(55, [
'IBN',
'INDY',
'INCO',
'EPI',
]),
(45, [
'INDA',
'INFY',
'IFN',
'HDB',
'PIN',
]),
],
# cryptos
'CRYP': [
(60, [
'LTC',
'WKC',
'SHIB',
]),
(30, [
'OLIVE'
'BTC',
'MANA',
]),
(10, [
'ETH',
]),
],
# Japan
'JPN': [
(100, [
'DBJP',
'DXJ',
'SONY'
]),
],
# misc
'X': [
(60, [
'XLK',
'PTNQ',
'NVDA',
'MSFT',
'AMZN',
'ARM',
]),
(40, [
'PANW',
'XLV',
'VEU',
'VSS',
'SPDW',
'AAPL',
'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