Gregg, GetLastBarInRange() ????
Regards, Ton.
----- Original Message -----Sent: Thursday, February 25, 2016 8:08 PMSubject: [amibroker] Re: Explore & Debug vs. Code Check: What's Different?
I have a new issue but very related to the previous post. I have code which is working in Debug & Code Check but hanging in Explore.I've sent this to AB support as well but want to see what others might experience. I have a formula (code included below) which works when I step through it with Debug and seems to work when I do code check, but when I use Explore it hangs. I have put a _Trace() statement at the beginning of the function, and not even that prints out, as if Explore is not calling the function at all. The _Trace statements do print out in Debug, in Code Check and when I send to Analysis from the Editor, but then in Analysis when I press Explore....nothing. In ! Debug mode, every variable result is the correct value through the entire run.I have no clue about what could be causing this. I can't find it using debug, since everything works there. I can't find it using Code Check, since everything works when I do Code Check. I can't find it using _Trace when I hit Explore, since the trace statements don't print out even on the very first line of the function. And I can't find it using AddColumn in Explore, since Explore is hanging.Is this something wrong with my copy of AB? I'm not calling any plug-in code.function GetDebug(fx) {return false;}procedure CleanUpStats() { // begin CleanUpStatsif (Status("action") == actionBacktest) {StaticVarRemove("pdf*");StaticVarRemove("MIresult*");StaticVarRemove("MIexists*");}!} // end CleanUpStatsfunction GetPdfBin(pdfName, bin) { //begin GetPdfBinif (typeof(bin) == "string") //bin is a stringsuffix = bin;else // bin is a numbersuffix = NumToStr(bin, 1.0);return StaticVarGet("pdfMatrix"+ pdfName+suffix);} // end GetPdfBinfunction GetPdfBinSize(pdfName) { //begin GetpdfBinSizereturn StaticVarGet("pdf"+pdfName+" binSize");} // end GetPdfBinSizefunction GetPdfNumBins(pdfName) { //begin GetPdfBinSizer! eturn StaticVarGet("pdf"+pdfName+" numBins");} // end GetPdfNumBinsfunction GetPdfLookBack(pdfName) { //begin GetPdfLookBackreturn StaticVarGet("pdf"+pdfName+" lb");} // end GetPdfLookBackfunction GetPdfModelingPeriod(pdfName) { //begin GetPdfModelingPeriodreturn StaticVarGet("pdf"+pdfName+" modelingPeriod");} // end GetPdfModelingPeriodprocedure SetPdfBin(pdfName, bin, value, persist) { //begin SetPdfBinif (typeof(bin) == "string") //bin is a stringsuffix = bin;else // bin is a numbersuffix = NumToStr(bin, 1.0);StaticVarSet("pdfMatrix"+ pdfName+"-"+suffix, value, persist);} // end SetPdfBinprocedure SetPdfBinSize(pdfName, value, persist) { //begin SetPdfBinSizeStaticVarSet("pdf"+pdfName+" binSize", value, persist);} // end SetPdfBinSizeprocedure SetPdfNumBins(pdfName, value, persist) { //begin SetPdfBinSizeStaticVarSet("pdf"+pdfName+" numBins", value, persist);} // end SetPdfNumBinsprocedure SetPdfLookBack(pdfName, value, persist) { //begin SetPdfLookBackStaticVarSet("pdf"+pdfName+" lb", value, ! persist);} // end SetPdfLookBackprocedure SetPdfModelingPeriod(pdfName, value, persist) { //begin SetPdfModelingPeriodStaticVarSet("pdf"+pdfName+" modelingPeriod", value, persist);} // end SetPdfModelingPeriodprocedure SetJointPdfBinSize1(pdfName, value, persist) { //begin SetPdfBinSize1StaticVarSet("pdf"+pdfName+" binSize1", value, persist);} // end SetPdfBinSize1procedure SetJointPdfBinSize2(pdfName, value, persist) { //begin SetPdfBinSize2StaticVarSet("pdf"+pdfName+" binSize2", value, persist);} // end SetPdfBinSize2procedure SetJointPdfOrder(jointPdf, pdf1, pdf2) { //begin SetJointPdfOrderStaticV! arSetText("pdfplace1"+ jointPdf, pdf1);StaticVarSetText("pdfplace2"+ jointPdf, pdf2);} // end SetJointPdfOrderfunction GetJointPdfOrder(jointPdf, number) { //begin GetJointPdfOrderif (number == 1)result = StaticVarGetText("pdfplace1"+ jointPdf);else if (number == 2)result = StaticVarGetText("pdfplace2"+ jointPdf);else {result = "GetJointPdfOrder argument out of range - should be 1 or 2";_TRACE(result);}return result;} // end GetJointPdfOrderfunction GetJointPdfBinSize1(pdfName, value) { //begin GetPdfBinSize1return StaticVarGet("pdf"+pdfName+" binSize1");} // end GetPdfBinSize1function GetJointPdfBinSize2(pdfName, value) { //begin GetPdfBinSize2return StaticVarGet("pdf"+pdfName+" binSize2", value);} // end GetPdfBinSize2function CreateJointIndex(i, j) { // begin CreateJointIndexreturn NumToStr(i, 1.0) + "." + NumToStr(j, 1.0);} // end CreateJointIndexfunction CreatePdfName(pdfName, numBins, binSize, modelingPeriod, index) {if (typeof(numBins) == "array")numBins = LastValue(Cum(numBins))/ BarCount;if (typeof(binSize) == "array")binSize = LastValue(Cum(binSize))/ BarCount;// return pdfName+"-"+NumToStr(numBins, 1.0)+"-"+NumToStr(binSize, 1.2)+"-"+NumToStr( modelingPeriod, 1.0)+"-"+NumToStr(index, 1.0);return pdfName+"-"+numBins+"-"+ binSize+"-"+modelingPeriod+"-" +index;}function CreateJointPdfName(pdfName, numBins1, numBins2, binSize1, binS! ize2, modelingPeriod, index) {//return pdfName+"-"+NumToStr(numBins1, 1.0)+"-"+NumToStr(numBins2, 1.0)+"-"+NumToStr(binSize1, 1.2)+"-"+NumToStr(binSize2, 1.2)+"-"+NumToStr( modelingPeriod, 1.0)+"-"+NumToStr(index, 1.0);return pdfName+"-"+numBins1+"-"+ numBins2+"-"+binSize1+"-"+ binSize2+"-"+modelingPeriod+"- "+index;}function PdfExists(pdfName) { //begin pdfExistsif (StaticVarGet("pdfexists"+ pdfName) == True)result = True;elseresult = False;return result;} // end pdfExistsfunction GetComponentPDF(jointPDF, number) { // Begin GetComponentPDF////Retrieves the name of an individual pdf that went into making a joint pdf//if (NOT PdfExists(jointPDF)) {_TRACE("GetComponentPDF: The joint pdf "+jointPDF+" does not exist or has a damaged record");Error("GetComponentPDF: The joint pdf "+jointPDF+" does not exist or has a damaged record");}if (number == 1)result = jointPDF+"data1";else if (number == 2)result = jointPDF+"data2";else {_TRACE("GetComponentPDF: Component number (second argument) is out of range. It must be 1 or 2");Error("GetComponentPDF: Component number (second argument) is out of range. It must be 1 or 2");}return result;} // End GetComponentPDFprocedure DisplayPDF(pdfName) { // begin DisplayPDF//// Display pdfs in explore mode//! numBins = GetPdfNumBins(pdfName);total = 0;for (bin = 1; bin <= numBins; bin++) {result = GetPdfBin(pdfName, bin);if (NOT typeof(result)=="array") _TRACE("DisplayPDF: Result is Null");AddColumn(result, "Bin " + bin, 1.5);total += result;}AddColumn(total, "Bin total", 1.2);} // end DisplayPDFprocedure DisplayJointPDF(jointPdf) { // begin DisplayJointPDF//// Display a joint pdf in explore mode//if (NOT PdfExists(jointPdf)) { // begin pdfexists_TRACE("DisplayJointPDF: Input pdf "+jointPDF+" does not exist or has a corrupted record");Error("DisplayJointPDF: Input pdf "+jointPDF+" does not exist or has a corrupted record");} // end pdfexistsnumBins = sqrt(GetPdfNumBins(jointPdf));_TRACE("DisplayJointPDF: jointPdf "+jointPdf);_TRACE("DisplayJointPDF: d2Name "+numBins);highBin = LastValue(HHV(numBins, BarCount));total = 0;for (i = 1; i <= 2; i++)for (j = 1; j <= 2; j++) { // begin for each binindex = CreateJointIndex(i, j);result = GetPdfBin(jointPdf, index);AddColumn(result, "Bin " + index, 1.5);total += result;} // end for each binAddColumn(total, "Bin total"! , 1.2);} // end DisplayJointPDFfunction OptimumBins_afl(sourceArray) { // begin OptimumBins// Calculate the optimal number of bins to use in constructing a pdf// Uses the Freeman-Diaconis formula:// bins = ceil((max - min)/((2*inner_quartile_range) *(samplesize^-0.333))//// Arguments:// SourceArrayend = GetLastBarInRange();begin = GetFirstBarInRange();barRange = end - begin + 1;maxValue = HHV(sourceArray, barRange);minValue = LLV(sourceArray, barRange);range = maxValue[end] - minValue[end];if (GetDebug("OptimumBins"))_TRACE("OptimumBins: maxvalue is "+maxValue+" and minValue is "+minValue+" and range is "+range);//innerquartile rangesorted = Sort(sourceArray, begin, end);quartileLength = Round(barRange/4);idx1 = begin + quartileLength;idx2 = end - quartileLength;firstQtr = sorted[idx1];thirdQtr = sorted[idx2];iqr = thirdQtr - firstQtr;denom = (2*iqr)*(range^-(1/3));denom = IIf(AlmostEqual(denom, 0), 0.001, denom);bins = ceil(range/denom);if (GetDebug("OptimumBins")) {_TRACE("OptimumBins: idx1 is "+idx1+" and idx2 is "+idx2+" and firstQtr is "+firstQtr+" and thirdQtr is "+thirdQtr);_TRACE("OptimumBins: iqr is "+iqr+" and denom is "+denom+" and optimum bins is "+bins);}return bins;} // end OptimumBinsfunction OptimumBinsR! olling_afl( sourceArray, periods) { // begin OptimumBins// Calculate the optimal number of bins to use in constructing a pdf of the trailing bars// given by periods.// Uses the Freeman-Diaconis formula:// bins = round((max - min)/((2*inner_quartile_range) *(barcount^-0.333))//maxValue = HHV(sourceArray, periods);minValue = LLV(sourceArray, periods);firstQtr = Percentile(sourceArray, periods, 25);thirdQtr = Percentile(sourceArray, periods, 75);iqr = thirdQtr-firstQtr;num = maxValue - minValue;left = (2*iqr);right = periods^(-1/3); denom = (2*iqr)*(periods^(-1/3));bins = Nz(ceil(num/denom), 2);if (GetDebug("OptimumBinsRolling" )) { //Begin debugAddColumn(maxValue, "OptimumBinsRolling: maxValue");AddColumn(minValue, "OptimumBinsRolling: minValue");AddColumn(bins, "OptimumBinsRolling: bins");} // end debugreturn bins;} // end OptimumBinsRolling/*test = OptimumBinsRolling_afl(C, 126);AddColumn(test, "Original OBR"); test = OptimumBinsRolling(C, 126);AddColumn(test, "Compiled OBR");*/function CreatePDF(pdfRootName, data, modelingPeriod, index, persist) { //Begin Createpdf// This function creates a probability density function out of the rate of change statistics// associated with the data argument.// Arguments:// pdfName - A string which provides the name for the probability density function.// data - The array of price movements providing input for the pdf// numBins - The number of "in range" bins for sorting the price movements in the data. The total// number of bins actually used will be 2 more than this to account for outlier price// movements in the left and right tails.// binSize - The size of each bin, on the scale where 1% change is represented as 1! .0.// modelingPeriod - The number of bars to look back on a rolling basis in constructing the pdf. If this// number is less than or equal to 1, the function constructs the pdf on a cumulative basis since bar// 0, rather than a rolling basis// index - A number used to make the identifier name for this pdf unique.// persist - A boolean flag which indicates whether or not the static variables should be persistent////Set the optimal parameters for each modeling period//Calculate optimal number of bins by averaging the optimum across each bar, if rollingif (modelingPeriod > 1) {numBins = OptimumBinsRolling_afl(data, modelingPeriod);numbins = LastValue(HHV(numBins, BarCount));}else // cumulative pdf rather than rollingnumBins = OptimumBins_afl(data);//Get the maximum and minimum values in the data set, to calculate the range// minValue = LastValue(LLV(data, BarCount));// maxValue = LastValue(HHV(data, BarCount));minValue = LLV(data, modelingPeriod);maxValue = HHV(data, modelingPeriod);range = maxValue-minValue;binSize = range/numBins;if (GetDebug("CreatePdf")) { // begin debug_TRACE("CreatePDF: numBins is "+numBins);_TRACE("CreatePDF: minValue is "+minValue);_TRACE("CreatePDF: maxValue is "+maxValue);_TRACE("CreatePDF: range is "+range);_TRACE("CreatePDF: binSize is "+binSize);AddColumn(data, "CreatePDF: Input data");} // end debugpdfName = CreatePdfName(pdfRootName, numBins, binSize, modelingPeriod, index);highBin = LastValue(HHV(numBins, BarCount));if (NOT PdfExists(pdfName)) { //begin PdfExists//Save parameter values for later useSetPdfNumBins(pdfName, numBins, persist);SetPdfBinSize(pdfName, binSize, persist);SetPdfModelingPeriod(pdfName, modelingPeriod, persist);if (modelingPeriod > 1) { // begin rolling modelstart = minValue;for( bin = 1; bin <= highBin; bin++ ) { // begin for each binbool = data >! = start AND data < start + binSize;inBin = Nz(Sum(bool, modelingPeriod))/ modelingPeriod;SetPdfBin(pdfName, bin, inBin, persist);start += binSize;} // end for each bin} // end rolling modelelse { // begin cumulative modelstart = minValue;for( bin = 1; bin <= highBin; bin++ ) { // begin for each binbool = data >= start AND data < start + binSize;inBin = Nz(Cum(bool)/(BarIndex()+1));SetPdfBin(pdfName, bin, inBin, persist);start += binSize;} // end for each bin} // end cumulative modelStaticVarSet("pdfexists"+ pdfName, True, persist);} // end PdfExistsreturn pdfName;} //End CreatePDFfunction CreateJointPDF(pdfRootName, data1, data2, modelingPeriod, index, persist) { //Begin Createpdf// This function creates a probability density function out of the values in two data arrays.!//// Arguments:// pdfRootName - A string which provides the name for the probability density function.// data1, data2 - The arrays providing input for the pdf// modelingPeriod - The number of bars to look back on a rolling basis in constructing the pdf. If this// number is less than or equal to 1, the function constructs the pdf on a cumulative basis since bar// 0, rather than a rolling basis// index - A number used to make the pdfName identifier unique.// persist - A boolean flag which indicates whether or not the static variables should be persistent//_TRACE("Entered CreateJointPDF");//Set the optimal parameters for each modeling period//Calculate optimal number of bins by using the highest value the optimumsnumBins1 = OptimumBinsRolling_afl(dat! a1, modelingPeriod);// For performance. Better would be to change numBins on a rolling basis// numbins1 = LastValue(HHV(numBins1, BarCount));numBins2 = OptimumBinsRolling_afl(data2, modelingPeriod);// For performance. Better would be to change numBins on a rolling basis// numbins2 = LastValue(HHV(numBins2, BarCount));numBins = round((numBins1 + numBins2)/2);// Lessen sparseness and saves computation// maxBins = ceil(modelingP! eriod/2);// if (numBins > maxBins) numBins = maxBins;// if (numBins < 2) numBins = 2; // Always have at least a binary distributionmaxBins = ceil(modelingPeriod/2); // Lessen sparseness and saves computationnumbins = IIf(numBins > maxBins, maxBins, numBins);// Always have at least a binary distributionnumbins = IIf(numBins < 2, 2, numBins);highBin = LastValue(HHV(numBins, BarCount));/**/ _TRACE("CreateJointPDF: highBin is "+highBin);// min1 = LastValue(LLV(data1, modelingPeriod));// max1 = LastValue(HHV(data1, modelingPeriod));min1 = LLV(data1, modelingPeriod);max1 = HHV(data1, modelingPeriod);range1 = max1-min1;// min2 = LastValue(LLV(data2, modelingPeriod));// max2 = LastValue(HHV(data2, modelingPeriod));min2 = LLV(data2, modelingPeriod);max2 = HHV(data2, modelingPeriod);range2 = max2-min2;binSize1 = range1/numBins;binSize2 = range2/numBins;if (GetDebug("CreateJointPDF")) { // begin debugAddColumn(numBins1, "CreateJointPDF: numBins1");AddColumn(numBins2, "CreateJointPDF: numBins2");AddColumn(numBins, "CreateJointPDF: numBins");AddColumn(min1, "CreateJointPDF: min1");AddColumn(min2, "CreateJointPDF: min2");AddColumn(binSize1, "CreateJointPDF: binSize1");AddColumn(binSize2, &qu! ot;CreateJointPDF: binSize2");} // end debugpdfName = CreateJointPdfName( pdfRootName, LastValue(numBins1), LastValue(numBins2), LastValue(binSize1), LastValue(binSize2), modelingPeriod, index);if (NOT PdfExists(pdfName)) { // begin pdf does not already existif (GetDebug("CreateJointPDF")) _TRACE("CreateJointPdf: Creating joint pdf "+pdfName);SetPdfNumBins(pdfName, numBins^2, persist);SetJointPdfBinSize1(pdfName, binSize1, persist);SetJointPdfBinSize2(pdfName, binSize2, persist);SetJointPdfOrder(pdfName, pdfName+"data1", pdfName+"data2"); SetPdfModelingPeriod(pdfName, modelingPeriod, persist);start1 = min1;start2 = min2;if (modelingPeriod > 1) { // begin rolling modelfor( bin1 = 1; bin1 <= highBin; bin1++ ) { // begin for each bin in data1// Create and store single pdf bin result for data1bool1 = data1 >= start1 AND data1 < (start1 + binSize1);StaticVarSet("data1bool"+bin1, bool1);in! Bin = Nz(Sum(bool1, modelingPeriod))/ modelingPeriod;SetPdfBin(pdfName+"data1", bin1, inBin, persist);//increment bin for data1start1 += binSize1;} // end for each bin in data1SetPdfNumBins(pdfName+"data1", numBins, persist);SetPdfBinSize(pdfName+"data1", binSize1, persist);for (bin2 = 1; bin2 <= hi! ghBin; bin2++) { // begin for each bin in data2// Create and store single pdf bin result for data2bool2 = data2 >= start2 AND data2 < (start2 + binSize2);StaticVarSet("data2bool"+bin2, bool2);inBin = Nz(Sum(bool2, modelingPeriod))/ modelingPeriod;SetPdfBin(pdfName+"data2", bin2, inBin, persist);//increment bin for data2start2 += binSize2;} // end for each bin in data2SetPdfNumBins(pdfName+"data2", n! umBins, persist);SetPdfBinSize(pdfName+"data2", binSize2, persist);for (bin1 = 1; bin1 <= highBin; bin1++)for (bin2 = 1; bin2 <= highBin; bin2++) { // do bin combinations to create joint// Create and store joint pdf bin resultbool1 = StaticVarGet("data1bool"+bin1) ;bool2 = StaticVarGet("data2bool"+bin2) ;inBin = Nz(Sum(bool1 AND bool2, modelingPeriod))/ modelingPeriod;SetPdfBin(pdfName, CreateJointI! ndex(bin1, bin2), inBin, persist);if (GetDebug("CreateJointPDF")) { // begin debugAddColumn(bool1, "Joint Bin Bool1 - "+bin1);AddColumn(bool2, "Joint Bin Bool2 - "+bin2);AddColumn(bool1 AND bool2, "Joint Bin - "+bin1+"."+bin2);} // end debug} // end inner for loop for bin combinations} // end rolling modelelse { // begin cumulative modelfor( bin1 = 1;! bin1 <= highBin; bin1++ ) { // begin for each bin in data1// Create and store single pdf bin result for data1bool1 = data1 >= start1 AND data1 < (start1 + binSize1);StaticVarSet("data1bool"+bin1, bool1);inBin = Nz(Cum(bool1))/(BarIndex()+1);SetPdfBin(pdfName+"data1", bin1, inBin, persist);//increment bin for data1start1 += binSize1;}SetPdfNumBins(pdfName+"data1", numBins, persist);SetPdfBinSize(pdfName+"data1", binSize1, persist);for (bin2 = 1; bin2 <= highBin; bin2++) { // begin for each bin in data2// Create and store single pdf bin result for data2bool2 = data2 >= start2 AND data2 < (start2 + binSize2);StaticVarSet("data2bool"+bin2, bool2);inBin = Nz(Cum(bool2))/(BarIndex()+1);SetPdfBin(pdfName+"data2", bin2, inBin, persist);//increment bin for data2!start2 += binSize2;} // end for each bin in data2SetPdfNumBins(pdfName+"data2", numBins, persist);SetPdfBinSize(pdfName+"data2", binSize2, persist);for (bin1 = 1; bin1 <= highBin; bin1++)for (bin2 = 1; bin2 <= highBin; bin2++) { // do bin combinations to create joint// Create and store joint pdf bin resultbool1 = StaticVarGet("data1bool"+bin1) ;bool2 = StaticVarGet("data2bool"+bin2) ;inBin = Nz(Cum(bool1 AND bool2))/(BarIndex()+1);SetPdfBin(pdfName, CreateJointIndex(bin1, bin2), inBin, persist);if (GetDebug("CreateJointPDF")) { //begin debugAddColumn(bool1, "Joint Bin Bool1 - "+bin1);AddColumn(bool2, "Joint Bin Bool2 - "+bin2);AddColumn(bool1 AND bool2, "Joint Bin - "+bin1+"."+bin2);} // end debug} // end inner for loop for bin co! mbinations} // end cumulative model} // begin pdf exists_TRACE("CreateJointPDF: End of function. About to remove temporary variables.");StaticVarRemove("data1bool*");StaticVarRemove("data2bool*");_TRACE("CreateJointPDF: End of function. Successfully removed temporary variables."); StaticVarSet("pdfexists"+ pdfName, True, persist);if (GetDebug("CreateJointPDF")) _TRACE("CreateJointPdf: Recorded joint pdf exists: "! 3;pdfName);return pdfName;} // end CreateJointPdfFilter = 1;CleanUpStats();ClearDebug("CreateJointPDF");pdfName = CreateJointPDF("testPDF", C, Foreign("SPY", "C"), 126, 0, false);====================================================
__._,_.___
Posted by: "Ton Sieverding" <ton.sieverding@scarlet.be>
Reply via web post | • | Reply to sender | • | Reply to group | • | Start a New Topic | • | Messages in this topic (6) |
**** IMPORTANT PLEASE READ ****
This group is for the discussion between users only.
This is *NOT* technical support channel.
TO GET TECHNICAL SUPPORT send an e-mail directly to
SUPPORT {at} amibroker.com
TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at
http://www.amibroker.com/feedback/
(submissions sent via other channels won't be considered)
For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/
This group is for the discussion between users only.
This is *NOT* technical support channel.
TO GET TECHNICAL SUPPORT send an e-mail directly to
SUPPORT {at} amibroker.com
TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at
http://www.amibroker.com/feedback/
(submissions sent via other channels won't be considered)
For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/
.
__,_._,___
EmoticonEmoticon