Overall Statistics
Total Trades
8
Average Win
463.65%
Average Loss
-17.83%
Compounding Annual Return
693.936%
Drawdown
80.200%
Expectancy
4.400
Net Profit
112.698%
Sharpe Ratio
8.448
Probabilistic Sharpe Ratio
63.725%
Loss Rate
80%
Win Rate
20%
Profit-Loss Ratio
26.00
Alpha
18.583
Beta
-3.138
Annual Standard Deviation
2.168
Annual Variance
4.699
Information Ratio
8.348
Tracking Error
2.183
Treynor Ratio
-5.835
Total Fees
$29.60
Estimated Strategy Capacity
$37000000.00
Lowest Capacity Asset
GC VQ3M41P8XYFX
class DeterminedYellowGreenMonkey(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2013, 12, 20)  # Set Start Date
        self.SetEndDate(2014,5,1)
        self.SetCash(10000)  # Set Strategy Cash
        self.notprinted=True
        
        #1. Request Gold futures and save the gold security
        self.gold = self.AddFuture(Futures.Metals.Gold, Resolution.Minute)
        
        #2. Set our expiry filter to return all contracts expiring within 90 days
        self.gold.SetFilter(7,90)
        
        #3. Widen the free portfolio percentage to 30% to avoid margin calls for futures
        self.Settings.FreePortfolioValuePercentage=0.3 # Limit trading to $7k
        
    def OnMarginCallWarning(self):
        self.Error("This is a margin call warning. The assets will e liquidated to cover losses.")


    def OnData(self, slice):

        # Loop over each available futures chain from slice.FutureChains data
        for chain in slice.FutureChains:
            
            #1. Filter to choose popular contracts (at least 1000 OpenInterest)
            self.popularContracts = [contract for contract in chain.Value if contract.OpenInterest > 1000]
            
            #2. if the length of contracts in this chain is zero, continue to next chain
            if len(self.popularContracts) == 0:
                continue
            
            #3. Sort our contracts by open interest in descending order and save to sortedByOIContracts
            sortedByOIContracts = sorted (self.popularContracts, key=lambda k : k.OpenInterest, reverse=True)

            #4. Save the contract with the highest open interest to self.liquidContract
            self.liquidContract = self.popularContracts[0]
            
                    
            if not self.Portfolio.Invested:
                
                #1. Save the notional value of the futures contract to self.notionalValue
                self.notionalValue = self.liquidContract.AskPrice * self.gold.SymbolProperties.ContractMultiplier
                
                #2. Save the contract security object to the variable future
                future = self.Securities[self.liquidContract.Symbol]
                
                
                initialMargin = self.Securities[self.liquidContract.Symbol].BuyingPowerModel.InitialOvernightMarginRequirement
                
                self.Debug("{} NEW CONTRACT Symbol:{} Expiry:{} LastPrice:{:.2f} OpenInterest:{:.0f} Multiplier:{:.0f} MarginReq:${:,.0f} RemMargin:${:,.0f}"
                    .format(self.Time, self.liquidContract.Symbol, self.liquidContract.Expiry, 
                    self.liquidContract.LastPrice, self.liquidContract.OpenInterest, self.gold.SymbolProperties.ContractMultiplier,
                    future.BuyingPowerModel.InitialOvernightMarginRequirement,self.Portfolio.MarginRemaining ))
                    
                #3. Calculate the number of contracts we can afford based on the margin requirement
                # Divide the margin remaining by the initial margin and save to self.contractsToBuy    
                self.contractsToBuy= self.Portfolio.MarginRemaining // initialMargin
                
                #4. Make a market order for the number of contracts we calculated
                self.MarketOrder(future, self.contractsToBuy)
                
                self.Debug ("{} InitialMargin:{} Contracts:{} RemMargin:{}".format(self.Time, str(initialMargin), str(self.contractsToBuy),
                    self.Portfolio.MarginRemaining))
                
                 # You can olaso use the SetHoldings function to automatically calculate your position size
                #self.SetHoldings(self.liquidContract.Symbol, 1)