Unable to get value from foreign ticker

Hello Friends,

I am trying to trade in options based on Future.
Reading Future's data while in Option chart.
Wrote below program, before and after SetForeign, volumes of options are displaying fine but volume of Future is displaying as zero. Any suggestions highly appreciate.

_SECTION_BEGIN("DSK-Trade");


SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 

printf("Volume of BNI Option = %.0f\n",Volume[BarCount-1]);
SetForeign("BANKNIFTY-I");
printf("Volume of BNI Fut = %.0f\n",Volume[BarCount-1]);
RestorePriceArrays();
printf("Volume of BNI Option = %.0f\n",Volume[BarCount-1]);

_SECTION_END();

SetForeign() or foreign() does not pass BarCount of the called foreign symbol, it captures O, H, L, C, V, OI, Aux1 and Aux2 arrays.

So, when you write:

you are essentially calling Volume of foreign symbol BANKNIFTY-I, however, the BarCount - 1 still refers to the base (options) chart symbol. BarCount - 1 of the base chart does not guarantee that the same bar number would be existent in the foreign symbol that you are calling. Most likely, it could result as a data hole, in other words, data could be missing for that foreign bar.

Both SetForeign() and foreign() function holds a fixup parameter which reads as:

fixup parameter controls if data holes are filled with previous bar data or not.

  • 0 - the holes are not fixed

  • 1 - default value - missing data bar OHLC fields are all filled using previous bar Close and volume is set to zero

  • 2 - (old pre-4.90 behaviour) - causes filling the holes in the data with previous O, H, L, C, V values

When you specifically ask for BarCount - 1, you get existent Volume value of the base (options) chart symbol, however, in your case since BarCount - 1 is possibly non-existent for the foreign Futures symbol (i.e. BANKNIFTY-I) you get 0 as fixup = 1 by default.

I am not sure from where you picked to use BarCount - 1 everywhere as an alternate to LastValue().


Consider going through:


If you are planning to capture more than one array of a foreign symbol or do more than one-liner operation with array(s) on foreign symbol, then using SetForeign() makes sense:

SetForeign( "BANKNIFTY-I" );
	futV = V;
	/* do something else */
RestorePriceArrays();

printf( "Volume of BNI Option = %1.0f\n", LastValue( V ) );
printf( "Volume of BNI Fut = %1.0f\n", LastValue( futV ) );

Otherwise, you are better with foreign(), like so:

futV = Foreign( "BANKNIFTY-I", "V" );

printf( "Volume of BNI Option = %1.0f\n", LastValue( V ) );
printf( "Volume of BNI Fut = %1.0f\n", LastValue( futV ) );

Also note, if BANKNIFTY-I foreign symbol does not exist at all in that DB or your 3rd party data-plugin is messing up something, in that case, you're likely to get foreign Volume as 0 as well.

Lastly, LastValue() fixates at an array's last element value. Alternately, SelectedValue() could also be used which will return array value at currently selected bar.

1 Like

Thanks a lot Cougar for your inputs.

I tried the two options given by you but still getting Future volume as zero.
BANKNIFTY-I is already available in my database. Below screen shot give all the details.

First of all, I did not give you any option. I was merely differentiating usage scenario of SetForeign() and Foreign().

No, that screenshot does not give "all" the details. It shows existence of Futures symbol BANKNIFTY-I and nothing more. Please re-read previous post carefully, the reason for still showing 0 is already answered.

Bar-by-bar tallying of current Option symbol quotes with Future symbol quotes via an Exploration would give "all" needed clarity.

Also consider reading:

Hi Cougar,

Thanks for your reply.
I have gone through all relevant links given by you.
I used exploration option to see the values of all the elements in the given range. Now I am able to see Future Volume when I am in Option chart.

I am trying to generate BUY signal in Option chart based on Future volume and Super Trend Indicator but it is not generating the BUY signal even after condition is met, see below code.

_SECTION_BEGIN("DSK-Trade");


SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 

Filter = 1;
Buy[BarCount-1]=0;
FutO = Foreign("BANKNIFTY-I","O");
FutH = Foreign("BANKNIFTY-I","H");
FutL = Foreign("BANKNIFTY-I","L");
FutC = Foreign("BANKNIFTY-I","C");
FutV = Foreign("BANKNIFTY-I","V");

