Overall Statistics
Total Trades
3
Average Win
0%
Average Loss
0%
Compounding Annual Return
4.113%
Drawdown
17.800%
Expectancy
0
Net Profit
6.221%
Sharpe Ratio
0.303
Probabilistic Sharpe Ratio
15.127%
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0.124
Annual Variance
0.015
Information Ratio
0.303
Tracking Error
0.124
Treynor Ratio
0
Total Fees
$4.33
Estimated Strategy Capacity
$4600000.00
Lowest Capacity Asset
BND TRO5ZARLX6JP
# region imports
from AlgorithmImports import *
# endregion

class CrawlingBlackDogfish(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2020, 11, 12)  # Set Start Date
        self.SetCash(100000)  # Set Strategy Cash

        # Setup trading framework
        # Transaction and submit/execution rules will use IB models 
        self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage, 
            AccountType.Margin)

        # Configure all algorithm securities
# Uncomment this line and run to replicate runtime error:
# During the algorithm initialization, the following exception has occurred: 
#  Attempted to divide by zero. in BuyingPowerModel.cs:line 110 
        self.SetSecurityInitializer(self.CustomSecurityInitializer)

        self.AddEquity("SPY", Resolution.Minute)
        self.AddEquity("BND", Resolution.Minute)
        self.AddEquity("AAPL", Resolution.Minute)

    def OnData(self, data: Slice):
        if not self.Portfolio.Invested:
            self.SetHoldings("SPY", 0.33)
            self.SetHoldings("BND", 0.33)
            self.SetHoldings("AAPL", 0.33)

    def CustomSecurityInitializer(self, security):
        """
        Define models to be used for securities as they are added to the 
        algorithm's universe.
        """
        # Set the custom buying power model
        security.SetBuyingPowerModel(CustomBuyingPowerModel(self))

################################################################################
class CustomBuyingPowerModel(BuyingPowerModel):
    """
    Custom model to determine if the portfolio has sufficient buying power for 
    a new desired order.
    """
    def __init__(self, algorithm):
        """Initialize the custom buying power model."""
        super().__init__() # now required for 3.0 IDE
        self.algorithm = algorithm

    def HasSufficientBuyingPowerForOrder(self, parameters):
        """Determine if the portfolio has the buying power for an order."""
        # This model will assume that there is always enough buying power
        sufficient_buying_power = HasSufficientBuyingPowerForOrderResult(True)
        return sufficient_buying_power