def test_duplicate_idx_in_struct(): with pytest.raises(ValueError): op.get(os.path.join(os.path.dirname(__file__), 'test_data', 'daily'), start=date(2016, 1, 5), end=date(2016, 1, 6), struct=invalid_struct, prompt=False)
def test_duplicate_idx_in_struct(mock_file_dir, invalid_struct): with pytest.raises(ValueError): op.get(mock_file_dir, start=date(2016, 1, 5), end=date(2016, 1, 6), struct=invalid_struct, prompt=False)
def test_invalid_fields(): with pytest.raises(ValueError): op.get(os.path.join(os.path.dirname(__file__), 'test_data', 'test_dod_a.csv'), start=date(2016, 1, 1), end=date(2016, 12, 31), struct=invalid_fields)
def test_invalid_idx(mock_file_dir, invalid_idx): with pytest.raises(ValueError): op.get(mock_file_dir, start=date(2016, 1, 1), end=date(2016, 12, 31), struct=invalid_idx, prompt=False)
def test_valid_fields(mock_daily_file, cboe_struct): try: op.get(mock_daily_file, start=date(2016, 1, 4), end=date(2016, 1, 5), struct=cboe_struct, prompt=False) except ValueError: pytest.fail('ValueError raised')
def test_valid_fields(): try: op.get(os.path.join(os.path.dirname(__file__), 'test_data', 'test_dod_a.csv'), start=date(2016, 1, 1), end=date(2016, 12, 31), struct=valid_fields, prompt=False) except ValueError: pytest.fail('ValueError raised')
def test_invalid_start_end_fields(mock_file_dir, invalid_fields): start = date(2016, 1, 1) end = date(2015, 1, 1) with pytest.raises(ValueError): op.get(mock_file_dir, start=start, end=end, struct=invalid_fields, prompt=False)
def test_invalid_start_end_fields(): start = date(2016, 1, 1) end = date(2015, 1, 1) with pytest.raises(ValueError): op.get(os.path.join(os.path.dirname(__file__), 'test_data', 'test_dod_a.csv'), start=start, end=end, struct=invalid_fields, prompt=False)
def data_dod_greeks(): return op.get(os.path.join(os.path.dirname(__file__), 'test_data', 'test_dod_a_daily.csv'), start=date(2016, 1, 1), end=date(2016, 1, 5), struct=dod_struct_with_opt_sym_greeks, prompt=False)
def data_dod_underlying(): return op.get(os.path.join(os.path.dirname(__file__), 'test_data', 'test_dod_a_daily.csv'), start=date(2016, 1, 1), end=date(2016, 1, 5), struct=dod_struct_underlying, prompt=False)
def data_hod_sym(): return op.get(os.path.join(os.path.dirname(__file__), 'test_data', 'test_hod_vxx_daily.csv'), start=date(2016, 12, 1), end=date(2016, 12, 1), struct=hod_struct_with_sym, prompt=False)
def run_strat(): # fetch the option chains from our data source d = op.get('data/VXX.csv', start=date(2016, 12, 1), end=date(2016, 12, 31), struct=vxx_struct, prompt=False) os = op.option_strategy.Vertical(option_type=op.OptionType.CALL, width=2) filters = [ op.filters.EntrySpreadPrice(ideal=1.0, lower=0.9, upper=1.10), op.filters.EntryDaysToExpiration(ideal=47, lower=40, upper=52), op.filters.EntryDayOfWeek(ideal=4), op.filters.ExitDaysToExpiration(ideal=1) ] # construct our strategy with our defined filter rules strategy = op.Strategy('Weekly Verticals', os, filters) # Create an instance of Optopsy with strategy settings, with default # initial capital of $10000 backtest = op.Optopsy(strategy, d) # Run over everything once backtest.run(progress_bar=False)
def run_strat(): # fetch the pre-formatted option spread data generated by option data = op.get( '../data/A.csv', start=date( 2016, 1, 1 ), # using a date function here to avoid entering the wrong date format end=date(2016, 12, 31), struct=struct)
def test_invalid_idx(): invalid_idx = (('symbol', -1), ('quote_date', -2)) with pytest.raises(ValueError): data = op.get(os.path.join(os.path.dirname(__file__), 'test_data', 'test_dod_a.csv'), start=date(2016, 1, 1), end=date(2016, 12, 31), struct=invalid_idx)
def test_invalid_start_end_fields(): start = date(2016, 1, 1) end = date(2015, 1, 1) invalid_fields = (('symbol', -1), ('invalid', -1)) with pytest.raises(ValueError): data = op.get(os.path.join(os.path.dirname(__file__), 'test_data', 'test_dod_a.csv'), start=start, end=end, struct=invalid_fields)
def start(): """ Main function of program. """ # program timer program_starts = time.time() # Analyse VXX vertical spreads option_chains = op.get("VXX", start="2016-01-29", end="2016-03-12") option_strategy = op.OptionStrategies.iron_condor(option_chains, op.Period.SEVEN_WEEKS, 5, 2, 2) program_ends = time.time() print("The simulation ran for {0} seconds.".format(round(program_ends - program_starts, 2)))
def test_valid_fields(): valid_fields = (('symbol', 0), ('underlying_price', 1), ('option_type', 4), ('expiration', 5), ('quote_date', 6), ('strike', 7), ('bid', 9), ('ask', 10), ('volume', 11), ('oi', 12), ('iv', 14), ('delta', 17), ('gamma', 18), ('theta', 19), ('vega', 20)) try: data = op.get(os.path.join(os.path.dirname(__file__), 'test_data', 'test_dod_a.csv'), start=date(2016, 1, 1), end=date(2016, 12, 31), struct=valid_fields) except ValueError: pytest.fail('ValueError raised')
def test_data_dod_import(): cols = list(zip(*dod_struct))[0] test_df = pd.DataFrame(dod_test_data, columns=cols) test_df['expiration'] = pd.to_datetime(test_df.expiration, format='%Y-%m-%d') test_df['quote_date'] = pd.to_datetime(test_df.quote_date, format='%Y-%m-%d') test_df.set_index(['quote_date'], inplace=True) data = op.get(os.path.join(os.path.dirname(__file__), 'test_data', 'test_dod_a.csv'), start=date(2016, 1, 1), end=date(2016, 12, 31), struct=dod_struct) assert test_df.equals(data)
def store_and_get_data(file_name): # absolute file path to our input file curr_file = os.path.abspath(os.path.dirname(__file__)) file = os.path.join(curr_file, "data", f"{file_name}.pkl") # check if we have a pickle store if os.path.isfile(file): print("pickle file found, retrieving...") return pd.read_pickle(file) else: print("no picked file found, retrieving csv data...") csv_file = os.path.join(curr_file, "data", f"{file_name}.csv") data = op.get(csv_file, SPX_FILE_STRUCT, prompt=False) print("storing to pickle file...") pd.to_pickle(data, file) return data
def test_data_cboe_import(): cols = list(zip(*cboe_struct))[0] test_df = pd.DataFrame(cboe_test_data, columns=cols) test_df['expiration'] = pd.to_datetime(test_df.expiration, infer_datetime_format=True, format='%Y-%m-%d') test_df['quote_date'] = pd.to_datetime(test_df.quote_date, infer_datetime_format=True, format='%Y-%m-%d') test_df.set_index('quote_date', inplace=True, drop=False) data = op.get(os.path.join(os.path.dirname(__file__), 'test_data', 'test_cboe_spx.csv'), start=date(2016, 1, 1), end=date(2016, 12, 31), struct=cboe_struct, prompt=False) pt.assert_frame_equal(test_df, data)
def test_data_import(mock_daily_file, cboe_struct): data = op.get(mock_daily_file, struct=cboe_struct, prompt=False) assert data.shape == (2, 15)
def test_data_import_bulk(mock_daily_dir, cboe_struct): data = op.get(mock_daily_dir, struct=cboe_struct, prompt=False) assert data.shape == (6, 13)
def test_invalid_fields(mock_file_dir, invalid_fields): with pytest.raises(ValueError): op.get(mock_file_dir, start=date(2016, 1, 1), end=date(2016, 12, 31), struct=invalid_fields)
def test_invalid_fields(mock_file_dir, invalid_fields): with pytest.raises(ValueError): op.get(mock_file_dir, struct=invalid_fields)
def test_invalid_idx(mock_file_dir, invalid_idx): with pytest.raises(ValueError): op.get(mock_file_dir, struct=invalid_idx, prompt=False)
def test_invalid_path_data_import(mock_daily_dir, cboe_struct): with pytest.raises(ValueError): op.get(mock_daily_dir, struct=cboe_struct, prompt=False)
def test_valid_fields(mock_daily_file, cboe_struct): try: op.get(mock_daily_file, struct=cboe_struct, prompt=False) except ValueError: pytest.fail("ValueError raised")