Overall Statistics
Total Trades
18
Average Win
1.58%
Average Loss
-1.64%
Compounding Annual Return
-1.888%
Drawdown
9.300%
Expectancy
0.042
Net Profit
-0.943%
Sharpe Ratio
-0.073
Probabilistic Sharpe Ratio
22.570%
Loss Rate
47%
Win Rate
53%
Profit-Loss Ratio
0.97
Alpha
-0.066
Beta
0.192
Annual Standard Deviation
0.134
Annual Variance
0.018
Information Ratio
-1.785
Tracking Error
0.171
Treynor Ratio
-0.051
Total Fees
$42.62
Estimated Strategy Capacity
$140000000.00
Lowest Capacity Asset
SPY R735QTJ8XC9X
'''
Let's say we want to long SPY for 10 days, and short for another 10 days
We don't want to lose the count on days during our stopped live algorithm
so we'd save the time count variable in ObjectStore 
and retrieve it after redeploy
so that out counting process will not start again
'''
from datetime import datetime

class SquareLightBrownManatee(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2021, 1, 21)
        self.SetCash(100000) 
        self.AddEquity("SPY", Resolution.Minute)
        
        # variable for the day count
        # we retrieve it if it exist in ObjectStore
        if self.ObjectStore.ContainsKey("switchTime"):
            self.switchTime = self.ObjectStore.Read("switchTime")
            # since we save it as string, we need to convert back to datetime object
            self.switchTime = datetime.strptime(self.switchTime, '%m/%d/%Y')        # match the format!
            
        # otherwise we set it, as it is a new instance
        else:
            self.switchTime = self.Time
        
    def OnData(self, data):
        # when time count reached, we switch position side
        if self.switchTime <= self.Time:
            
            # check if now is in 10-day long status
            if self.Portfolio["SPY"].IsLong:
                self.SetHoldings("SPY", -1.)
                
            # this is where you're not invested or is shorting
            else:
                self.SetHoldings("SPY", 1.)
            
            # we reset the time count
            self.switchTime = self.Time + timedelta(days=10)
            
            # we also write in our ObjectStore to save it offline, as string
            self.ObjectStore.Save("switchTime", self.switchTime.strftime('%m/%d/%Y'))   # we only care the precision up to "day"