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