Back to Community
Monte Carlo simulation

Recently, I read a thread about the Monte Carlo simulations feature needed.

I believe you can certainly do Monte Carlo on Quantopian. Monte Carlo is just a method with random simulation. If you can do it on Python, so certainly you can do it on Quantopian.

The following is I used truncated Euler method to do CIR model simulation, which is very crude, but enough to show MC you want. (you can ignore the order of AAPL part)

Clone Algorithm
55
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
import numpy as np
from math import sqrt

def initialize(context):
    context.r = 0.1
 
def handle_data(context, data):
    order(sid(24), 0)
    
    
    record(CIR = context.r)
    #Monte Carlo simulation
    #CIR model parameter initilization
    a = 0.2
    b = 0.15
    c = 0.2
    
    temp = []
   
    #trancuate the negative part
    if context.r < 0:
        rt = 0
    else:
        rt = context.r
        
    W = np.random.normal(0, 1, 1000)
    for i in range(1000):
        temp.append( context.r + a*(b-context.r)/365 + c*sqrt(rt/365)*W[i] )
    context.r = np.mean(temp)
    
This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes.
There was a runtime error.
3 responses

And the following is a more complicated one for get much more precise result of CIR model through simulation, if you need.

Clone Algorithm
23
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
import numpy as np
from math import sqrt, exp

def initialize(context):
    context.r = 0.1
 
def handle_data(context, data):
    #please ignore the trasiaction part
    order(sid(24), 0)
    
    record(CIR = context.r)
    #Monte Carlo simulation
    #initial the CIR model parameter
    a = 0.2
    b = 0.15
    sigma = 0.2
    r = context.r
    
    #we are running 1000 paths here
    samplesize = 1000
    
    #250 days each year
    delta = 1/250.0
    
    d = 4*b*a/(sigma*sigma)
    c = sigma*sigma*(1-exp(-a*delta))/(4*a)
    lam = (r/c)*exp(-a*delta)
    
    #some random number we need
    X = np.random.chisquare(d, samplesize)
    Z = np.random.normal(0, 1, samplesize)
    temp = []
    for i in range(samplesize):
        temp.append( c*((Z[i]+sqrt(lam))**2+X[i] ))
    
    #average the simulation    
    context.r = np.mean(temp)
                    
This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes.
There was a runtime error.

Why would you generate a time series followed by a MC simulation? Is there any benefits of using MC in an algorithm?

I think cross sectional bootstrapping would make more sense , though I don't think it's possible given current computational power provided.