How to Add IB option Data Symbols Automatically

Here is the code to add IB option data Symbol.

/*+-------------------------------------------------------------------------------------------------------------------*
//|Coded By Ashok Meheta(824_956_2097)                                                                                  |
Use of Parameter
Expiry Date(DDMMYYYY) - Write the expiry date in format DATE-Month-YEAR(31012022)
Select Expiry ? - Selct the Option type to add
"Option Of" - Which index of symbol option you want to add
"IB Underlying Symbol" - Symbol name of interactive broker

Click at Add option symbol
/*--------------------------------------------------------------------------------------------------------------------*/
_N( Formula_Name = "" );
_N( System_Ver = "" + Name() );
Day_OfWeek = DayOfWeek();
No_Month = Month();
SetBarsRequired( sbrAll, 0 );
RequestTimedRefresh( 1, False );
_N( Day_Name = WriteIf( Day_OfWeek == 0, "Sun", WriteIf( Day_OfWeek == 1, "Mon", WriteIf( Day_OfWeek == 2, "Tue", WriteIf( Day_OfWeek == 3, "Wed",	WriteIf( Day_OfWeek == 4, "Thu",	WriteIf( Day_OfWeek == 5, "Fri", WriteIf( Day_OfWeek == 6, "Sat",	"Unknown" ) ) ) ) ) ) ) );
_N( Month_Name = Writeif( No_Month == 1, "Jan", Writeif( No_Month == 2, "Feb", Writeif( No_Month == 3, "Mar", Writeif( No_Month == 4, "Apr", Writeif( No_Month == 5, "May",
                          Writeif( No_Month == 6, "Jun", Writeif( No_Month == 7, "Jul", Writeif( No_Month == 8, "Aug", Writeif( No_Month == 9, "Sept", Writeif( No_Month == 10, "Oct", Writeif( No_Month == 11, "Nov", Writeif( No_Month == 12, "Dec", "Unknown" ) ) ) ) ) ) ) ) ) ) ) ) );
_N( Hour_Min_Sec = WriteIf( Hour() > 9, "" + Hour(), "0" + Hour() ) + ":" + WriteIf( Minute() > 9, "" + Minute(), "0" + Minute() ) + ":" + WriteIf( Second() > 9, "" + Second(), "0" + Second() ) );
_N( CustomDateFormat = Day_Name + " " + NumToStr( Day(), 1.0 ) + " " + Month_Name + " " + NumToStr( Year(), 1.0, False ) + "  " + WriteIf( Interval() < inDaily, Hour_Min_Sec, "" ) );
Yogya_Upcolor = ColorRGB( 0, 177, 88 );
Yogya_DownColor = ColorRGB( 246, 70, 65 );
Setchartoptions( 0, Chartshowarrows | chartShowDates );
SetbarfillColor( IIf( C > O,  Yogya_Upcolor, Yogya_DownColor ) );
Plot( C, "Price", IIf( C > O,  Yogya_Upcolor, Yogya_DownColor ), styleCandle | styleNoTitle );
SetFormulaName( Formula_Name + "" + System_Ver );
_N( Title = EncodeColor( ColorRGB( 255, 150, 150 ) ) + StrFormat( Formula_Name + "" + System_Ver + "  {{INTERVAL}}  " ) + CustomDateFormat + "" + StrFormat( "  Open = %g, Hi = %g, Lo = %g, Close = %g, Volume = %g Change = (%.1f%%) {{VALUES}}", O, H, L, C, V, SelectedValue( ROC( C, 1 ) ) ) );

function Strike_Calculation( Price_Entry, S_Interval, Leg_Dist,Type)
{
    S_Interval2 = S_Interval / 2;
    Remind = Price_Entry % S_Interval;
    Add_P = IIf( Remind <= S_Interval2, -Remind, ( S_Interval - Remind ) );
    Strike = Price_Entry + Add_P;
    if(Type=="CE")
    {
     Leg_Strike_Value = Strike + S_Interval * Leg_Dist;
     } else if(Type=="PE")
    {
     Leg_Strike_Value = Strike - S_Interval * Leg_Dist;
     }
  
    return  Leg_Strike_Value;
}

//"\nIB";
//Monthly Option
//BANKNIFTY 22MAR 33800 CE-NSE-OPT-INR
//BANKNIFTY 22317 34900 CE-NSE-OPT-INR
//IB Option Symbol Function
function Option_Ticker_IB_Weekly( Underline_Symbol, ExpiryDate, strike_price, type)
{
Day_Get = StrToNum(StrLeft( ExpiryDate, 2));
Day_Get = WriteIf(Day_Get < 10,"0"+Day_Get,""+Day_Get);
Month_Get = StrToNum(StrMid( ExpiryDate, 2, 2));
Year_Get = StrToNum(StrRight( ExpiryDate, 2));
Date_Fomat_Needed = ""+Year_Get+""+Month_Get+""+Day_Get;

return Underline_Symbol+""+Date_Fomat_Needed+""+strike_price+""+type+"-NSE-OPT-INR";
}

