Getting current values of ATM options

I am backesting a strategy on options in which I need OI for the current atm strike

This is how i get current atm strike.

CurrentSpotStrike = Foreign( "BANKNIFTY", "C" );
CurrentSpotStrike = round(Ref(CurrentSpotStrike, -1)/100)*100;
printf("CurrentSpotStrike %g \n", CurrentSpotStrike);

Now to get CE and PE OI at this junction

callOI= Foreign(StrLeft(Name(), 16) + CurrentSpotStrike + "CE.NFO", "I");
putOI = Foreign(StrLeft(Name(), 16) + CurrentSpotStrike + "PE.NFO", "I");

Everything looks good in the interpretation window. But in backtest this gives me OI not of the current strike but of the Strike at end of day.

The solution is string array or is there is simpler solution available?

As I already responded in another post, AmiBroker does not support arrays of strings. I also explained why your code doesn't work the way you want it to.

You could create a loop to call Foreign for each bar, but that is likely to be quite slow. Something like this untested snippet:

baseName = StrLeft(Name(), 16);
callOI = putOI = 0;
for (bar=0; bar<BarCount; ++bar)
{
    callOI[bar] = Foreign(baseName  + CurrentSpotStrike[bar] + "CE.NFO", "I");
    putOI[bar] = Foreign(baseName  + CurrentSpotStrike[bar] + "PE.NFO", "I");
}

Alternatively, you could revise your strategy rules so that you don't need to find the ATM option for every bar of the backtest. For example, you might just need that information when there's an entry signal.

First of all, thank you sir for your reply. I wasn't able to reply to that post because of some forum rule.

I did try this way but was getting an error.

Error 8.
Type mismatch. the value assigned to an array element has to be a number. You cannot use array on the right side of this argument.

Sorry about that. Foreign is also returning an array, so you would need to do something like this:

baseName = StrLeft(Name(), 16);
callOI = putOI = 0;
for (bar=0; bar<BarCount; ++bar)
{
    callOIArray = Foreign(baseName  + CurrentSpotStrike[bar] + "CE.NFO", "I");
    callOI[bar] = callOIArray[bar];
    putOIArray = Foreign(baseName  + CurrentSpotStrike[bar] + "PE.NFO", "I");
    putOI[bar] = putOIArray[bar];
}
1 Like

Thanks a lot sir. It is working now.

I don't know about this concept and it seems important to understand. callOIArray is a number or an array, it is showing properties of both.

Can you please help me understand?

Yes, it is absolutely essential that you fully understand both arrays and scalars (single values) in AmiBroker.

The Foreign function returns an array, which is stored in the variable callOIArray. That's why I used "Array" in the variable name: to make it clear that variable is an array.

To reference a single value from the array, we have to provide an index inside square brackets. The statement callOIArray[bar] returns the array element (which is a scalar) stored at index bar.

1 Like

Got it.

This line was new to me, assigning a single value to an array inside a loop without referencing a single array. Now that i think about it, this seems logical.

callOIArray = Foreign(baseName + CurrentSpotStrike[bar] + "CE.NFO", "I");

Thank you sir. You have been extremely helpful.

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