Overall Statistics
Total Trades
29
Average Win
4.24%
Average Loss
0%
Compounding Annual Return
32.328%
Drawdown
49.200%
Expectancy
0
Net Profit
70.860%
Sharpe Ratio
0.753
Probabilistic Sharpe Ratio
27.545%
Loss Rate
0%
Win Rate
100%
Profit-Loss Ratio
0
Alpha
0.042
Beta
4.809
Annual Standard Deviation
0.51
Annual Variance
0.26
Information Ratio
0.774
Tracking Error
0.404
Treynor Ratio
0.08
Total Fees
$93.48
Estimated Strategy Capacity
$330000000.00
Lowest Capacity Asset
SPY R735QTJ8XC9X
using System;
using QuantConnect.Data;
using QuantConnect.Orders;
using QuantConnect.Securities;
using QuantConnect.Securities.Equity;

namespace QuantConnect.Algorithm.CSharp {
public class SpyLever : QCAlgorithm {
  private Equity _spy;
  private bool _shouldRebalance = true;

  public override void Initialize() {
    SetStartDate(2013, 10, 7);
    SetCash(100000);
    _spy = AddEquity("SPY", Resolution.Daily);
    _spy.SetBuyingPowerModel(new SecurityMarginModel(5m));

    // Schedule rebalances monthly
    Schedule.On(DateRules.MonthStart(3), TimeRules.Midnight, () => { _shouldRebalance = true; });
    
    // Portfolio.MarginCallModel = MarginCallModel.Null;
  }

  public override void OnData(Slice data) {
    Plot("Leverage", "Value", Portfolio.TotalAbsoluteHoldingsCost / Portfolio.TotalPortfolioValue);
    Plot("Values", "Holdings Cost", Portfolio.TotalAbsoluteHoldingsCost);
    Plot("Values", "Portfolio Value", Portfolio.TotalPortfolioValue);
    Plot("Values", "Cash", Portfolio.Cash);
    
    if (_shouldRebalance) {
      var close = data.Bars[_spy.Symbol].Close;
      var power = 5m * Portfolio.TotalPortfolioValue * 0.95m;
      var goal = (int)Math.Truncate(power / close);
      var current = Portfolio[_spy.Symbol].Quantity;
      var toBuy = goal - current;

      Debug($"Rebalance SPY@{close} with Power {power}: {current} -> {goal} buy {toBuy}");
      MarketOrder(_spy.Symbol, toBuy);

      _shouldRebalance = false;
    }
  }

  public override void OnOrderEvent(OrderEvent ev) {
    if (ev.Status == OrderStatus.Invalid) {
      throw new Exception($"Failed order {ev.Message} for {ev.Quantity}");
    }
  }
}
}