[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[amibroker] Re: Correct use of BarsSince inside a loop



PureBytes Links

Trading Reference Links

You can use array functions inside loops, but it will be slow. The
reason this is not working is because BarsSince() takes an array
argument. You are passing in a number. I haven't tried this, but I'm
guessing AmiBroker is internally converting the number to an array
where each element has the same value. Remove the [i] notation.
Alternatively, change the logic so it does not use array functions
inside the loop.

Cheers,
Steve

--- In amibroker@xxxxxxxxxxxxxxx, "ozzyapeman" <zoopfree@xxx> wrote:
>
> Hi GP,
> 
> But even when I put HoldTime inside the loop, as below, it still does
> not work. In fact Amibroker ends up hanging. Are there certain functions
> that just can't be used inside loops?
> 
> 
> 
> for (i = Length; i < (BarCount-TradeDelay); i++)
> {
> wasLong = longContractCount > 0;
> wasShort = shortContractCount > 0;
> HoldTime = BarsSince(ShortSignal[i]);
> 
> if (shortContractCount > 0)
> {
> reachedProfitLevel = C[i] < profitLevel;
> if (reachedProfitLevel)
> {
> coverSignal[i] = 3;
> shortContractCount = 0;
> }
> else if( HoldTime[i] > 1728) // 6 days worth of 5-min bars
> {
> coverSignal[i] = 2;
> shortContractCount = 0;
> }
> }
> // Short entry
> if (NOT wasShort AND (longContractCount == 0))
> {
> shortSignal[i] = 1;
> shortContractCount = 1;
> }
> }
> --- In amibroker@xxxxxxxxxxxxxxx, "gp_sydney" <gp.investment@> wrote:
> >
> > Where and how is ShortSignal first defined? You define it iteratively
> > near the end of the loop, but HoldTime is based on its value before
> > entering the loop. If it was just all Null before that, then the test
> > for HoldTime[i] > 1728 would always fail (ie. effectively be False).
> >
> > GP
> >
> >
> > --- In amibroker@xxxxxxxxxxxxxxx, "ozzyapeman" zoopfree@ wrote:
> > >
> > > Hello, hoping someone can help me with this code bug. I'm simply
> > > trying to cover a short position if it has been held for more than 6
> > > days ( 1728 five-min bars). The backtest results indicate that the
> 'if
> > > else'condition is being ignored. Positions continue to be held well
> > > beyond the 6 days.
> > >
> > > If I try declaring HoldTime inside the loop, for e.g.:
> > >
> > > HoldTime = BarsSince(ShortSignal[i]==1)
> > > or just  HoldTime = BarsSince(ShortSignal[i])
> > >
> > > what happens is that AB just hangs indefinitely when I try to verify
> > > AFL syntax from within the formula editor (using AB 5.16)
> > >
> > > And when I try declaring HoldTime outside the loop, the 'if else'
> > > condition is ignored (but at least AB does not hang). For now,
> please
> > > assume that I need the loop. The below code snippet is a piece of a
> > > more involved system. What's wrong?
> > >
> > > HoldTime = BarsSince(ShortSignal==1);
> > >
> > > for (i = Length; i < (BarCount-TradeDelay); i++)
> > > {
> > > wasLong = longContractCount > 0;
> > > wasShort = shortContractCount > 0;
> > >
> > > if (shortContractCount > 0)
> > > {
> > > reachedProfitLevel = C[i] < profitLevel;
> > > if (reachedProfitLevel)
> > > {
> > > coverSignal[i] = 3;
> > > shortContractCount = 0;
> > > }
> > > else if( HoldTime[i] > 1728) // 6 days worth of 5-min bars
> > > {
> > > coverSignal[i] = 2;
> > > shortContractCount = 0;
> > > }
> > > }
> > > // Short entry
> > > if (NOT wasShort AND (longContractCount == 0))
> > > {
> > > shortSignal[i] = 1;
> > > shortContractCount = 1;
> > > }
> > > }
> > >
> >
>



------------------------------------

Please note that this group is for discussion between users only.

To get support from AmiBroker please send an e-mail directly to 
SUPPORT {at} amibroker.com

For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/

For other support material please check also:
http://www.amibroker.com/support.html
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/amibroker/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/amibroker/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:amibroker-digest@xxxxxxxxxxxxxxx 
    mailto:amibroker-fullfeatured@xxxxxxxxxxxxxxx

<*> To unsubscribe from this group, send an email to:
    amibroker-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/