Giter Site home page Giter Site logo

Comments (3)

pchaganti avatar pchaganti commented on May 23, 2024

Some testing code to get familiar with the lib. Not optimized just trying things out :)

import pandas as pd
import yfinance as yf
import time
import datetime
import optopsy as op
import tabulate as tb

ticker = 'SPY'
tickerData = yf.Ticker(ticker)

spy_df = pd.DataFrame()
opt = tickerData.option_chain(option_date)
opt_calls = pd.DataFrame(opt.calls)
opt_calls['type'] = 'call'
opt_puts = pd.DataFrame(opt.puts)
opt_puts['type'] = 'put'
spy_df = spy_df.append(opt_calls)
spy_df = spy_df.append(opt_puts)
spy_df['underlying'] = 'SPY'
spy_df['underlying_last'] = spotPrice
spy_df['expiration'] = datetime.datetime.strptime(option_date, "%Y-%m-%d")
spy_df['quotedate'] = datetime.datetime.today()

new_df = pd.DataFrame() 
new_df['underlying_symbol'] = spy_df['underlying']
new_df['underlying_price'] = spy_df['underlying_last']
new_df['option_type'] = spy_df['type']
new_df['expiration'] = spy_df['expiration']
new_df['quote_date'] = spy_df['quotedate']
new_df['strike'] = spy_df['strike']
new_df['bid'] = spy_df['bid']
new_df['ask'] = spy_df['ask']

print(new_df.head())
straddles = op.long_straddles(spx_data).round(2)
print(straddles)

Prints out the df with what I thought are the required columns from the sample code in the repo.

  underlying_symbol  underlying_price option_type expiration                 quote_date  strike     bid     ask
0               SPY        417.609985        call 2021-05-21 2021-04-26 18:53:01.308009   165.0  252.21  252.59
1               SPY        417.609985        call 2021-05-21 2021-04-26 18:53:01.308009   170.0  219.85  220.58
2               SPY        417.609985        call 2021-05-21 2021-04-26 18:53:01.308009   185.0  210.73  211.53
3               SPY        417.609985        call 2021-05-21 2021-04-26 18:53:01.308009   200.0  217.22  217.60
4               SPY        417.609985        call 2021-05-21 2021-04-26 18:53:01.308009   210.0  185.80  186.61

Ends in the following error

Traceback (most recent call last):
  File "test_straddles.py", line 90, in <module>
    run_strategy()
  File "test_straddles.py", line 71, in run_strategy
    straddles = op.long_straddles(new_df).round(2)
  File "/Users/prabhakar/Dev/hermes/lib/python3.8/site-packages/optopsy/strategies.py", line 117, in long_straddles
    return _straddles(data, [(Side.long, _puts), (Side.long, _calls)], **kwargs)
  File "/Users/prabhakar/Dev/hermes/lib/python3.8/site-packages/optopsy/strategies.py", line 43, in _straddles
    return _process_strategy(
  File "/Users/prabhakar/Dev/hermes/lib/python3.8/site-packages/optopsy/core.py", line 191, in _process_strategy
    _evaluate_all_options(
  File "/Users/prabhakar/Dev/hermes/lib/python3.8/site-packages/pandas/core/generic.py", line 5050, in pipe
    return com.pipe(self, func, *args, **kwargs)
  File "/Users/prabhakar/Dev/hermes/lib/python3.8/site-packages/pandas/core/common.py", line 471, in pipe
    return func(obj, *args, **kwargs)
  File "/Users/prabhakar/Dev/hermes/lib/python3.8/site-packages/optopsy/core.py", line 219, in _format_output
    return data.pipe(
  File "/Users/prabhakar/Dev/hermes/lib/python3.8/site-packages/pandas/core/generic.py", line 5050, in pipe
    return com.pipe(self, func, *args, **kwargs)
  File "/Users/prabhakar/Dev/hermes/lib/python3.8/site-packages/pandas/core/common.py", line 471, in pipe
    return func(obj, *args, **kwargs)
  File "/Users/prabhakar/Dev/hermes/lib/python3.8/site-packages/optopsy/core.py", line 67, in _group_by_intervals
    grouped_dataset = data.groupby(cols)["pct_change"].describe()
  File "/Users/prabhakar/Dev/hermes/lib/python3.8/site-packages/pandas/core/groupby/generic.py", line 682, in describe
    return result.unstack()
  File "/Users/prabhakar/Dev/hermes/lib/python3.8/site-packages/pandas/core/series.py", line 3903, in unstack
    return unstack(self, level, fill_value)
  File "/Users/prabhakar/Dev/hermes/lib/python3.8/site-packages/pandas/core/reshape/reshape.py", line 424, in unstack
    unstacker = _Unstacker(
  File "/Users/prabhakar/Dev/hermes/lib/python3.8/site-packages/pandas/core/reshape/reshape.py", line 92, in __init__
    self.index = index.remove_unused_levels()
AttributeError: 'Index' object has no attribute 'remove_unused_levels'

What am I doing wrong?

thanks!

from optopsy.

michaelchu avatar michaelchu commented on May 23, 2024

hi @pchaganti

Are you still having problems with this? One thing i noticed is that the quote date should be a formatted date like the expiration column with "YYYY-MM-DD".

Also, what is the date range for your test run? Perhaps the data is not sufficient for the date ranges you want to work with, and it cannot calculate the "pct_changes" with the data you supplied.

from optopsy.

arrtvv852 avatar arrtvv852 commented on May 23, 2024

hi @pchaganti

Are you still having problems with this? One thing i noticed is that the quote date should be a formatted date like the expiration column with "YYYY-MM-DD".

Also, what is the date range for your test run? Perhaps the data is not sufficient for the date ranges you want to work with, and it cannot calculate the "pct_changes" with the data you supplied.

I'm facing the same issue with the error massage "AttributeError: 'Index' object has no attribute 'remove_unused_levels'". I already check the datetime format of expiration & quote_date are correct and the same format as SPX sample data.

My date range is from 2019-10-01 to 2019-11-15 and the farest expiration date is up to 2021-12-31.

from optopsy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.