Hi @mradtke
Trying to put the details in a more clearer manner here:
Here is a sample code with which I am able to reproduce the issue:
_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) Vol " +WriteVal( V, 1.0 ) +" {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 )) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
_SECTION_END();
SetBarsRequired(-2,-2);
symbol ="BANKNIFTY";
strikeinterval = 100;
offset = 0;
Closing = Foreign("BANKNIFTY-I.NFO","Close");
tn = TimeNum();
CE = "CE.NFO";
PE = "PE.NFO";
for(i=1;i<BarCount;i++)
//for (i = FirstBar; i < LastBar; i++)
{
ltp = Closing[i];
iPE = ltp - (ltp%strikeinterval); //nearest strike price 11536-36 = 11500 , 11536 is the spot price
iCE = ltp - (ltp%strikeinterval);
iPE = iPE - (offset*strikeinterval);
iCE = iCE + (offset*strikeinterval);
if(( ltp % strikeinterval) > (strikeinterval/2)) //11525 reminder = 36 > 25, CE = 11550, PE = 11500 11502 , CE = 11500, PE = 11500
{
iCE = ltp - (ltp%strikeinterval)+strikeinterval;
iPE= ltp - (ltp%strikeinterval)+(strikeinterval);
iPE = iPE - (offset*strikeinterval);
iCE = iCE + (offset*strikeinterval);
}
strike[i] = iPE;
}
expiry = "01APR21";
//expiry = expiry_string;
ce_max_strike = pe_max_strike = 0;
//CE_max_strike = 0;
CE_max_OI = 0;
//PE_max_strike = 0;
PE_max_OI = 0;
CE_OI = PE_OI = 0;
for ( k = -50 ; k <=50 ; k++)
{
ATMS = Strike;
SelStrike = ATMS + (k * strikeinterval);
//Building the CE symbol
CE_symbol = symbol + expiry + StrFormat("%.0f",SelStrike) + CE;
//Getting OI and price of the symbol
CE_OI_temp_arr = Foreign(CE_symbol,"I");
CE_Price_arr = Foreign(CE_symbol,"C");
// Getting start of day OI and calculating change
CE_oi_temp_day_start = IIf(!IsNull(CE_OI_temp_arr),ValueWhen(TimeNum() == 092500,CE_oi_temp_arr),0);
CE_Change_OI = IIf(!IsNull(CE_OI_temp_arr),CE_OI_temp_arr - CE_oi_temp_day_start,Null);
CE_OI = IIf ( !IsNull(CE_Change_OI), CE_OI + CE_Change_OI, CE_OI);
//Same for Put side
PE_symbol = symbol + expiry + StrFormat("%.0f",SelStrike) + PE;
PE_OI_temp_arr = Foreign(PE_symbol,"I");
PE_oi_temp_day_start = IIf(!IsNull(PE_OI_temp_arr),ValueWhen(TimeNum() == 092500,PE_oi_temp_arr),0);
PE_Price_arr = Foreign(PE_symbol,"C");
PE_Change_OI = IIf(!IsNull(PE_OI_temp_arr),(PE_OI_temp_arr - PE_oi_temp_day_start),Null);
PE_OI = iif(!IsNull(PE_Change_OI),PE_OI + PE_Change_OI,PE_OI);
PE_max_OI = IIf ( PE_max_OI > PE_Change_OI OR IsNull(PE_Change_OI), PE_max_OI, PE_Change_OI);
}
PE_OI_temp = IIf( PE_OI < 0 AND CE_OI < 0, abs(CE_OI), PE_OI);
CE_OI_temp = IIf ( PE_OI < 0 AND CE_OI < 0, abs(PE_OI), CE_OI);
printf("\n CE OI temp:" +StrFormat("%.0f",CE_OI_temp));
printf("\n PE OI temp:" +StrFormat("%.0f",PE_OI_temp));
multiplier = IIf ( PE_OI_temp > CE_OI_temp, abs(1 / PE_OI_temp), abs(1 / CE_OI_temp));
//Calculation of units
CE_units = IIf ( CE_OI_temp <= 0 AND PE_OI_temp >= 0 , 0 ,
IIf ( CE_OI_temp >= 0 AND PE_OI_temp <= 0 , 1 ,
IIf (PE_OI_temp > CE_OI_temp, CE_OI_temp * multiplier, 1)));
PE_units = IIf ( PE_OI_temp <= 0 AND CE_OI_temp >= 0 , 0 ,
IIf ( PE_OI_temp >= 0 AND CE_OI_temp <= 0 , 1 ,
IIf (CE_OI_temp > PE_OI_temp, PE_OI_temp * multiplier, 1)));
future_units = IIf ( CE_units == PE_units, 0,
IIf (CE_units > PE_units, -1 * (CE_units - PE_units), 1 * (PE_units - CE_units)));
future_units_pos = 0;
for(i=1;i<BarCount;i++)
{
future_units_pos[i] = future_units_pos[i-1];
if (tn[i] == 151500)
{
future_units_pos[i] = future_units[i];
}
}
GfxSetOverlayMode(0);
GfxSetBkMode(0);
GfxSelectFont("Tahoma",10,400);
GfxSelectSolidBrush(colorLightGrey);
GfxRectangle(20,330,320,770);
GfxTextOut("Future Units Pos :"+StrFormat("%.2f",future_units_pos),28,410);
If you look at the code, the first part of the code (First if loop) is calculating the strike value.
The second loop is running through multiple different symbols to get some value based on Open interest.
The third loop is just setting the calculated unit value to a variable at a specified time and the value should remain the same till the next day as per the condition [The actual program has a lot more conditions here due to which I am using the for loop]
future_units_pos[i] = future_units_pos[i-1];
if (tn[i] == 151500)
{
future_units_pos[i] = future_units[i];
}
Taking one instance, value of Future position at 15:20 hours on 25/3/2020 is 0.00

When I move two bars out, the value changes to -0.04.

If I change the second for loop (OI calculation loop) to:
for ( k = -100 ; k <=100 ; k++)
The value does not change from moving from one bar to another.
By debugging, I have come to a point where I understand that :
SelStrike = ATMS + (k * strikeinterval);
//Building the CE symbol
CE_symbol = symbol + expiry + StrFormat("%.0f",SelStrike) + CE;
The CE symbol differs at each point based on the current price of the underlying instrument and hence this issue comes up.
How do I avoid this scenario?
I am presuming it is because the for loop ( i = 1 to Barcount) is running everytime and hence new calculations coming up. Can I probably change the loop to only run the last couple of values / can I have someway of storing some of the variables so that it doesnt change?
Hope my query was clear this time around.
Look forward to your suggestions.
Thanks
Siddhant Kankani