Lewuathe / COVID19-SIR

COVID-19 SIR model estimation
https://www.lewuathe.com/covid-19-dynamics-with-sir-model.html
Apache License 2.0
131 stars 75 forks source link

Constantly recieving "IndexError: single positional indexer is out-of-bounds" #21

Open astroducky opened 4 years ago

astroducky commented 4 years ago

Can you please provide an example of how the list of countries should be entered? I am constantly receiving "IndexError: single positional indexer is out-of-bounds"

sarldbru commented 4 years ago

Yeah, having the same problem...

sday5417 commented 4 years ago

having the same problem trying to run a zipline backtest. looked at every iloc function and still can't find error. below is the code with errors

%matplotlib notebook

import zipline from zipline.api import future_symbol, \ set_commission, set_slippage, schedule_function, \ date_rules, time_rules, continuous_future, order_target from datetime import datetime import pytz import matplotlib.pyplot as plt import matplotlib import pyfolio as pf import pandas as pd import numpy as np from zipline.finance.commission import PerTrade, PerContract from zipline.finance.slippage import FixedSlippage, VolatilityVolumeShare

We'll use this to find a future date, X months out.

from dateutil.relativedelta import relativedelta

settings

spread_months = 12 pos_per_side = 5 target_exposure_per_side = 1.5 initial_portfolio_millions = 1 volume_order_cap = 0.25

DataFame for storing and updating the data that we want to graph

dynamic_results = pd.DataFrame()

fig = plt.figure(figsize=(10, 6)) ax = fig.add_subplot(211) ax.set_title('Curve Trading') ax2 = fig.add_subplot(212) ax2.set_title('Drawdown')

def initialize(context): """ Friction Settings """ context.enable_commission = True context.enable_slippage = True

if context.enable_commission:
    comm_model = PerContract(cost=0.85, exchange_fee=1.5)
else:
    comm_model = PerTrade(cost=0.0)
set_commission(us_futures=comm_model)

if context.enable_slippage:
    slippage_model=VolatilityVolumeShare(volume_limit=0.3)
else:
    slippage_model=FixedSlippage(spread=0.0)      
set_slippage(us_futures=slippage_model)

"""
Markets to trade
""" 
most_liquid_commods = [
    'CL','HO','XB','NG','GC','LE','CN','SY','WC','SB', 'HG', 'CT', 'KC'
]

context.universe = [
    continuous_future(market, offset=0, roll='volume', adjustment='mul') 
    for market in most_liquid_commods               
    ]

schedule_function(weekly_trade, date_rules.week_start(), time_rules.market_close()) 

schedule_function(update_chart,date_rules.month_start(), time_rules.market_close())       

def update_chart(context,data):

This function continuously update the graph during the backtest

today = data.current_session.date()
pv = context.portfolio.portfolio_value
exp = context.portfolio.positions_exposure
dynamic_results.loc[today, 'PortfolioValue'] = pv

drawdown = (pv / dynamic_results['PortfolioValue'].max()) - 1
exposure = exp / pv
dynamic_results.loc[today, 'Drawdown'] = drawdown

if ax.lines:
    ax.lines[0].set_xdata(dynamic_results.index)
    ax.lines[0].set_ydata(dynamic_results.PortfolioValue)
    ax2.lines[0].set_xdata(dynamic_results.index)
    ax2.lines[0].set_ydata(dynamic_results.Drawdown)
else:
    ax.plot(dynamic_results.PortfolioValue)
    ax2.plot(dynamic_results.Drawdown)

ax.set_ylim(
    dynamic_results.PortfolioValue.min(),
    dynamic_results.PortfolioValue.max()
)
ax.set_xlim(
    dynamic_results.index.min(),
    dynamic_results.index.max()
)
ax2.set_ylim(
    dynamic_results.Drawdown.min(),
    dynamic_results.Drawdown.max()
)
ax2.set_xlim(
    dynamic_results.index.min(),
    dynamic_results.index.max()
)

fig.canvas.draw()

