mardi 3 novembre 2020

Python3 How to use a for loop to simulate investment trading?

enter image description here

I am trying to come up with an investment strategy, whereby I will simulate a list of market price every month:

prices = [80, 102, 84, 41, 74, 55, 32, 77, 92, 40, 91, 26]

An initial investment of $50k, and a monthly topup of $1k. If the market price is greater than the avg price for that month, then sell $1000 worth of Stock. If the avg price is greater than market price for that month, then buy $1000 worth of stock.

The problem I am facing now is that I am unable to correctly simulate the table above with the appropriate columns like "Units purchased", "cash spent", "total Units", 'total cash spent", "cash remaining" and "avg price". I am also stuck with implementing the if-else statement regarding the directions to either buy or sell stock in the given month. Please help!

Here is my current code:

INPUT:

prices = [80, 102, 84, 41, 74, 55, 32, 77, 92, 40, 91, 26]
init_invt = 50000
monthly_init = 1000

monthly_cash_spent = 0
total_cash_spent = 0
cash_remaining = 0
total_stock_units = 0


for market_price in prices:
    monthly_stock_units = init_invt / market_price
    monthly_cash_spent = monthly_stock_units * market_price
    total_cash_spent = monthly_cash_spent
    cash_remaining =  init_invt - monthly_cash_spent
    avg_price = monthly_cash_spent / total_stock_units
    
    if avg_price > market_price: #buy $1000 worth of stock A at the market price of the month
        total_stock_units = monthly_init / market_price
    else:
        market_price * total_stock_units < monthly_init #sell $1000 worth of stock A at the market price of the month
            

    print('Month: {:0}, Units purchased: ', monthly_stock_units, 'Cash Spent: ', monthly_cash_spent, 'Cash Remaining: ', cash_remaining, 'Avg Price: ', avg_price)

OUTPUT:

Month: {:0}, Units purchased:  625.0 Cash Spent:  50000.0 Cash Remaining:  0.0 Avg Price:  26.0
Month: {:0}, Units purchased:  490.19607843137254 Cash Spent:  50000.0 Cash Remaining:  0.0 Avg Price:  26.0
Month: {:0}, Units purchased:  595.2380952380952 Cash Spent:  49999.99999999999 Cash Remaining:  7.275957614183426e-12 Avg Price:  26.0
Month: {:0}, Units purchased:  1219.5121951219512 Cash Spent:  50000.0 Cash Remaining:  0.0 Avg Price:  26.0
Month: {:0}, Units purchased:  675.6756756756756 Cash Spent:  49999.99999999999 Cash Remaining:  7.275957614183426e-12 Avg Price:  26.0
Month: {:0}, Units purchased:  909.0909090909091 Cash Spent:  50000.0 Cash Remaining:  0.0 Avg Price:  26.0
Month: {:0}, Units purchased:  1562.5 Cash Spent:  50000.0 Cash Remaining:  0.0 Avg Price:  26.0
Month: {:0}, Units purchased:  649.3506493506494 Cash Spent:  50000.00000000001 Cash Remaining:  -7.275957614183426e-12 Avg Price:  26.0
Month: {:0}, Units purchased:  543.4782608695652 Cash Spent:  50000.0 Cash Remaining:  0.0 Avg Price:  26.0
Month: {:0}, Units purchased:  1250.0 Cash Spent:  50000.0 Cash Remaining:  0.0 Avg Price:  26.0
Month: {:0}, Units purchased:  549.4505494505495 Cash Spent:  50000.0 Cash Remaining:  0.0 Avg Price:  26.0
Month: {:0}, Units purchased:  1923.076923076923 Cash Spent:  50000.0 Cash Remaining:  0.0 Avg Price:  26.0

Aucun commentaire:

Enregistrer un commentaire