Leverage

Make sure to watch the video and slides for this lecture for the full explanation!

$ Leverage Ratio = \frac{Debt + Capital Base}{Capital Base}$

Leverage from Algorithm

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:

  • you have created
  • you are a collaborator on

The following algo will long Amazon and short IBM. Run it in IDE:

In [ ]:
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)

Backtest Info

Note: Run the following codes in Notebooks

Use get_backtest() to get the backtest info

In [9]:
bt = get_backtest('5986b969dbab994fa4264696')
100% Time: 0:00:00|###########################################################|

Use algo_id to get the backtest_id

In [10]:
bt.algo_id
Out[10]:
'5986b969dbab994fa4264696'

Use recorded_vars to get back the recorded results

In [11]:
bt.recorded_vars
Out[11]:
Exposure Leverage amzn_close ibm_close
2016-07-26 00:00:00+00:00 -0.002203 0.992140 735.430 161.990
2016-07-27 00:00:00+00:00 -0.000207 0.997486 736.090 161.840
2016-07-28 00:00:00+00:00 0.006717 0.998964 752.630 161.440
2016-07-29 00:00:00+00:00 0.000162 0.996444 760.120 160.770
2016-08-01 00:00:00+00:00 0.002431 1.003876 767.755 161.320
2016-08-02 00:00:00+00:00 0.000207 0.995943 760.640 160.510
2016-08-03 00:00:00+00:00 -0.004409 1.003540 753.460 160.600
2016-08-04 00:00:00+00:00 0.003457 1.001679 760.960 161.410
2016-08-05 00:00:00+00:00 -0.002347 1.008046 765.890 163.450
2016-08-08 00:00:00+00:00 0.003158 0.995528 766.360 162.050
2016-08-09 00:00:00+00:00 0.001780 0.996853 767.705 161.750
2016-08-10 00:00:00+00:00 0.000503 0.999465 768.480 162.020
2016-08-11 00:00:00+00:00 -0.002647 1.007480 771.410 163.465
2016-08-12 00:00:00+00:00 0.006231 0.991732 772.520 161.970
2016-08-15 00:00:00+00:00 0.000830 0.994101 768.330 161.870
2016-08-16 00:00:00+00:00 -0.000607 0.997131 764.645 160.730
2016-08-17 00:00:00+00:00 0.001551 0.996547 764.192 160.370
2016-08-18 00:00:00+00:00 -0.000044 1.001369 764.230 161.400
2016-08-19 00:00:00+00:00 -0.000422 0.994643 757.420 160.070
2016-08-22 00:00:00+00:00 0.000555 1.001931 759.075 159.990
2016-08-23 00:00:00+00:00 0.002023 0.998017 762.730 160.340
2016-08-24 00:00:00+00:00 -0.000596 0.993433 756.280 158.910
2016-08-25 00:00:00+00:00 0.003533 0.995940 759.135 158.540
2016-08-26 00:00:00+00:00 0.009068 0.993721 769.440 158.390
2016-08-29 00:00:00+00:00 -0.000505 1.005921 771.540 159.740
2016-08-30 00:00:00+00:00 -0.000020 0.996528 767.130 159.240
2016-08-31 00:00:00+00:00 0.001833 0.997897 769.140 158.900
2016-09-01 00:00:00+00:00 -0.003963 1.006987 770.880 159.580
2016-09-02 00:00:00+00:00 0.002053 0.996049 772.535 159.565
2016-09-06 00:00:00+00:00 0.009555 1.001295 789.310 160.210
... ... ... ... ...
2017-06-14 00:00:00+00:00 -0.002005 0.993778 974.826 153.810
2017-06-15 00:00:00+00:00 0.000823 0.999913 964.060 154.210
2017-06-16 00:00:00+00:00 -0.000724 1.007212 988.330 155.240
2017-06-19 00:00:00+00:00 0.001355 0.992624 994.400 154.600
2017-06-20 00:00:00+00:00 -0.002487 1.000034 993.020 154.880
2017-06-21 00:00:00+00:00 0.008139 0.990287 1002.435 153.680
2017-06-22 00:00:00+00:00 0.001759 0.996882 1001.520 154.420
2017-06-23 00:00:00+00:00 0.000366 0.998513 1003.230 154.230
2017-06-26 00:00:00+00:00 -0.007408 1.002952 993.520 155.225
2017-06-27 00:00:00+00:00 -0.006849 0.998813 978.668 154.890
2017-06-28 00:00:00+00:00 0.005374 1.000617 989.950 155.390
2017-06-29 00:00:00+00:00 0.000542 0.992852 975.920 154.210
2017-06-30 00:00:00+00:00 -0.005486 0.997816 968.630 154.010
2017-07-03 00:00:00+00:00 -0.017962 1.014632 952.649 155.840
2017-07-05 00:00:00+00:00 0.013339 0.987050 972.526 153.770
2017-07-06 00:00:00+00:00 0.004378 0.993076 965.470 152.455
2017-07-07 00:00:00+00:00 0.002492 1.003599 978.960 152.970
2017-07-10 00:00:00+00:00 0.005343 1.005452 997.910 153.510
2017-07-11 00:00:00+00:00 0.000796 1.000549 993.990 153.270
2017-07-12 00:00:00+00:00 0.002393 1.001917 1006.500 153.810
2017-07-13 00:00:00+00:00 -0.001796 1.002179 999.400 153.690
2017-07-14 00:00:00+00:00 -0.001548 1.002784 1000.700 154.270
2017-07-17 00:00:00+00:00 0.005278 0.996348 1009.650 153.020
2017-07-18 00:00:00+00:00 0.002428 1.012750 1023.440 154.035
2017-07-19 00:00:00+00:00 0.009528 0.981475 1026.214 147.290
2017-07-20 00:00:00+00:00 -0.002181 0.999621 1029.000 147.820
2017-07-21 00:00:00+00:00 0.006378 0.996061 1024.849 146.950
2017-07-24 00:00:00+00:00 0.008965 0.994146 1039.680 146.010
2017-07-25 00:00:00+00:00 0.003150 0.998578 1039.450 146.130
2017-07-26 00:00:00+00:00 0.008499 0.993306 1052.760 145.270

253 rows × 4 columns

You can plot the leverage & exposure ratios

In [13]:
bt.recorded_vars['Leverage'].plot()
Out[13]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f77c62a6fd0>
In [14]:
bt.recorded_vars['Exposure'].plot()
Out[14]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f77c63d5310>

High Leverage Example

You can actually specify to borrow on margin (NOT RECOMMENDED) by changing the order_target_percent() from +/- 0.5 to +/- 2.0

In [ ]:
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)
In [15]:
bt = get_backtest('5986bd68ceda5554428a005b')
100% Time: 0:00:00|###########################################################|
In [16]:
bt.recorded_vars['Leverage'].plot()
Out[16]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f77c6253810>

Set Hard Limit on Leverage

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

In [ ]:
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)