def weekly_trade(context, data):

Empty DataFrame to be filled in later.

carry_df = pd.DataFrame(index = context.universe)

for continuation in context.universe:
    # Get the chain
    chain = data.current_chain(continuation)

    # Transform the chain into dataframe
    df = pd.DataFrame(index = chain)
    for contract in chain:
        df.loc[contract, 'future'] = contract
        df.loc[contract, 'expiration_date'] = contract.expiration_date

    # Locate the contract closest to the target date.
    # X months out from the front contract.
    closest_expiration_date = df.iloc[0].expiration_date
    target_expiration_date = closest_expiration_date + relativedelta(months=+spread_months)
    df['days_to_target'] = abs(df.expiration_date - target_expiration_date) 
    target_contract = df.loc[df.days_to_target == df.days_to_target.min()]

    # Get prices for front contract and target contract
    prices = data.current(
        [
            df.index[0], 
            target_contract.index[0]
        ],
        'close'
    )

    # Check the exact day difference between the contracts
    days_to_front = int(
        (target_contract.expiration_date - closest_expiration_date)[0].days
    )

    # Calculate the annualized carry
    annualized_carry = (np.power(
                        (prices[0] / prices[1]), (365 / days_to_front ))
                        ) - 1

    carry_df.loc[continuation, 'front'] = df.iloc[0].future
    carry_df.loc[continuation, 'next'] = target_contract.index[0]
    carry_df.loc[continuation, 'carry'] = annualized_carry

# Sort on carry
carry_df.sort_values('carry', inplace=True, ascending=False)
carry_df.dropna(inplace=True)

new_portfolio = []
new_longs = []
new_shorts = []

# Contract Selection
for i in np.arange(0, pos_per_side): 
    j = -(i+1)

    # Buy top, short bottom
    long_contract = carry_df.iloc[i].next
    short_contract = carry_df.iloc[j].next

    new_longs.append(long_contract)
    new_shorts.append(short_contract)

# Get data for the new portfolio
new_portfolio = new_longs + new_shorts
hist = data.history(new_portfolio,  fields=['close','volume'], 
    frequency='1d', 
    bar_count=10,
    )

# Simple Equal Weighted
target_weight = (
    target_exposure_per_side  * context.portfolio.portfolio_value 
    ) / pos_per_side

# Trading
for contract in new_portfolio:
    # Slice history for contract
    h = hist.xs(contract, 2)

    # Equal weighted, with volume based cap.
    contracts_to_trade = target_weight / \
        contract.price_multiplier / \
        h.close[-1]

    # Position size cap
    contracts_cap = int(h['volume'].mean() * volume_order_cap)

    # Limit trade size to position size cap.
    contracts_to_trade = min(contracts_to_trade, contracts_cap)

    # Negative position for shorts
    if contract in new_shorts:
        contracts_to_trade *= -1

    # Execute
    order_target(contract, contracts_to_trade)

# Close any other open position
for pos in context.portfolio.positions:
    if pos not in new_portfolio:
        order_target(pos, 0.0)

start = datetime(2001, 1, 1, 8, 15, 12, 0, pytz.UTC) end = datetime(2018, 12, 30, 8, 15, 12, 0, pytz.UTC)

perf = zipline.run_algorithm( start=start, end=end, initialize=initialize, capital_base=initial_portfolio_millions * 1000000,
data_frequency = 'daily', bundle='norgatedata-tradingevolved-futures' )


IndexError Traceback (most recent call last)

