# Monte Carlo

#### mcdon030

##### Member
I wanted to share this TOS tool which I think is beneficial to any trader, but need to give a little background info:

I started trading three years ago with a small account size, grew rapidly fast (this is an understatement) and almost completely wiped out my account 100 times faster than it grew. I learned some very good lessons:

2). Backtest backtest backtest

3). Learn to expect loss or you’re in the wrong profession.

4). Never trade an indicator that you don’t know backwards and forwards like the back of your #\$%

5). Repeat 1,2, 3 and 4 over and over again

Additionally, this all started my exploration into coding as I have zero patients. I had a background in VBA coding, but had not coded in 7 years. During this time, I ended up purchasing an indicator which resulted in further reason to learn as I couldn’t believe it was even sold. I’m fairly certain that I can now code most indicators, which is a good thing, but this journey led me to fully believe trade management is way more important than the signal itself. My gestimate is 90% trade management 10% signal. So much in fact, the screenshot below is a back test with a randomized entry, randomized atr length and randomized atr multiples for trailing stop for each trade. While one back test isn’t statistical proof of anything, you can see there is something worth exploring.

There are versions of Monte Carlo simulations in excel which are free, but like I said I’m inpatient so I would rather see before transferring data from TOS to analysis. Personally, I like the probability of losing streak, max losing streak and Av trade loss. Once you have reasonable expectancy from the strategy, managing account % per trade becomes easier.

-mcdon030

Code:
``````## Monte Carlo : Trade System Expectancy
##  Author:  Mcdon030
## Date: 10/14/2020
script Rev{
input v1 = 0.0 ;
input v2 = 0.0;
def bn = BarNumber();
def lbar = HighestAll(if IsNaN(close) then 0 else bn);
def va1 = compoundValue(1, fold id = 1 to lbar with t = 0 while t == 0 do if GetValue(v1, -id) then id else 0,0);
plot for =  GetValue(v2, -va1);
}

declare upper;
input startinCapital = 1000;
input RiskPercent =.02; #
input WinningPercet = 93.44;
input RR  =  2; # hint:  (AVG Wins) / (AVG Losses)
input commission = 0.0;
input Runs = 500;
def x = Compoundvalue(1,x[1]+1,1);
addlabel(yes, "Starting Capital "  + startinCapital ,color.white);
addlabel(yes, " WinningPercet "  +  WinningPercet,color.white);
addlabel(yes, " Runs  "  +  Runs ,color.white);
##Statistical Probabilty Of Consec. Wins & Losses.
def SProbva =(100-WinningPercet)/100;
def SProbvb  = 1- SProbva;
def probConLossStreak = log(Runs)/-log(SProbva );
def probConWinsStreak = log(Runs)/-log(SProbvb );

## incomplete internal code
script BinomialDist{
input  n = 500;# input('Number of trials: ');
input  x = 5; # input('Exact number of successes: ');
input   p = 95;# input('Probability of success: ');
def m1 = log(n);
def m2 = log(p);
def m3 = log( n-x);
def r = exp(m1 - m2 - m3 + x*log(p) + (n-x)*log(1-p));
plot binom = r;
}
def binomd = binomialdist(Runs,232,.9344);

def cnt = Compoundvalue(1,if x==1 then 1 else if x[1]>=Runs then 0 else  x,1);
addlabel(yes, "Prob Cons. Losses  "  + probConLossStreak ,color.light_red);
addlabel(yes, "Prob Cons. Wins  "  + probConWinsStreak  ,color.white);
addlabel(yes, "Prob Cons. Wins  "  +  binomd  ,color.white);
## end Probability
#####################  start run and random counts

def rand = round(100*random(),2);
def rand1 = Compoundvalue(1,if cnt!=cnt[1] then rand else if  cnt!=0 then 0 else rand1[1],0);
def isloss = if cnt>cnt[1] && cnt!=00 && rand1> WinningPercet then 1 else 0;
def isWin = if cnt>cnt[1] &&  cnt!=00 && rand1< WinningPercet then 1 else 0;
def countloss = Compoundvalue(1, if isLoss then countloss[1]+1  else countloss[1],0);
def countWin = Compoundvalue(1, if isWin then countWin[1]+1  else  countWin[1],0);
def countloss2 = Compoundvalue(1, if isLoss  then countloss2[1]+1 else if iswin then 0 else countloss2[1],0);
def countwin2 = Compoundvalue(1, if iswin  then countwin2[1]+1  else if isloss  then 0 else countwin2[1],0);
def maxsecloss = highestall(countloss2);
def maxsecWin= highestall(countwin2);
## end counts

def capital  = Compoundvalue(1, if  isWin then  ((1+RiskPercent*RR)*capital[1])-commission  else if isloss then ((1-RiskPercent)*capital[1])-commission
else capital[1],startinCapital);

