Overall Statistics
Total Trades
4
Average Win
0%
Average Loss
0%
Compounding Annual Return
2507.604%
Drawdown
57.800%
Expectancy
0
Net Profit
36.711%
Sharpe Ratio
17.227
Probabilistic Sharpe Ratio
61.566%
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
30.618
Beta
4.795
Annual Standard Deviation
1.919
Annual Variance
3.684
Information Ratio
17.281
Tracking Error
1.884
Treynor Ratio
6.895
Total Fees
$294.15
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(1000000)
        self.SetStartDate(2020, 7, 31)
        #self.SetEndDate(2020, 5, 15)
        
        #self.Settings.FreePortfolioValuePercentage = 0.0
    
        # 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.fut_vol = self.AddFuture(Futures.Indices.VIX)
        self.fut_vol.SetFilter(TimeSpan.Zero, TimeSpan.FromDays(60))
        
        #self.fut_tb_two = self.AddFuture(Futures.Financials.Y2TreasuryBond)
        #self.fut_tb_two.SetFilter(TimeSpan.Zero, TimeSpan.FromDays(60))
        
        #self.fut_tb_five = self.AddFuture(Futures.Financials.Y5TreasuryBond)
        #self.fut_tb_five.SetFilter(TimeSpan.Zero, TimeSpan.FromDays(60))
        
        #self.fut_tb_ten = self.AddFuture(Futures.Financials.Y10TreasuryBond)
        #self.fut_tb_ten.SetFilter(TimeSpan.Zero, TimeSpan.FromDays(60))
        
        #self.fut_tb_thirty = self.AddFuture(Futures.Financials.Y30TreasuryBond)
        #self.fut_tb_thirty.SetFilter(TimeSpan.Zero, TimeSpan.FromDays(60))
        
        self.prev_contract = None
        
        # SCHEDULE FUNCTIONS

        self.Schedule.On(self.DateRules.MonthEnd("SPY"), self.TimeRules.At(9, 45), self.Trade) 
        #self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.At(9, 45), self.Roll_contracts) 
            
    # TRADING
    
    
    def Trade(self):
        
        # trade SPX
        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]
        sorted_sp = sorted(sp, key=lambda k: k.OpenInterest, reverse = True)
        trade_sp = sorted_sp[0]
        
        self.SetHoldings(trade_sp.Symbol, 0.25)
        #self.MarketOrder(trade_sp.Symbol, 1)
        
        
        # trade gold
        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]
        sorted_gc = sorted(gc, key=lambda k: k.OpenInterest, reverse = True)
        trade_gc = sorted_gc[0]
        
        self.SetHoldings(trade_gc.Symbol, 0.25)
        
        
    def Roll_contracts(self):
        
        for chain in self.CurrentSlice.FutureChains:
            
            fut = [fut for fut in chain if fut.OpenInterest > 0]
            sorted = sorted(fut, key=lambda k: k.OpenInterest, reverse = True)
            trade_fut = sorted_fut[0]
            
            if self.prev_contract is None:
                self.prev_contract = trade_fut
            
            if self.prev_contract.Symbol != trade_fut.Symbol:
                self.Liquidate(self.prev_contract.Symbol)
                self.prev_contract = trade_fut
            
            self.SetHoldings(trade_fut.Symbol, 0.25)