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
-8.894
Tracking Error
0.108
Treynor Ratio
0
Total Fees
$0.00
class TransdimensionalOptimizedChamber(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2020, 12, 10)  # Set Start Date
        # self.SetEndDate(2020, 6, 16)
        self.SetCash(100000)  # Set Strategy Cash
        self.AddEquity("SPY", Resolution.Hour)
        
        
        history = self.History(self.Symbol("SPY"), 3640, Resolution.Hour)
        self.TrainOnHistory(history)
        self.Log("test");


    def OnData(self, data):
        
        # if not self.Portfolio.Invested:
        #    self.SetHoldings("SPY", 1)
        
        pass
        
    def TrainOnHistory(self, history):
        
        rsiLevelsToTest = [35, 30, 25, 20, 15, 10]
            
        targetPointsCombination = [5, 10, 20]
        stoplossPointsCombination = [5, 10, 20]
            
        analysis = []
        
        for rsiLevel in rsiLevelsToTest:
            
            #---------------Data gathering------------------
            for targetPoints in targetPointsCombination:
                
                for stoplossPoints in stoplossPointsCombination:
                    self.rsi = self.RSI("SPY", 14, MovingAverageType.Wilders, Resolution.Hour)
            
                    positions = {}
                    openPositions = []
            
                    profitable = []
                    losses = []
                    profitPoints = []
                    drawdownPoints = []
                    
                    for bar in history.itertuples():
                        self.rsi.Update(bar.Index[1], bar.close)
                        
                        if self.rsi.IsReady:
                            #create a fake position when rsi is below 30
                            if self.rsi.Current.Value < rsiLevel and len(openPositions) == 0:
                                positions[bar.Index[1]] = TrainModel(bar, self.rsi.Current.Value)
                                openPositions.append(bar.Index[1])
                                
                            for time in openPositions:
                                trainModel = positions[time]
                                
                                trainModel.priceTracker.append(bar.close)
                                
                                if(trainModel.startingBar.close > bar.close):
                                    if(trainModel.startingBar.close - bar.close >= stoplossPoints):
                                        #loss
                                        losses.append(time)
                                        drawdownPoints.append(trainModel.startingBar.close - bar.close)
                                        
                                        openPositions.remove(time)
                                        continue
                                
                                if(trainModel.startingBar.close < bar.close):
                                    if(bar.close - trainModel.startingBar.close >= targetPoints):
                                        #win
                                        profitable.append(time)
                                        profitPoints.append(bar.close - trainModel.startingBar.close)
                                        
                                        openPositions.remove(time)
                                        continue
                    
                    self.Log("--------------- " + str(rsiLevel) + "-------------------")
                    self.Log("Training tracked " + str(len(positions)) + " positions")
                    if len(positions) > 0 :
                        self.Log("STOPLOSS: " + str(stoplossPoints) + " - Target : " + str(targetPoints))
                        
                        strikeRate = len(profitable) / (len(profitable) + len(losses))
                        self.Log("Strikerate: " + str(strikeRate * 100))
                        maxProfit = max(profitPoints)
                        self.Log("Maximum profit: " + str(maxProfit))
                        minProfit = min(profitPoints)
                        self.Log("Minimum profit: " + str(minProfit))
                        averageProfit = sum(profitPoints) / len(profitPoints)
                        self.Log("Average profit: " + str(averageProfit))
                        maxDrawdown = max(drawdownPoints)
                        self.Log("Maximum drawdown: " + str(maxDrawdown))
                        # averageHoldingPeriod = sum(holdingPeriod) / len(holdingPeriod)
                        # self.Log("Average holding period: " + str(averageHoldingPeriod))
                        
                        strategyScore = ( averageProfit * (len(positions) * strikeRate) ) - ( maxDrawdown * (len(positions) * (1-strikeRate)) )
                        self.Log("STRATEGY SCORE: " + str(strategyScore))
                    
                    self.Log("---------------------------------------------------")
                    
                    # strategyScore = ( averageProfit * (len(positions) * strikeRate) ) - ( maxDrawdown * (len(positions) * (1-strikeRate)) )
                    
                    analysis.append(AnalyseModel(rsiLevel, strategyScore))
                
                self.bestSettings = sorted(analysis, key=lambda f: f.strategyScore, reverse=True)[0]
            
    
class TrainModel:
    def __init__(self, bar, startingRsi):
        self.startingBar = bar
        self.startingRsi = startingRsi
        self.priceTracker = [] #close prices [int]
        
class AnalyseModel:
    def __init__(self, rsi, strategyScore):
        self.rsi = rsi
        self.strategyScore = strategyScore