Overall Statistics
Total Orders
68
Average Win
0.78%
Average Loss
-0.55%
Compounding Annual Return
13.158%
Drawdown
4.100%
Expectancy
0.706
Start Equity
100000
End Equity
110867.37
Net Profit
10.867%
Sharpe Ratio
0.628
Sortino Ratio
0.46
Probabilistic Sharpe Ratio
70.971%
Loss Rate
30%
Win Rate
70%
Profit-Loss Ratio
1.42
Alpha
-0.001
Beta
0.304
Annual Standard Deviation
0.057
Annual Variance
0.003
Information Ratio
-0.974
Tracking Error
0.087
Treynor Ratio
0.119
Total Fees
$54.95
Estimated Strategy Capacity
$18000000.00
Lowest Capacity Asset
SPY R735QTJ8XC9X
Portfolio Turnover
17.61%
# region imports
from AlgorithmImports import *
from datetime import datetime
# endregion

class JumpingGreenChicken(QCAlgorithm):

    def initialize(self):
        self.set_start_date(2024, 1, 1)
        self.set_end_date(2024, 10, 31)
        
        self.set_cash(100000)
        self.spy = self.add_equity("SPY", Resolution.MINUTE).symbol
        self.indicator = self.add_data(Signal, "CUSTOM1", Resolution.MINUTE).symbol

        # self.set_brokerage_model(BrokerageName.INTERACTIVE_BROKERS_BROKERAGE, AccountType.MARGIN)
        self.set_brokerage_model(BrokerageName.INTERACTIVE_BROKERS_BROKERAGE, AccountType.CASH)
        self.set_benchmark(self.spy)
        

        # self.Schedule.On(self.DateRules.EveryDay(self.spy),
        #          self.time_rules.before_market_close(self.spy, 15),      
        #          self.ExitPositions)
    
    # def ExitPositions(self):
    #     if self.portfolio.invested:
    #         self.liquidate(self.spy)
    #     else:
    #         return
    def on_data(self, data: Slice):
        
        if self.indicator in data:
            
            received_signal = data[self.indicator].value
        
            if not self.portfolio.invested and received_signal == 1:
                self.set_holdings(self.spy, 1)
                self.log("Buy Order" + str(self.time) + str('  ') + str(received_signal))
            
            elif self.portfolio.invested and received_signal == 0:
                self.liquidate(self.spy)
                self.log("Sell Order" + str(self.time) + str('  ') + str(received_signal))
            else:
                # self.set_holdings(self.spy, 0)
                self.log("Do Nothing " + str(self.time) + str('  ') + str(received_signal))

class Signal(PythonData):    

    def get_source(self, config, date, isLive):
        source = "https://www.dropbox.com/scl/fi/mynxp0hpeexq2dfjeiset/ml_output_old.csv?rlkey=fd2jghurd7pgzraw8zpnw4fyf&st=yqtpi1fd&dl=1"
        return SubscriptionDataSource(source, SubscriptionTransportMedium.REMOTE_FILE)
    
    def reader(self, config, line, date, isLive):
       
        
        if not (line.strip() and line[0].isdigit()):                
            return None
         
        csv = line.split(',')
        
        xyz = Signal()
        
        
        xyz.symbol = config.symbol
        xyz.time = datetime.strptime(csv[0], '%Y-%m-%d %H:%M:%S') 
                  
        
        xyz.value = int(csv[1])      
        
    
        return xyz