Comparing Futures Contracts

Hi Guys, I've set up an AFL for comparing data, especially volume, between two tickets from different expirations, the same day of the year, plotting both on screen (it applies a 252-day Offset to the foreign ticket and overlays it with the current contract).
It works perfectly with regular Stocks, but it doesn't with futures contracts, which is precisely what interest me most. I assume this is because the old contract doesn't have Bars up to today (it expired a while ago), and therefore the offset applied isn't working.

So, Is there a way to do this, or to fill bars of the expired contract with 0 values ​​up to today?

thanks!

Ticker2 = ParamStr("Ticker a Comparar", "AAPL");
PrevYEAR= lastvalue(Year()-1);

VOL_Tick1_ThisYEAR = Volume;
VOL_Tick2_ThisYEAR = Foreign(Ticker2, "V");
VOL_Tick2_prevYEAR = Ref(VOL_Tick2_ThisYEAR,-252);

coloractual = colorGrey40;
colorprevio = colorPaleTurquoise;

// PLOT VOLUME
Plot(VOL_Tick1_ThisYEAR, "Vol " + Name() + " Actual", coloractual, styleHistogram|styleLeftAxisScale, 0, LastValue(HHV(VOL_Tick1_ThisYEAR,100)), Null, 1, 5);
Plot(VOL_Tick2_prevYEAR, "Vol " + Ticker2 + " Año Anterior", colorprevio, styleHistogram|styleOwnScale, 0, LastValue(HHV(VOL_Tick2_prevYEAR,100)), Null, Null, 8);

// RATIO
Ratio = IIf(VOL_Tick2_prevYEAR > 0, 
           VOL_Tick1_ThisYEAR / VOL_Tick2_prevYEAR, 
           0);

Title = "COMPARATIVA VOLUMEN : " + Name() + " (Actual) vs " + Ticker2 + " (Año Anterior)" +
        "\nFecha Actual: " + Date() 
     + "\n"
     + EncodeColor(coloractual) + "\nVol " + Name()  + " (" + Year() + ")  " + WriteVal(VOL_Tick1_ThisYEAR, 1.0) 
     + EncodeColor(colorprevio) + "\nVol " + Ticker2 + " (" + PrevYEAR + ")  " + WriteVal(VOL_Tick2_prevYEAR, 1.0) 
     + EncodeColor(colorBlack) + "\nRatio: " + WriteVal(Ratio, 1.3);

// Opcional: Medias móviles
Plot(wMA(VOL_Tick1_ThisYEAR, 10), "MA " + Name(), coloractual, styleLine, Null, Null, NULL, 1, 3);
Plot(wMA(VOL_Tick1_ThisYEAR, 10), "", colorWhite, styleLine, Null, Null, NULL, 3, 0);

Plot(wMA(VOL_Tick2_prevYEAR, 10), "MA " + Ticker2 + " Prev", colorprevio, styleLine, Null, Null, NULL, 2, 3);
Plot(wMA(VOL_Tick2_prevYEAR, 10), "", colorWhite, styleLine, Null, Null, NULL, 3, 1);

Hi Guys, any hints?

Without testing it myself, my guess is that the problem is the opposite of what you speculated. Specifically, when you call Foreign(), the data that's returned is aligned to the current symbol. So if the new (current) contract only has data back to, say October 2024, then the old (foreign) data will be truncated before October 2024.

One possible way to work around this would be to enable Pad & Align to a long-running symbol and then do your analysis using an Exploration instead of a Chart, as charts don't use Pad & Align. Or, if you really like the charting approach, you could have your Exploration store the previous year's volume in the Aux1 or Aux2 field of the current contract, and then use Aux1 or Aux2 in the chart.

1 Like

hi, below some code that seems to work. Note I use the continuous symbol @W# as the selected symbol in the chart. You can also use the symbol @W#C. I tested the code in a 5min IQFeed database.

// symbol in chart is the continuous symbol for wheat @W# or @W#C

separator = Ref( Nz( TimeFrameExpand( 1, inYearly, expandPoint ) ), -1 );
bi = BarIndex();
sbi1 = ValueWhen( separator, bi, 1 );
sbi2 = ValueWhen( separator, bi, 2 );

dsbi = sbi1 - sbi2;
//"dsbi: " + dsbi;

SetChartBkColor( ColorRGB( 0, 0, 0 ) );
SetChartOptions( 0, chartShowArrows | chartShowDates );
Plot( V, "V", colorWhite, styleHistogram, null, Null, 0, 0, 1 );
Plot( V, "", colorWhite, styleLine, null, Null, 0, 0, 1 );

V1 = Foreign( "@WN25", "V" );
Plot( V1, "V1", colorRed, styleHistogram, null, Null, 0, 1, 1 );
Plot( V1, "", colorRed, styleLine, null, Null, 0, 1, 1 );

V2 = Foreign( "@WN24", "V" );
Plot( Ref( V2, -dsbi ), "V2", colorBlue, styleHistogram, null, Null, 0, 1, 1 );
Plot( Ref( V2, -dsbi ), "", colorBlue, styleLine, null, Null, 0, 1, 1 );

Plot( IIf( separator, 1, Null ), "", ColorRGB( 0, 255, 255 ), styleHistogram | styleOwnScale | styleNoLabel, 0, 1, 0, 10, 5 );

1 Like

for the separator your can also just use:

separator = Year() != Ref( Year(), -1 );

Thank you Both Guys!, I will try both approaches and i´ll tell you which one is more practical for the case.