I've spent two days pulling my hair out trying to track down this issue, and finally put together the below test code.
The hair pulling part is in the "D3" section, which was what my actual code was doing.
Can you explain why the D3 section doesn't work, and what the hell is ROCShortD3 and ROCLongD3 actually returning?
To be clear, I've got this working (just don't code "D3", use one of the other approaches), but I'd like to get a better understanding.
I'm particularly interested in rows where ROCShort and ROCLong are both negative. See 2018-12-31 or 2020-03-31.
I have read this KB article: https://www.amibroker.com/kb/2014/10/20/foreign-timeframeset/
I admit I was using Foreign() instead of SetForeign() in my code, but the KB article did not explain the different behaviour between the two, i.e. why SetForeign() must be before TimeFrameSet(), while Foreign() must be after TimeFrameSet().
// See https://www.amibroker.com/kb/2014/10/20/foreign-timeframeset/
ShortPeriod = 1;
LongPeriod = 5;
// Current chart symbol should be SPY, Daily periodicity
// Either walk this through the debugger or run an Explore with *Current SPY
// Monthly ROC (Works) (Remember current symbol is SPY)
TimeFrameSet(inMonthly);
ROCShortM = ROC(C,ShortPeriod);
ROCLongM = ROC(C,LongPeriod);
TimeFrameRestore();
// Convert to Daily
ROCShortD1 = TimeFrameExpand(ROCShortM,inMonthly,expandLast);
ROCLongD1 = TimeFrameExpand(ROCLongM,inMonthly,expandLast);
// Use the SetForeign function (Works)
SetForeign("SPY");
TimeFrameSet(inMonthly);
ROCShortM = ROC(C,ShortPeriod);
ROCLongM = ROC(C,LongPeriod);
TimeFrameRestore();
ROCShortD2 = TimeFrameExpand(ROCShortM,inMonthly,expandLast);
ROCLongD2 = TimeFrameExpand(ROCLongM,inMonthly,expandLast);
// Use the Foreign function (DOES NOT WORK)
cSymbol = Foreign("SPY","C");
TimeFrameSet(inMonthly);
ROCShortM = ROC(cSymbol,ShortPeriod);
ROCLongM = ROC(cSymbol,LongPeriod);
TimeFrameRestore();
ROCShortD3 = TimeFrameExpand(ROCShortM,inMonthly,expandLast);
ROCLongD3 = TimeFrameExpand(ROCLongM,inMonthly,expandLast);
// Use the Foreign function *inside* the TimeFrameSet (Works)
TimeFrameSet(inMonthly);
cSymbol = Foreign("SPY","C");
ROCShortM = ROC(cSymbol,ShortPeriod);
ROCLongM = ROC(cSymbol,LongPeriod);
TimeFrameRestore();
ROCShortD4 = TimeFrameExpand(ROCShortM,inMonthly,expandLast);
ROCLongD4 = TimeFrameExpand(ROCLongM,inMonthly,expandLast);
IsEOM = TimeFrameExpand(1,inMonthly,expandPoint);
if (Status("action") == actionExplore)
{
//Filter = 1;
Filter = IsEOM;
//Filter = ROCShortD < 0 AND ROCLongD < 0;
//Filter = ROCShortM < 0 AND ROCLongM < 0;
AddColumn(Close,"Close",1.2);
AddColumn(ROCShortM,"ROCShortM",1.2);
AddColumn(ROCLongM,"ROCLongM",1.2);
AddColumn(ROCShortD1,"ROCShortD1",1.2);
AddColumn(ROCLongD1,"ROCLongD1",1.2);
AddColumn(ROCShortD2,"ROCShortD2",1.2);
AddColumn(ROCLongD2,"ROCLongD2",1.2);
AddColumn(ROCShortD3,"ROCShortD3",1.2);
AddColumn(ROCLongD3,"ROCLongD3",1.2);
AddColumn(ROCShortD4,"ROCShortD4",1.2);
AddColumn(ROCLongD4,"ROCLongD4",1.2);
//SetSortColumns(4,5);
}