Overall Statistics
using Newtonsoft.Json;

namespace QuantConnect 
{
    /// <summary>
    /// Custom Data Type: Bitcoin data from Quandl - http://www.quandl.com/help/api-for-bitcoin-data
    /// </summary>
    public class Bitcoin : BaseData
    {
        //Set the defaults:
        /// <summary>
        /// Open Price
        /// </summary>
        public decimal Open = 0;
        
        /// <summary>
        /// High Price
        /// </summary>
        public decimal High = 0;
        
        /// <summary>
        /// Low Price
        /// </summary>
        public decimal Low = 0;

        /// <summary>
        /// Closing Price
        /// </summary>
        public decimal Close = 0;

        /// <summary>
        /// Volume in BTC
        /// </summary>
        public decimal VolumeBTC = 0;

        /// <summary>
        /// Volume in USD
        /// </summary>
        public decimal VolumeUSD = 0;
        
        /// <summary>
        /// Volume in USD:
        /// </summary>
        public decimal WeightedPrice = 0;

        /// <summary>
        /// 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".
        /// </summary>
        public Bitcoin()
        {
            Symbol = "BTC";
        }

        /// <summary>
        /// 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.
        /// </summary>
        /// <param name="config">Configuration object</param>
        /// <param name="date">Date of this source file</param>
        /// <param name="isLiveMode">true if we're in live mode, false for backtesting mode</param>
        /// <returns>String URL of source file.</returns>
        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("http://www.quandl.com/api/v1/datasets/BITCOIN/BITSTAMPUSD.csv?sort_order=asc", SubscriptionTransportMedium.RemoteFile);
        }

        /// <summary>
        /// 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
        /// </summary>
        /// <param name="line">string line from the data source file submitted above</param>
        /// <param name="config">Subscription data, symbol name, data type</param>
        /// <param name="date">Current date we're requesting. This allows you to break up the data source into daily files.</param>
        /// <param name="isLiveMode">true if we're in live mode, false for backtesting mode</param>
        /// <returns>New Bitcoin Object which extends BaseData.</returns>
        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
                {
                    var liveBTC = JsonConvert.DeserializeObject<LiveBitcoin>(line);
                    coin.Time = DateTime.Now;
                    coin.Open = liveBTC.Last;
                    coin.High = liveBTC.High;
                    coin.Low = liveBTC.Low;
                    coin.Close = liveBTC.Last;
                    coin.VolumeBTC = liveBTC.Volume;
                    coin.WeightedPrice = liveBTC.VWAP;
                    coin.Symbol = "BTC";
                    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]);
                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.Symbol = "BTC";
                coin.Value = coin.Close;
            }
            catch { /* Do nothing, skip first title row */ }

            return coin;
        }
    }
    
    /// <summary>
    /// Live data structure
    /// </summary>
    public class LiveBitcoin
    {
        public int Timestamp = 0;
        public decimal Last = 0;
        public decimal High = 0;
        public decimal Low = 0;
        public decimal Bid = 0;
        public decimal Ask = 0;
        public decimal VWAP = 0;
        public decimal Volume = 0;
    }
}
using Newtonsoft.Json;

namespace QuantConnect
{
    /*
    *   QuantConnect University - Example of Hooking up Custom Live Data
    *
    *   There are many great API's for live bitcoin data - including "bitstamp". In this algorithm
    *   we connect the live datafeed of bitstamp to a QuantConnect algorithm and can make trading 
    *   decisions based on the custom datafeed.
    *
    *   Currently only polling REST API's supported but we will soon add support for socket connections.
    *
    *   SEE BITCOIN.CS FOR THE LIVE DATA IMPLEMENTATION
    */
    public class BTCLive : QCAlgorithm 
    {
        //Initialize the data and resolution you require for your strategy:
        public override void Initialize()
        {
            var res = Resolution.Second; 
            SetStartDate(2013,01,01);
            SetEndDate(2014,01,01);
            AddData<Bitcoin>("BTC", res);
            SetCash(10000);
        }

        //New Bitcoin Data Event:
        public void OnData(Bitcoin data)
        {
        	if (!Portfolio.Invested) Order("BTC", 100);
        	
            //Generate running statistic for headers.
            if (LiveMode)
            {
                SetRuntimeStatistic("BTC", data.Close.ToString("C"));
            }
        }
        
        //Unused OnData TradeBars
        public void OnData(TradeBars data)
        {  }
    }
}