Adding a condition to Amibroker code

1.8k Views Asked by At

This is my Amibroker code for a 2 bar swing chart, I need to add a condition that if price falls below the previous swing low in one bar, then to treat it as a two bar move. The problem I have is, holding the last swing low price variable to check against todays low. I have commented the problem lines in caps. What I have I thought would work but the condition is not showing up on the swing chart. Can someone tell me what I am doing wrong.Thanks.

_SECTION_BEGIN("2 day swing");
upBar = H>Ref(H,-1);
dnBar = L<Ref(L,-1);

HighBarPrice=LowBarPrice=Null;
inLong=inShort=upCount=dnCount=fupbar=fdnbar=0;

for( i=1; i<BarCount; i++ )
   {
    if(inLong==0 AND inShort==0)
      {
       if(upBar[i])
         {
          upCount=upCount+1;
          if(upCount==2)
            {
             fupbar[i] = 1;
             inLong=1;
             dnCount=0;
            }
         }

       if(dnBar[i])
         {
          dnCount=dnCount+1;
          if(dnCount==2)
            {
             fdnbar[i] = 1;
             inShort=1;
             upCount=0;
            }
         }
    if(inLong==1)
      {
       if(dnBar[i])
         {
          dnCount=dnCount+1;
          if(L[i]<LowBarPrice) {dnCount=2;} //THIS IS THE PROBLEM
          if(dnCount==2)
            {
             fdnbar[i]=1;
             inShort=1;
             if(upBar[i])
               {
                upCount=1;
               }
             else
               {
                upCount=0;
               }
          continue;
            }
         }
       if(upBar[i]) {HighBarPrice=H[i];}
       if(upBar[i] AND NOT dnBar[i]){ dnCount=0;}
      }
    if(inShort==1)
      {
       if(upBar[i])
         {
          upCount=upCount+1;
          if(H[i]>HighBarPrice) {upCount=2;}
          if(upCount==2)
            {
             fupbar[i]=1;
             inLong=1;
             if(dnBar[i])
               {
                dnCount=1;
               }
             else
               {
                dnCount=0;
               }
          continue;
            }
      }
       if(dnBar[i]) {LowBarPrice=L[i];}// DOWN BAR IN SHORT SWING SHOULD GIVE NEW LOW
       if(dnBar[i] AND NOT upBar[i]){ upCount=0;}
      }
   }


// Swing chart drawn here 
_SECTION_END();
1

There are 1 best solutions below

2
On

Your LowBarPrice doesn't have an array indexer on it. Also, you initialize it as null and it stays that way because you never assign any value to it after initialization. So technically, in your condition, you're saying, if L[i] < null.

Write your conditions outside the loop. That'll create an array that will hold your price until you reference it in the loop.

So, for example, initialize LowBarPrice like this:

LowBarPrice = ValueWhen(DownBar, Ref(L,-1));

Thereafter, you'll get the price when you reference it in the loop.

if(L[i] < LowBarPrice[i])

This article really helped me get my head around looping in AmiBroker. It might give some context around your issue. The part that relates specifically to your question is under the section "Array Indexing

http://www.amibrokerforum.com/index.php?topic=50.0