[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 CleanUpStats
if (Status("action") == actionBacktest) {
StaticVarRemove("pdf*"); 
StaticVarRemove("MIresult*");
StaticVarRemove("MIexists*");
}
} // end CleanUpStats

function GetPdfBin(pdfName, bin) { //begin GetPdfBin
if (typeof(bin) == "string") //bin is a string
suffix = bin;
else // bin is a number
suffix = NumToStr(bin, 1.0);

return StaticVarGet("pdfMatrix"+ pdfName+suffix);
} // end GetPdfBin

function GetPdfBinSize(pdfName) { //begin GetpdfBinSize
return StaticVarGet("pdf"+pdfName+" binSize");
} // end GetPdfBinSize

function GetPdfNumBins(pdfName) { //begin GetPdfBinSize
return StaticVarGet("pdf"+pdfName+" numBins");
} // end GetPdfNumBins

function GetPdfLookBack(pdfName) { //begin GetPdfLookBack
return StaticVarGet("pdf"+pdfName+" lb");
} // end GetPdfLookBack

function GetPdfModelingPeriod(pdfName) { //begin GetPdfModelingPeriod
return StaticVarGet("pdf"+pdfName+" modelingPeriod");
} // end GetPdfModelingPeriod

procedure SetPdfBin(pdfName, bin, value, persist) { //begin SetPdfBin
if (typeof(bin) == "string") //bin is a string
suffix = bin;
else // bin is a number
suffix = NumToStr(bin, 1.0);
StaticVarSet("pdfMatrix"+ pdfName+"-"+suffix, value, persist);
} // end SetPdfBin

procedure SetPdfBinSize(pdfName, value, persist) { //begin SetPdfBinSize
StaticVarSet("pdf"+pdfName+" binSize", value, persist);
} // end SetPdfBinSize

procedure SetPdfNumBins(pdfName, value, persist) { //begin SetPdfBinSize
StaticVarSet("pdf"+pdfName+" numBins", value, persist);
} // end SetPdfNumBins

procedure SetPdfLookBack(pdfName, value, persist) { //begin SetPdfLookBack
StaticVarSet("pdf"+pdfName+" lb", value, persist);
} // end SetPdfLookBack

procedure SetPdfModelingPeriod(pdfName, value, persist) { //begin SetPdfModelingPeriod
StaticVarSet("pdf"+pdfName+" modelingPeriod", value, persist);
} // end SetPdfModelingPeriod

procedure SetJointPdfBinSize1(pdfName, value, persist) { //begin SetPdfBinSize1
StaticVarSet("pdf"+pdfName+" binSize1", value, persist);
} // end SetPdfBinSize1

procedure SetJointPdfBinSize2(pdfName, value, persist) { //begin SetPdfBinSize2
StaticVarSet("pdf"+pdfName+" binSize2", value, persist);
} // end SetPdfBinSize2

procedure SetJointPdfOrder(jointPdf, pdf1, pdf2) { //begin SetJointPdfOrder
StaticVarSetText("pdfplace1"+ jointPdf, pdf1);
StaticVarSetText("pdfplace2"+ jointPdf, pdf2);
} // end SetJointPdfOrder

function GetJointPdfOrder(jointPdf, number) { //begin GetJointPdfOrder
if (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 GetJointPdfOrder

function GetJointPdfBinSize1(pdfName, value) { //begin GetPdfBinSize1
return StaticVarGet("pdf"+pdfName+" binSize1");
} // end GetPdfBinSize1

function GetJointPdfBinSize2(pdfName, value) { //begin GetPdfBinSize2
return StaticVarGet("pdf"+pdfName+" binSize2", value);
} // end GetPdfBinSize2

function CreateJointIndex(i, j) { // begin CreateJointIndex
return NumToStr(i, 1.0) + "." + NumToStr(j, 1.0);
} // end CreateJointIndex

function 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, binSize2, 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 pdfExists
if (StaticVarGet("pdfexists"+ pdfName) == True)
result = True;
else
result = False;
return result;
} // end pdfExists

function 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 GetComponentPDF

procedure 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 DisplayPDF

procedure 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 pdfexists

numBins = 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 bin
index = CreateJointIndex(i, j);
result = GetPdfBin(jointPdf, index);
AddColumn(result, "Bin " + index, 1.5);
total += result;
} // end for each bin
AddColumn(total, "Bin total", 1.2);
} // end DisplayJointPDF

