Overall Statistics
Total Trades
1
Average Win
0%
Average Loss
0%
Compounding Annual Return
-4.766%
Drawdown
0.100%
Expectancy
0
Net Profit
-0.054%
Sharpe Ratio
-11.257
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0.009
Beta
-2.949
Annual Standard Deviation
0.004
Annual Variance
0
Information Ratio
-11.949
Tracking Error
0.005
Treynor Ratio
0.015
Total Fees
$1.00
import numpy as np
import pandas as pd
from datetime import datetime, timedelta
from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Data.Market import TradeBar

class BasicTemplateAlgorithm(QCAlgorithm):
    '''High beta strategy'''

    def Initialize(self):
  
        '''Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialize.'''
        
        #Initial investment and backtest period
        self.SetStartDate(2019,2,25)    # Set Start Date
        self.SetEndDate(2019,2,26)      # Set End Date
        self.SetCash(10000)             # Set Strategy Cash
        
        #Capture initial investment for risk off purposes
        self.ClosingPortValue = self.Portfolio.TotalPortfolioValue
        self.CurrentPortValue = self.Portfolio.TotalPortfolioValue
        self.CurrentHoldValue = self.Portfolio.TotalHoldingsValue
        
        #Universe
        self.AddEquity("SPY",  Resolution.Minute)
        
        '''Schedule Function Here'''        
        self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.Every(TimeSpan.FromMinutes(6)), Action(self.UpdatePortValues))        
        self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.Every(TimeSpan.FromMinutes(7)), Action(self.CheckDailyLosses))        
        
        '''Set Warmup Here'''
        
        self.SetWarmup(TimeSpan.FromDays(30))

    # OnData        
    def OnData(self, data):
        
        #Verify all indicators have warmed up before anything happens   
        #if self.IsWarmingUp: return
        if not self.Portfolio.Invested:
            self.SetHoldings("SPY", 0.10)    
    
    # Update Portfolio Values    
    def UpdatePortValues(self):
        if not self.Portfolio.Invested or not self.IsMarketOpen("SPY"): return
        self.marginRemaining = self.Portfolio.MarginRemaining
        self.CurrentPortValue = self.Portfolio.TotalPortfolioValue
        self.CurrentHoldValue = self.Portfolio.TotalHoldingsValue

        self.Log("Portfolio Values Have Been Updated")            
            
    # CheckLosses            
    #Check intraday losses and run defensive function if a 5.6% drop is recognized        
    def CheckDailyLosses(self):
        if not self.Portfolio.Invested or not self.IsMarketOpen("SPY"): return
    
        self.CurrentPerformance = round((self.CurrentPortValue/self.ClosingPortValue-1)*100,2)
        
        if self.CurrentPortValue <= self.ClosingPortValue*0.90:
            self.HighLosses()
        else: 
            self.Log("Current Performance: {0}%".format(self.CurrentPerformance))
    
    def OnEndOfDay(self):
        self.ClosingPortValue = self.Portfolio.TotalPortfolioValue