2. Simple equity basket strategy
Create a simple basket strategy that maintains specific weightings for two different equity instruments.
In this example, you will create a simple basket strategy to hedge investment in Apple stock against market risk. The basket strategy will long Apple and short S&P 500 ETF.
In SigTech's Research environment, create a new notebook and run the following code blocks:
Note that for this tutorial we will need the pandas library:
# Import necessary libraries
import sigtech.framework as sig
import datetime as dtm
import pandas as pd
from uuid import uuid4
# Initalise the environment
sig.init()
As you'll need to use the start and end dates multiple times, store them as variables:
start_date = dtm.date(2016, 1, 1)
end_date = dtm.date(2020, 12, 31)
This time, you will define two
ReinvestmentStrategy
instances—one for the Apple stock and one for the S&P 500 ETF.apple_rs = sig.ReinvestmentStrategy(
start_date=start_date,
end_date=end_date,
currency='USD',
underlyer='1000045.SINGLE_STOCK.TRADABLE',
ticker=f'APPLE {str(uuid4())[:5]}'
)
spy_rs = sig.ReinvestmentStrategy(
start_date=start_date,
end_date=end_date,
currency='USD',
underlyer='SPY UP EQUITY',
ticker=f'SPY {str(uuid4())[:5]}'
)
The
BasketStrategy
building block is a strategy of strategies—it is a container for managing multiple sub-strategies. With the following code build a
BasketStrategy
to manage the two ReinvestmentStrategy
instances created above:my_bs = sig.BasketStrategy(
start_date=start_date,
end_date=end_date,
# Long apple, short SPX, equal weighting
constituent_names=[apple_rs.name, spy_rs.name],
weights=[1, -1],
# Rebalance the basket every month
rebalance_frequency='1M',
currency='USD',
ticker=f'BASKET {str(uuid4())[:5]}',
# Set the initial cash to be invested
initial_cash=1000,
)
From a simple plot it looks like this strategy has performed well.
my_bs.history().plot()
As you will see from running this code, the return of your basket strategy is much worse than just investing in the Apple stock alone:
# Compare the basket strategy's perfomance with that of the Apple stock alone
pd.concat({
apple_rs.name:apple_rs.history(),
my_bs.name:my_bs.history()
}, axis=1).dropna().plot()
Running the following two blocks of code will reveal deeper insights about the performance of both your basket strategy and the Apple stock. If you look at market risk instead of return, you can see that your basket strategy has partially hedged against market risk.
my_bs.plot.performance()
apple_rs.plot.performance()
Add an additional instrument and indicate whether to long or short it.
Last modified 1yr ago