Overall Statistics
Total Trades
196
Average Win
-2.64%
Average Loss
-9.50%
Compounding Annual Return
-78.976%
Drawdown
96.800%
Expectancy
-0.630
Net Profit
-45.719%
Sharpe Ratio
46.906
Probabilistic Sharpe Ratio
67.130%
Loss Rate
49%
Win Rate
51%
Profit-Loss Ratio
-0.28
Alpha
143.552
Beta
5.426
Annual Standard Deviation
3.155
Annual Variance
9.956
Information Ratio
47.665
Tracking Error
3.088
Treynor Ratio
27.279
Total Fees
$84319.30
import math 
import numpy as np
import pandas as pd
import talib

from calendar import monthrange
from datetime import date, datetime, time, timedelta 
from QuantConnect.Python import PythonQuandl


class Algorithm(QCAlgorithm):

    def Initialize(self):

        self.SetCash(5000000)
        self.SetStartDate(2020, 4, 1)
        #self.SetEndDate(2020, 5, 15)
        
        self.Settings.FreePortfolioValuePercentage = 0.2
    
        # SECURITIES

        self.usequities = ["SPY"]
        for s in self.usequities:
            x = self.AddEquity(s, Resolution.Daily) 

        self.fut_sp = self.AddFuture(Futures.Indices.SP500EMini)
        self.fut_sp.SetFilter(TimeSpan.Zero, TimeSpan.FromDays(60))
        self.fut_gold = self.AddFuture(Futures.Metals.Gold)
        self.fut_gold.SetFilter(TimeSpan.FromDays(30), TimeSpan.FromDays(60))
        
        self.prev_sp_contract = None
        self.prev_gold_contract = None
        
        # SCHEDULE FUNCTIONS

        self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.At(9, 45), self.Trade_spfut) 
        self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.At(10, 45), self.Trade_goldfut) 
            
    # TRADING
    
    
    def Trade_spfut(self):
        
        if self.fut_sp.Symbol not in self.CurrentSlice.FutureChains:
            return
        
        chain = self.CurrentSlice.FutureChains[self.fut_sp.Symbol]
            
        sp = [sp for sp in chain if sp.OpenInterest > 0]
        if len(sp) == 0:
            if self.prev_sp_contract is not None:
                self.Liquidate(self.prev_sp_contract.Symbol)
            return
                
        sorted_sp = sorted(sp, key=lambda k: k.OpenInterest, reverse = True)
        trade_sp = sorted_sp[0]
        
        if self.prev_sp_contract is None:
            self.prev_sp_contract = trade_sp
        
        if self.prev_sp_contract.Symbol != trade_sp.Symbol:
            self.Liquidate(self.prev_sp_contract.Symbol)
            self.prev_sp_contract = trade_sp
        
        self.SetHoldings(trade_sp.Symbol, 0.5)
        

    def Trade_goldfut(self):
        if self.fut_gold.Symbol not in self.CurrentSlice.FutureChains:
            return
        
        chain = self.CurrentSlice.FutureChains[self.fut_gold.Symbol]
            
        gc = [gc for gc in chain if gc.OpenInterest > 0]
        if len(gc) == 0:
            if self.prev_gold_contract is not None:
                self.Liquidate(self.prev_gold_contract.Symbol)
            return
        sorted_gc = sorted(gc, key=lambda k: k.OpenInterest, reverse = True)
        trade_gc = sorted_gc[0]
        
        if self.prev_gold_contract is None:
            self.prev_gold_contract = trade_gc
        
        if self.prev_gold_contract.Symbol != trade_gc.Symbol:
            self.Liquidate(self.prev_gold_contract.Symbol)
            self.prev_gold_contract = trade_gc
        
        self.SetHoldings(trade_gc.Symbol, 0.5)