Hello,
Actually the comment and the code is correct. You
misunderstood the explanations and coding.
In the example we are using 10% max. loss stop (see
APPLYSTOP line).
This means that trade is exited when we lose 10% of entry trade value (not
equity).
ApplyStop stops are based on individual stock price
movement, so in our case
if trade loses 10% of its entry value it is
exited.
The newsletter example used fixed 10%
stop.
Other schemes may involve dynamic (for example
ATR-based stops) and then
calculation of risk would involve that dynamic
value and calculate risk
as a fraction of entry value depending how wide
your stop is.
And yes I can use GetEntryValue to calculate the
risk because it gives actual
amount invested. Once we know what the amount
invested is and our max. loss stop
is we can calculate the highest amount given
trade can lose.
MaxDollarAmountTradeCanLose = EntryDollarValue * (
MaxLossStop% / 100 )
This maximum dollar amount the trade can lose is
the risk.
This is risk per trade.
"What you have done is divided
your $1,000 risk among all 5 stocks"
No, I did not. Analyse the code again. If I entered
5 trades then I have 5 open positions and
each position entry value is
$1000.
So in the code I iterate through 5 OPEN
POSITONS
SumProfitPerRisk = 0;
NumTrades = 0;
// iterate through closed trades first
for( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade() )
{
// risk is calculated as the maximum value we can loose per
trade
// in this
example we are using max. loss stop
// it means
we can not lose more than (MaxLoss%) of invested amount
// hence
ris
Risk = ( MaxLossPercentStop
/ 100 ) * trade.GetEntryValue();
RMultiple = trade.GetProfit()/Risk;
trade.AddCustomMetric("Initial risk $", Risk );
trade.AddCustomMetric("R-Multiple", RMultiple );
SumProfitPerRisk = SumProfitPerRisk
+ RMultiple;
NumTrades++;
}
expectancy3 =
SumProfitPerRisk / NumTrades
The part marked in BOLD typeface in the case you
mentioned would execute FIVE TIMES (because there are five trades).
Risk for each trade (iteration) would be 10% from
entry value ($1000).
So we would iterate 5 times
Risk = 10% * $1000 = $100
RMultiple = Profit / Risk = Profit /
$100
SumRisk = SumRisk + RMultiple;
NumTrades++ ; // increment !
Assuming that each trade gained the amount of 10%,
RMultiple for each trade was 1R we would end up with
SumProfitPerTrade equal to 5 (not
one!)
and NumTrades = 5.
After dividing SumProfitPerRisk
/ NumTrades
we would end up with expectancy of 1R
which is correct value.
If you still having problems with understanding, try to
make up some example and ITERATE "by hand"
through the code. Remember that GetEntryValue is the entry
value of the TRADE, and we are iterating
multiple positions here.
Conclusion: the code and comment is OK.
Best regards,
Tomasz Janeczko
amibroker.com
----- Original Message -----
Sent: Tuesday, January 25, 2005 2:13
AM
Subject: [amibroker] Expectancy
TJ:
I noticed an error in your Example 3 calculation of
Expectancy. Here is the questionable line:
Risk = ( MaxLossPercentStop / 100 ) *
trade.GetEntryValue();
In your text, you
write:
// risk is calculated as the
maximum value we can lose per trade
// in this example we are using max.
loss stop
// it
means we can not lose more than (MaxLoss%) of invested amount
// hence risk
The third
comment line should read: //it means we cannot lose more
than MaxLoss% of total equity per trade. It does NOT mean
we cannot lose more than MaxLoss% of INVESTED AMOUNT. That would be way too
conservative. We always compute risk as a fraction of TOTAL CURRENT EQUITY.
The term "trade.GetEntryValue()" is the
investment or dollar value of a particular trade, i.e., its positionsize in
dollars. Since risk is a fraction of total EQUITY you are willing to lose on
any given trade, you cannot use trade.GetEntryValue() in the risk assignment
code. So, if, at the very beginning of a backtest, you start out with an
initial equity of $100,000, each trade initiated risks $1,000, regardless of
the stock's price or positionsize. If you buy 5 stocks on day 1, the risk PER
STOCK is $1,000, whether the positionsize is $10,000 or $45,000; so, your
total risk for the first day is 5% (5 trades * $1,000 per trade). What you
have done is divided your $1,000 risk among all 5 stocks. So, the
trade.GetEntryValue() term must be replaced by an equity function
(InitialEquity for the first trades on Day 1, and Equity for subsequent
trades). As of yet, I don't know how to call InitialEquity() or Equity() in
this new interface. What would you use in place of trade.GetEntryValue()?
Al Venosa
Check AmiBroker web
page at:
http://www.amibroker.com/
Check
group FAQ at: http://groups.yahoo.com/group/amibroker/files/groupfaq.html
Check AmiBroker web page at:
http://www.amibroker.com/
Check group FAQ at: http://groups.yahoo.com/group/amibroker/files/groupfaq.html
Yahoo! Groups Links