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
-1.419
Tracking Error
0.175
Treynor Ratio
0
Total Fees
$0.00
Estimated Strategy Capacity
$0
Lowest Capacity Asset
Portfolio Turnover
0%
# region imports
from AlgorithmImports import *
# endregion

class FatRedPenguin(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(1999, 1, 1)  # Set Start Date
        self.SetEndDate(2012, 1, 1)  # Set Start Date
        self.SetCash(100000)  # Set Strategy Cash
        self.symbol =  self.AddEquity("SPY", Resolution.Minute).Symbol

        self.rollingWindow = RollingWindow[TradeBar](3)
        self.Consolidate(self.symbol, Resolution.Daily, self.CustomBarHandler)
        self.entryTime = datetime.min
        self.entryPrice = 0
        self.entryTicket = None
        self.profitLimitTicket = None
        self.entryTime = datetime.min
        self.highestprice = 0
        stop_loss = 0.001
        self.StopMarketTicket = None
        self.TPTicket = None
        self.window = timedelta(days=7)
   
    def OnData(self, data):
        
        if not self.rollingWindow.IsReady:
            return     
        
        if not (self.Time.hour == 9 and self.Time.minute == 31):
            return

        self.ser = pd.Series([self.rollingWindow[c].Close for c in range(0,self.rollingWindow.Size)])
        if self.ser.is_monotonic_increasing and not self.Portfolio.Invested:
            quantity = self.CalculateOrderQuantity(self.symbol, 0.1)
            self.entryTicket = self.MarketOrder(self.symbol,quantity)
            self.Debug(f"Quantity filled: {self.entryTicket.QuantityFilled}; Date: {str(self.Time)}")
            self.entryTime = self.Time
    
        if self.Portfolio.Invested and self.Time >= self.entryTime + self.window:
            self.Liquidate(self.symbol)
            self.Debug(f"Time stop hit, liquidating holdings at Date: {str(self.Time)}")

    def CustomBarHandler(self, bar):
        self.rollingWindow.Add(bar)

    def OnOrderEvent(self, orderEvent):

        if orderEvent.Status != OrderStatus.Filled:
            return

        if self.Portfolio.Invested:
             self.TPTicket = self.LimitOrder(self.symbol, -orderEvent.Quantity, orderEvent.FillPrice * (1.0014))


        if self.TPTicket is not None and self.TPTicket.OrderId == orderEvent.OrderId:
            self.exitTime = self.Time
            self.Debug(f"Profit Target Hit, Date: {str(self.Time)}")
            self.entryTicket = None