Looping Through GetExtraDataForeign && Transitioning to StaticVarAdd

Hi! I'm trying to make this code more efficient so using the Optimize feature is more efficient by either 1) Making the looping part of this code more effective by not having to loop through each symbol of a watchlist or 2) transitioning the resulting BOPArray into a StaticVar. I've read all the articles on StaticVarAdd including Tomasz's "Correct Use of StatifVarAdd," and I'm still not able to get the code to work. I've removed all the code I tried to add in to get it to work since I'm quite certain I had it in the wrong place. I thought the code might be more efficient if I stored BOPArray into a StaticVar, that may not even be the case.

To be clear, the code works, i just feel like it's very inefficient. To simplify the code I've removed the trading model rules and Optimize variables to concentrate feedback on the looping part.

I use TC2000 as a datasource which I'm aware not many people do so I modified the call to get the proprietary indicator, BOP, from TC2000 using BOPHistory = GetExtraDataForeign("BOP", sym);and substituted it with something somewhat similar that anyone can runBOPHistory = Foreign(sym, "L");`
The gist of the code is to aggregate the net changes in, Low, from one day to the next, within all the stocks in a watchlist (like S&P 500), then normalize that within an oscillator that goes from -100 to 100. When extremes are reached, the market is likely to turn the other way. Again, this is not intended to be used on "Low" but it creates a similar chart to using it with "BOP" none the less.

Thanks in advance, Robbie

/*Modified Code for Posting to AmiBroker forum - removed references to TC2000 datasource so people can run with their setup */

This function walks through every stock in the watchlist passed to it (listnum) to calculate the cumulative increases or decreases in BOP
This will tell us whether the collective BOP is increasing or decreasing.  We will normalize the data and plot as an oscillator from -100 to 100

function CalculateBOPShift( listnum, iBarsRequired )
	list = CategoryGetSymbols( categoryWatchlist, listnum ); /* retrive comma-separated list of symbols in watch list */
	Returnvalue = 0; //Initialize return variable
	if(list != "")   //if the watchlist is not empty
		BOPHistoryYesterday = 0; /* just in case there are no watch list members */
		BOPArrayTally = 0; /* just in case there are no watch list members */
		iStockCounter = 0; /*counts stocks included without nulls */
		sIncludedSymbols = "";  /*string of excluded symbols */
		BOPDivisorArray = 0;
		BOPDelta = 0;
		for( i = 0; ( sym = StrExtract( list, i ) ) != ""; i++ )
			/* The next line should be: BOPHistory = GetExtraDataForeign("BOP", sym); but since this is proprietary indicator from TC2000, I replaced it with BOPHistory = Foreign(sym, "L"); which everyone can run */
			BOPHistory = Foreign(sym, "L");  //Not the original code
				iStockCounter++; /*count how many stocks included from the watchlist */
				BOPDivisorArray += NOT(IsNull(BOPHistory));
				BOPHistory = Nz(BOPHistory,0);
				BOPHistoryYesterday = Ref( BOPHistory, -1);
				BOPDelta = iif(BOPHistory==BOPHistoryYesterday, 0, iif(BOPHistory>BOPHistoryYesterday,1,-1));
				BOPArrayTally += BOPDelta;
				sIncludedSymbols = sIncludedSymbols += sym+",";  /* keep track of symbols that were excluded due to missing/incomplete data to print in the commentary */
		printf("loaded: " + iStockCounter + "out of:" + i + "/nIncluded symbols: " + sIncludedSymbols);
		Returnvalue = ((BOPArrayTally / BOPDivisorArray) * 100); /* divide by number of components */
	return Returnvalue;	
/*You will need a watchlist defined to make this work.*/
/*Index Parameters: listnum values for different indexes - S&P=0, Nasdaq 100=2, Russel 2000=3  <-- unique to my setup */
IndexParam = Param("Index Selection",0,0,3,1);  /* Determine which index you want to be analyzed based on Watchlist position */
ChartTextParam = ParamStr( "ChartNameText", CategoryGetName( categoryWatchlist, IndexParam ));

BOPArray = CalculateBOPShift(IndexParam,2000);
Buy = 0;

Plot(BOPArray, "Index Charted: " + ChartTextParam + "Avg of WL 0:"+LastValue(BOPArray,True), colorGreen, styleLine|styleDashed|styleThick|styleDots,-100,100);

//Horizontal Lines at +80,0,-80)
PlotGrid( 0, colorGrey40, 10, 2, False ); // solid line 2 pixels thick, no label 
PlotGrid( 80, colorGreen, 10, 2, False ); // solid line 2 pixels thick, no label 
PlotGrid( -80, colorRed, 10, 2, False ); // solid line 2 pixels thick, no label 

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