Overall Statistics
Total Trades
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Net Profit
0%
Sharpe Ratio
0
Probabilistic Sharpe Ratio
0%
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
-0.776
Tracking Error
0.165
Treynor Ratio
0
Total Fees
$0.00
Estimated Strategy Capacity
$0
from convertdate import utils

class TradeTwoTimesPerYear(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2015, 1, 1)  # Set Start Date
        self.SetCash(100000)  # Set Strategy Cash
        # self.AddEquity("SPY", Resolution.Minute)
        
        self.AddEquity("SPY", Resolution.Daily)
        self.time_to_trade = False
        
        # At market open, everyday we check if we can trade
        self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.At(9,30), self.CheckTimeToTrade)
        
        # At 1 min of market open, we trade if the timing is per our condition
        self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.At(9,31), self.ExecuteTrade)

    # Function to check if we can trade today
    def CheckTimeToTrade(self):
        
        # Check for January:
        
        if self.Time.month == 1:
            
            # If January 2nd is Tuesday to Friday:
            if ((self.Time.day == 2) and ((self.Time.weekday() >= 1) and (self.Time.weekday() <= 4))):
                self.time_to_trade = True
                return
                
            # If January 2nd is Monday, we can trade only on January 3rd, which is Tuesday
            if (self.Time.day == 3 and (self.Time.weekday() == 1)):
                self.time_to_trade = True
                return
            
            # If January 2nd is Saturday, we can trade only of January 4th, which is Monday
            if (self.Time.day == 4 and (self.Time.weekday() == 0)):
                self.time_to_trade = True
                return
        
        # Check for July:
        
        if self.Time.month == 7:
            
            # If July 1st is Monday to Friday:
            if ((self.Time.day == 1) and ((self.Time.weekday() >= 0) and (self.Time.weekday() <= 4))):
                self.time_to_trade = True
                return
                
            # If July 1st is Saturday, we can trade only on July 3rd, which is Monday
            if (self.Time.day == 3 and (self.Time.weekday() == 0)):
                self.time_to_trade = True
                return
            
            # If July 1st is Sunday, we can trade only of July 2nd, which is Monday
            if (self.Time.day == 2 and (self.Time.weekday() == 0)):
                self.time_to_trade = True
                return
                    
    
    def ExecuteTrade(self):
        
        # We check if we are OK to trade
        if self.time_to_trade:
            
            date = self.Time.strftime("%A %d. %B %Y")
            self.Debug(f"Execute trade at Date: {date}")
            
            ###########################
            ##                       ##
            ## Your Trading Strategy ##
            ##                       ##
            ###########################
            
            # Once we trade, we reset the trigger and wait for the next trading period
            self.time_to_trade = False

    def OnData(self, data):
        '''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
            Arguments:
                data: Slice object keyed by symbol containing the stock data
        '''

        # if not self.Portfolio.Invested:
        #    self.SetHoldings("SPY", 1)