Overall Statistics
Total Trades
410
Average Win
0.58%
Average Loss
-0.08%
Compounding Annual Return
12.871%
Drawdown
41.900%
Expectancy
7.514
Net Profit
747.553%
Sharpe Ratio
0.773
Probabilistic Sharpe Ratio
10.822%
Loss Rate
3%
Win Rate
97%
Profit-Loss Ratio
7.77
Alpha
0.127
Beta
-0.093
Annual Standard Deviation
0.152
Annual Variance
0.023
Information Ratio
0.08
Tracking Error
0.242
Treynor Ratio
-1.266
Total Fees
$0.00
from execution import *
from alpha import *

class NadionCalibratedCircuit(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2003, 1, 1)
        self.SetCash(100000)
        self.Resolution = Resolution.Daily
        self.UniverseSettings.Resolution = Resolution.Daily
        
        self.risk = "QQQ"
        self.safe = "SHY"
        
        symbols = [
            Symbol.Create(self.risk, SecurityType.Equity, Market.USA),
            Symbol.Create(self.safe, SecurityType.Equity, Market.USA)
        ]
        self.AddUniverseSelection(ManualUniverseSelectionModel(symbols))
        
        self.AddAlpha( RiskBalance(symbols[0], symbols[1], self, self.Resolution) )
        
        self.Settings.RebalancePortfolioOnInsightChanges = False
        self.Settings.RebalancePortfolioOnSecurityChanges = False
        self.SetPortfolioConstruction( InsightWeightingPortfolioConstructionModel(self.RebalanceFunction) )
        self.SetSecurityInitializer(self.CustomSecurityInitializer)
        
        self.SetExecution( SlowExecutionModel() )
        
        self.AddRiskManagement( MaximumDrawdownPercentPortfolio(0.03) )
        
        self.month = -1
        
    def CustomSecurityInitializer(self, security):
        security.SetFeeModel(ConstantFeeModel(0))

    def RebalanceFunction(self, time):
        if self.month == -1:
            self.month = time.month
            return time
            
        if self.month != time.month:
            self.month = time.month
            return time
        return None
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Common")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Algorithm.Framework")

from System import *
from QuantConnect import *
from QuantConnect.Orders import *
from QuantConnect.Algorithm import *
from QuantConnect.Algorithm.Framework import *

class RiskBalance:
    
    def __init__(self, risk, safe, algorithm, resolution):
        
        self.algorithm = algorithm
        
        self.risk = risk
        self.safe = safe
        
        self.resolution = resolution
        
        self.psars = {}
        
        self.bull_insights =  Insight.Group([
            Insight.Price(self.risk, timedelta(days = 30), InsightDirection.Up, None, None, None, 0.90),
            Insight.Price(self.safe, timedelta(days = 30), InsightDirection.Up, None, None, None, 0.10)
        ])
        self.bear_insights =  Insight.Group([
            Insight.Price(self.risk, timedelta(days = 30), InsightDirection.Up, None, None, None, 0.50),
            Insight.Price(self.safe, timedelta(days = 30), InsightDirection.Up, None, None, None, 0.50)
        ])
        
        self.month = -1

    def Update(self, algorithm, slice):
        if self.month == -1:
            self.month = algorithm.Time.month
            return self.bull_insights
            
        if slice.ContainsKey(self.risk) and self.month != algorithm.Time.month:
            self.month = algorithm.Time.month
            
            if slice[self.risk].Close > self.psar.Current.Value:
                return self.bull_insights
            elif slice[self.risk].Close < self.psar.Current.Value:
                return self.bear_insights
            
        return []
    
    def OnSecuritiesChanged(self, algorithm, changes):
        self.psar = ParabolicStopAndReverse(5)
        consolidator = TradeBarConsolidator(timedelta(days=30))
        self.algorithm.SubscriptionManager.AddConsolidator(self.risk, consolidator)
        self.algorithm.RegisterIndicator(self.risk, self.psar, consolidator)
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Common")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Algorithm.Framework")

from System import *
from QuantConnect import *
from QuantConnect.Orders import *
from QuantConnect.Algorithm import *
from QuantConnect.Algorithm.Framework import *
from QuantConnect.Algorithm.Framework.Execution import *
from QuantConnect.Algorithm.Framework.Portfolio import *

class SlowExecutionModel(ExecutionModel):

    def __init__(self):
        self.targetsCollection = PortfolioTargetCollection()
        self.month = -1

    def Execute(self, algorithm, targets):
        
        self.targetsCollection.AddRange(targets)
        
        if self.targetsCollection.Count > 0:
            
            if self.month == -1:
                self.PlaceOrders(algorithm, 1)
                self.month = algorithm.Time.month
                return
            
            if self.month != algorithm.Time.month:
                self.month = algorithm.Time.month
                self.PlaceOrders(algorithm, (1/12))
            
    def PlaceOrders(self, algorithm, factor):
        for target in self.targetsCollection.OrderByMarginImpact(algorithm):
            quantity = OrderSizing.GetUnorderedQuantity(algorithm, target)
            if quantity != 0:
                quantity = quantity*factor
                algorithm.MarketOrder(target.Symbol, quantity)
        self.targetsCollection.ClearFulfilled(algorithm)