Make sure to watch the video and slides for this lecture for the full explanation!
$ Leverage Ratio = \frac{Debt + Capital Base}{Capital Base}$
Make sure to watch the video for this! Basically click Run Full Backtest
at the upper right corner of the IDE and grab your own backtestid
The get_backtest function provides programmatic access to the results of backtests run on the Quantopian platform. It takes a single parameter, the ID of a backtest for which results are desired.
You can find the ID of a backtest in the URL of its full results page, which will be of the form:
https://www.quantopian.com/algorithms/<algorithm_id>/<backtest_id>
For example:
https://www.quantopian.com/algorithms/5aa0986c36ac88437f4265be/5aa09985b9e6b9458c2f3050
The backtest_id will be 5aa09985b9e6b9458c2f3050
You are only entitled to view the backtests that either:
The following algo will long Amazon and short IBM. Run it in IDE:
def initialize(context):
context.amzn = sid(16841)
context.ibm = sid(3766)
schedule_function(rebalance,date_rules.every_day(),time_rules.market_open())
schedule_function(record_vars,date_rules.every_day(),time_rules.market_close())
def rebalance(context,data):
order_target_percent(context.amzn,0.5)
order_target_percent(context.ibm,-0.5)
def record_vars(context,data):
record(amzn_close=data.current(context.amzn,'close'))
record(ibm_close=data.current(context.ibm,'close'))
record(Leverage = context.account.leverage)
record(Exposure = context.account.net_leverage)
Note: Run the following codes in Notebooks
Use get_backtest()
to get the backtest info
bt = get_backtest('5986b969dbab994fa4264696')
Use algo_id
to get the backtest_id
bt.algo_id
Use recorded_vars
to get back the recorded results
bt.recorded_vars
You can plot the leverage & exposure ratios
bt.recorded_vars['Leverage'].plot()
bt.recorded_vars['Exposure'].plot()
You can actually specify to borrow on margin (NOT RECOMMENDED) by changing the order_target_percent()
from +/- 0.5 to +/- 2.0
def initialize(context):
context.amzn = sid(16841)
context.ibm = sid(3766)
schedule_function(rebalance,date_rules.every_day(),time_rules.market_open())
schedule_function(record_vars,date_rules.every_day(),time_rules.market_close())
def rebalance(context,data):
order_target_percent(context.ibm,-2.0)
order_target_percent(context.amzn,2.0)
def record_vars(context,data):
record(amzn_close=data.current(context.amzn,'close'))
record(ibm_close=data.current(context.ibm,'close'))
record(Leverage = context.account.leverage)
record(Exposure = context.account.net_leverage)
bt = get_backtest('5986bd68ceda5554428a005b')
bt.recorded_vars['Leverage'].plot()
Use set_max_leverage()
to set had limit on leverage, note an run-time error will occur if limit is reached
http://www.zipline.io/appendix.html?highlight=leverage#zipline.api.set_max_leverage
def initialize(context):
context.amzn = sid(16841)
context.ibm = sid(3766)
set_max_leverage(1.03)
schedule_function(rebalance,date_rules.every_day(),time_rules.market_open())
schedule_function(record_vars,date_rules.every_day(),time_rules.market_close())
def rebalance(context,data):
order_target_percent(context.ibm,-0.5)
order_target_percent(context.amzn,0.5)
def record_vars(context,data):
record(amzn_close=data.current(context.amzn,'close'))
record(ibm_close=data.current(context.ibm,'close'))
record(Leverage = context.account.leverage)
record(Exposure = context.account.net_leverage)