Overall Statistics
Total Trades
217
Average Win
0.62%
Average Loss
-0.58%
Compounding Annual Return
4.319%
Drawdown
14.200%
Expectancy
0.333
Net Profit
20.900%
Sharpe Ratio
0.642
Loss Rate
36%
Win Rate
64%
Profit-Loss Ratio
1.07
Alpha
0.036
Beta
0.002
Annual Standard Deviation
0.057
Annual Variance
0.003
Information Ratio
-0.535
Tracking Error
0.147
Treynor Ratio
23.44
Total Fees
$299.08
namespace QuantConnect 
{   
    /*
    *   QuantConnect University: Full Basic Template:
    *
    *   The underlying QCAlgorithm class is full of helper methods which enable you to use QuantConnect.
    *   We have explained some of these here, but the full algorithm can be found at:
    *   https://github.com/QuantConnect/QCAlgorithm/blob/master/QuantConnect.Algorithm/QCAlgorithm.cs
    */
    public class PsySignalAlgorithm : QCAlgorithm
    {
        decimal noiseLevel = 0.1m;
      
        //Initialize the data and resolution you require for your strategy:
        public override void Initialize() 
        {
            //Start and End Date range for the backtest:
            SetStartDate(2011, 1, 18);         
            SetEndDate(2015, 07, 13);
            
            //Cash allocation 
            SetCash(100000);
            
            //Add as many securities as you like. All the data will be passed into the event handler:
            AddSecurity(SecurityType.Equity, "SPY", Resolution.Daily);
            AddData<Mix>("MIX", Resolution.Daily);
        } 

        //Data Event Handler: New data arrives here. "TradeBars" type is a dictionary of strings so you can access it by symbol.
        public void OnData(TradeBars data) 
        { } 
        
        public void OnData(Mix data) 
        {
            if (Time.DayOfWeek == DayOfWeek.Wednesday)
            {
                var fraction = data.OscillatorStocktwitsTwitter;
                if (Math.Abs(fraction) > 1.8m) fraction = 1.8m;
                
                if (data.OscillatorStocktwitsTwitter > noiseLevel) {
                    SetHoldings("SPY", fraction);
                } else if (data.OscillatorStocktwitsTwitter < -noiseLevel) {
                    SetHoldings("SPY", -fraction);
                } else {
                    Liquidate();
                } 
            }
        }
    }
    
    public class Mix : BaseData
    {
        public decimal OscillatorTwitter = 0;
        public decimal OscillatorStocktwits = 0;
        public decimal OscillatorStocktwitsTwitter = 0;
        
        public Mix()
        {
            this.Symbol = "MIX";
        }
        public override SubscriptionDataSource GetSource(SubscriptionDataConfig config, DateTime date, bool isLive)
        {
            return new SubscriptionDataSource("http://secure.psychsignal.com/data/mood-index", SubscriptionTransportMedium.RemoteFile);
        }
        
        public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, bool isLive)
        {
            Mix mix = new Mix();
            try
            {
                string[] data = line.Split(',');
                //Required.
                mix.Symbol = config.Symbol;
                mix.Time = DateTime.ParseExact(data[0], "yyyy-MM-dd", CultureInfo.InvariantCulture);
                mix.OscillatorStocktwitsTwitter = Convert.ToDecimal(data[1]);
                mix.OscillatorTwitter = Convert.ToDecimal(data[2]);
                mix.OscillatorStocktwits = Convert.ToDecimal(data[3]);
                mix.Value = mix.OscillatorStocktwitsTwitter;
            }
            catch {   
            }
            return mix;
        }
    }
}