mirror of
https://github.com/wassname/options_backtester.git
synced 2026-07-16 11:20:21 +08:00
57 KiB
57 KiB
In [3]:
from backtester import Backtest
from backtester.strategy import Strategy, StrategyLeg
from backtester.enums import Type, Direction, Stock
from backtester.datahandler import HistoricalOptionsData, TiingoData
# Cleaned up data
options_data = HistoricalOptionsData(
"/Users/lambdaclass/options_backtester_copy/backtester/examples/options_data_clean_v2.h5",
key="/SPX",
where='quotedate >= "2014-12-15" & quotedate <= "2015-03-15"')
options_data._data = options_data._data[(options_data._data['optionroot'] == "SPX160617P00650000")
| (options_data._data['optionroot'] == "SPX160617C00650000")
| (options_data._data['optionroot'] == "SPX160617P00700000")
| (options_data._data['optionroot'] == "SPX160617C00700000")]
schema = options_data.schema
options_data._data.at[5857589, 'ask'] = 100 #SPX6500 call 2014-12-15
options_data._data.at[5857589, 'bid'] = 0.5 #SPX6500 call 2014-12-15
options_data._data.at[5857590, 'ask'] = 10 #SPX7000 call 2014-12-15
options_data._data.at[5857590, 'bid'] = 0.5 #SPX7000 call 2014-12-15
options_data._data.at[6045133, 'ask'] = 1 #SPX6500 put 2015-02-02
options_data._data.at[6045133, 'bid'] = 0.5
options_data._data.at[6045059, 'bid'] = 1 #SPX6500 call 2015-02-02
options_data._data.at[6045060, 'ask'] = 1
options_data._data.at[6151669, 'bid'] = 100 #SPX6500 call 2015-03-02
options_data._data.at[6151743, 'bid'] = 100
options_schema = options_data.schema
put_otm = Strategy(options_schema)
leg_1 = StrategyLeg("leg_1", options_schema, option_type=Type.PUT, direction=Direction.BUY)
leg_1.entry_filter = (options_schema.underlying == "SPX") & (options_schema.dte >= 60)
leg_1.exit_filter = (options_schema.dte <= 30)
leg_2 = StrategyLeg("leg_2", options_schema, option_type=Type.CALL, direction=Direction.BUY)
leg_2.entry_filter = (options_schema.underlying == "SPX") & (options_schema.dte >= 60)
leg_2.exit_filter = (options_schema.dte <= 30)
put_otm.add_legs([leg_1, leg_2])
options_data.columns = [
'underlying', 'underlying_last', 'optionroot', 'type', 'expiration', 'quotedate', 'strike', 'last', 'bid', 'ask',
'volume', 'openinterest', 'impliedvol', 'delta', 'gamma', 'theta', 'vega', 'optionalias', 'dte'
]
options_data.quotedate = options_data.quotedate.dt.tz_localize(None)
options_data._data.quotedate = options_data._data.quotedate.dt.tz_localize(None)
allocation = {'cash': 0, 'stocks': 97, 'options': 3}
bt = Backtest(allocation=allocation)
#asset_data = HistoricalAssetData('/Users/lambdaclass/options_backtester_copy/data/ivy_5assets.csv')
asset_data = TiingoData('/Users/lambdaclass/options_backtester_copy/data/ivy_5assets.csv')
asset_data._data.date = asset_data._data.date.dt.tz_localize(None)
asset_data.start_date = min(asset_data._data.date)
asset_data.end_date = max(asset_data._data.date)
VTI = Stock("VTI", 0.2)
VEU = Stock("VEU", 0.2)
BND = Stock("BND", 0.2)
VNQ = Stock("VNQ", 0.2)
DBC = Stock("DBC", 0.2)
bt.stocks = [VTI, VEU, BND, VNQ, DBC]
asset_data._data = asset_data._data.query('date >= "2014-12-15" & date <= "2015-03-15"')
bt.options_data = options_data
bt._options_strategy = put_otm
bt.stocks_data = asset_data
bt.stocks_data.start_date = min(bt.stocks_data['date'])
bt.stocks_data.end_date = max(bt.stocks_data['date'])
bt.current_cash = 1_000_000
bt.run(rebalance_freq=1)
bt.balance
# Exit thresholds: we exit if the loss or profit on the investment on an entry is greater than 25%.
#put_otm.exit_thresholds = (0.25, 0.25)Out [3]:
0% [██████████████████████████████] 100% | ETA: 00:00:00 Total time elapsed: 00:00:00
| total capital | cash | VTI | VEU | BND | VNQ | DBC | options qty | calls capital | puts capital | stocks qty | options capital | stocks capital | % change | accumulated return | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 2014-12-14 | 1000000.000000 | 1000000.000000 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 2015-01-02 | 964109.713884 | -20071.346493 | 201294.434010 | 199500.085077 | 194650.276734 | 202603.740636 | 186132.523921 | 2.0 | 0.0 | 80.0 | 23291.0 | 80.0 | 984181.060377 | -0.035890 | 0.964110 |
| 2015-01-05 | 955268.364042 | -20071.346493 | 197835.636144 | 195098.090912 | 195215.504398 | 203712.615403 | 183477.863678 | 2.0 | 0.0 | 150.0 | 23291.0 | 150.0 | 975339.710535 | -0.009170 | 0.955268 |
| 2015-01-06 | 952427.771297 | -20071.346493 | 195897.188989 | 193345.840807 | 195780.732062 | 205733.231644 | 181742.124289 | 2.0 | 0.0 | 160.0 | 23291.0 | 160.0 | 972499.117790 | -0.002974 | 0.952428 |
| 2015-01-07 | 959050.963830 | -20071.346493 | 198234.728206 | 195482.731179 | 195898.487825 | 208887.364312 | 180618.998802 | 2.0 | 0.0 | 130.0 | 23291.0 | 130.0 | 979122.310323 | 0.006954 | 0.959051 |
| 2015-01-08 | 966480.074540 | -20071.346493 | 201731.534839 | 198217.950854 | 195592.322840 | 209675.897479 | 181333.715021 | 2.0 | 0.0 | 330.0 | 23291.0 | 330.0 | 986551.421034 | 0.007746 | 0.966480 |
| 2015-01-09 | 963818.481599 | -20071.346493 | 200059.149058 | 197106.767861 | 195922.038978 | 209774.464125 | 181027.408070 | 2.0 | 0.0 | 330.0 | 23291.0 | 330.0 | 983889.828092 | -0.002754 | 0.963818 |
| 2015-01-12 | 959459.914778 | -20071.346493 | 198633.820267 | 196294.749519 | 196181.101657 | 211376.172121 | 177045.417706 | 2.0 | 0.0 | 330.0 | 23291.0 | 330.0 | 979531.261271 | -0.004522 | 0.959460 |
| 2015-01-13 | 959225.606153 | -20071.346493 | 198177.715054 | 197363.194705 | 196369.510878 | 211055.830522 | 176330.701487 | 2.0 | 0.0 | 150.0 | 23291.0 | 150.0 | 979296.952646 | -0.000244 | 0.959226 |
| 2015-01-14 | 961583.974613 | -20071.346493 | 197094.465173 | 196593.914172 | 196816.982779 | 212879.313471 | 178270.645511 | 2.0 | 0.0 | 150.0 | 23291.0 | 150.0 | 981655.321106 | 0.002459 | 0.961584 |
| 2015-01-15 | 959741.680990 | -20071.346493 | 195099.004866 | 197833.310587 | 197688.375427 | 213372.146701 | 175820.189902 | 2.0 | 0.0 | 170.0 | 23291.0 | 170.0 | 979813.027483 | -0.001916 | 0.959742 |
| 2015-01-16 | 969134.437588 | -20071.346493 | 197683.411030 | 200012.938766 | 197099.596611 | 215220.271311 | 179189.566364 | 2.0 | 0.0 | 270.0 | 23291.0 | 270.0 | 989205.784081 | 0.009787 | 0.969134 |
| 2015-01-20 | 965270.691803 | -20071.346493 | 197968.666832 | 200226.627803 | 196887.636237 | 213519.996669 | 176739.110755 | 2.0 | 0.0 | 130.0 | 23291.0 | 130.0 | 985342.038296 | -0.003987 | 0.965271 |
| 2015-01-21 | 968728.294063 | -20071.346493 | 198861.872874 | 202192.566945 | 196510.817794 | 213372.146701 | 177862.236243 | 2.0 | 0.0 | 300.0 | 23291.0 | 300.0 | 988799.640556 | 0.003582 | 0.968728 |
| 2015-01-22 | 976840.790089 | -20071.346493 | 201883.569910 | 203645.652397 | 196557.920099 | 217166.962568 | 177658.031608 | 2.0 | 0.0 | 300.0 | 23291.0 | 300.0 | 996912.136582 | 0.008374 | 0.976841 |
| 2015-01-23 | 973628.635153 | -20071.346493 | 200971.359484 | 202448.993789 | 197193.801221 | 216550.921031 | 176534.906121 | 2.0 | 0.0 | 300.0 | 23291.0 | 300.0 | 993699.981647 | -0.003288 | 0.973629 |
| 2015-01-26 | 977406.124705 | -20071.346493 | 201807.552374 | 204414.932931 | 196958.289695 | 218374.403980 | 175922.292219 | 2.0 | 0.0 | 60.0 | 23291.0 | 60.0 | 997477.471199 | 0.003880 | 0.977406 |
| 2015-01-27 | 976074.707092 | -20071.346493 | 199451.008774 | 204329.457316 | 197076.045458 | 218448.328964 | 176841.213073 | 2.0 | 0.0 | 280.0 | 23291.0 | 280.0 | 996146.053585 | -0.001362 | 0.976075 |
| 2015-01-28 | 967255.507918 | -20071.346493 | 196752.386263 | 201508.762026 | 197782.580038 | 216994.470937 | 174288.655147 | 2.0 | 0.0 | 280.0 | 23291.0 | 280.0 | 987326.854411 | -0.009035 | 0.967256 |
| 2015-01-29 | 970837.993659 | -20071.346493 | 198633.820267 | 203474.701168 | 197570.619664 | 217758.362443 | 173471.836611 | 2.0 | 0.0 | 280.0 | 23291.0 | 280.0 | 990909.340152 | 0.003704 | 0.970838 |
| 2015-01-30 | 965676.063416 | -20071.346493 | 195935.197757 | 200226.627803 | 198653.972686 | 213273.580055 | 177658.031608 | 2.0 | 0.0 | 280.0 | 23291.0 | 280.0 | 985747.409909 | -0.005317 | 0.965676 |
| 2015-02-02 | 943421.397474 | 301.521937 | 188578.258444 | 188632.490894 | 188611.975949 | 188637.365685 | 188659.784565 | 66.0 | 200.0 | 13220.0 | 22863.0 | 13420.0 | 943119.875537 | -0.023046 | 0.943421 |
| 2015-02-03 | 955551.600831 | 301.521937 | 191472.227497 | 191572.424409 | 188028.732446 | 190276.360406 | 193900.334136 | 66.0 | 0.0 | 7320.0 | 22863.0 | 7320.0 | 955250.078893 | 0.012858 | 0.955552 |
| 2015-02-04 | 948335.526279 | 301.521937 | 190802.997153 | 190022.999989 | 188096.029774 | 189489.642940 | 189622.334486 | 66.0 | 0.0 | 8600.0 | 22863.0 | 8600.0 | 948034.004342 | -0.007552 | 0.948336 |
| 2015-02-05 | 957213.410059 | 301.521937 | 192901.124717 | 192367.001035 | 187692.245810 | 191762.382285 | 192189.134275 | 66.0 | 0.0 | 7000.0 | 22863.0 | 7000.0 | 956911.888122 | 0.009362 | 0.957213 |
| 2015-02-06 | 947842.787062 | 301.521937 | 192322.330906 | 189824.355833 | 186480.893919 | 186189.800236 | 192723.884231 | 66.0 | 0.0 | 8290.0 | 22863.0 | 8290.0 | 947541.265125 | -0.009789 | 0.947843 |
| 2015-02-09 | 946914.291746 | 301.521937 | 191363.703658 | 189307.881026 | 186413.596591 | 184878.604460 | 194648.984074 | 66.0 | 0.0 | 13090.0 | 22863.0 | 13090.0 | 946612.769809 | -0.000980 | 0.946914 |
| 2015-02-10 | 947853.287460 | 301.521937 | 193317.132769 | 190499.745964 | 186077.109955 | 185468.642559 | 192189.134275 | 66.0 | 0.0 | 8290.0 | 22863.0 | 8290.0 | 947551.765522 | 0.000992 | 0.947853 |
| 2015-02-11 | 944827.414097 | 301.521937 | 193407.569301 | 189427.067520 | 186077.109955 | 184922.310985 | 190691.834398 | 66.0 | 0.0 | 7970.0 | 22863.0 | 7970.0 | 944525.892160 | -0.003192 | 0.944827 |
| 2015-02-12 | 955013.543436 | 301.521937 | 195270.561880 | 192565.645191 | 186166.839725 | 187129.490542 | 193579.484162 | 66.0 | 0.0 | 6370.0 | 22863.0 | 6370.0 | 954712.021499 | 0.010781 | 0.955014 |
| 2015-02-13 | 957753.923767 | 301.521937 | 196193.014516 | 193916.425454 | 185740.623318 | 185883.854555 | 195718.483987 | 66.0 | 0.0 | 5730.0 | 22863.0 | 5730.0 | 957452.401830 | 0.002869 | 0.957754 |
| 2015-02-17 | 957485.643655 | 301.521937 | 196500.498727 | 194432.900261 | 185000.352718 | 185424.936033 | 195825.433978 | 66.0 | 0.0 | 5730.0 | 22863.0 | 5730.0 | 957184.121717 | -0.000280 | 0.957486 |
| 2015-02-18 | 958278.198260 | 301.521937 | 196609.022567 | 195227.476886 | 185516.298894 | 187151.343805 | 193472.534171 | 66.0 | 0.0 | 5730.0 | 22863.0 | 5730.0 | 957976.676323 | 0.000828 | 0.958278 |
| 2015-02-19 | 953245.293590 | 301.521937 | 196554.760647 | 195068.561561 | 185359.271797 | 183130.343425 | 192830.834222 | 66.0 | 0.0 | 6050.0 | 22863.0 | 6050.0 | 952943.771652 | -0.005252 | 0.953245 |
| 2015-02-20 | 957163.017112 | 301.521937 | 197658.086349 | 196816.630137 | 185404.136682 | 184900.457723 | 192082.184284 | 66.0 | 0.0 | 5090.0 | 22863.0 | 5090.0 | 956861.495175 | 0.004110 | 0.957163 |
| 2015-02-23 | 956735.682530 | 301.521937 | 197639.999042 | 195823.409355 | 185785.488203 | 186386.479602 | 190798.784389 | 66.0 | 0.0 | 5180.0 | 22863.0 | 5180.0 | 956434.160592 | -0.000446 | 0.956736 |
| 2015-02-24 | 955816.407980 | 301.521937 | 198092.181707 | 197253.647281 | 186548.191246 | 182715.131429 | 190905.734380 | 66.0 | 0.0 | 3460.0 | 22863.0 | 3460.0 | 955514.886043 | -0.000961 | 0.955816 |
| 2015-02-25 | 958974.061077 | 301.521937 | 198019.832480 | 197372.833775 | 186727.650785 | 182758.837955 | 193793.384145 | 66.0 | 0.0 | 3250.0 | 22863.0 | 3250.0 | 958672.539140 | 0.003304 | 0.958974 |
| 2015-02-26 | 954301.589814 | 301.521937 | 197893.221334 | 196935.816631 | 186211.704609 | 180770.191027 | 192189.134275 | 66.0 | 0.0 | 3260.0 | 22863.0 | 3260.0 | 954000.067877 | -0.004872 | 0.954302 |
| 2015-02-27 | 957392.131534 | 301.521937 | 197187.816378 | 197015.274294 | 186368.731706 | 182190.653118 | 194328.134101 | 66.0 | 0.0 | 3240.0 | 22863.0 | 3240.0 | 957090.609597 | 0.003239 | 0.957392 |
| 2015-03-02 | 999472.405291 | 31660.297797 | 193519.352851 | 193583.644762 | 193537.947483 | 193570.626312 | 193600.536086 | 65.0 | 10000.0 | 11600.0 | 23108.0 | 21600.0 | 967812.107494 | 0.043953 | 0.999472 |
| 2015-03-03 | 998093.629404 | 31660.297797 | 192725.445396 | 192413.247877 | 193303.781787 | 193201.260776 | 194789.595771 | 65.0 | 0.0 | 2240.0 | 23108.0 | 2240.0 | 966433.331607 | -0.001380 | 0.998094 |
| 2015-03-04 | 992921.717475 | 31660.297797 | 191949.180329 | 191593.970058 | 193303.781787 | 191354.433095 | 193060.054411 | 65.0 | 0.0 | 2245.0 | 23108.0 | 2245.0 | 961261.419679 | -0.005182 | 0.992922 |
| 2015-03-05 | 993214.121335 | 31660.297797 | 192249.100923 | 191671.996517 | 193420.864635 | 191908.481399 | 192303.380065 | 65.0 | 0.0 | 2045.0 | 23108.0 | 2045.0 | 961553.823539 | 0.000294 | 0.993214 |
| 2015-03-06 | 978101.577405 | 31660.297797 | 189585.100350 | 189097.123370 | 192296.869296 | 185536.925900 | 189925.260694 | 65.0 | 0.0 | 2045.0 | 23108.0 | 2045.0 | 946441.279609 | -0.015216 | 0.978102 |
| 2015-03-09 | 980056.445546 | 31660.297797 | 190290.795866 | 189175.149829 | 192531.034991 | 187014.388045 | 189384.779019 | 65.0 | 0.0 | 4805.0 | 23108.0 | 4805.0 | 948396.147749 | 0.001999 | 0.980056 |
| 2015-03-10 | 971230.320507 | 31660.297797 | 187397.444251 | 185117.773961 | 193280.365217 | 186875.875968 | 186898.563312 | 65.0 | 0.0 | 2565.0 | 23108.0 | 2565.0 | 939570.022710 | -0.009006 | 0.971230 |
| 2015-03-11 | 972980.580181 | 31660.297797 | 187256.305148 | 185663.959174 | 193491.114344 | 187037.473391 | 187871.430328 | 65.0 | 0.0 | 1925.0 | 23108.0 | 1925.0 | 941320.282384 | 0.001802 | 0.972981 |
| 2015-03-12 | 979925.481459 | 31660.297797 | 189602.742738 | 187731.660338 | 193631.613761 | 190292.507178 | 187006.659647 | 65.0 | 0.0 | 645.0 | 23108.0 | 645.0 | 948265.183662 | 0.007138 | 0.979925 |
In [3]:
bt.trade_logOut [3]:
| leg_1 | leg_2 | totals | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| contract | underlying | expiration | type | strike | cost | order | contract | underlying | expiration | type | strike | cost | order | cost | qty | date | |
| 0 | SPX160617P00650000 | SPX | 2016-06-17 | put | 650.0 | 390.0 | Order.BTO | SPX160617C00650000 | SPX | 2016-06-17 | call | 650.0 | 10000.0 | Order.BTO | 10390.0 | 2.0 | 2014-12-15 |
| 1 | SPX160617P00700000 | SPX | 2016-06-17 | put | 700.0 | 350.0 | Order.BTO | SPX160617C00700000 | SPX | 2016-06-17 | call | 700.0 | 100.0 | Order.BTO | 450.0 | 64.0 | 2015-02-02 |