Overall Statistics
Total Trades
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Net Profit
0%
Sharpe Ratio
0
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
0
Tracking Error
0
Treynor Ratio
0
Total Fees
$0.00
using System;
using System.Linq;

using QuantConnect.Data;
using QuantConnect.Securities;

namespace QuantConnect.Algorithm.CSharp
{

	public class FuturesTest : QCAlgorithm
	{

		public override void Initialize()
		{
			SetStartDate( 2013, 10, 07 );
			SetEndDate( 2013, 10, 11 );
			SetCash( 1000000 );

			// Add our future
			var futureGold = AddFuture( Futures.Metals.Gold );

			// Get all expiries
			futureGold.SetFilter( universe => universe.Expiration( TimeSpan.Zero, TimeSpan.FromDays( 9999 ) ) );
		}

		public override void OnData( Slice slice )
		{

			// Explore each future contract chain
			foreach ( var chain in slice.FuturesChains ) {

				// Get all contracts in this chain
				var contracts = chain.Value.Contracts;

				// Get front-month/spot contract
				var spot = contracts.
						OrderBy( x => x.Value.Expiry ).
						FirstOrDefault().
						Value;

				// Get front-month/spot asks and bids
				var spotAsk = spot.AskPrice;
				var spotBid = spot.BidPrice;

				// Check if we have prices
				if ( spotAsk == 0m || spotBid == 0m )
					continue;

				// Calculate mid
				var spotMid = ( spotAsk + spotBid ) / 2m;

				// Get expiry of front month/spot
				var spotExpiry = spot.Expiry;

				// Log( $"[SPOT/FM ({spot.Symbol.Value}] \t Expiry: {spotExpiry.ToString(DateFormat.UI)}, \t Mid: {spotMid}" );

				// Go through all contracts received
				foreach ( var contractKvp in contracts ) {

					// Get contract
					var contract = contractKvp.Value;

					// Get contract symbol
					var contractSymbol = contract.Symbol.Value;

					// Calculate mid
					var mid = ( contract.AskPrice + contract.BidPrice ) / 2m;

					// Calculate spread to spot
					var spread = mid - spotMid;

					// Calculate yield to spot
					var yield = Math.Pow( (double)( mid / spotMid ), 1.0 / ( ( contract.Expiry - spotExpiry ).TotalDays / 365.2425 ) );
					yield -= 1.0;

					// Log( $"[{contractSymbol}] \t Expiry: {contract.Expiry.ToString(DateFormat.UI)}, \t Mid: {mid}, \t Spread: {spread}, \t Yield: {yield.ToString("p3")}" );
				}
			}
		}
	}
}