Overall Statistics
Total Trades
7
Average Win
0%
Average Loss
0%
Compounding Annual Return
0.145%
Drawdown
2.600%
Expectancy
0
Net Profit
0.036%
Sharpe Ratio
0.045
Probabilistic Sharpe Ratio
28.511%
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0.024
Beta
0.215
Annual Standard Deviation
0.041
Annual Variance
0.002
Information Ratio
0.744
Tracking Error
0.141
Treynor Ratio
0.009
Total Fees
$7.00
Estimated Strategy Capacity
$170000000.00
Lowest Capacity Asset
SPY R735QTJ8XC9X
# region imports
from AlgorithmImports import *
import pandas as pd
import numpy as np
from datetime import time, datetime, timedelta
# endregion

class CombinedAlgorithm(QCAlgorithm):
	
    def Initialize(self):

        # INITIALIZE
        self.SetStartDate(2022, 1, 1)  # Set Start Date
        self.SetEndDate(2022, 4, 1)
        self.SetCash(10000)  # Set Strategy Cash
        self.spy = self.AddEquity('SPY', Resolution.Minute)
        self.spy.SetDataNormalizationMode(DataNormalizationMode.Raw)

        # SCHEDULED EVENTS
        self.Schedule.On(self.DateRules.WeekStart("SPY"), self.TimeRules.AfterMarketOpen("SPY",1), self.WeekStart)
        self.Schedule.On(self.DateRules.WeekEnd("SPY"), self.TimeRules.BeforeMarketClose("SPY"), self.WeekEnd)

        # TOGGLES
        # self.short_ema_trail = True
        self.invest = False
        self.weekly_trigger = False

        # VARIABLES
        self.trigger_week_high = 0
        self.trigger_week_low = 0

        self.SetWarmUp(timedelta(days = 20))

        weeklyConsolidator = TradeBarConsolidator(timedelta(weeks=1))
        self.SubscriptionManager.AddConsolidator(self.spy.Symbol, weeklyConsolidator)
        weeklyConsolidator.DataConsolidated += self.OnTwoWeekBar
        self.weekBarWindow = RollingWindow[TradeBar](1) # 2

        # CUSTOM CHART
        log_plot = Chart('Custom Chart')
        log_plot.AddSeries(Series('Orders',SeriesType.Scatter,0))
        log_plot.AddSeries(Series('Spy Price', SeriesType.Line,0))
        self.AddChart(log_plot) 

    def Rebalance(self):
        if self.IsWarmingUp:
            return

    def OnData(self, data):
        # VARIABLES
        spy_price = self.Securities[self.spy.Symbol].Price
        held_stocks = self.Portfolio[self.spy.Symbol].Quantity
        shares_to_buy = int(self.Portfolio.Cash / self.Securities[self.spy.Symbol].Price)
        profit = self.Portfolio[self.spy.Symbol].UnrealizedProfitPercent

        if self.weekly_trigger and (spy_price > self.trigger_week_high) and self.invest:
            self.MarketOrder(self.spy.Symbol, (shares_to_buy*0.1))
            self.invest = False
            self.weekly_trigger = False
            self.Log(f'{self.trigger_week_high} previous weeks high ORDER')

    def OnTwoWeekBar(self, sender, bar):
        self.weekBarWindow.Add(bar)

        if not self.weekBarWindow.IsReady:
            return         
        
        trigger_week = self.weekBarWindow[0]
       
        self.trigger_week_high = trigger_week.High
        self.trigger_week_low = trigger_week.Low
        self.Log(f'{self.trigger_week_high} previous weeks high')

    def WeekStart(self):
            self.weekly_trigger = True
            self.invest = True

    def WeekEnd(self):
        if self.weekly_trigger:
            self.weekly_trigger = False
            self.invest = False

    def OnOrderEvent(self, orderEvent):
        if orderEvent.FillQuantity == 0:
            return

        fetched = self.Transactions.GetOrderById(orderEvent.OrderId)
        symbol = orderEvent.Symbol
        fill_price = orderEvent.FillPrice