Overall Statistics
Total Trades
4
Average Win
0.03%
Average Loss
0%
Compounding Annual Return
0.032%
Drawdown
0.100%
Expectancy
0
Net Profit
0.059%
Sharpe Ratio
0.401
Probabilistic Sharpe Ratio
15.068%
Loss Rate
0%
Win Rate
100%
Profit-Loss Ratio
0
Alpha
0
Beta
-0
Annual Standard Deviation
0.001
Annual Variance
0
Information Ratio
-0.139
Tracking Error
0.159
Treynor Ratio
-0.537
Total Fees
$4.00
Estimated Strategy Capacity
$770000000.00
Lowest Capacity Asset
PTIE RWCYT7J7MGKL
# region imports
from AlgorithmImports import *
# endregion

import statistics

class SmoothTanFlamingo(QCAlgorithm):

    def Initialize(self):

        # Set Start Date
        self.SetStartDate(2021, 1, 1)

        # Set Strategy Cash
        self.SetCash(100000)

        self.AddUniverse(self.CoarseFilterFunction)

        self.UniverseSettings.Resolution = Resolution.Hour

        self.symbols_list = []

        self.minute_consolidator = {}

        self.MACD_indicators = {}

        self.storage = {}

        self.start = False

        self.counter = 0

        self.step_1 = {}

    def CoarseFilterFunction(self, coarse):

        if self.start == False:
            self.start = True
            # return [x.Symbol for x in coarse if x.Price > 5 and x.Price < 200 and x.HasFundamentalData]
            return [x.Symbol for x in coarse if x.Symbol.Value == "GME" or x.Symbol.Value == "SAVA"]
            # invested = [x.Key for x in self.Portfolio if x.Value.Invested]
            # open_orders = self.Transactions.GetOpenOrderTickets()
            # for ticket in open_orders:
            #     symbol = ticket.Symbol
            #     if symbol not in invested:
            #         invested.append(symbol)
            # for symbol in invested:
            #     if symbol not in new_list:
            #         new_list.append(symbol)
            # return new_list

        else:
            return Universe.Unchanged

    def OnSecuritiesChanged(self, changes):

        self.ready = False

        history = self.History([x.Symbol for x in changes.AddedSecurities], 200, Resolution.Hour)

        for security in changes.AddedSecurities:
            symbol = security.Symbol

            if str(symbol) in history.index:
                symbol_history = history.loc[symbol]
                if len(symbol_history) == 200:

                    self.symbols_list.append(symbol)
                    self.minute_consolidator[symbol] = TradeBarConsolidator(period = timedelta(hours = 4))
                    self.minute_consolidator[symbol].DataConsolidated += self.consolidation_handler
                    self.SubscriptionManager.AddConsolidator(symbol, self.minute_consolidator[symbol])
                    self.storage[symbol] = []
                    self.MACD_indicators[symbol] = MovingAverageConvergenceDivergence(12, 26, 2)
                    self.RegisterIndicator(symbol, self.MACD_indicators[symbol], self.minute_consolidator[symbol])


                    for time, row in symbol_history.iterrows():
                        tradebar = TradeBar(time - timedelta(hours = 1), symbol, row.open, row.high, row.low, row.close, row.volume, timedelta(hours = 1))
                        self.minute_consolidator[symbol].Update(tradebar)

        self.ready = True

        # for security in changes.RemovedSecurities:
        #     symbol = security.Symbol
        #     self.symbols_list.remove(symbol)
        #     self.SubscriptionManager.RemoveConsolidator(symbol, self.minute_consolidator[symbol])
        #     self.minute_consolidator.pop(symbol)
        #     self.storage.pop(symbol)

    def consolidation_handler(self, sender, bar):
        symbol = bar.Symbol
        self.storage[symbol].append(bar)
        if len(self.storage[symbol]) >= 30:
            self.storage[symbol] = self.storage[symbol][-30:]

        if self.ready:
            self.counter += 1
            if self.counter == len(self.storage):
                self.counter = 0

                invested = [x.Key for x in self.Portfolio if x.Value.Invested]
                for i in invested:
                    if self.MACD_indicators[i].Current.Value < self.MACD_indicators[i].Signal.Current.Value:
                        self.Liquidate(i)

                remove_list = []
                for symbol in self.step_1:
                    if len(self.step_1[symbol]) == 1:
                        if self.storage[symbol][-1].High < self.step_1[symbol][0] * 0.95:
                            self.step_1[symbol].append(self.storage[symbol][-1].High)
                    elif len(self.step_1[symbol]) == 2:
                        if self.storage[symbol][-1].High > self.step_1[symbol][0]:
                            if not self.Portfolio[symbol].Invested:
                                remove_list.append(symbol)
                                self.MarketOrder(symbol, 1)
                
                for i in remove_list:
                    self.step_1.pop(i)
                    
                for symbol in self.storage:
                    # if symbol not in self.step_1:

                        last_three = self.storage[symbol][-10:]
                        last_last_three = self.storage[symbol][:-10]

                        last_three_mean = statistics.mean([x.High for x in last_three])
                        last_last_three_mean = statistics.mean([x.High for x in last_last_three])

                        if last_three_mean > last_last_three_mean * 1.15 and statistics.stdev([x.High for x in last_last_three]) < 0.9:

                            last_three_volume = sum([x.Volume for x in last_three])
                            last_last_three_volume = sum([x.Volume for x in last_last_three])

                            if last_three_volume > last_last_three_volume * 3:
                            
                                self.step_1[symbol] = [max([x.High for x in last_three])]
                                self.Debug(symbol.Value)

    def OnData(self, data: Slice):
        pass