function 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:
// SourceArray

end = 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 range
sorted = 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 OptimumBins

function OptimumBinsRolling_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 debug
AddColumn(maxValue, "OptimumBinsRolling: maxValue");
AddColumn(minValue, "OptimumBinsRolling: minValue");
AddColumn(bins, "OptimumBinsRolling: bins");
} // end debug
return 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 rolling
if (modelingPeriod > 1) {
numBins = OptimumBinsRolling_afl(data, modelingPeriod);
numbins = LastValue(HHV(numBins, BarCount));
}
else // cumulative pdf rather than rolling
numBins = 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 debug
pdfName = CreatePdfName(pdfRootName, numBins, binSize, modelingPeriod, index);
highBin = LastValue(HHV(numBins, BarCount));
if (NOT PdfExists(pdfName)) { //begin PdfExists
//Save parameter values for later use
SetPdfNumBins(pdfName, numBins, persist);
SetPdfBinSize(pdfName, binSize, persist);
SetPdfModelingPeriod(pdfName, modelingPeriod, persist);
if (modelingPeriod > 1) { // begin rolling model
start = minValue;
for( bin = 1; bin <= highBin; bin++ ) { // begin for each bin 
bool = 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 model
else { // begin cumulative model
start = minValue;
for( bin = 1; bin <= highBin; bin++ ) { // begin for each bin 
bool = 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 model

StaticVarSet("pdfexists"+ pdfName, True, persist);
} // end PdfExists
return pdfName;
} //End CreatePDF

function 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 optimums
numBins1 = OptimumBinsRolling_afl(data1, 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(modelingPeriod/2); 
// if (numBins > maxBins) numBins = maxBins;
// if (numBins < 2) numBins = 2; // Always have at least a binary distribution
maxBins = ceil(modelingPeriod/2); // Lessen sparseness and saves computation
numbins = IIf(numBins > maxBins, maxBins, numBins);
// Always have at least a binary distribution
numbins = 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 debug
AddColumn(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, "CreateJointPDF: binSize2");
} // end debug
pdfName = CreateJointPdfName( pdfRootName, LastValue(numBins1), LastValue(numBins2), LastValue(binSize1), LastValue(binSize2), modelingPeriod, index);

if (NOT PdfExists(pdfName)) { // begin pdf does not already exist
if (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 model
for( bin1 = 1; bin1 <= highBin; bin1++ ) { // begin for each bin in data1 
// Create and store single pdf bin result for data1
bool1 = data1 >= start1 AND data1 < (start1 + binSize1);
StaticVarSet("data1bool"+bin1, bool1);
inBin = Nz(Sum(bool1, modelingPeriod))/ modelingPeriod; 
SetPdfBin(pdfName+"data1", bin1, inBin, persist);
//increment bin for data1
start1 += binSize1; 
} // end for each bin in data1
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 data2
bool2 = 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 data2
start2 += binSize2;
} // end for each bin in data2

SetPdfNumBins(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 result
bool1 = StaticVarGet("data1bool"+bin1) ;
bool2 = StaticVarGet("data2bool"+bin2) ;
inBin = Nz(Sum(bool1 AND bool2, modelingPeriod))/ modelingPeriod; 
SetPdfBin(pdfName, CreateJointIndex(bin1, bin2), inBin, persist);

if (GetDebug("CreateJointPDF")) { // begin debug
AddColumn(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 model
else { // begin cumulative model
for( bin1 = 1; bin1 <= highBin; bin1++ ) { // begin for each bin in data1 
// Create and store single pdf bin result for data1
bool1 = 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 data1
start1 += 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 data2
bool2 = 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 data2

SetPdfNumBins(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 result
bool1 = 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 debug
AddColumn(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 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: "+pdfName);
return pdfName;
} // end CreateJointPdf


Filter = 1;
CleanUpStats();
ClearDebug("CreateJointPDF");
pdfName = CreateJointPDF("testPDF", C, Foreign("SPY", "C"), 126, 0, false);

====================================================

__._,_.___

Posted by: rosenberggregg@yahoo.com
Reply via web post Reply to sender Reply to group Start a New Topic Messages in this topic (5)
**** 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/


.

__,_._,___


EmoticonEmoticon