in () 217 capital_base=initial_portfolio_millions * 1000000, 218 data_frequency = 'daily', --> 219 bundle='norgatedata-tradingevolved-futures' ) C:\Users\Scott\Anaconda3\envs\zipline35\lib\site-packages\zipline\utils\run_algo.py in run_algorithm(start, end, initialize, capital_base, handle_data, before_trading_start, analyze, data_frequency, data, bundle, bundle_timestamp, trading_calendar, metrics_set, default_extension, extensions, strict_extensions, environ, blotter) 429 local_namespace=False, 430 environ=environ, --> 431 blotter=blotter, 432 ) C:\Users\Scott\Anaconda3\envs\zipline35\lib\site-packages\zipline\utils\run_algo.py in _run(handle_data, initialize, before_trading_start, analyze, algofile, algotext, defines, data_frequency, capital_base, data, bundle, bundle_timestamp, start, end, output, trading_calendar, print_algo, metrics_set, local_namespace, environ, blotter) 228 ).run( 229 data, --> 230 overwrite_sim_params=False, 231 ) 232 C:\Users\Scott\Anaconda3\envs\zipline35\lib\site-packages\zipline\algorithm.py in run(self, data, overwrite_sim_params) 754 try: 755 perfs = [] --> 756 for perf in self.get_generator(): 757 perfs.append(perf) 758 C:\Users\Scott\Anaconda3\envs\zipline35\lib\site-packages\zipline\gens\tradesimulation.py in transform(self) 204 for dt, action in self.clock: 205 if action == BAR: --> 206 for capital_change_packet in every_bar(dt): 207 yield capital_change_packet 208 elif action == SESSION_START: C:\Users\Scott\Anaconda3\envs\zipline35\lib\site-packages\zipline\gens\tradesimulation.py in every_bar(dt_to_use, current_data, handle_data) 132 metrics_tracker.process_commission(commission) 133 --> 134 handle_data(algo, current_data, dt_to_use) 135 136 # grab any new orders from the blotter, then clear the list. C:\Users\Scott\Anaconda3\envs\zipline35\lib\site-packages\zipline\utils\events.py in handle_data(self, context, data, dt) 214 context, 215 data, --> 216 dt, 217 ) 218 C:\Users\Scott\Anaconda3\envs\zipline35\lib\site-packages\zipline\utils\events.py in handle_data(self, context, data, dt) 233 """ 234 if self.rule.should_trigger(dt): --> 235 self.callback(context, data) 236 237 in weekly_trade(context, data) 124 # Locate the contract closest to the target date. 125 # X months out from the front contract. --> 126 closest_expiration_date = df.iloc[1,7].expiration_date 127 target_expiration_date = closest_expiration_date + relativedelta(months=+spread_months) 128 df['days_to_target'] = abs(df.expiration_date - target_expiration_date) C:\Users\Scott\Anaconda3\envs\zipline35\lib\site-packages\pandas\core\indexing.py in __getitem__(self, key) 1365 except (KeyError, IndexError): 1366 pass -> 1367 return self._getitem_tuple(key) 1368 else: 1369 # we by definition only have the 0th axis C:\Users\Scott\Anaconda3\envs\zipline35\lib\site-packages\pandas\core\indexing.py in _getitem_tuple(self, tup) 1735 def _getitem_tuple(self, tup): 1736 -> 1737 self._has_valid_tuple(tup) 1738 try: 1739 return self._getitem_lowerdim(tup) C:\Users\Scott\Anaconda3\envs\zipline35\lib\site-packages\pandas\core\indexing.py in _has_valid_tuple(self, key) 202 if i >= self.obj.ndim: 203 raise IndexingError('Too many indexers') --> 204 if not self._has_valid_type(k, i): 205 raise ValueError("Location based indexing can only have " 206 "[{types}] types" C:\Users\Scott\Anaconda3\envs\zipline35\lib\site-packages\pandas\core\indexing.py in _has_valid_type(self, key, axis) 1670 return True 1671 elif is_integer(key): -> 1672 return self._is_valid_integer(key, axis) 1673 elif is_list_like_indexer(key): 1674 return self._is_valid_list_like(key, axis) C:\Users\Scott\Anaconda3\envs\zipline35\lib\site-packages\pandas\core\indexing.py in _is_valid_integer(self, key, axis) 1711 l = len(ax) 1712 if key >= l or key < -l: -> 1713 raise IndexError("single positional indexer is out-of-bounds") 1714 return True 1715 IndexError: single positional indexer is out-of-bounds