jeudi 5 novembre 2020

How to simulate stock prices with impact of news

I'm writing a function that generates simulated stock market prices and part of the code incorporates the impact of news (e.g. political turmoil, a natural disaster) on share price over a number of days.

# Set up the default_rng from Numpy
rng = np.random.default_rng()

def news(chance, volatility):
    '''
    Simulate the impact of news on stock prices with %chance
    '''
    # Choose whether there's news today
    news_today = rng.choice([0,1], p=chance)
    if news_today:
        # Calculate m and drift
        m = rng.normal(0,0.3)
        drift = m * volatility
        # Randomly choose the duration
        duration = rng.integers(7,7*12)
        final = np.zeros(duration)
        for i in range(duration):
            final[i] = drift
        return final
    else:
        return np.zeros(duration)

I'm getting several error messages, one of them being:

news_today = rng.choice([0,1], p=chance)
TypeError: object of type 'int' has no len()



Aucun commentaire:

Enregistrer un commentaire