AddColumn(FutC,"FutC");
AddColumn(FutV,"FutV");

//Calcuating SuperTrend of BANKNIFTY Future
Factor=Param("Factor",2,1,10,1);
Pd=Param("ATR Periods",14,1,100,1);

Up=(FutH+FutL)/2+(Factor*ATR(Pd));
Dn=(FutH+FutL)/2-(Factor*ATR(Pd));
iATR=ATR(Pd);
TrendUp=TrendDown=Null;
trend[0]=1;
changeOfTrend=0;
flag=flagh=0;

for (i = 1; i <BarCount-1; i++) {
      TrendUp[i] = Null;
      TrendDown[i] = Null;
    
      trend[i]=1;
  
      
      if (FutC[i]>Up[i-1]) {
        trend[i]=1;
        if (trend[i-1] == -1) changeOfTrend = 1;
        
      }
      else if (FutC[i]<Dn[i-1]) {
        trend[i]=-1;
        if (trend[i-1] == 1) changeOfTrend = 1;
      }
      else if (trend[i-1]==1) {
        trend[i]=1;
        changeOfTrend = 0;      
      }
      else if (trend[i-1]==-1) {
        trend[i]=-1;
        changeOfTrend = 0;
      }

      if (trend[i]<0 && trend[i-1]>0) {
        flag=1;
      }
      else {
        flag=0;
      }
      
      if (trend[i]>0 && trend[i-1]<0) {
        flagh=1;
      }
      else {
        flagh=0;
      }
      
      if (trend[i]>0 && Dn[i]<Dn[i-1]){
        Dn[i]=Dn[i-1];
      }
      
      if (trend[i]<0 && Up[i]>Up[i-1])
        { Up[i]=Up[i-1];
      }
      
      if (flag==1)
      {  Up[i]=(FutH[i]+FutL[i])/2+(Factor*iATR[i]);;
        } 
      if (flagh==1)
        { Dn[i]=(FutH[i]+FutL[i])/2-(Factor*iATR[i]);;
        }
      if (trend[i]==1) {
        TrendUp[i]=Dn[i];
        if (changeOfTrend == 1) {
            TrendUp[i-1] = TrendDown[i-1];
            changeOfTrend = 0;
        }
      }
      else if (trend[i]==-1) {
        TrendDown[i]=Up[i];
        if (changeOfTrend == 1) {
            TrendDown[i-1] = TrendUp[i-1];
            changeOfTrend = 0;
        }
      }
  } 

Plot(TrendUp,"Trend",colorGreen);
Plot(TrendDown,"Down",colorRed);


SuperTrendLong = trend==1;
SuperTrendShort=trend==-1;


AddColumn(C,"OptC");
AddColumn(V,"OptV");
AddColumn(SuperTrendLong,"STL");

if(FutV[BarCount-1] > 15000 AND SuperTrendLong[BarCount-1] == 1 )
{
Buy[BarCount-1]=1;
PlotShapes(shapeUpArrow,colorGreen);
}

AddColumn(Buy,"Buy");



_SECTION_END();


Same thing I checked in exploration also (Screen Shot below). Not sure where I went wrong, your inputs are highly appreciated.

Thanks
Shiva

Now we are deviating from the Original Topic.

Sadly, you do not listen!

  1. Assignment, comparison or any operation directly on array[ BarCount - 1 ] is utterly pathetic AFL coding practice.

  2. You still do not comprehend how and when to use SetForeign, Foreign.

Before anything else, consider understanding how AFL works.

Last but not the least, (although poorly-written) that SuperTrend code is not yours.

Tip: Treat Buy just like any other array.

Hi Cougar,

Thanks for your response.
I agree with your comments. In hurry to automate, I did not go through the concepts in detail and trying to write code. Now I gone through the details and modified my code, now it is working fine.
Below is the code I wrote to buy option based on futures.

SetForeign("BANKNIFTY-I");
FutC = C;
FutV = V;
FutRSI = RSI(14);
RestorePriceArrays();

Buy = IIf( Cross(EMA(FutC,8),EMA(FutC,21)) AND FutRSI>35 AND FutV>4000,1,0 );
PlotShapes(IIf(Buy,shapeUpArrow,shapeNone),colorBlue);

You experience an unparalleled delight when you ultimately make it to work, isn't it!
All the very best @DSK!

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.