I wrote this quick Python 3 code which will perform a quick Monte Carlo simulation (selection without replacement) and creates a simple “report.txt” file with the information. It will calculated the average maximal drawdown achieved in each run as well as the standard deviation. From there one is able to calculate the confidence intervals.
If you’re not sure what Monte Carlo is and how it can benefit you, a simple search on your favorite search engine will do.
I have attached a sample P/L text file; this is the format yours need to be in as well. (Can be in pips, $, or %, doesn’t matter)
You should place “Monte Carlo SWOR.py” in the same folder as your CSV/text file.
NOTE: I just whipped this up quickly and currently cannot do % drawdown only absolute. If someone wants to modify it they are welcome to, but I ask them to share it with the rest of us as well.
Thanks for this code! I have been analysing it and can’t understand some parts:
#Calculate running total
def calc_rTotal(alist, blist):
runningTotal = 0
for i in range(len(alist)):
runningTotal = runningTotal + alist[i]
blist.append(runningTotal)
return blist
# Monte Carlo
for r in range(iterations):
rTotal_rand = []
drawdown_Abs_rand = []
random.shuffle(PnL_rand)
calc_rTotal(PnL_rand, rTotal_rand)
for i in range(1, len(rTotal_rand)):
dd_Abs = rTotal_rand[i]-maximumVal(rTotal_rand[:i])-1
drawdown_Abs_rand.append(dd_Abs)
maxddAbs_rand = round(minimumVal(drawdown_Abs_rand), 4)
maxDD_Abs.append(maxddAbs_rand)
Why do you do [B]rTotal_rand[i]-maximumVal(rTotal_rand[:i])-1[/B] to know the drawdown up to i? If I have this list of returns from trades: [2.0,4.0,-1.0,-2.0] shouldn’t the drawdowns be [0.0,0.0,1.0,3.0] ?
In the code it would do [2.0-2.0-1,6.0-4.0-1,5.0-4.0-1,3.0-4.0-1] = [-1.0,1.0,0.0,-2.0]
Also I don’t understand the logic behind the -1 in [B]rTotal_rand[i]-maximumVal(rTotal_rand[:i])-1[/B] or why afterwards you perform a minimum to get the maximum drawdown [B]maxddAbs_rand = round(minimumVal(drawdown_Abs_rand), 4)[/B]