S&P condition before stock Explore

I would like to learn how to create a code which says, in effect:

  1. When ( This Indicator ) applied to the S&P = X

  2. Then explore for stocks using ( these other indicators )

My question: How can I refer to a single stock (S&P in this case) using it's own indicator before proceeding to the main strategy?

Thanks for any clues

@JohnHoward you can use Foreign or SetForeign functions

https://www.amibroker.com/guide/afl/foreign.html
https://www.amibroker.com/guide/afl/setforeign.html

and an example

portfoliobilder, thank you - that gets me partly there with

Foreign( "SPY", "Close" ) ; // Today's S&P close

However, I don't see how to add an indicator to that "Foreign" code line to say, for instance,

Only scan for stocks if the PDI (3) is up from yesterday for the S&P.

I can code for the [ PDI (3) is up from yesterday ], but can't see how to include it in the Foreign ("SPY", "...")

Thanks again

@JohnHoward the example in the Knowledge Base is what you should refer to. If you want to calculate an indicator using various inputs from the symbol (SPY in your example) then use SetForeign. The Plus Directional indicator needs the various values of Open/Hi/Low/Close from the SPY (it helps if you are familiar with how to construct the indicators you choose such as the underlying calculations and the required inputs for the PDI).

SetForeign( "SPY" );
//
// now we can calculate any indicator based on SPY symbol data
//
SPY_PDI_rising = PDI( 3 ) > Ref(PDI(3), -1); 
//
// now go back to original data (current symbol)
//
RestorePriceArrays();

Now you can use the condition SPY_PDI_rising while running your exploration on other symbols.

You mention have other conditions for the rest of the Exploration symbols, so code your other condition and then you may do something like this,

Filter = SPY_PDI_rising AND Condition2;

3 Likes

Portfoliobuilder, thank you again. That answers it for me. Thanks much for your time.

There's just Pdi etc. that needs to be inside Setforeign and not whole condition required in there.
Price/Volume data.

SetForeign( "SPY" );
// ONLY Price/Volume data required
SPY_PDI = PDI( 3 ); 
SPY_ADX = ADX( 15 ); 
//etc.
RestorePriceArrays();

SPY_PDI_rising = ROC(SPY_PDI, 1) > 0; 
SPY_ADX_above = SPY_ADX  > 30; 
//etc

Buy = SPY_PDI_rising AND SPY_ADX_above;
1 Like