Overall Statistics
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic; 
using QuantConnect.Securities;  
using QuantConnect.Models;   

namespace QuantConnect { 
    
    public partial class QCUWeatherBasedRebalancing : QCAlgorithm
    {
        //Initialize: Storage for our custom data:
        //Source: http://www.wunderground.com/history/
        //Make sure to link to the actual file download URL if using dropbox.
        //private string url = "https://www.dropbox.com/s/txgqzv2vp5lzpqc/10065.csv";
        private decimal fraction;
        private int rebalanceFrequency = 2, tradingDayCount = 0;
        private Symbol symbol = QuantConnect.Symbol.Create("CORNUSD", SecurityType.Cfd, Market.Oanda);

        ///<summary>
        /// Initialize our algorithm:
        ///</summary>
        public override void Initialize()
        {
            SetStartDate(2013, 1, 1);         
            SetEndDate(2014, 5, 31); 
            SetCash(25000);
			SetBenchmark(time => 25000);

			SetBrokerageModel(BrokerageName.OandaBrokerage);

            AddCfd(symbol, Resolution.Minute);
            AddData<Weather>("NYCTEMP", Resolution.Minute);
        }
        
        //Save the instance of the weather.
        public void OnData(Weather data)
        {
        	//Scale from -5C to +25C :: -5C == 100%, +25C = 0% invested
            fraction = -(data.MinC + 5m) / 30m;
            Plot("Weather", "Scale", fraction);
        }
        
        ///<summary>
        /// When we have a new event trigger, buy some stock:
        ///</summary>
        public void OnData(TradeBars data) 
        {   
            //Rebalance every 10 days:
            if (tradingDayCount >= rebalanceFrequency) 
            {   
                SetHoldings(symbol, fraction); 
                tradingDayCount = 0;
            }
        }
        
        ///<summary>
        /// After each trading day
        ///</summary>
        public override void OnEndOfDay() {
            tradingDayCount++;
        }
    }
    
    /// <summary>
    /// Weather based rebalancing
    /// </summary>
    public class Weather : BaseData
    {
        public decimal MaxC = 0;
        public decimal MeanC = 0;
        public decimal MinC = 0;
        public string errString = "";

        public Weather()
        {
            this.Symbol = "NYCTEMP";
        }
        
        public override SubscriptionDataSource GetSource(SubscriptionDataConfig config, DateTime date, bool isLive)
        {
            return new SubscriptionDataSource("https://www.dropbox.com/s/txgqzv2vp5lzpqc/10065.csv?dl=1", SubscriptionTransportMedium.RemoteFile);
        }
        
        public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, bool isLive)
        {
        	// Header handling
            if (char.IsLetter(line[0])) return null;

            var data = line.Split(',');
            return new Weather()
            {
            	// Make sure we only get this data AFTER trading day - don't want forward bias.
                Time = DateTime.Parse(data[0]).AddHours(20),
                MaxC = Convert.ToDecimal(data[1]),
                MeanC = Convert.ToDecimal(data[2]),
                MinC = Convert.ToDecimal(data[3]),
            };
        }
    }
}