Nick,
I have not studied your code in depth, nor tested it out yet. But,
assuming everything else is correct, two things stand out.
1. In your initialization loop for MSL, you should really be
iterating from bar=0 to bar<BarCount, not bar=1. As it stands, if
you get a signal on bar=0, you will not store the risk for it. When
iterating through your trades later, all trades would be using the
wrong risk, offset by one position in the array.
That being said, there's no need for this loop at all. I believe
that the entire loop can be replaced with the following:
MSL = IFF(Buy OR Short, Risk, 0);
2. It appears that you are trying to create MSL as a non sparse
array (i.e. values all at consecutive indices) rather than a sparse
array (i.e. values of interest whose indices may be widely spaced
apart). This is likely the root of your problem.
For example, you are populating the array per signal, but then
querying the values per trade. Due to portfolio restrictions, not
all signals will necessarily have become trades (e.g. lack of
funds, max open positions, etc). As soon as any signal did not
result in a trade, your algorithm falls apart and you will be
referencing the wrong risk index for all subsequent trades.
Using the sparse array assignment for MSL given above, and the bar
indexing of the trade as suggested in my earlier notes, you would
not suffer this problem.
You might also run into alignment problems if the array size of
~~~Equity is different than ~MSL. Again, using bar index of trade
would avoid that.
Mike
--- In [email protected], Nick Willemse <nick.wille...@...>
wrote:
>
> Hi,
>
> Attached is the afl. This runs on EC 5min database. If you want me
> to upload this db to the files section, i'll be happy to do it.
I've
> tried everything. For some reason I just cannot pass the risk per
> trade to the backtester just to output the values and calc r-
> multiples. I can't believe how difficult and cumbersome this
process
> is. The values are in the ~MSL before the backtester.
>
> Mike, if will be much harder to recalc the ATR array in the
bactester
> and trying to figure out which bar the trade happened and take that
> value from the array as the original risk. That's why i'm trying to
> pass it. Also, if the trade object had a method getEntryBarNum it
> would have been easier.
>
> Any help appreciated!
> Nick
>
>
>
> On Jul 15, 2009, at 9:04 PM, Mike wrote:
>
> > Nick,
> >
> > Since ATR uses OHLC I'm assuming that it will use the OHLC of the
> > foreign symbol once bracketed by SetForeign/RestorePriceArrays.
> >
> > The point that I was trying to stress was that once you've called
> > SetForeign, you can do all kinds of array manipulations on that
> > symbol, then reference the value you want for the bar in
question.
> >
> > In other words, instead of trying to reference your external
array
> > FROM the backtester code, calculate the array IN the backtester
code.
> >
> > Look again at the earlier post and try using ATR between
SetForeign/
> > RestorePriceArrrays.
> >
> > That being said, when looking again at your original code example
> > and taking a cue from Bruce's reply; try using "X" in both the
> > AddToComposite and Foreign, and use atcFlagEnableInPortfolio
instead
> > of atcFlagEnableInBacktest.
> >
> > Mike
> >
> > --- In [email protected], Nick Willemse <nick.willemse@>
> > wrote:
> > >
> > > Thanks Mike,
> > >
> > > Yea, van tharp's stuff makes a lot of sense to me :)
> > >
> > > The example you've posted still uses only price data, which is
> > kind of
> > > straight forward referring back to price. I am using that
value of
> > > the ATR of price. The problem is trying to make this
value(array)
> > > available in the backtester part of the code.
> > >
> > >
> > > Nick
> > >
> > > On Jul 15, 2009, at 1:32 PM, Mike wrote:
> > >
> > > >
> > > >
> > > > This is a popular topic lately. Van Tharp must be doing
well with
> > > > his new book ;)
> > > >
> > > > See my earlier post on the subject for how to calculate the
risk
> > > > anyway you see fit. Note that the expectancy shown in that
post
> > was
> > > > the formula provided by the original poster, it is not the
formula
> > > > used by Van Tharp, so refer to the methodology, but not the
> > formula:
> > > >
> > > > http://finance.groups.yahoo.com/group/amibroker/message/
139969
> > > >
> > > > Mike
> > > >
> > > > --- In [email protected], Nick Willemse
<nick.willemse@>
> > > > wrote:
> > > > >
> > > > > Hi Everyone,
> > > > >
> > > > > I'm trying to add Risk and R-Multiples to the TradeList.
I've
> > looked
> > > > > at the example using MaxLossPercent to add r-multiples and
> > that is
> > > > > pretty straight forward. However, my risk per trade is
based
> > on some
> > > > > multiple of the ATR. So I put the risk points per trade
in an
> > array,
> > > > > then I tried using the addtocomposite and Foreign
functions to
> > get
> > > > > this array to the backtester section, but it seems that all
> > the data
> > > > > is zero when it gets to the backtester. Is there any
other way
> > to
> > > > > make this array holding the risks per trade available to
the
> > > > > backtester to output this for each trade?
> > > > >
> > > > > Here's some of the code:-
> > > > >
> > > > >
> > > > > Risk = 2.5 * ATR(14);
> > > > > i = 0;
> > > > > MSL = 0;
> > > > > for( bar=1; bar < BarCount; bar++)
> > > > > {
> > > > > if (Buy[bar] == 1 OR Short[bar] == 1)
> > > > > {
> > > > > MSL[i] = Risk[bar];
> > > > > // _TRACE("MSL = " + MSL[i]); //this outputs correct
values
> > > > > i++;
> > > > > }
> > > > > }
> > > > > AddToComposite( MSL,"~MSL","V", atcFlagDeleteValues |
> > > > > atcFlagEnableInBacktest); // if i plot this it looks fine
> > > > >
> > > > > ...
> > > > >
> > > > > if(AAAction == actionPortfolio)
> > > > > {
> > > > > MSL = Foreign("~MSL", "X",0);
> > > > > _TRACE("MSL=" + MSL[0]); // this output 0 for every index
in the
> > > > > array
> > > > > ...
> > > > >
> > > > > }
> > > > >
> > > > >
> > > > >
> > > > > Nick
> > > > >
> > > >
> > > >
> > > >
> > >
> >
> >
> >
>