function Option_Ticker_IB_Monthly( Underline_Symbol, ExpiryDate, strike_price, type)
{
Month_Get = StrToNum(StrMid( ExpiryDate, 2, 2));
Year_Get = StrToNum(StrRight( ExpiryDate, 2));
Month_Name = Writeif( Month_Get == 1, "JAN", Writeif( Month_Get == 2, "FEB", Writeif( Month_Get == 3, "MAR",
                             Writeif( Month_Get == 4, "APR", Writeif( Month_Get == 5, "MAY",
                          Writeif( Month_Get == 6, "JUN", Writeif( Month_Get == 7, "JUL", Writeif( Month_Get == 8, "AUG", 
                          Writeif( Month_Get == 9, "SEP", Writeif( Month_Get == 10, "OCT", Writeif( Month_Get == 11, "NOV", "DEC" ) ) ) ) ) ) ) ) ) ) );
Date_Fomat_Needed = ""+Year_Get+""+Month_Name;

return Underline_Symbol+""+Date_Fomat_Needed+""+strike_price+""+type+"-NSE-OPT-INR";
}

function Option_Ticker_IB( Expiry_Type, Underlying_Symbol, Expiry_Date, Option_Strike, Option_Type )
{
     if( Expiry_Type == "Weekly" )
    {
        Option_Symbol_To_Add = Option_Ticker_IB_Weekly( Underlying_Symbol, Expiry_Date, Option_Strike, Option_Type );
    }

    if( Expiry_Type == "Monthly")
    {
        Option_Symbol_To_Add = Option_Ticker_IB_Monthly( Underlying_Symbol, Expiry_Date, Option_Strike, Option_Type );
    }

    return  Option_Symbol_To_Add;
}

function Get_LTP(DataBase_Symbol)
{
return LastValue(Foreign( DataBase_Symbol, "C" ));
}

function Add_Option_Ticker_IB( Down_No, Up_No, Database_Symbol, Expiry_Type, Underlying_Symbol, Expiry_Date,Index_Strike_Interval,Option_Type )
{

    Total_Option_Symbol_Added =  Down_No + Up_No + 1;
    AB = CreateObject( "Broker.Application" );
  

    for( i = -Down_No; i <= Up_No; i++ )
    {
        Underlying_Close = LastValue( Foreign( Database_Symbol, "C" ) );
        Option_Strike =  Strike_Calculation( Underlying_Close, Index_Strike_Interval, i , Option_Type ) ;
        Option_Symbol_To_Add = Option_Ticker_IB( Expiry_Type, Underlying_Symbol, Expiry_Date, Option_Strike, Option_Type ) ;
        docs = AB.Documents;
        Sts = AB.Stocks;
        Sts.add( Option_Symbol_To_Add ) ;
    }

    AB.RefreshAll();
      doc = AB.ActiveDocument;
    doc.Name = Database_Symbol;
    PopupWindow( "" + Total_Option_Symbol_Added + " " + Expiry_Type + " Symbols Added", "" + Total_Option_Symbol_Added + " " + Expiry_Type + " Symbols Added",  5, 640 * mtRandom(), 480 * mtRandom() );
    Say( "" + Total_Option_Symbol_Added + " " + Expiry_Type + " Symbols Added" );
    return   0 ;
}

_SECTION_BEGIN( "Add IB Option Symbols(Akmeheta@gmail.com)" );
Expiry_Date = ParamStr( "Expiry Date(DDMMYYYY)", "31032022" );
Select_Expiry = Paramlist("Select Expiry ?", "Weekly|Monthly", 1);
No_Of_OTM_Para = Param( "No of OTM", 15, 0, 100, 1 );
No_Of_ITM_Para = Param( "No of ITM", 5, 0, 100, 1 );
Underlying_Symbol_Para =   ParamStr(  "Option Of", "BANKNIFTY" );
Index_Symbol_IB = ParamStr("IB Underlying Symbol", "BANKNIFTY-NSE-IND-INR");
Strike_Interval_Para = StrToNum( ParamStr( "Index Strike Interval", "100" ) );
Add_Symbols_IB = ParamTrigger( "Add Option Symbol", "Click here" );
_SECTION_END();

Index_LTP = Get_LTP( Index_Symbol_IB );

if( Add_Symbols_IB  AND Index_LTP > 0)
{
Add_Option_Ticker_IB( No_Of_OTM_Para, No_Of_ITM_Para,  Index_Symbol_IB,Select_Expiry, Underlying_Symbol_Para, Expiry_Date,  Strike_Interval_Para, "CE" );
Add_Option_Ticker_IB(  No_Of_OTM_Para, No_Of_ITM_Para, Index_Symbol_IB, Select_Expiry, Underlying_Symbol_Para, Expiry_Date, Strike_Interval_Para, "PE" );
}
4 Likes

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