QSTK Tutorial 5

From Quantwiki
Jump to: navigation, search

Part 2 of 2: Backtesting Allocation Strategies

QSTK provides a backtesting simulator known as qstksim, that allows one to test strategies and portfolio allocations. The following are examples of how the back tester may be used.

Testing an Allocation Using the Module Method

A feature of the qstkSim backtester allows you to test an allocation created in a fashion similar to that described in tutorial 4 against historical data. If you import the qstksim module then you may directly use the tradesim function to test an allocation DataFrame using a DataFrame with historical data and a starting fund value.

import QSTK.qstksim as qstksim
#setup alloc
#setup historic
(ts_funds, ts_leverage, f_commission, f_slippage, f_borrow_cost) = qstksim.tradesim(df_alloc,
                    df_close, f_start_cash=10000.0, i_leastcount=1, b_followleastcount=True,
                    f_slippage=0.0005, f_minimumcommision=5.0, f_commision_share=0.0035,
                    i_target_leverage=1, f_rate_borrow=3.5, log="transaction.csv")

The allocation module should have a '_CASH' column in it which can be added using

df_alloc['_CASH'] = 0.0

Input and Output param details


df_alloc : Allocation dataframe to be simulated - Dataframe with the allocation ratio between equities specified on any rebalancing date (Only day of rebalancing are part of the allocation frame).

df_close : Historical close prices for the allocation equities.

f_start_cash : Starting cash value for the simulation

i_leastcount : Leastcount of shares to be held - Integer number of shares - Can be 1/10 etc.

b_followleastcount : True - will make sure that the simulator only trades integer shares.

f_slippage : Slippage for simulation - 5 bps in example

f_minimumcommision : Minimum commission per trade.

f_commision_share : Commission paid per traded share, cost of commission is calculated as max(minimumcommision, no_shares * commision_share).

i_target_leverage : Leverage for the simulation (should be 1)

f_rate_borrow : Borrowing rate in percent - example pays 3.5% borrowing cost.

log : Name of csv to store the log for the simulation in - If not specified it won't create a log.


ts_funds : Timeseries of the fund value between the first and the last allocation date.

ts_leverage : Timeseries of the leverage value of the fund between the first and the last allocation date.

f_commission : Total Commission costs occurred in the simulation.

f_slippage : Total Slippage costs occurred in the simulation.

f_borrow cost : Total Borrowing Cost due to shorts occurred in the simulation.