Overall Statistics
Total Trades
88
Average Win
0.23%
Average Loss
-0.09%
Annual Return
6.123%
Drawdown
5.700%
Expectancy
1.582
Net Profit
12.708%
Sharpe Ratio
1.5
Loss Rate
31%
Win Rate
69%
Profit-Loss Ratio
2.73
Trade Frequency
Weekly trades
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 Weather yesterday;
        private int rebalanceFrequency = 2, tradingDayCount = 0;
        
        ///<summary>
        /// Initialize our algorithm:
        ///</summary>
        public override void Initialize()
        {
            SetStartDate(2013, 1, 1);         
            SetEndDate(2014, 5, 31); 
            SetCash(25000);
            AddSecurity(SecurityType.Equity, "SPY", Resolution.Minute);
            SetRunMode(RunMode.Series);
            
            AddData<Weather>("NYCTEMP", Resolution.Minute);
        }
        
        //Save the instance of the weather.
        public void OnData(Weather data)
        {
            yesterday = data;
        }
        
        ///<summary>
        /// When we have a new event trigger, buy some stock:
        ///</summary>
        public void OnData(TradeBars data) 
        {   
            //Rebalance every 10 days:
            if (tradingDayCount >= rebalanceFrequency) 
            {   
                //Scale from -5C to +25C :: -5C == 0, +25C = 100% invested.
                if (yesterday != null) {
                    decimal fraction = (yesterday.MinC + 5m) / 30m;
                    SetHoldings("SPY", 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 string GetSource(SubscriptionDataConfig config, DateTime date, DataFeedEndpoint datafeed)
        {
            return "https://www.dropbox.com/s/txgqzv2vp5lzpqc/10065.csv?dl=1";
        }
        
        public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, DataFeedEndpoint datafeed)
        {
            Weather weather = new Weather();
            try {
                string[] data = line.Split(',');
                weather.Time = DateTime.Parse(data[0]).AddHours(20); // Make sure we only get this data AFTER trading day - don't want forward bias.
                weather.MaxC = Convert.ToDecimal(data[1]);
                weather.MeanC = Convert.ToDecimal(data[2]);
                weather.MinC = Convert.ToDecimal(data[3]);
            } catch (Exception err)
            {
                return null;
            }
            return weather;
        }
    }
}