I want to get a second value from the SAR function. I have read so much and coded using VarGet, VarSet, yet still do not understand how to code this correctly. Maybe I misunderstand VarGet, VarSet.
In the SAR code 'af' is used for calculations. I want to know/plot when the reversal condition 'af > MaxAF' occurs as well as the normal SAR output.
I believe that when the reversal condition is true it may be passed back through an input parameter. Everything I have coded so far has left me more confused.
The reference seems clear enough yet does not illustrate to me what needs to be where in my code. If I knew why it doesn't make sense to me I might be able to better ask how to get this coded.
I understand what you mean by reversal but it is not the SAR, Close cross I am interested in. The SAR code has a condition ' af > MaxAF ' which triggers the subsequent SAR reversal. This condition occurs one bar before the cross. I want to test if that one bar has significance.
I coded a global variable as Boolean and can check that way, however I was curious about passing multiple variable in and out of a function. The Global variable works, but I also want to know what AFL can do and VarGet/VarSet appear to do that.
No matter how I coded VarSet/VarGet, it always returned the input. I could never get it to return a new function generated value. I saw many examples of VarGet/VarSet using a loops index as a suffix. I could not get anything I coded to work.
/////////////////////////////////
// Parabolic SAR re-implemented in
// native AFL.
//
// Example of for/if/else control statements
//
// Requires:
// AmiBroker 4.31.1
//
// Written by: Tomasz Janeczko
IAF = 0.02; // acceleration factor
MaxAF = 0.2; // max acceleration
psar = Close; // initialize
long = 1; // assume long for initial conditions
af = IAF; // init acelleration factor
ep = Low[ 0 ]; // init extreme point
hp = High [ 0 ];
lp = Low [ 0 ];
for( i = 2; i < BarCount; i++ )
{
if ( long )
{
psar [ i ] = psar [ i-1 ] + af * ( hp - psar [ i-1 ] );
}
else
{
psar [ i ] = psar [ i-1 ] + af * ( lp - psar [ i-1 ] );
}
reverse = 0;
//check for reversal
if ( long )
{
if ( Low [ i ] < psar [ i ] )
{
long = 0; reverse = 1; // reverse position to Short
psar [ i ] = hp; // SAR is High point in prev trade
lp = Low [ i ];
af = IAF;
}
}
else
{
if ( High [ i ] > psar [ i ] )
{
long = 1; reverse = 1; //reverse position to long
psar [ i ] = lp;
hp = High [ i ];
af = IAF;
}
}
if ( reverse == 0 )
{
if ( long )
{
if ( High [ i ] > hp )
{
hp = High [ i ];
af = af + IAF;
here and in the following else phrase.
if( af > MaxAF ) af = MaxAF;
}
if( Low[ i - 1 ] < psar[ i ] ) psar[ i ] = Low[ i - 1 ];
if( Low[ i - 2 ] < psar[ i ] ) psar[ i ] = Low[ i - 2 ];
}
else
{
if ( Low [ i ] < lp )
{
lp = Low [ i ];
af = af + IAF;
if( af > MaxAF ) af = MaxAF;
}
if( High[ i - 1 ] > psar[ i ] ) psar[ i ] = High[ i - 1 ];
if( High[ i - 2 ] > psar[ i ] ) psar[ i ] = High[ i - 2 ];
}
}
}
As I understand the code, setting 'af = MaxAF' sets the condition for a reversal and occurs the bar before the 'psar' reversal. I understand that Ref() is similar but not identical. Setting 'af = MaxAF' may or may not trigger a reversal, depending upon the next bars data. This also means Ref() needs the next bars data which does not exist yet.
The internal 'af > MaxAF' condition is the data needed. I do not believe Ref() can see the 'af > MaxAF' condition real time.
Although setting a Global variable works in real time, I was also interested in passing multiple variables in a function but after reading many examples and trying for too long, it was obvious I did not understand what I was doing.
Perhaps a Global variable makes best sense in this case.
You don't need VarGet/VarSet. These functions are for dynamic variables, i.e. those that have identifiers that are build in runtime.
You just need ordinary variables nothing more.
And no, the reversal condition inside the function does not occur earlier, see the code comparing both built-in and reimplemented SAR:
/////////////////////////////////
// Parabolic SAR re-implemented in
// native AFL.
//
// Example of for/if/else control statements
//
// Requires:
// AmiBroker 4.31.1
//
// Written by: Tomasz Janeczko
function ParabolicSAR(IAF, MaxAF, reversal )
{
//IAF = 0.02; // acceleration factor
//MaxAF = 0.2; // max acceleration
reversal = Null;
psar = Close; // initialize
long = 1; // assume long for initial conditions
af = IAF; // init acelleration factor
ep = Low[ 0 ]; // init extreme point
hp = High [ 0 ];
lp = Low [ 0 ];
for( i = 2; i < BarCount; i++ )
{
if ( long )
{
psar [ i ] = psar [ i-1 ] + af * ( hp - psar [ i-1 ] );
}
else
{
psar [ i ] = psar [ i-1 ] + af * ( lp - psar [ i-1 ] );
}
rev = 0;
//check for reversal
if ( long )
{
if ( Low [ i ] < psar [ i ] )
{
long = 0; rev = 1; // rev position to Short
psar [ i ] = hp; // SAR is High point in prev trade
lp = Low [ i ];
af = IAF;
}
}
else
{
if ( High [ i ] > psar [ i ] )
{
long = 1; rev = 1; //rev position to long
psar [ i ] = lp;
hp = High [ i ];
af = IAF;
}
}
if ( rev == 0 )
{
if ( long )
{
if ( High [ i ] > hp )
{
hp = High [ i ];
af = af + IAF;
if( af > MaxAF ) af = MaxAF;
}
if( Low[ i - 1 ] < psar[ i ] ) psar[ i ] = Low[ i - 1 ];
if( Low[ i - 2 ] < psar[ i ] ) psar[ i ] = Low[ i - 2 ];
}
else
{
if ( Low [ i ] < lp )
{
lp = Low [ i ];
af = af + IAF;
if( af > MaxAF ) af = MaxAF;
}
if( High[ i - 1 ] > psar[ i ] ) psar[ i ] = High[ i - 1 ];
if( High[ i - 2 ] > psar[ i ] ) psar[ i ] = High[ i - 2 ];
}
}
reversal[ i ] = rev;
}
return psar;
}
acc = Param("Acceleration", 0.02, 0, 1, 0.001 );
accm = Param("Max. acceleration", 0.2, 0, 1, 0.001 );
my_sar = SAR( acc, accm );
my_sar2 = ParabolicSAR( acc, accm, &reversal2 );
Plot( my_sar, _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle("Style", styleDots | styleNoLine, maskDefault | styleDots | styleNoLine ) );
Plot( my_sar2, _DEFAULT_NAME(), ParamColor( "Color2", colorCycle ), ParamStyle("Style", styleDots | styleNoLine, maskDefault | styleDots | styleNoLine ) );
dn_reversal = Cross( C, my_sar );
up_reversal = Cross( my_sar, C );
Plot( Ref( up_reversal OR dn_reversal, 1 ) , "Bar before cross", colorGreen, styleHistogram | styleOwnScale );
Plot( reversal2 , "Reversal from function", colorYellow, styleHistogram | styleOwnScale );