#def riskpercentc  =startinCapital*RiskPercent;
#def outcome =Compoundvalue(1,  if  cnt!=cnt[1] && cnt!=00 && rand<WinningPercet then 1 else if  cnt!=cnt[1] && cnt!=00 && rand>WinningPercet then 2 else 0,0);
#def WL = Compoundvalue(1,if isWin  then riskpercentc*RR-commission else if isloss then  -riskpercentc-commission else wl[1],close);

################################################## loss  ##################################################
def capitalloss  = if isloss then (capital- capital[1])-commission else capitalloss[1];
def Totalloss = Compoundvalue(1,if  isloss && countloss==1 then capitalloss
else if  isloss && countloss>=2 then Totalloss[1]+capitalloss  else Totalloss [1],capitalloss);
def MaxLossRev = if countloss2==highestall(countloss2) then 1 else 0;
def MaxLossRev_=  Rev(capitalloss,MaxLossRev);
def MaxConlossa = Compoundvalue(1,if MaxLossRev_ then capitalloss else MaxConlossa[1],capitalloss);
def MostConloss = Compoundvalue(1,if   isloss &&  MaxConlossa[1] then capitalloss[1]+ MaxConlossa else MostConloss[1],0);
def ACtualLosingPercent = countloss/(countloss+countWin);
################################################## END loss  ##################################################
################################################## Win  ##################################################
def capitalwin  = if iswin then ( capital[1]-capital)-commission else capitalwin[1];
def Totalwin= Compoundvalue(1,if  iswin && countwin==1 then capitalwin
else if  iswin && countwin>=2 then Totalwin[1]+capitalwin  else Totalwin[1],capitalloss);
def MaxWinRev = if countwin2==highestall(countwin2) then 1 else 0;
def MaxWinRev_=  Rev(capitalwin,MaxWinRev);
def MaxConWina = Compoundvalue(1,if MaxWinRev_ then capitalwin else MaxConWina[1],capitalWin);
def MostConWin= Compoundvalue(1,if   isWin &&  MaxConWina[1] then capitalwin[1]+ MaxConWina else MostConWin[1],0);
def ACtualWinningPercent = countwin/500;
################################################## END Win  ##################################################

## labels
AddLabel(1," Total Losses : " +asDollars(Totalloss),Color.light_red);
AddLabel(1," Actual Losing % : " +aspercent(ACtualLosingPercent ), Color.light_red);
AddLabel(1," Most Consc. Loss  Streak : " +(maxsecloss),Color.light_red);
################################################################################
AddLabel(1," Total Wins : " +asDollars(Totalwin),Color.light_green);
AddLabel(1," Actual Winning % : " +aspercent(ACtualWinningPercent), Color.light_green);
AddLabel(1," Most Consc. Win  Streak : " +(maxsecwin),Color.light_green);
AddLabel(1, "Capital : " +capital  , Color.green);
## end``````

so i like this concept. seems pretty advanced to the point that i wouldnt need anything else although what has me confused.......i have read the code, i have also not added to the code and for some reason it believes I will have a 93% win rate and end up with 8million....can you please explain lol bc i need those numbers lmao

so i like this concept. seems pretty advanced to the point that i wouldnt need anything else although what has me confused.......i have read the code, i have also not added to the code and for some reason it believes I will have a 93% win rate and end up with 8million....can you please explain lol bc i need those numbers lmao

That's an input your supposed to use for whatever strategy you have back tested. For easy use, input any Win/Loss and RR from every strategy back test you have used. The Monte Carlo will estimate the max probable loss streak. You need an account size large enough to cover. Once your account size grows, the larger you can risk per trade. There are other uses; however, much like Max Draw Down, this measures risk to your account. Some of the best traders measure risk this way. I have not met nor read of any trader who doesn't measure probable risk. Many forum topics here discuss measuring risk per trade with ATR, VWAP, etc. Having reasonable expectations should be a must unless you plan of letting the house win

That's an input your supposed to use for whatever strategy you have back tested. For easy use, input any Win/Loss and RR from every strategy back test you have used. The Monte Carlo will estimate the max probable loss streak. You need an account size large enough to cover. Once your account size grows, the larger you can risk per trade. There are other uses; however, much like Max Draw Down, this measures risk to your account. Some of the best traders measure risk this way. I have not met nor read of any trader who doesn't measure probable risk. Many forum topics here discuss measuring risk per trade with ATR, VWAP, etc. Having reasonable expectations should be a must unless you plan of letting the house win
Very correct. I trade support and resistance. Normally, if your in a winning trade and end up losing, you were either greedy or did not manage the trade correctly.

After 20% I always re evaluate my trade plan, decide how much to let ride and how much to take off the table.

The best way to describe it is I try to make myself the house.

Good job @mcdon030, I'm happy you included the part about being prepared to accept losses.

87k+ Posts
196 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?