Hi, I try to make the indicator show me the percentage of shares above the average of 200. This code makes me go through the list and I see the tape go down continuously, even, half slows down Ami. Sure there is a way to do it but I don't give. I download a list daily on my google drive and wish I could take it to my amibroker.
Regards
_SECTION_BEGIN("PercentSharesAboveSMA200");
// Using StaticVarAdd and run EXPLORE
// pick watchlist in AA window
//wlnum = GetOption( "FilterIncludeWatchlist" );
List = CategoryGetSymbols( categoryGroup, 5);
if( Status( "stocknum" ) == 0 )
{
// cleanup variables created in previous runs (if any)
StaticVarRemove( "~SymbolCount*" );
StaticVarRemove( "~above*" );
for( n = 0; ( Symbol = StrExtract( List, n ) ) != ""; n++ )
{
SetForeign( symbol );
above = C >= MA( C, 200 );
StaticVarAdd( "~SymbolCount", 1 );
StaticVarAdd( "~above", above );
RestorePriceArrays();
}
}
ABOVEcomp = StaticVarGet( "~above" );
svSymbolCount = StaticVarGet( "~SymbolCount" );
PercentAbove = ( ABOVEcomp / svSymbolCount ) * 100;
///////////////
// Explore
///////////////
// various Filter settings are possible depending on what you want your
// Exploration output to look like
Filter = Status( "stocknum" ) == 0 ;
SetOption( "NoDefaultColumns", True );
AddColumn( DateTime(), "Date", formatDateTime );
AddColumn( svSymbolCount, "Total Number of Stocks", 1.0 );
AddColumn( ABOVEcomp, "Number>200 day ma", 1.0 );
AddColumn( PercentAbove, "Percentage > 200 day ma", 1.2 );
///////////////
// Chart
///////////////
Plot( PercentAbove, "Percentage > 200 day ma", colorBlueGrey, styleThick );
_SECTION_END();
This one alone already is crazy idea and pointless waste.
for( n = 0; ( Symbol = StrExtract( List, n ) ) != ""; n++ )
{
SetForeign( symbol );
StaticVarAdd( "~SymbolCount", 1 );
RestorePriceArrays();
}
svSymbolCount = StaticVarGet( "~SymbolCount" );
It is like someone moving fingernails on chalkboard.
Symbol count can be done in one line
svSymbolCount = StrCount(List, ",")+1;
And would return same result as svSymbolCount of that loop.
You most probably copy&pasted incorrect code from somewhere.
You should not use StaticVarAdd within loop.
Run it over list of symbols (Filter) in Analysis (without loop).
There is a growing number of codes that abuse SetForeign in a loop. This is all wrong. Composites should NOT be re-calculated over and over again. They should be calculated JUST ONCE using process described in the manual: http://www.amibroker.com/guide/a_addtocomposite.html
StaticVarAdd may be added instead of AddToComposite BUT the procedure should stay the same, i.e. run scan ONCE to create static variable and don't use SetForeign AT ALL.
First of all, sorry if I haven't added the code and photo correctly but I think so.
The second, for simplicity, I just want an indicator that shows me, on the NYSE, the percentage of shares that are above the sma 200 . I have retouched the code avoiding the explorer's errors and changing what was proposed, but I didn't do it well.
a greeting
...
You do not carefully read documentation. Please study it carefully. StaticVarAdd is not supposed to be run on first scanned symbol only ( Status( "stocknum" ) == 0).
/// Set symbol list in Analysis Filter - Include
/// And enable pad&align
/// Set Periodicity in analysis General settings
/// Create composite ARRAY via SCAN
/// @link https://forum.amibroker.com/t/percentage-stock-price-nyse-above-the-200-day-moving-average/19377/5
list_name = CategoryGetName(categoryGroup, 5);
symbol_name = "~AboveMA_"+list_name;
cnt = "~cnt_"+list_name;
if ( Status( "action" ) == actionScan ) {
/// @link https://www.amibroker.com/guide/afl/staticvaradd.html
if( Status( "stocknum" ) == 0 ) {
// remove any earier composite values
StaticVarRemove(symbol_name);
StaticVarRemove(cnt);
}
StaticVarAdd(symbol_name, C >= MA(C, 200), True, persist = False);
StaticVarAdd(cnt, 1, True, persist);
Buy = 0;
}
pcnt_aboveMA = Nz(StaticVarGet(symbol_name) / StaticVarGet(cnt))*100;
Plot(pcnt_aboveMA, "Percentage > 200 day ma", colorBlueGrey, styleThick);
Thanks for the help. I'm going to change the data necessary for it to work. I really have read the manual. I'm going to read again. But there are many things that you need to explain with examples and to be able to ask questions. For those who do not understand programming it is very difficult.
a greeting
I was very interested to see the application of StaticVar in this afl as opposed to creating a composite symbol. In this afl, how would one have more than one instance of the afl on a particular chart as Technical Analysts like to look at the % stocks above 200, 50 and 10 day moving averages in one glance because the shape and position of each plot adds to the story? Creating more than one instance of this afl as it stands returns the latest used moving average scan in all the instances of the plots of this afl.
This code is very instructive in demonstrating the capabilities of StaticVarAdd and how to count the number of symbols in a watchlist. It has proven to be quite useful to me (thanks fxshrat) but I would also like it to produce an actual symbol that can be placed in a watchlist in addition to being plotted on a chart. I have tried to accomplish this by adding an AddToComposite line such as:
Running a Scan with this addition does produce a matching indicator that could be placed in a watchlist, however, when this symbol is displayed in a Price chart the scaling information on the right side is not scaled from 0% to 100% as I would like it to be. See below:
The top pane charts the indicator generated from AddToComposite and the lower one from the AFL Plot statement.
I have made multiple attempts to change the scaling for the upper pane using Remap and also with manual control through SetChartOptions, StyleOwnScale, etc., but none gave the desired result.
Any help on the best way to create a symbol that can be put in a watchlist and have its chart pane reflect the correct 0% to 100% scaling would be much appreciated.
Please read the instructions being present at the very top of the code (first four commented lines):
/// Set symbol list in Analysis Filter - Include
/// And enable pad&align
/// Set Periodicity in analysis General settings
/// Create composite ARRAY via SCAN