/* * 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.Globalization; using Newtonsoft.Json; using QuantConnect.Data; using QuantConnect.Interfaces; namespace QuantConnect.Algorithm.CSharp { /// /// Regression test to demonstrate importing and trading on custom data. /// /// /// /// /// public class CustomDataRegressionAlgorithm : QCAlgorithm, IRegressionAlgorithmDefinition { /// /// Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized. /// public override void Initialize() { SetStartDate(2011, 9, 13); SetEndDate(2015, 12, 01); //Set the cash for the strategy: SetCash(100000); //Define the symbol and "type" of our generic data: var resolution = LiveMode ? Resolution.Second : Resolution.Daily; AddData("BTC", resolution); } /// /// Event Handler for Bitcoin Data Events: These Bitcoin objects are created from our /// "Bitcoin" type below and fired into this event handler. /// /// One(1) Bitcoin Object, streamed into our algorithm synchronised in time with our other data streams public void OnData(Bitcoin data) { //If we don't have any bitcoin "SHARES" -- invest" if (!Portfolio.Invested) { //Bitcoin used as a tradable asset, like stocks, futures etc. if (data.Close != 0) { //Access custom data symbols using . Order("BTC.Bitcoin", Portfolio.MarginRemaining / Math.Abs(data.Close + 1)); } } } /// /// This is used by the regression test system to indicate if the open source Lean repository has the required data to run this algorithm. /// public bool CanRunLocally { get; } = true; /// /// This is used by the regression test system to indicate which languages this algorithm is written in. /// public Language[] Languages { get; } = { Language.CSharp, Language.Python }; /// /// This is used by the regression test system to indicate what the expected statistics are from running the algorithm /// public Dictionary ExpectedStatistics => new Dictionary { {"Total Trades", "1"}, {"Average Win", "0%"}, {"Average Loss", "0%"}, {"Compounding Annual Return", "155.210%"}, {"Drawdown", "84.800%"}, {"Expectancy", "0"}, {"Net Profit", "5123.170%"}, {"Sharpe Ratio", "1.199"}, {"Loss Rate", "0%"}, {"Win Rate", "0%"}, {"Profit-Loss Ratio", "0"}, {"Alpha", "0.989"}, {"Beta", "0.157"}, {"Annual Standard Deviation", "0.84"}, {"Annual Variance", "0.706"}, {"Information Ratio", "1.052"}, {"Tracking Error", "0.846"}, {"Treynor Ratio", "6.431"}, {"Total Fees", "$0.00"} }; /// /// Custom Data Type: Bitcoin data from Quandl - http://www.quandl.com/help/api-for-bitcoin-data /// public class Bitcoin : BaseData { [JsonProperty("timestamp")] public int Timestamp = 0; [JsonProperty("open")] public decimal Open = 0; [JsonProperty("high")] public decimal High = 0; [JsonProperty("low")] public decimal Low = 0; [JsonProperty("last")] public decimal Close = 0; [JsonProperty("bid")] public decimal Bid = 0; [JsonProperty("ask")] public decimal Ask = 0; [JsonProperty("vwap")] public decimal WeightedPrice = 0; [JsonProperty("volume")] public decimal VolumeBTC = 0; public decimal VolumeUSD = 0; /// /// 1. DEFAULT CONSTRUCTOR: Custom data types need a default constructor. /// We search for a default constructor so please provide one here. It won't be used for data, just to generate the "Factory". /// public Bitcoin() { Symbol = "BTC"; } /// /// 2. RETURN THE STRING URL SOURCE LOCATION FOR YOUR DATA: /// This is a powerful and dynamic select source file method. If you have a large dataset, 10+mb we recommend you break it into smaller files. E.g. One zip per year. /// We can accept raw text or ZIP files. We read the file extension to determine if it is a zip file. /// /// Configuration object /// Date of this source file /// true if we're in live mode, false for backtesting mode /// String URL of source file. public override SubscriptionDataSource GetSource(SubscriptionDataConfig config, DateTime date, bool isLiveMode) { if (isLiveMode) { return new SubscriptionDataSource("https://www.bitstamp.net/api/ticker/", SubscriptionTransportMedium.Rest); } //return "http://my-ftp-server.com/futures-data-" + date.ToString("Ymd") + ".zip"; // OR simply return a fixed small data file. Large files will slow down your backtest return new SubscriptionDataSource("https://www.quantconnect.com/api/v2/proxy/quandl/api/v3/datasets/BCHARTS/BITSTAMPUSD.csv?order=asc&api_key=WyAazVXnq7ATy_fefTqm", SubscriptionTransportMedium.RemoteFile); } /// /// 3. READER METHOD: Read 1 line from data source and convert it into Object. /// Each line of the CSV File is presented in here. The backend downloads your file, loads it into memory and then line by line /// feeds it into your algorithm /// /// string line from the data source file submitted above /// Subscription data, symbol name, data type /// Current date we're requesting. This allows you to break up the data source into daily files. /// true if we're in live mode, false for backtesting mode /// New Bitcoin Object which extends BaseData. public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, bool isLiveMode) { var coin = new Bitcoin(); if (isLiveMode) { //Example Line Format: //{"high": "441.00", "last": "421.86", "timestamp": "1411606877", "bid": "421.96", "vwap": "428.58", "volume": "14120.40683975", "low": "418.83", "ask": "421.99"} try { coin = JsonConvert.DeserializeObject(line); coin.EndTime = DateTime.UtcNow.ConvertFromUtc(config.ExchangeTimeZone); coin.Value = coin.Close; } catch { /* Do nothing, possible error in json decoding */ } return coin; } //Example Line Format: //Date Open High Low Close Volume (BTC) Volume (Currency) Weighted Price //2011-09-13 5.8 6.0 5.65 5.97 58.37138238, 346.0973893944 5.929230648356 try { string[] data = line.Split(','); coin.Time = DateTime.Parse(data[0], CultureInfo.InvariantCulture); coin.Open = Convert.ToDecimal(data[1], CultureInfo.InvariantCulture); coin.High = Convert.ToDecimal(data[2], CultureInfo.InvariantCulture); coin.Low = Convert.ToDecimal(data[3], CultureInfo.InvariantCulture); coin.Close = Convert.ToDecimal(data[4], CultureInfo.InvariantCulture); coin.VolumeBTC = Convert.ToDecimal(data[5], CultureInfo.InvariantCulture); coin.VolumeUSD = Convert.ToDecimal(data[6], CultureInfo.InvariantCulture); coin.WeightedPrice = Convert.ToDecimal(data[7], CultureInfo.InvariantCulture); coin.Value = coin.Close; } catch { /* Do nothing, skip first title row */ } return coin; } } } }