Overall Statistics
Total Trades
13
Average Win
4.22%
Average Loss
-2.06%
Compounding Annual Return
8520305.464%
Drawdown
5.200%
Expectancy
1.034
Net Profit
15.621%
Sharpe Ratio
378819.697
Probabilistic Sharpe Ratio
0%
Loss Rate
33%
Win Rate
67%
Profit-Loss Ratio
2.05
Alpha
28329.358
Beta
0.67
Annual Standard Deviation
0.075
Annual Variance
0.006
Information Ratio
563995.27
Tracking Error
0.05
Treynor Ratio
42362.264
Total Fees
$13.00
Estimated Strategy Capacity
$16000000.00
Lowest Capacity Asset
TSLA UNU3P8Y3WFAD
#region imports
from AlgorithmImports import *
#endregion

# Stop Loss and Take Profit
# -----------------------------------
STOCK = "TSLA"; SL = 0.02; TP = 0.04;
# -----------------------------------

class StopLossTakeProfit(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2022, 3, 14)
        self.SetEndDate(2022, 3, 18) 
        self.SetCash(100000)  
        self.stock = self.AddEquity(STOCK, Resolution.Minute).Symbol
        self.price = 0
        self.stopMarketOrderFillTime = datetime.min
        
    def OnData(self, data):

        if self.Time.hour < 10: return # or self.Time.minute != 1: return
        if self.Time.hour >= 16: return # or self.Time.minute != 1: return

        if not self.Portfolio[self.stock].Invested:  
            #if (self.Time - self.stopMarketOrderFillTime).days < 2:
            #    return  

            self.SetHoldings(self.stock, 1.0)
            #self.MarketOrder(self.stock, 500)
            self.price = data[self.stock].Price 
            
        elif self.Portfolio[self.stock].Invested:  
            if self.price > 0: 
                curr_price = data[self.stock].Price 
                if curr_price >= self.price*(1 + TP):
                    self.Liquidate(self.stock, "Take Profit")
                    self.stopMarketOrderFillTime = self.Time
                    self.price = 0
                elif curr_price < self.price*(1 - SL):    
                    self.Liquidate(self.stock, "Stop Loss")
                    self.stopMarketOrderFillTime = self.Time
                    self.price = 0

    def OnOrderEvent(self, orderEvent):
        order = self.Transactions.GetOrderById(orderEvent.OrderId)
        if orderEvent.Status == OrderStatus.Filled: 
            self.Log("{0}: \nLast TP/SL: {1}".format(orderEvent, self.stopMarketOrderFillTime))