Appendix 1
{****************************************************
The Simplest System #3 with Money Management.
Copyright (c) 2002 DT
****************************************************}
Input: Price((H+L)*.5),
PtUp(4.), PtDn(4.), {Max correction to change trend}
MM_Model(2), {1 = % Risk Model; 2 = % Volatility Model;
3 = Drawdown Model; 4 = Kelly Model; 5 = Williams' Model;
6 = Fixed Ratio Model; 7= Market Money Model}
MM(1), {% Risk parameter}
MM_add(0), {% Risk for playing market money; 0 to disactivate}
MaxVolat(100), {% Risk for playing market money; 100 to disactivate}
MaxDD(20), {% Drawdown}
InitCapital(100000); {Initial capital to trade}
Vars: LL(99999), HH(0), Trend(0), Volat(TrueRange);
Vars: MP(0), Risk(Range), Num(1), add_num(0), red_num(0), FRDelta(0), DD(0),
Equity(InitCapital), TotalEquity(InitCapital), EqTop(InitCapital),
AssuredProfit(0), HPositionProfit(0), Kelly(0);
MP = MarketPosition;
Volat = .5 * TrueRange + .5*Volat[1];
if MP <= 0 then begin
if Price < LL then LL = Price;
if Price cross above LL*(1 + PtUp*.01) then begin
Trend = 1;
HH = Price;
end;
end;
if MP >= 0 then begin
if Price > HH then HH = Price;
if Price cross below HH*(1 - PtDn*.01) then begin
Trend = -1;
LL = Price;
end;
end;
If trend = 1 then Risk = PtDn * .01 * close {+ Slippage};
If trend = -1 then Risk = PtUp * .01 * close {+ Slippage};
HPositionProfit = maxlist( OpenPositionProfit, HPositionProfit);
AssuredProfit = HPositionProfit - Risk;
Equity = InitCapital + NetProfit;
TotalEquity = Equity + OpenPositionProfit;
EqTop = MaxList(EqTop, TotalEquity);
if MM_Model = 1 then { % Risk Model }
Num = floor(MM * Equity *.01/Risk);
if MM_Model = 2 then { % Volatility Model }
Num = floor(MM * Equity *.01/ Volat / BigPointValue );
if MM_Model = 3 then begin { Drawdown Model }
Num = floor(MM * (Equity - (1 - MaxDD*.01) * EqTop) * .01 / Volat / BigPointValue);
end;
if MM_Model = 4 then begin { Kelly Model }
If TotalTrades > 20 and GrossProfit > 0 then
Kelly = NumWinTrades/TotalTrades * (1 - GrossLoss/GrossProfit)
else
Kelly = 0.1;
if Kelly > .9 then Kelly = .9;
Num = floor(MM * Kelly * Equity * .01 / Risk);
{Print(Kelly);}
end;
if MM_Model = 5 then begin { Larry Williams' Model }
value11 = MaxList(-LargestLosTrade / MaxList(CurrentContracts, 1) , Risk);
Num = floor(MM * Equity *.01 / value11);
end;
if MM_Model = 6 then begin { Fixed Ratio Model }
{ DD = MaxList(DD, (EqTop - TotalEquity)/MaxList(CurrentContracts, 1)) ; {Max Drawdown}
if TotalTrades > 20 and DD > 0 then FRDelta = MM * DD *.01
else }
FRDelta = MM * volat * BigPointValue * .01; {Delta}
value12 = MaxList(Equity - .5*close*(close + FRDelta)/FRDelta, 0.25);
Num = floor(SquareRoot(2*value12/FRDelta + .25) + .5);
end;
if MM_Model = 7 then { Playing the market money }
num = floor((MM * (InitCapital + MinList(NetProfit, 0)) + MM_add * MaxList(NetProfit, 0)) * .01 / Volat / BigPointValue);
{ Entries}
if trend = 1 and trend[1] <> 1 then buy("Trend.LE") num contracts at market;
if trend = -1 and trend[1] <> -1 then sell("Trend.SE") num contracts at market;
add_num = floor( MM_add * AssuredProfit * .01/ Volat / BigPointValue); { Assured Profit Pyramiding }
if add_num > 0 and OpenPositionProfit > Volat * BigPointValue then begin
if Trend = 1 and MP = 1 then buy("Add.LE") add_num contracts at market;
if Trend = -1 and MP = -1 then sell("Add.SE") add_num contracts at market;
end;
red_num = floor((CurrentContracts * Volat * BigPointValue - MaxVolat * TotalEquity * .01)/ close);
if red_num > 0 then begin
if Trend = 1 and MP = 1 then exitlong("Red.LX") red_num contracts at market;
if Trend = -1 and MP = -1 then exitshort("Red.SX") red_num contracts at market;
end;
if Num < 1 then Num = 1;
Appendix 2.
Add the following lines to the code of the system handling the lot:
{**************************************************** Excel output for optimal f computation Copyright © 2002 DT ****************************************************} Var: Trades(0), Str(""); Trades = totaltrades; if currentbar = 1 then begin FileDelete("D:\TS_Export\M-Trading3_OptF.csv"); Str = "Initial Equity" + "," + "Max Loss" + "," + "f" + "," + "Trades" + "," + "Geom Mean" + NewLine + NewLine + "Profit" + "," + "HPR" + "," + "TWR" + "," + "Equity" + "," + "Num" + NewLine; FileAppend("D:\TS_Export\M-Trading3_OptF.csv", Str); end; if trades <> trades[1] then FileAppend("D:\TS_Export\M-Trading3_OptF.csv", NumToStr(PositionProfit(1),3) + newline); {****************************************************} |
and launch the system. When we open in Excel the resulting file, we’ll see a table:
Calculating the optimal f in Excel. | A | B | C | D | E |
1 | Initial Equity | Max Loss | f | Trades | Geom Mean |
2 | 100000 | -49.9 | 0.1 | 184 | 1.004012 |
3 | Profit | HPR | TWR | Equity | Num |
4 | -12 | 0.966014 | 0.966014 | 96601.43 | 273 |
5 | 11 | 1.031154 | 0.996109 | 99610.91 | 282 |
… | | | | | |
Enter the starting capital in the A3 field, the formula =MIN (A4:A
Х) in the B3, where AX designates the last non-enpty field in the A column. Enter =1- C$2*A4/B$2 in B4 and continue till line X.
Enter any value from 0 to 1 in the C2 field, С4 = В4, С5 = С4*В5, D4 =A$2*C4, D2 = COUNTIF(A4:AX,"<>0"), E2 = POWER (CX,1/D$2), E4 = INTEGER (D4/(B$2/-C$2)). Continue the formulas in the С4:Е4 fields till the last non-empty line Х. In the E column we have the number of lots for the f value given in the С2 field.
To calculate the optimal f use the menu: Service à Solution Search à designate target field: $C$Y (where Y – number of the line corresponding to the trade previous to the one we optimize f for); Changing fields: $C$2; Limits: $C$2 >=0; $C$2 <= 1 à Execute.
Excel’s in-built optimizer will find the value of the optimal f, maximizing the TWR function.
Appendix 3.
The method states the number of lots traded to capital growth needed to increase the number of lots should be a constant value. This will be written down as:
En + n * D = En+1
where En – current capital, n – current number of lots, D – the Delta parameter. Then, recursively,
En = En-1+ (n – 1) * D = En-2 + (n – 2) * D + (n – 1) * D = …
= E1 + (1 + 2 + … + (n – 1)) * D,
hence, considering the fact the bracketed expression is a sum of an arithmetical progression members
En = E1 + 0.5 * n * (n – 1) * D.
This equation is a square equation in relation to n:
n^2 – n – 2 * (En – E1 ) / D = 0.
High school analysis course tells us this equation has two roots:
n1 = 0.5 + (2 * (En – E1 ) / D + 0.25)^(0.5)
and
n2 = 0.5 – (2 * (En – E1 ) / D + 0.25)^(0.5),
where n2 <= 0. If we consider En – E1 to be the profit, then:
Num_lots = 0.5 + (2 * Profit / Delta + 0.25)^(0.5).
This formula starts trading with one lot. If the starting capital allows to trade many lots at once, we must find the En – E1 considering the starting capital. Clearly, En = Starting Capital + profit. One Delta can buy k = Price / Delta stocks. The corresponding Ek capital is equal to
k * Price = Price^2 / Delta. Then
E1 = Ek
Deriving the formula for the fixed fraction method.– 0.5 * k * (k – 1) * D = 0.5 * Цена * (Цена + D) / D,
And our desired formula will be
Number_lots = 0.5 + (2 * (Starting_capital + profit - 0.5 * Price * (Price + Delta) / Delta) / Delta + 0.25)^(0.5).
. Controlling the drawdowns.