Mostly I write auto trading programs and I send an order with a trailing stop
target and forget about it. If I control the exit ordering within the program I
set a static var with the trade price and use that when calculating the stop.
But, it seems you are trying to set this in back test, something I don't waste
much time on, and are trying to simulate your real time results.
A trend starts when you have a buy or short signal. You can set the price of
the trade in BuyPrice or ShortPrice. Then you can use BarsSince to find that
value. If the BarsSince Short is a smaller number than BarsSince Buy you know
you are in a down trend and you would use the number of bars returned from the
short in Ref(ShortPrice, -BarsSinceShort); Then you don't care about for{}
loops. You access the ShortPrice directly and add your trailing offset and
compare that to the current C, MA crossover value or whatever. Exit the
position when the C is above the stop value, in the Short case, or below it in
the long case.
Give that a go and see if you can make it work. I think you will find the code
much more concise than using a loop. And it is a lot faster.
Barry
--- In [email protected], "edwol53" <ed...@...> wrote:
>
> Hello Barry
>
> Thankyou for the heads up on this issue - your valuable experience and
> comments are much appreciated.
>
> As for your comments regarding a way of devising a dynamic way of accessing
> eaqrlier bars using standard AFL logic would be ideal, but I am trying to
> resolve a problem with a trailing stop which I refered to in message 138653
> Re: Trailing Stop S & C tips 1-2008 & 138598 Trailing Stop S & C tips
> 1-2008
>
> All the trailing stops I have seen in AB seem to fall back on using flow
> control structures such as "for i = " loops to ratchet the stop.
>
> In this case it requires a starting point at a given date on the chart
> applying a stop value on that date to start the calculation (ie access to an
> array element).
>
> If there is a way of using AFL array logic to circumvent array element
> manipulation,I am open to suggestion.
>
> Thank again for your input and other suggestions that you have regarding my
> comments above are most welcome.
>
> Cheers
> Ed
>
> --- In [email protected], "Barry Scarborough" <razzbarry@> wrote:
> >
> > Unless you specify SetBarsRequired(nnnn,nnnn) where nnnn is larger than the
> > number of bars in your database Ami will only load enough bars, into a
> > temporary array, so that it can display your charts correctly. When you add
> > more bars to a window the temp array is enlarged and the indicator is
> > recalculated so that what is plotted will again be correct. So the number
> > you get in your test will be the number of bars displayed + the longest
> > period in any of your indicators. It gets worse. If you try to manually
> > access data that is beyond the scope of the temporary array you will get an
> > access violation which will either blow your program up or return invalid
> > data.
> >
> > There is another thing you need to know. When you set anything in an array
> > you will have to set it every time the indicator is scanned. When the
> > indicator is scanned all the indicators plotted are recalculated. In real
> > time trading this can be many times a second and since you will be adding
> > data the point you want to access may be outside the scope of the bars. If
> > you are using static data it will scan your indicator every time you click
> > on a bar or change the number of bars displayed or scroll through the bars.
> > Managing data as you plan will get very complicated and hell to debug when
> > it works incorrectly. Expect errors.
> >
> > You will be much better off if you can devise a dynamic way to access data
> > in earlier bars using standard AFL logic. Then reference it with the Ref()
> > statement. I see some people using for, do and while loops in Ami. Most of
> > the time that can be replaced with AFL array logic which is much faster and
> > less prone to calculation errors. For example someone sent me a formula the
> > other day to debug. It was using all the bars in the ticker. I was testing
> > an auto trading formula on a 5 minute chart and when I started his formula
> > Ami ground to a crawl. It was processing his loop three times a second,
> > well it was trying to and doing badly. Avoid loops wherever possible.
> >
> > Barry
> >
> > --- In [email protected], "edwol53" <edwol@> wrote:
> > >
> > > Hello
> > >
> > > I am trying to access/extract a subscript [i] if an array element a[i].
> > >
> > > The aim is to input a date and extract the bar subscript from that date's
> > > bar so that the subscript can be used over-write/modify an associated
> > > array element[i] (if I understand how array data is constructed in AFL
> > > correctly ???)
> > >
> > > A test code segment and debug outputis shown below. The issue I am having
> > > difficulty with is that in the first line the index and barcount are
> > > correct if all the data (including bars not displayed on the screen) is
> > > taken into account. In line 2 the barcount readjusts to number of bars
> > > displayed on the screen. I can accept that as a quirk but works. I then
> > > zoom out (as shown by barcount)loading all the bars (data) onto the
> > > screen(debug shows zoomout effect in line 9 - 26. But the subscript [i]
> > > is now empty from line 20 - as I continue to zoom out on the chart. Why?
> > >
> > > Is this a bug or is there something I am missing??
> > >
> > > Can anyone offer an explanation as to what is happening?
> > >
> > > // Array or number ?
> > > dt = ParamDate("Date of the trend", "2009-05-25" ,0);
> > > i=0;
> > > i = ValueWhen(DateNum() == dt, BarIndex(), 1);
> > >
> > > _TRACE(" i = " + NumToStr(i, 1.0) + " index date = " + NumToStr(dt, 1.0)
> > > + " Barcount = " + NumToStr(BarCount, 1.0) );
> > >
> > > 00000000 0.00000000 [2440] i = 6,413 index date = 1,090,525
> > > Barcount = 6,416
> > > 00000001 0.03319501 [2440] i = 6,413 index date = 1,090,525
> > > Barcount = 69
> > > 00000002 2.32265329 [2440] i = 6,413 index date = 1,090,525
> > > Barcount = 69
> > > 00000003 7.29352045 [2440] i = 6,413 index date = 1,090,525
> > > Barcount = 69
> > > 00000004 12.29429150 [2440] i = 6,413 index date = 1,090,525
> > > Barcount = 69
> > > 00000005 17.29241562 [2440] i = 6,413 index date = 1,090,525
> > > Barcount = 69
> > > 00000006 22.29343414 [2440] i = 6,413 index date = 1,090,525
> > > Barcount = 69
> > > 00000007 27.29189682 [2440] i = 6,413 index date = 1,090,525
> > > Barcount = 69
> > > 00000008 32.29233551 [2440] i = 6,413 index date = 1,090,525
> > > Barcount = 69
> > > 00000009 36.40725327 [2440] i = 6,413 index date = 1,090,525
> > > Barcount = 92
> > > 00000010 36.77444458 [2440] i = 6,413 index date = 1,090,525
> > > Barcount = 127
> > > 00000011 37.05470276 [2440] i = 6,413 index date = 1,090,525
> > > Barcount = 179
> > > 00000012 37.29121780 [2440] i = 6,413 index date = 1,090,525
> > > Barcount = 179
> > > 00000013 37.30448532 [2440] i = 6,413 index date = 1,090,525
> > > Barcount = 257
> > > 00000014 37.54277802 [2440] i = 6,413 index date = 1,090,525
> > > Barcount = 374
> > > 00000015 37.72683716 [2440] i = 6,413 index date = 1,090,525
> > > Barcount = 550
> > > 00000016 37.91091156 [2440] i = 6,413 index date = 1,090,525
> > > Barcount = 814
> > > 00000017 38.09486389 [2440] i = 6,413 index date = 1,090,525
> > > Barcount = 1,210
> > > 00000018 38.33518219 [2440] i = 6,413 index date = 1,090,525
> > > Barcount = 1,804
> > > 00000019 39.79113770 [2440] i = 6,413 index date = 1,090,525
> > > Barcount = 2,695
> > > 00000020 40.27950287 [2440] i = {EMPTY} index date = 1,090,525
> > > Barcount = 4,031
> > > 00000021 40.58501816 [2440] i = {EMPTY} index date = 1,090,525
> > > Barcount = 6,035
> > > 00000022 40.80830765 [2440] i = {EMPTY} index date = 1,090,525
> > > Barcount = 6,416
> > > 00000023 42.29502487 [2440] i = {EMPTY} index date = 1,090,525
> > > Barcount = 6,416
> > > 00000024 47.29590988 [2440] i = {EMPTY} index date = 1,090,525
> > > Barcount = 6,416
> > > 00000025 52.29293823 [2440] i = {EMPTY} index date = 1,090,525
> > > Barcount = 6,416
> > > 00000026 57.29567719 [2440] i = {EMPTY} index date = 1,090,525
> > > Barcount = 6,416
> > >
> >
>