Strategy
The Strategy
class is the basis for all backtesting in the SigTech platform. Functionality embedded within the Strategy
class is also accessible via the classes inheriting from it.
Since all building blocks inherit from Strategy
, using the full functionality of the SigTech platform’s building blocks requires a familiarity with the base class.
This page is relevant to all users of the SigTech platform—learn how to:
Build and backtest a strategy: Plot the value of a strategy through time.
Name a strategy: Specify a name for a strategy via the ticker parameter.
Retrieve a strategy: Fetch strategy objects from your cache via the
sig.obj.get
API.Clone a strategy: Create a copy of a strategy and change its parameters.
Fix the parameters of a strategy: Set the initial cash held by a strategy, determine if that cash accrues interest, and decide whether or not transaction costs are included in the backtest.
Agenda
Environment
Set up your environment using the following three steps:
Import the relevant internal and external libraries
Configure the environment parameters
Initialise the environment
Learn more: setting up the environment.
Backtesting
Backtests are conducted by creating an instance of an object that inherits from the Strategy
class and calling build
on it.
The following code block cycles through some commonly used building blocks and checks that they inherit from Strategy
:
Once a strategy has been built, the details of the backtest are stored within it and are accessible for further analysis. This includes the value of the strategy and holdings through time, and lower level details, such as the generation and execution of orders.
Note: calling build
on a strategy that has already been built has no additional effect.
The following instance of a RollingFutureStrategy
object is an example:
Note: strategies generally require a currency
and start_date
. If an end_date
isn't used, the asofdate
for the configured environment is used.
Working through the build cycle:
Once a strategy has been built, calling history
retrieves a pd.Series
of its values or prices:
Note: this is the equivalent of calling history
on an instrument to return its value through time.
Tip: it is unnecessary to call build
and history
separately. Calling history
on an unbuilt strategy calls build
automatically.
Strategy tickers
When a strategy object is constructed you can assign a name
to it, using the ticker
parameter. Once constructed, it is added to the cache of the environment.
If a ticker
is not provided on construction, the strategy name
is determined using the strategy details:
Strategy type: such as
S
forStrategy
orBS
forBasketStrategy
.Currency.
A random, eight-digit, alphanumeric combination.
To create a Strategy
and display its automatically generated name:
To create a Strategy
called TEST STRATEGY
:
Note: strategy names are always written in upper case, withSTRATEGY
added to the end.
In Backtesting, we built a RollingFutureStrategy
called rfs
. But we did not assign a ticker
value. To display the automatically generated name of rfs
:
Cache
When a strategy object is created, its name is added to the cache. You can access the cache directly via the configured environment:
Any object that has been accessed is also added to the cache. We can check the size of the cache with the following call:
You can filter the objects in the cache for those whose name ends with STRATEGY
:
Note: The results of the previous two cells will vary at different points as you progress through the notebook and add more items to the cache.
Any object listed in the cache is retrieved using the sig.obj.get
API. This includes strategies created by users:
Check this with the original version:
Examples
To rebuild rfs
and specify a ticker:
Object name clashes result in an ObjectError
, unless the new strategy has identical characteristics to the existing strategy.
In the following code block, despite rfs_duplicate
having the same ticker
as rfs_ticker
, there is no ObjectError
as the characteristics of both strategies are identical:
In contrast, the following example displays an error being produced due to rfs_error
and rfs_ticker
sharing the same ticker
, but not the same start_date
:
Cloning strategies
The clone_strategy
method allows you to create a copy of an existing strategy and vary some of its parameters.
A dictionary of {parameter: new_value}
pairs should be supplied. The following example creates a version of rfs
in 'EUR'
with an updated ticker
:
Strategy details & parameters
Initial cash holdings
A strategy begins by holding a number of cash units, denominated in the currency
of the strategy. The number of units is determined by the initial_cash
parameter, with a default value of 1000
.
Using the RollingFutureStrategy
in Backtesting, plotting the history of rfs
results in an initial value of 1000:
The portfolio table also records an initial 1000 units of USD CASH:
To create a variant of rfs
whose initial_cash
level is 100
:
As expected, the history series begins at 100
:
Assuming that the transaction cost model is AUM independent, varying initial_cash
in this way is purely a matter of scaling and will not impact performance characteristics:
An exception to this rule is when initial_cash
is set to zero. In this case no positions are opened. It is not possible to compute common metrics, such as return, for such a strategy.
There are multiple ways to remove cash from a strategy whilst still investing in underlyings. In the case of the RollingFutureStrategy
, you will need to provide a fixed_contracts
parameter and set the initial_cash
to zero.
Similar functionality is available for ReinvestmentStrategy
objects, using the initial_shares
parameter.
Learn more: examples of these cases are provided in Further examples.
Total return
Accruing interest on the cash held within a strategy is optional and can be controlled with the boolean input total_return
.
To create a variant of rfs
that does not accrue interest on cash held:
The difference in annualised return between the TotalReturn
version of the strategy and the ExcessReturn
version is approximately 32 basis points:
As a further example, a custom strategy that simply holds its initial cash is created:
Learn more: Custom strategies.
To create an alternative version with total_return
set to False
and plot the performance of both versions of the strategy:
To switch off total_return
at the global level when configuring the environment, use the command: sig.env()[sig.config.EXCESS_RETURN_ONLY] = True
.
Learn more: Setting up your environment.
Transaction costs
The inclusion of transaction costs in a backtest can be controlled with the include_trading_costs
parameter.
You can create a variant of rfs
that does not assume any trading costs:
To compute the impact of the transaction cost assumptions, you can compare the performance of the two strategy variants:
The impact here is 2bp annualised. This can vary considerably, depending on the underlying and the transaction cost model.
Learn more: Transaction costs - TradePricer
To switch off transaction costs at the global level when configuring the environment, use the following command: sig.env()[sig.config.IGNORE_T_COSTS] = True
.
Learn more: Setting up your environment.
Not reinvesting P&L
You can fix the amount of cash used to determine the position of a strategy at its inception.
Example: signify a fixed capital allocation for a PM regardless of performance.
The following portfolio table displays a position of USD 1MM targeted at rebalance, despite the strategy holding close to USD 2MM in cash:
Long & short positions
Short versions of strategies are created by passing the direction
parameter with a value of 'short'
, as opposed to its default of 'long'
:
In the case of a RollingFutureStrategy
, the 'short'
version coincides with the negative of the long version:
Going short on the long version of the RollingFutureStrategy
through a basket delivers the same result as going long on the short version of the RollingFutureStrategy
through a basket:
For certain assets there are expected differences caused by market realities. An example is the treatment of dividends when creating a ReinvestmentStrategy
:
Further examples
Note: These examples illustrate how to specify a specific number of units of underlying to trade whilst using a Building Block wrapper. The precise syntax can vary by asset class.
Rolling Future Strategy
By default the number of contracts held by a RollingFutureStrategy
will be determined by the strategy's value and the value of each futures contract. It is possible to specify that a fixed number of contracts be traded by using the fixed_contracts
parameter.
Reinvestment Strategy
To create a ReinvestmentStrategy
that holds precisely one unit of the underlying stock the initial_shares
parameter can be used. You can also choose whether to include an initial cash balance on the strategy.
Last updated