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
0
Tracking Error
0
Treynor Ratio
0
Total Fees
$0.00
Estimated Strategy Capacity
$0
Lowest Capacity Asset
#region imports
from AlgorithmImports import *
#endregion
class RetrospectiveTanButterfly(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2022, 8, 17)  # Set Start Date
        self.SetEndDate(2022, 8, 17)
        self.SetCash(100000)  # Set Strategy Cash
        
        self.symbolData = {}

        self.canLong = True
        self.canShort = True

        symbol = self.AddSecurity(SecurityType.Future, Futures.Indices.SP500EMini , Resolution.Tick, extendedMarketHours = True).Symbol
        
        
        self.symbolData[symbol] = SymbolData()
        self.symbolData[symbol].bidPrice = self.Securities[symbol].BidPrice
        self.symbolData[symbol].askPrice = self.Securities[symbol].AskPrice


        
            
            
    def OnData(self, data):
        for symbol, symbolData in self.symbolData.items():
            if not data.Ticks.ContainsKey(symbol): continue
        
            ticks = data.Ticks[symbol]
            for tick in ticks:
                if tick.TickType == TickType.Quote:
                    symbolData.bidPrice = tick.BidPrice if tick.BidPrice != 0 else symbolData.bidPrice
                    symbolData.askPrice = tick.AskPrice if tick.AskPrice != 0 else symbolData.askPrice

                elif tick.TickType == TickType.Trade:
                    if tick.Price - symbolData.bidPrice > symbolData.askPrice - tick.Price:
                        symbolData.sellVolume += tick.Quantity
                        
                    else:
                        symbolData.buyVolume += tick.Quantity
                        
                
                if symbolData.buyVolume - symbolData.sellVolume < 5:
                    self.canLong = True
                elif symbolData.sellVolume - symbolData.buyVolume < 5:
                    self.canShort = True


                if symbolData.buyVolume - symbolData.sellVolume >= 2000 and self.canLong:
                    self.Log(f"volume buy Delta: {symbolData.buyVolume - symbolData.sellVolume}")
                    self.canLong = False
                elif symbolData.sellVolume - symbolData.buyVolume >= 2000 and self.canShort:
                    self.Log(f"volume sell Delta: {symbolData.sellVolume - symbolData.buyVolume}")
                    self.canShort = False
    
    
    def OnEndOfDay(self, symbol):
        symbolData = self.symbolData[symbol]
        self.Debug(f"{symbol.Value}'s buy volume is {symbolData.buyVolume} and sell volume is {symbolData.sellVolume} for today")
        self.Log(f"{symbol.Value}'s buy volume is {symbolData.buyVolume} and sell volume is {symbolData.sellVolume} for today")
        
        symbolData.ClearDay()


        
class SymbolData:
    
    def __init__(self):
        self.buyVolume = 0
        self.sellVolume = 0
        self.bidPrice = 0
        self.askPrice = 0
        self.canShort = True
        self.canLong = True
        
    def ClearDay(self):
        self.buyVolume = 0
        self.sellVolume = 0