Overall Statistics
#region imports
from AlgorithmImports import *
#endregion

import sys, bz2, lzma, base64  # uuencode, base91, 85 requests

class FatOrangePelican(QCAlgorithm):
    s = ''
    #b = b''
    
    def Initialize(self):
        #self.SetStartDate(2021, 1, 4);
        #self.SetEndDate(2021, 1, 9);
        self.SetStartDate (2021, 5, 1);
        self.SetEndDate (2022, 6, 1);
        self.SetCash(1000000);
        
        ticker =  'SPX' #'NDX', 'VIX'
        self.symbol = self.AddIndex(ticker, Resolution.Minute).Symbol
        self.AddIndexOption(self.symbol, Resolution.Minute).SetFilter (-1, 1, timedelta (days=0), timedelta (days=45))  # no data: timedelta(7))

    def buy_fripp (self):
        b = bytes (self.s, 'ascii')
        lb = len (b)
        b2 = bz2.compress (b)
        l2 = len (b2)
        #b3 = base64.b64encode (b2)
        b3 = base64.a85encode (b2, foldspaces=True, wrapcol=100)
        l3 = len (b3)
        self.Debug (b3)
        done = 'Yes'

    def buy_fleeb (self, value):
        tm = (value.Time.hour * 3600 + value.Time.minute * 60) * 1000

        stuff =  (
                tm,
                value.Open,
                value.High,
                value.Low,
                value.Close,                
                #data.values()[0].Open, 
                #data.values()[0].High, 
                #data.values()[0].Low, 
                #data.values()[0].Close, 
                #data.values()[0].Price, 
                #data.values()[0].Value, 
                #value.Time,
                value.Period.seconds,
            )

        #self.s += bytes (str (stuff) [1:-1] + '\n', 'ascii')
        self.s += str (stuff) [1:-1] + '\n'
        
        l = len (self.s)

        if self.y == 2022 and self.m == 6:        #if tm == 55200000:
        #if self.y == 2021 and self.m == 11:        
            #if tm >= 55100000:
            #self.b = bz2.compress (s)
            #self.buy_fleeb()
            self.buy_fripp()


    def OnData(self, data):
        #value = data.values() [0]

        self.d = data.Time.day
        self.y = data.Time.year
        self.m = data.Time.month
        self.h = data.Time.hour
        self.mn = data.Time.minute

        values = data.values()
        if values: 
            value = values [0]
            self.buy_fleeb (value)          

        #self.Debug (dir (data))
        #syss.exit() even commented this still kills it..

        #Plot("Portfolio", Portfolio.TotalPortfolioValue);

        if not data.ContainsKey(self.symbol) or self.Portfolio.Invested:
            return

        for chain in data.OptionChains.Values:
            for contract in chain.Contracts.Values:
                self.MarketOrder(contract.Symbol, 1)