Median Price on multiple arrays

hi AB community,

Attempting to develop my (first) function and encountering some challenges and hoping some of you may be able to direct me.

The function is to return the median price for multiple arrays for a given interval (day, week or month). for example the interval is weekly, the function to collect all daily Open, High, Low & Close for the selected week and return the median price.
in case the function is used on chart, the interval is based on chart interval.

i receive following errors:
Line 51: MxFromString invalid format
Line 52: Incorrect column specified

also, is below logic ok (or could this be coded better)

thanks in advance for your time & efforts - regards,

Jurgen


function udf_Median( barIntervale )
{
	TimeFrameSet(inDaily);
	strPriceList = "";
	medArr = Null;
		for (i = 0; i <= BarCount; i++)
		{
			//get data into string (backwards) - stop at start of week / month
			for (j = i; j >= 0; j--)
			{
			strPriceList = strPriceList + Open[j];
			strPriceList = strPriceList + ",";
			strPriceList = strPriceList + High[j];
			strPriceList = strPriceList + ",";
			strPriceList = strPriceList + Low[j];
			strPriceList = strPriceList + ",";
			strPriceList = strPriceList + Close[j];
			strPriceList = strPriceList + ",";
			
			if ((j == 0)
				OR (barIntervale == inDaily)
				OR ((barIntervale == inWeekly) AND (DayOfWeek(j) < DayOfWeek(j-1)))
				OR ((barIntervale == inMonthly) AND (Day(j) < Day(j-1))))
				break;
			}
			
			//get data into string (forward) - stop at end of week / month
			if (i < BarCount)
			{
				for (p = i + 1; p < BarCount; p++)
				{
				
				if ((barIntervale == inDaily)
					OR ((barIntervale == inWeekly) AND (DayOfWeek(p) > DayOfWeek(p+1)))
					OR ((barIntervale == inMonthly) AND (Day(p) > Day(p+1))))
					break;
				
				strPriceList = strPriceList + Open[p];
				strPriceList = strPriceList + ",";
				strPriceList = strPriceList + High[p];
				strPriceList = strPriceList + ",";
				strPriceList = strPriceList + Low[p];
				strPriceList = strPriceList + ",";
				strPriceList = strPriceList + Close[p];
				}
			}
			
			
			//convert & sort string into matrix
			mxPriceList = MxFromString("{" + strPriceList + "}");
			mxPriceList = MxSortRows(mxPriceList, -1, TRUE);
			
			//get medPrice
			k = MxGetSize(mxPriceList, 1);
			
			if ((k/2) == int(k/2))
				{
				medArr = (mxPriceList[ceil(k/2)]);
				}
			else
				{
				medArr = (mxPriceList[int(k/2)] + mxPriceList[int(k/2) + 1]) / 2;
				}
		}
	TimeFrameRestore();
	return medArr;
}


_SECTION_BEGIN("medPrice");

Plot (udf_Median(Interval()), "medPrice", colorRed);

_SECTION_END();

Hi all,

my bad; below modified code and it passes syntax verification

thanks & regards,

Jurgen

function udf_Median( barIntervale )
{
	TimeFrameSet(inDaily);
	strPriceList = "";
	medArr = Null;
		for (i = 0; i < BarCount; i++)
		{
			//get data into string (backwards) - stop at start of week / month
			for (j = i; j > 1; j--)
			{
			
			if (NOT(strPriceList == ""))
				strPriceList = strPriceList + ",";
			
			strPriceList = strPriceList + Open[j];
			strPriceList = strPriceList + ",";
			strPriceList = strPriceList + High[j];
			strPriceList = strPriceList + ",";
			strPriceList = strPriceList + Low[j];
			strPriceList = strPriceList + ",";
			strPriceList = strPriceList + Close[j];
			
			if ((j == 0)
				OR (barIntervale == inDaily)
				OR ((barIntervale == inWeekly) AND (DayOfWeek(j) < DayOfWeek(j-1)))
				OR ((barIntervale == inMonthly) AND (Day(j) < Day(j-1))))
				break;
			}
			
			//get data into string (forward) - stop at end of week / month
			if (i < BarCount)
			{
				for (p = i + 1; p < BarCount; p++)
				{
				
				if ((barIntervale == inDaily)
					OR ((barIntervale == inWeekly) AND (DayOfWeek(p) > DayOfWeek(p+1)))
					OR ((barIntervale == inMonthly) AND (Day(p) > Day(p+1))))
					break;
				
				strPriceList = strPriceList + ",";
				strPriceList = strPriceList + Open[p];
				strPriceList = strPriceList + ",";
				strPriceList = strPriceList + High[p];
				strPriceList = strPriceList + ",";
				strPriceList = strPriceList + Low[p];
				strPriceList = strPriceList + ",";
				strPriceList = strPriceList + Close[p];
				}
			}
			
			
			//convert & sort string into matrix
			//mx = MxFromString("{ { 1, 2, 3 , 4, 5, 6 } }" );
			mxPriceList = MxFromString("{ {" + strPriceList + "} }");
			mxPriceList = MxSort(mxPriceList, -1, TRUE);
			
			//get medPrice
			k = MxGetSize(mxPriceList, 1);
			
			if ((k/2) == int(k/2))
				{
				medArr = (mxPriceList[ceil(k/2)]);
				}
			else
				{
				medArr = (mxPriceList[int(k/2)] + mxPriceList[int(k/2) + 1]) / 2;
				}
		}
	TimeFrameRestore();
	return medArr;
}


_SECTION_BEGIN("medPrice");

Plot (udf_Median(Interval()), "medPrice", colorRed);

_SECTION_END();

That is not proper code.
Do not use strings. Also you are using array functions improperly. And you are using them within Barcount loop.

To collect OHLC arrays per week in weekly matrix columns you just need SparseCompress and iterating from 1 to cum(week). Then you can store those arrays to Matrix including NullCount within Ref function. Also see MxSetBlock/MxGetBlock.

OHLC of each week's day are stored to weekly columns of a matrix. So at maximum each column has 4*5 rows (OHLC * 5 days) -> max. 20 rows. Then on each column you calculate the median. Then you can plot each weekly median in chart pane (and/or output in exploration table).

Here is sample result (having used faster code):

2017-10-24_123510

2017-10-24_123350

hi fxshrat - thanks for the feedback and back to the drawing board.

cheers - jurgen