Option scanner with the help of chatgpt

Discussion in 'Artificial Intelligence' started by cesfx, Apr 30, 2023.

  1. Dismiss Notice
  1. cesfx

    cesfx

    I have basic skills on excel, learning as I go, for what I need.

    This is above my head, so I asked chatgpt for help.

    Before this query, I asked info on how to create an xls file with imported data to analyse volatility and term structure, where could I get the data from, I played a bit with the googlefinance function... and finally found out, always through the AI info, that one of my option to obtain what I wanted, was to extrapolate data from a number of sources and import them on a custom xls file. One of these sources is IB, and I have realtime data with them.
    First code is an "example code", so I asked for full code and for ES and SPX chain data.

    Any human opinion will be appreciated.



    ------------------------------------------------------------------------


    Me: can you help me create a calendar and diagonal spread scanner, using excel and interactive brokers tws api

    ChatGPT
    Sure, I can help you create a calendar and diagonal spread scanner using Excel and Interactive Brokers TWS API. Here's how you can do it:

    1. First, you need to download and install the IB TWS API software from the Interactive Brokers website.

    2. Open a new Excel workbook and enable the "Developer" tab by going to File > Options > Customize Ribbon and checking the box next to "Developer" in the right-hand column.

    3. In the "Developer" tab, click on "Visual Basic" to open the VBA editor.

    4. In the VBA editor, click on "Tools" > "References" and check the box next to "TWS API" to enable the API.

    5. Next, you'll need to write VBA code to connect to the TWS API and retrieve option chain data for the ES futures. Here's an example code that you can use as a starting point:
    [EXAMPLE CODE REMOVED]


    This code will connect to the TWS API, request option chain data for the ES futures, and display the data in the "MktDepth" and "OptionChains" tabs of the TWS API window.

    1. Once you have the option chain data in Excel, you can use Excel's built-in functions to filter and analyze the data. For example, you can use the "FILTER" function to only display options with a specific strike price, expiration date, or implied volatility.

    2. To scan for calendar and diagonal spreads, you can use VBA code to loop through the option chain data and identify options that meet your criteria. For example, you might look for options that have a specific spread between the front-month and back-month implied volatility, or options that have a specific price difference between the long and short legs of the spread.

    3. Finally, you can use Excel's conditional formatting feature to highlight any spreads that meet your criteria, making it easy to quickly identify potential trading opportunities.
    I hope this helps get you started with creating a calendar and diagonal spread scanner using Excel and the Interactive Brokers TWS API. If you have any further questions, feel free to ask!


    (after some discussions....)

    Me: Can you write a VBA code to connect to the TWS API and retrieve option chain data for the ES futures and SPX?

    ChatGPT: I apologize for the confusion earlier. The code I provided before was incorrect and incomplete. Here is the correct code for connecting to the TWS API and retrieving option chain data for ES futures and SPX index options:

    vbnet
    ' Reference the TWS API COM library
    Private WithEvents Tws1 As AxTWSLib.AxTws

    Private Sub ConnectToTws()
    Set Tws1 = New AxTWSLib.AxTws
    Tws1.CreateControl
    Tws1.connect ("127.0.0.1"), 7497, 0
    End Sub

    Private Sub Tws1_tickPrice(ByVal Id As Long, ByVal TickType As Long, ByVal Price As Double, ByVal CanAutoExecute As Boolean)
    ' Handle tick price events here
    End Sub

    Private Sub Tws1_tickSize(ByVal Id As Long, ByVal TickType As Long, ByVal Size As Long)
    ' Handle tick size events here
    End Sub

    Private Sub Tws1_tickString(ByVal Id As Long, ByVal TickType As Long, ByVal Value As String)
    ' Handle tick string events here
    End Sub

    Private Sub Tws1_tickGeneric(ByVal Id As Long, ByVal TickType As Long, ByVal Value As Double)
    ' Handle tick generic events here
    End Sub

    Private Sub Tws1_tickOptionComputation(ByVal Id As Long, ByVal TickType As Long, ByVal ImpliedVolatility As Double, ByVal Delta As Double, ByVal OptPrice As Double, ByVal PvDividend As Double, ByVal Gamma As Double, ByVal Vega As Double, ByVal Theta As Double, ByVal UndPrice As Double)
    ' Handle tick option computation events here
    End Sub

    Private Sub Tws1_tickSnapshotEnd(ByVal Id As Long)
    ' Handle tick snapshot end events here
    End Sub

    Private Sub Tws1_tickReqParams(ByVal Id As Long, ByVal MinTick As Double, ByVal BboExchange As String, ByVal SnapshotPermissions As Long)
    ' Handle tick request parameters events here
    End Sub

    Private Sub Tws1_error(ByVal Id As Long, ByVal ErrorCode As Long, ByVal ErrorMsg As String)
    ' Handle error events here
    End Sub

    Private Sub Tws1_contractDetails(ByVal ReqId As Long, ByVal ContractDetails As TWSLib.IContractDetails)
    ' Handle contract details events here
    End Sub

    Private Sub Tws1_contractDetailsEnd(ByVal ReqId As Long)
    ' Handle contract details end events here
    End Sub

    Private Sub Tws1_updateMktDepth(ByVal Id As Long, ByVal Position As Long, ByVal Operation As Long, ByVal Side As Long, ByVal Price As Double, ByVal Size As Long)
    ' Handle market depth events here
    End Sub

    Private Sub Tws1_updateMktDepthL2(ByVal Id As Long, ByVal Position As Long, ByVal MarketMaker As String, ByVal Operation As Long, ByVal Side As Long, ByVal Price As Double, ByVal Size As Long, ByVal IsSmartDepth As Boolean)
    ' Handle market depth L2 events here
    End Sub

    Private Sub Tws1_updateNewsBulletin(ByVal NewsMsgId As Integer, ByVal NewsMsgType As Integer, ByVal NewsMessage As String, ByVal OriginExch As String)
    ' Handle news bulletin events here
    End Sub

    Private Sub Tws1_managedAccounts(ByVal AccountsList As String)
    ' Handle managed accounts events here
    End Sub

    Private Sub Tws1_nextValidId(ByVal Id As Long)
    ' Handle next valid ID events here
    End Sub

    Private Sub Tws1_orderStatus(ByVal OrderId As Long, ByVal Status As String, ByVal Filled As Double, ByVal Remaining As Double, ByVal AvgFillPrice As Double, ByVal PermId As Long, ByVal ParentId As Long,



    Me: so this is a VBA code that I can use

    ChatGPT
    Yes, the code I provided is a VBA code that can be used to connect to the TWS API and retrieve option chain data for the ES futures and SPX. However, please note that it may require modification to fit your specific needs and account information.

    -------------------------------------------------------


    Now I will go back a few step, get the API set and connect real time data to xls... try to follow the steps and see what happens.
     
    Adam777, Sprout and BlueWaterSailor like this.
  2. I've been using ChatGPT to produce Python code for my analytics, and it's short-cutting the process by at least 10:1 - including the debugging, where I can just feed the code errors right back into it. Love it with much love. :)
     
    cesfx likes this.
  3. Sprout

    Sprout

    Along the lines of xls data processing;
     
  4. cesfx

    cesfx

    Interesting.

    On my simple task, I am getting errors on compiling excel but I think it might be on the API tws set-up side. It's because I am total noob. I wiill get there eventually.


    Today the bot was gave me some useless tips on TWS settings for other configurations.
    Like being able to see total delta of a selected group of positions, like watchlist total delta.
    Ai was giving me different way of doing it, I tried without success, then looked it up and apparently you can't do it on TWS... So I notified the bot, and it apologised for making an error.
     
  5. Sprout

    Sprout

    It can only give a valid answer if TWS codebase was part of the original training set - else you would have to fine tune.
     
    cesfx likes this.