Can I get the date range used by Explore in my AFL?

I've got this code:

// the watchlist must contain all the symbols desired to derive the sector average
wlnum = GetOption("FilterIncludeIndustry");

// get the list of symbols in this watchlist
symbolList = CategoryGetSymbols(categoryIndustry, wlnum);

if (Status("stocknum") == 0)
{
    // cleanup variables created in previous runs (if any)
    StaticVarRemove("sv*");  // name all static variables with the "sv" prefix

    // loop over all symbols in the watchlist
    for (i = 1; (symbol = StrExtract(symbolList, i))  != ""; i++)
    {
        // set the price arrays to this symbol
        SetForeign(symbol);
        
        // derive the ROC's
		ROC3 =ROC(C,3);
		ROC10=ROC(C,10);
		ROC30=ROC(C,30);
		ROC60=ROC(C,60);
		
		// derive the average ROC's
		sumROC3 =0;
		sumROC10=0;
		sumROC30=0;
		sumROC60=0;
		
		cntROC3 =0;
		cntROC10=0;
		cntROC30=0;
		cntROC60=0;
		
		for (j=0; j <= BarCount - 1; j++) {
			if (NOT IsNull(ROC3[j])) {
				sumROC3+=ROC3[j];
				cntROC3+=1;
			}
			if (NOT IsNull(ROC10[j])) {
				sumROC10+=ROC10[j];
				cntROC10+=1;
			}
			if (NOT IsNull(ROC30[j])) {
				sumROC30+=ROC30[j];
				cntROC30+=1;
			}
			if (NOT IsNull(ROC60[j])) {
				sumROC60+=ROC60[j];
				cntROC60+=1;
			}
		}
		
		if (cntROC3  > 0 ) { avgROC3  =sumROC3  /cntROC3;  } else { avgROC3=Null; };
		if (cntROC10 > 0 ) { avgROC10 =sumROC10 /cntROC10; } else { avgROC10=Null; };
		if (cntROC30 > 0 ) { avgROC30 =sumROC30 /cntROC30; } else { avgROC30=Null; };
		if (cntROC60 > 0 ) { avgROC60 =sumROC60 /cntROC60; } else { avgROC60=Null; };
		
		StaticVarSet("svAvgROC3" +symbol,avgROC3);
		StaticVarSet("svAvgROC10"+symbol,avgROC10);
		StaticVarSet("svAvgROC30"+symbol,avgROC30);
		StaticVarSet("svAvgROC60"+symbol,avgROC60);
		
		RestorePriceArrays();
	}
}

// derive symbol ranks
StaticVarGenerateRanks("RankROC3", "svAvgROC3" , 0, 1224);
StaticVarGenerateRanks("RankROC10","svAvgROC10", 0, 1224);
StaticVarGenerateRanks("RankROC30","svAvgROC30", 0, 1224);
StaticVarGenerateRanks("RankROC60","svAvgROC60", 0, 1224);

Cond = C > 2;
Filter = Cond;

symbol = Name();

ROC3 =ROC(C,3);
ROC10=ROC(C,10);
ROC30=ROC(C,30);
ROC60=ROC(C,60);

AvgROC3   = StaticVarGet("svAvgROC3" + symbol);
AvgROC10  = StaticVarGet("svAvgROC10" + symbol);
AvgROC30  = StaticVarGet("svAvgROC30" + symbol);
AvgROC60  = StaticVarGet("svAvgROC60" + symbol);

RankROC3  = StaticVarGet("RankROC3svAvgROC3" + symbol);
RankROC10 = StaticVarGet("RankROC10svAvgROC10" + symbol);
RankROC30 = StaticVarGet("RankROC30svAvgROC30" + symbol);
RankROC60 = StaticVarGet("RankROC60svAvgROC60" + symbol);

AddColumn(O,"Open");
AddColumn(C,"Close");
AddMultiTextColumn(C > O,"DOWN\nUP\n","Direction",1,colorWhite,IIF(C > O,colorGreen,colorRed));

AddColumn(ROC3, "ROC3");
AddColumn(ROC10,"ROC10");
AddColumn(ROC30,"ROC30");
AddColumn(ROC60,"ROC60");

AddColumn(avgROC3, "Avg ROC3");
AddColumn(avgROC10,"Avg ROC10");
AddColumn(avgROC30,"Avg ROC30");
AddColumn(avgROC60,"Avg ROC60");

AddColumn(RankROC3, "RankROC3");
AddColumn(RankROC10,"RankROC10");
AddColumn(RankROC30,"RankROC30");
AddColumn(RankROC60,"RankROC60");

SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - %s {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", 
FullName(),O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 

I want to derive the average ROC (x 4) for the ticker. This average will repeat across the dates (rows) for a given ticker.

If I paste my current results into Excel and check the average, I see I'm not getting the desired average. That's because I'm deriving the average across all the bars, instead of the range of data returned by Explore.

So, is there a way for the AFL to know what date range was specified for Explore? Even better would be that range expressed in BarIndex, i.e. the begin and end range for my loop. But I should be able to derive that if I knew what the beginning and end dates were.

Second, I'd like to derive ranking based on this repeated average. It should be derived in reverse sorting order, so rank #1 would be the largest average for the ticker, etc. I've read the doc and tried different approaches but can't get that to work.

See Status function

"firstbarinrange", "lastbarinrange", "barinrange", "rangefromdate", "rangetodate"

1 Like

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