8e35155c40
Regression Tests / build (push) Has been cancelled
Python Virtual Environments / build (push) Has been cancelled
Benchmarks / build (push) Has been cancelled
Build & Test Lean / build (push) Has been cancelled
Research Regression Tests / build (push) Has been cancelled
* Adapted futures commission fees for IB * Unit tests * Some cleanup * Fix exchange fees * Update regression algorithms stats * Update unit tests
198 lines
8.1 KiB
C#
198 lines
8.1 KiB
C#
/*
|
|
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
|
|
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*
|
|
*/
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using QuantConnect.Data;
|
|
using QuantConnect.Interfaces;
|
|
using QuantConnect.Orders;
|
|
using QuantConnect.Securities;
|
|
using QuantConnect.Securities.Future;
|
|
|
|
namespace QuantConnect.Algorithm.CSharp
|
|
{
|
|
/// <summary>
|
|
/// This example demonstrates how to add futures with daily resolution.
|
|
/// </summary>
|
|
/// <meta name="tag" content="using data" />
|
|
/// <meta name="tag" content="benchmarks" />
|
|
/// <meta name="tag" content="futures" />
|
|
public class BasicTemplateFuturesDailyAlgorithm : QCAlgorithm, IRegressionAlgorithmDefinition
|
|
{
|
|
protected virtual Resolution Resolution => Resolution.Daily;
|
|
protected virtual bool ExtendedMarketHours => false;
|
|
|
|
// S&P 500 EMini futures
|
|
private const string RootSP500 = Futures.Indices.SP500EMini;
|
|
|
|
// Gold futures
|
|
private const string RootGold = Futures.Metals.Gold;
|
|
private Future _futureSP500;
|
|
private Future _futureGold;
|
|
|
|
/// <summary>
|
|
/// Initialize your algorithm and add desired assets.
|
|
/// </summary>
|
|
public override void Initialize()
|
|
{
|
|
SetStartDate(2013, 10, 08);
|
|
SetEndDate(2014, 10, 10);
|
|
SetCash(1000000);
|
|
|
|
_futureSP500 = AddFuture(RootSP500, Resolution, extendedMarketHours: ExtendedMarketHours);
|
|
_futureGold = AddFuture(RootGold, Resolution, extendedMarketHours: ExtendedMarketHours);
|
|
|
|
// set our expiry filter for this futures chain
|
|
// SetFilter method accepts TimeSpan objects or integer for days.
|
|
// The following statements yield the same filtering criteria
|
|
_futureSP500.SetFilter(TimeSpan.Zero, TimeSpan.FromDays(182));
|
|
_futureGold.SetFilter(0, 182);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Event - v3.0 DATA EVENT HANDLER: (Pattern) Basic template for user to override for receiving all subscription data in a single event
|
|
/// </summary>
|
|
/// <param name="slice">The current slice of data keyed by symbol string</param>
|
|
public override void OnData(Slice slice)
|
|
{
|
|
if (!Portfolio.Invested)
|
|
{
|
|
foreach(var chain in slice.FutureChains)
|
|
{
|
|
// find the front contract expiring no earlier than in 90 days
|
|
var contract = (
|
|
from futuresContract in chain.Value.OrderBy(x => x.Expiry)
|
|
where futuresContract.Expiry > Time.Date.AddDays(90)
|
|
select futuresContract
|
|
).FirstOrDefault();
|
|
|
|
// if found, trade it
|
|
if (contract != null)
|
|
{
|
|
// Let's check if market is actually open to place market orders. For example: for daily resolution, data can come at a
|
|
// time when market is closed, like 7:00PM.
|
|
if (Securities[contract.Symbol].Exchange.ExchangeOpen)
|
|
{
|
|
MarketOrder(contract.Symbol, 1);
|
|
}
|
|
else
|
|
{
|
|
// MOO are not allowed for futures, so to make sure, use limit order instead. We use a very big limit price here
|
|
// to make the order fill on next bar.
|
|
LimitOrder(contract.Symbol, 1, contract.AskPrice * 2);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// Same as above, let's check if market is open to place market orders.
|
|
if (Portfolio.Values.Any(x => !Securities[x.Symbol].Exchange.ExchangeOpen))
|
|
{
|
|
foreach (var holdings in Portfolio.Values.OrderBy(x => x.Symbol))
|
|
{
|
|
|
|
// use a very low limit price here to make the order fill on next bar.
|
|
LimitOrder(holdings.Symbol, -holdings.Quantity, 1m);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Liquidate();
|
|
}
|
|
}
|
|
|
|
foreach (var changedEvent in slice.SymbolChangedEvents.Values)
|
|
{
|
|
if (Time.TimeOfDay != TimeSpan.Zero)
|
|
{
|
|
throw new Exception($"{Time} unexpected symbol changed event {changedEvent}!");
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// This is used by the regression test system to indicate if the open source Lean repository has the required data to run this algorithm.
|
|
/// </summary>
|
|
public virtual bool CanRunLocally { get; } = true;
|
|
|
|
/// <summary>
|
|
/// This is used by the regression test system to indicate which languages this algorithm is written in.
|
|
/// </summary>
|
|
public virtual Language[] Languages { get; } = { Language.CSharp, Language.Python };
|
|
|
|
/// <summary>
|
|
/// Data Points count of all timeslices of algorithm
|
|
/// </summary>
|
|
public virtual long DataPoints => 11721;
|
|
|
|
/// <summary>
|
|
/// Data Points count of the algorithm history
|
|
/// </summary>
|
|
public virtual int AlgorithmHistoryDataPoints => 0;
|
|
|
|
/// <summary>
|
|
/// This is used by the regression test system to indicate what the expected statistics are from running the algorithm
|
|
/// </summary>
|
|
public virtual Dictionary<string, string> ExpectedStatistics => new Dictionary<string, string>
|
|
{
|
|
{"Total Trades", "56"},
|
|
{"Average Win", "0.30%"},
|
|
{"Average Loss", "-3.89%"},
|
|
{"Compounding Annual Return", "-15.101%"},
|
|
{"Drawdown", "15.300%"},
|
|
{"Expectancy", "-0.784"},
|
|
{"Net Profit", "-15.209%"},
|
|
{"Sharpe Ratio", "-1.13"},
|
|
{"Probabilistic Sharpe Ratio", "0.000%"},
|
|
{"Loss Rate", "80%"},
|
|
{"Win Rate", "20%"},
|
|
{"Profit-Loss Ratio", "0.08"},
|
|
{"Alpha", "-0.093"},
|
|
{"Beta", "-0.081"},
|
|
{"Annual Standard Deviation", "0.091"},
|
|
{"Annual Variance", "0.008"},
|
|
{"Information Ratio", "-1.694"},
|
|
{"Tracking Error", "0.132"},
|
|
{"Treynor Ratio", "1.279"},
|
|
{"Total Fees", "$117.06"},
|
|
{"Estimated Strategy Capacity", "$130000000.00"},
|
|
{"Lowest Capacity Asset", "ES VP274HSU1AF5"},
|
|
{"Fitness Score", "0.011"},
|
|
{"Kelly Criterion Estimate", "0"},
|
|
{"Kelly Criterion Probability Value", "0"},
|
|
{"Sortino Ratio", "-0.225"},
|
|
{"Return Over Maximum Drawdown", "-0.99"},
|
|
{"Portfolio Turnover", "0.028"},
|
|
{"Total Insights Generated", "0"},
|
|
{"Total Insights Closed", "0"},
|
|
{"Total Insights Analysis Completed", "0"},
|
|
{"Long Insight Count", "0"},
|
|
{"Short Insight Count", "0"},
|
|
{"Long/Short Ratio", "100%"},
|
|
{"Estimated Monthly Alpha Value", "$0"},
|
|
{"Total Accumulated Estimated Alpha Value", "$0"},
|
|
{"Mean Population Estimated Insight Value", "$0"},
|
|
{"Mean Population Direction", "0%"},
|
|
{"Mean Population Magnitude", "0%"},
|
|
{"Rolling Averaged Population Direction", "0%"},
|
|
{"Rolling Averaged Population Magnitude", "0%"},
|
|
{"OrderListHash", "b05e6b7d992a1c42edeef553b9f6c1f1"}
|
|
};
|
|
}
|
|
}
|