How to query the indicator value

Hi All,
This is the code :

Filter = 1;

_d = StochD( 9 );
_k = StochK( 9 );

_dVal = LastValue( _d );
_kVal = LastValue( _k );


AddColumn( _d, "%D" );
AddColumn( _k, "%K" );

AddColumn( _dVal, "Last %D" );
AddColumn( _kVal, "Last %K" );

This is the output:
image

Why the "Last %K" and "Last %D" not reflect any change ?

I would like to have an update "Last %K" to execute logic like:

// if don't like arrays - I need the last value..
if ( LastK >= 50 )
{
code ...
code ....
}

I did read Understanding how AFL works
Without much of success .. as you all see..

Thanks;

Is this good practice ?

Filter = 1;

_d = StochD( 9 );
_k = StochK( 9 );

_dVal = LastValue( StochD( 9 ) );
_kVal = LastValue( _k );

/****** Good Practice ?? ********/
_x = StochD( 9 ) > 50;
if ( 1 == LastValue( _x ) )
{
	// code ..
	// code ..
}
/**********************************/

AddColumn( _x, " 50 < D" ); 

AddColumn( _d, "%D" );
AddColumn( _k, "%K" );

AddColumn( _dVal, "Last %D" );
AddColumn( _kVal, "Last %K" );

Thank you all;

@leob

Why the "Last %K" and "Last %D" not reflect any change?

because the LastValue() function returns the LAST calculated value (a NUMBER) of the array that you are passing to it as a parameter.

Add this line to your exploration:

SetSortColumns(-2);

and you will immediately see that the "last values" will correspond to the most recent row in your exploration.

Adding the above line will sort the result by the Date/Time column from the most recent one descending - i.e. the first row has the most recent bar data where from the _d and _k arrays you are getting the _dVal and _kVal NUMBERS using the LastValue() function.

You probably need to read the AFL document again and try to write some additional explorations and code samples of your own until you get it.

And maybe this other tutorial will also help you to learn when it is appropriate to use arrays and when to use the single values of them (looping). For now, try to understand well the content of the first pages.

Then search and study other code examples here in the forum and you'll see that the buy/sell triggering conditions (and/or Filter for explorations) are actually ARRAY values that change from bar to bar, so you can have multiple signals in the range selected - both intraday or over a date range.

Finally, when you will be comfortable with the above material you can read this recent topic by @Tomasz that further explains AmiBroker data types.

1 Like

Thank you very much "beppe" for your effort.
I'm not insist on "LastValue", it was just one of my attempts ...

I just want to complete very simple task :

pseudo code:

If ( my_indicator_value > 50 )
{
...start lose money ...
}

that it, that all.

And really, I cover all the documents..

Thank you all;

@leob the problem, as I see it, is that you should learn the arrays logic to go ahead!

In any case, here is a very basic example to use an indicator level as a signal generator for an elementary trading system.
The code provides also an exploration to help you to figure out the ARRAY values for each bar in your exploration range.

Apply this code to any single ticker (like SPY, QQQ, etc) for a recent range (like 100/200 days) and see the exploration results (this is only for LONG positions). Then try to do a backtest.

As you can see in the code, there is NO "if" since in both in backtest and in the exploration every single bar in the selected range is evaluated to see (according to your code) if there is a valid signal to buy or sell.

// System and exploration example - not to be traded

_d_period = 9;  
_d = StochD( _d_period );

// Mean reverting....
levelToBuyD  = 20;
levelToSellD = 80;
signalToBuyD  = ( _d < levelToBuyD );
signalToSellD = ( _d > levelToSellD );

// Add your other conditions.....

// These will be different from signalToBuyD/signalToSellD 
// when you'll add some other conditions...
buySignal  = signalToBuyD;  // AND/OR any other condition
sellSignal = signalToSellD; // AND/OR any other condition

// Buy/Sell on the next bar at the open
BuyPrice = Open;
SellPrice = Open;

Buy = Ref( buySignal, -1 );
Sell = Ref( sellSignal, -1 );

//  Remove any excessive signals
Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );

// Basic Indicator
Plot( C, "Close", colorDefault, styleCandle );
PlotShapes( shapeUpArrow * Buy, colorWhite, 0, O, 0 );
PlotShapes( shapedownArrow * Sell, colorYellow, 0, O, 0 );


/////// Exploration

// A small utility function to improve readibility of exploration
// results for boolean arrays - only ones (1) will appear
function nil( array )
{
    return ( IIf( array, 1, Null ) );
}

// Filter = buySignal or SellSignal;
Filter = 1; // See all bars/days signals
AddColumn( C, "Close" );
AddColumn( _d, "StochD(" + _d_period + ")" );
AddColumn( nil( signalToBuyD ), "_d > " + levelToBuyD, 1.0 );
AddColumn( nil( signalToSellD ), "_d < " + levelToSellD, 1.0 );
AddColumn( nil( buySignal ), "Buy Signal", 1.0 );
AddColumn( nil( sellSignal ), "Sell Signal", 1.0 );
AddColumn( nil( Buy ), "Buy", 1.0 ); // at next open
AddColumn( nil( Sell ), "Sell", 1.0 ); // at next open
SetSortColumns(-2);

The "1" (ones) indicate when a particular condition is TRUE. You can combine multiple rules (and add further columns to check their values) to create your final Buy/Sell signals.

But maybe some more experienced users than me will be able to better explain it and provide you more relevant examples.

In any case, re, my previous answer, it was timely brought to my attention that the looping PDF I linked (hosted - apparently uncredited - on the linked site) is the original work of another author.
That document is due to a user that was very active in the AussieStockForums with the nickname of "GreatPig" (see this other thread for details and to get a link to the original document).

1 Like

Thank you "beppe" , really appreciate your effort.

Examine your answer closely helped me with the solution I looked for.

BuySignal = IIf( some_value >= Some_AFL_Indicator( param1, param2 ), Do_Your_Logic, Null );

Again, Thank you.

Oh really, it is uncredited. Then what is this:

40%20AM44%20AM57%20AM

That's right. Multiple credits to the esteemed Great Pig who by the way I wasn't able to track down.

Don't worry guys, I have removed the pdf now from my servers. Good luck finding it elsewhere

@marwood I wrote apparently uncredited since a Google search resulted in direct access to the document where there is no such visible credit.
Your clarification is welcome.
I'm happy to see that I was wrong and I apologize if by any chance I offended you.

Anyway, for your reference, the document is still online in the original form in the Aussie Stock Forums (to access it you must register).

By the way, in the original post, the author says that "The document may be freely distributed", so IMHO you can still keep it on your site and simply - as a kind gesture to the author - provide a line of credit directly in the PDF.

1 Like
  1. When I wrote about it 7 months ago in this post:

... you didn't find it necessary to respond in any way, inspite of the fact, that you must have been notified about my post as I have mentioned your forum's user name.

  1. We can't verify your info because you have removed all the materials and seems pretty happy that the access to this pdf became difficult...

Thanks for that ... :-1:

2 Likes

I shared this document to help some people with looping and as can be seen I did give credit. But if you found the PDF directly via a google search you wouldn't have known that.

Let's not make a big deal out of this. People can still access the PDF if they want to, just not from my server anymore.