AmiBroker - Please help me understand this code

Discussion in 'App Development' started by DonStar, Nov 19, 2011.

  1. DonStar

    DonStar

    I am trying to learn AmiBroker AFL. I have code from an indicator that I am trying to understand.

    See my comments that begin with "//"

    Please check my interpretation for each line of code.

    I am lost on the 5th line of code.

    // Set the variable "BarColor" equal to Color (Is "Color a variable also?)
    BarColor = Color;

    // Set the variable "Range" equal to the current bar's High minus the current bar's Low
    Range = High - Low;

    // Set the variable "Value1" equal to the current bar's Volume
    Value1 = Volume;

    // Set the variable "Value2" equal to the current bar's Volume multiplied by the current bar's calculated Range
    Value2 = Volume * Range;

    // Here is the line of code that I need help interpreting
    Value3 = IIf(Range < Ref(Range, -1) OR Range > Ref(Range, -1), V, IIf(Range == Ref(Range, -1), V / Range , 0));

    Thank you...

    Don
     
  2. ntfs

    ntfs

    If the range is less than the previous range
    or
    if the range is greater than the previous range,
    Volume,
    if the range is equal to the previous range,
    Vol/Range
    If not either,
    0
    I don't understand the logic in this. After all either the range is greater than,
    less than or equal to the previous range.

    With a little cleaning up you have this

    Value3 = IIf(Range < Ref(Range, -1) OR Range > Ref(Range, -1), V, V / Range);

    nt
     
  3. byteme

    byteme

    Hi Don,

    I don't know AmiBroker but I would assume IIF is an Immediate If similar to that found in VB or ColdFusion.

    IIFs can be re-written as simpler IF/ELSE blocks.

    On this basis, your example can thus be re-written as the following:

    Code:
    if (Range < Ref(Range,-1) OR Range > Ref(Range, -1))
      Value3 = V
    else if (Range == Ref(Range,-1))
      Value3 = V/Range
    else
      Value3 = 0
    
    I don't know where V is being defined though or what it is. Also, since I don't know AmiBroker I can't say what the Ref(x,y) function does but I can guess it is a data series function whereby x is the data series and y is how far to look back in the series.

    So, purely speculating, here is what I'm guessing the code means:

    Code:
    if Range is less than or greater than the previous Range value:
      Set Value3 to V
    else if Range is equal to the previous Range value:
      Set Value3 to V divided by the current Range value
    otherwise:
      Set Value3 to zero
    
    NOTE: I see someone else above already came to the same conclusion.