Overall Statistics
Total Trades
2
Average Win
0%
Average Loss
0%
Compounding Annual Return
985.083%
Drawdown
2.100%
Expectancy
0
Net Profit
3.095%
Sharpe Ratio
10.785
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0.37
Beta
106.848
Annual Standard Deviation
0.144
Annual Variance
0.021
Information Ratio
10.716
Tracking Error
0.144
Treynor Ratio
0.014
Total Fees
$15.59
import numpy as np

class BasicTemplateAlgorithm(QCAlgorithm):

    def Initialize(self):

        self.SetStartDate(2012,10, 1)  #Set Start Date
        self.SetEndDate(2012,10,5)    #Set End Date
        self.SetCash(100000)           #Set Strategy Cash
        
        self.max_leverage = 2.0       # Set total leverage you want
        self.qt_factor = 1.0          # Set total leverage level you want to order each time
        
        self.AddEquity("WMT", Resolution.Minute)  # Set Resolution 'Minute'
        self.Securities["WMT"].SetLeverage(2.0)   # Set Leverage 2.0
        
        # Schedule the rebalance function (twice everyday 30 min. & 35 min. after market open)
        self.Schedule.On(self.DateRules.EveryDay("WMT"), 
        self.TimeRules.AfterMarketOpen("WMT", 30), 
        Action(self.rebalance))
        
        self.Schedule.On(self.DateRules.EveryDay("WMT"), 
        self.TimeRules.AfterMarketOpen("WMT", 35), 
        Action(self.rebalance))
        

    def OnData(self, data):
        
        pass
    
    def rebalance(self):
        
        self.Log('Cash Before: ' + str(self.Portfolio.Cash))
        self.Log('GetBuyingPower Before: ' + str(self.Portfolio.GetBuyingPower("WMT", OrderDirection.Buy)))
        self.Log('CalculateOrderQuantity: ' + str(self.CalculateOrderQuantity("WMT", 1)))

        
        # Order by quantity each time equivelant with 1.0x of total portfolio value --------------
        
        # Get maximum leveraged Portfolio Value
        total_leverage = float(self.Portfolio.TotalPortfolioValue) * self.max_leverage
        self.Log('Total Portfolio Value with max Leverage: ' + str(total_leverage))
        
        price = float(self.Securities["WMT"].Price)
        
        # Get Quantity you want to trade each time (here we trade 1.0x of total portfolio value each time.)
        qt = (total_leverage * (self.qt_factor/self.max_leverage)) / price   
        
        # Market Order
        self.MarketOrder("WMT", qt)
        
        
        self.Log('Total Cost:  ' + str(self.Portfolio["WMT"].HoldingsCost))
        self.Log('AveragePrice: ' + str(self.Portfolio["WMT"].AveragePrice))
        self.Log('TotalFees: ' + str(self.Portfolio.TotalFees) +'\n')
        self.Log('Cash After: ' + str(self.Portfolio.Cash))
        self.Log('GetMarginRemaining After: ' + str(self.Portfolio.GetMarginRemaining("WMT", OrderDirection.Buy))+'\n')