Percentage Stock price nyse above the 200 day moving average

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();

1 Like

Code is incorrect.

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.

These four lines are supposed to be run ONCE

There is example in the MANUAL how you should use StaticVarAdd
http://www.amibroker.com/guide/afl/staticvaradd.html

if( status("stocknum") == 0 ) 
{ 
    
// remove any earier composite values 
   StaticVarRemove("~Composite"); 
} 

StaticVarAdd( "~Composite", MACD() > Signal() );  
Buy = 0;

As you can see there is NO SETFOREIGN call.

2 Likes

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
...
Captura

_SECTION_BEGIN("PercentSharesAboveSMA200");
// 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*" );

        above = C >= MA( C, 200 );
        StaticVarAdd( "~above", above );
svSymbolCount = StrCount(List, ",")+1;
}
ABOVEcomp = StaticVarGet( "~above" );

PercentAbove = ( ABOVEcomp / svSymbolCount ) * 100;
svSymbolCount = StrCount(List, ",")+1;
// Chart
Plot( PercentAbove, "Percentage > 200 day ma", colorBlueGrey, styleThick );
_SECTION_END();

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);

13

4 Likes

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.

You just need few modifications to achieve that. So you do not need multiple AFL instances but just single AFL and loop to iterate periods.

/// 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/8
/// by AmiBroker.com and fxshrat@gmail.com
list_name = CategoryGetName(categoryGroup, 5);
symbol_name = "~AboveMA_"+list_name;
cnt = "~cnt_"+list_name;
periods = MxFromString("[10;50;200]");// MA periods
rows = MxGetSize(periods, 0);
persist = False;
if ( Status( "action" ) == actionScan ) {
    /// derived from AB manual
    /// @link https://www.amibroker.com/guide/afl/staticvaradd.html
    if ( Status( "stocknum" ) == 0 ) {
        // remove any earier composite values
        StaticVarRemove(symbol_name+"*");
        StaticVarRemove(cnt);
    }   
    // Iterating periods
    for ( i = 0; i < rows; i++ ) 
		StaticVarAdd(symbol_name+"_"+ i, C >= MA(C, periods[i][0]), True, persist);
    StaticVarAdd(cnt, 1, True, persist);
    Buy = 0;
}

colors = "colorBlueGrey,colorViolet,colorBrown";
// Iterating periods
for ( i = 0; i < rows; i++ ) {
	pcnt_aboveMA = Nz(StaticVarGet(symbol_name+"_"+i) / StaticVarGet(cnt))*100;
	titles = StrFormat("Percentage > %g-day MA", periods[i][0]);
	Plot(pcnt_aboveMA, titles, VarGet(StrExtract(colors,i)), styleThick);
}

13

5 Likes

That is really neat and efficient - thank you so much!

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:

AddToComposite(pcnt_aboveMA,"~Above200MA","X",13);

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:
AmibrokerChart
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.

Hello, nice indicator, but for me it only draws a flat line. Why?

@gawdar, Your question does not give us any information that we can use to help you.

You have not shown what your code is, or what steps you have taken to debug it.

We are left with only being able to guess, or to simply ignore.

I am trying to use this code below that I found in this forum but it only draws a straight line in my chart.

/// 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/8
/// by AmiBroker.com and fxshrat@gmail.com
list_name = CategoryGetName(categoryGroup, 5);
symbol_name = "~AboveMA_"+list_name;
cnt = "~cnt_"+list_name;
periods = MxFromString("[10;50;200]");// MA periods
rows = MxGetSize(periods, 0);
persist = False;
if ( Status( "action" ) == actionScan ) {
    /// derived from AB manual
    /// @link https://www.amibroker.com/guide/afl/staticvaradd.html
    if ( Status( "stocknum" ) == 0 ) {
        // remove any earier composite values
        StaticVarRemove(symbol_name+"*");
        StaticVarRemove(cnt);
    }   
    // Iterating periods
    for ( i = 0; i < rows; i++ ) 
		StaticVarAdd(symbol_name+"_"+ i, C >= MA(C, periods[i][0]), True, persist);
    StaticVarAdd(cnt, 1, True, persist);
    Buy = 0;
}

colors = "colorBlueGrey,colorViolet,colorBrown";
// Iterating periods
for ( i = 0; i < rows; i++ ) {
	pcnt_aboveMA = Nz(StaticVarGet(symbol_name+"_"+i) / StaticVarGet(cnt))*100;
	titles = StrFormat("Percentage > %g-day MA", periods[i][0]);
	Plot(pcnt_aboveMA, titles, VarGet(StrExtract(colors,i)), styleThick);
}

2021-03-13 (2)

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 

Hello, Fxshrat

If I want to filter the % of 10 sma cross 50 sma or 50 sma cross 200 sma, how can I modify this code?

Excuse me but I don't understand how to turn on / enable "pad and align"
Too..it´s possible to make the indicator without having to scanner?
Greetings

1 Like

Gracias por su ayuda. No era capaz de encontrarlo.

Saludos

Just coming here copy&pasting and demanding is not welcomed.
Besides AmiBroker help has search field already.
So simply insert "Pad&align" there.

1 Like

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.