Code - Monte Carlo
Code - Monte Carlo
Code - Monte Carlo
TRADERS’ TIPS
SUM( CL*(VO/(VMA * n)) , n )
where:
CL is the last price
VO is the day volume
VMA is the volume moving average over n periods.
TRADERS’ TIPS
—Gary Geniesse, TradingSolutions Project Lead TrRisk = 1000;
NeuroDimension, Inc. EntPr = H + EntFrac * (H - L);
800 634-3327, 352 377-5144 If C > C[1] then
Buy next bar at EntPr Stop;
info@tradingsolutions.com
www.tradingsolutions.com Exitlong(“MMStop”) next bar at EntryPrice - TrRisk/BigPointValue
stop;
If BarsSinceEntry >= 1 and open of next bar > EntryPrice then
! TECHNIFILTER PLUS ExitLong(“ProfOpen”) next bar at market;
Here is the TechniFilter Plus formula that computes the Value1 = MonteCarlo (ASize, DDGoal, RiskPer, TrRisk, NRand);
volume-weighted moving average described in Buff
Dormeier’s article in this issue, “Buff Up Your Moving {This last line calls the MonteCarlo function, which randomizes the
Averages.” The formula uses the TechniFilter Plus F-modifier trades and summarizes the results. Delete it if you don’t want the
to compute this average. MonteCarlo results.}
—Michael R. Bryant
Formula for a volume-weighted average
NAME: Buff_Avg ! MONTE CARLO SYSTEM
PARAMETERS: 10 The EasyLanguage 2000i function named MonteCarlo, which
FORMULA: (C*V)F&1 / VF&1 was referred to in my article in this issue, “Position Sizing
With Monte Carlo Simulation,” uses a Monte Carlo simulation
Visit RTR’s website at http://www.rtrsoftware.com to to estimate the probability that a series of trades will produce
download this formula as well as program updates. Release a specified, maximum closed-trade drawdown. It consists of
8.3 is available for download. the following steps:
—Clay Burch, RTR Software
Collect the profit/loss for each trade and the amount risked
919 510-0608, E-mail: rtrsoft@aol.com
http://www.rtrsoftware.com on each trade. After all trades are collected, loop over the
number of random sequences:
TRADERS’ TIPS
N = Risk%/100 * Equity/TradeRisk
{ Generate random sequence of trades }
For ii = 0 to NTrades - 1 Begin { generate random
where Risk% is the fixed fraction in percent (that is, percent numbers }
of account risked on each trade), Equity is the current equity RandNums[ii] = IntPortion(NTrades * Random(10));
in the account up to that point, and TradeRisk is the dollar
amount being risked on the next trade. Because the number of end;
For ii = 0 to NTrades - 1 Begin { sort index of
contracts must be a whole number, N is rounded down to the RandNums array }
nearest integer. The number of contracts is then multiplied by MaxNum = -1;
the trade profit/loss and added to the current equity to get the For jj = 0 to NTrades - 1 Begin { find biggest # left
new equity value. }
If RandNums[jj] > MaxNum then Begin
The EasyLanguage 2000i code for function MonteCarlo MaxNum = RandNums[jj];
follows: iMax = jj;
end;
Input: ASize (NumericSimple), { account size, $ } end;
DDGoal (NumericSimple), { max closed out trade RandIndx[ii] = iMax; { record location of max
drawdown, % } number }
RiskPer (NumericSimple), { percentage risk per trade } RandNums[iMax] = -1; { “remove” max # from array }
TrRisk (NumericSeries), { risk for current trade, $ } end;
NRand (NumericSimple); { number of random
sequences } { Calculate account balance and drawdown for current
sequence }
Array: Trades[200](0), { Trades, $ (win +; loss -) } Equity = ASize;
TrRisks[200](0), { trade risks, $ (+) } EqtyHigh = Equity;
RandNums[200](0), { array of random numbers } DDmax = 0;
RandIndx[200](-1); { randomly chosen indices for trade For ii = 0 to NTrades - 1 Begin
array } NCon = IntPortion(RiskPer/100 * Equity/
TrRisks[RandIndx[ii]]);
Var: NTrades (0), { Number of trades } NewEquity = Equity + NCon * Trades[RandIndx[ii]];
iSeq (0), { sequence number }
MaxNum (0), { max number left in array RandNums } { Calculate closed trade percent drawdown }
iMax (0), { index (location) of MaxNum in array If (NewEquity > EqtyHigh) then
RandNums } EqtyHigh = NewEquity
NCon (0), { number of contracts } else Begin
Equity (0), { account equity } DD = 100 * (EqtyHigh - NewEquity)/EqtyHigh;
NewEquity (0), { account equity } if (DD > DDmax) then
EqtyHigh (0), { highest equity } DDmax = DD;
DD (0), { closed trade drawdown } end;
DDmax (0), { worst case closed trade drawdown } Equity = NewEquity;
PReturn (0), { percent return } end; { for ii }
AveRet (0), { average percent return } PReturn = 100 * (Equity - ASize)/ASize;
ProbDD (0), { probability of drawdown goal }
ii (0), { loop counter } { Accumulate results for probability calculations }
jj (0); { loop counter } AveRet = AveRet + PReturn;
if (DDmax <= DDGoal) then
{ Collect profit/loss and risk } ProbDD = ProbDD + 1;
If TotalTrades > NTrades then Begin End; { for iSeq }
If TotalTrades - NTrades < 2 then Begin
NTrades = NTrades + 1; { Calculate probabilities by dividing sums by number of
Trades[NTrades - 1] = PositionProfit(1); sequences }
TrRisks[NTrades - 1] = TrRisk; AveRet = AveRet/NRand;
end ProbDD = 100 * ProbDD/NRand;
else Begin{ case where TotalTrades increments by 2 } MessageLog(“RiskPer=”, RiskPer:5:1, “ DDGoal=”,
NTrades = NTrades + 1; DDGoal:5:1,
Trades[NTrades - 1] = PositionProfit(2); “ ProbDD=”,ProbDD:5:1);
TrRisks[NTrades - 1] = TrRisk[1]; MessageLog(“Average Return = “, AveRet:6:2);
NTrades = NTrades + 1; end;
Trades[NTrades - 1] = PositionProfit(1);
TrRisks[NTrades - 1] = TrRisk; MonteCarlo = 1;
end;
end; —Michael R. Bryant
310 370-4069
{ Calculate results 1 day from last bar on chart }
If Date = JulianToDate(DateToJulian(LastCalcDate) - 1) then
Begin