Overall Statistics
Total Trades
62
Average Win
1.02%
Average Loss
-1.93%
Compounding Annual Return
-82.049%
Drawdown
26.300%
Expectancy
-0.407
Net Profit
-22.393%
Sharpe Ratio
-4.637
Loss Rate
61%
Win Rate
39%
Profit-Loss Ratio
0.53
Alpha
-1.659
Beta
-0.011
Annual Standard Deviation
0.357
Annual Variance
0.128
Information Ratio
-3.315
Tracking Error
0.412
Treynor Ratio
147.977
Total Fees
$62.00
using System;
using System.Collections.Generic;
using System.Linq;
using QuantConnect.Data.Market;
using QuantConnect.Data.UniverseSelection;

namespace QuantConnect.Algorithm.CSharp
{
    public class CoarseFundamentalTop5Algorithm : QCAlgorithm
    {
        // initialize our security changes to nothing
        DateTime lastTradeTime;
        SecurityChanges _changes = SecurityChanges.None;
        static readonly decimal EqualWeightPercentage = 1m/3;

        public override void Initialize()
        {
        	
        	SetBrokerageModel(BrokerageName.TradierBrokerage, AccountType.Cash);
        	// this sets the resolution for securities added via universe selection
            UniverseSettings.Resolution = Resolution.Second;

            SetStartDate(2016, 1, 1);
            SetEndDate(DateTime.Now.Date.AddDays(-1));
            SetCash(5000);
            
            foreach (var symbol in _changes.AddedSecurities)
            {
            	var myString = symbol.ToString();
                AddSecurity(SecurityType.Equity, myString, Resolution.Minute, 
                    fillDataForward: true, 
                    extendedMarketHours: false, 
                    leverage: 1
                    );
            }

            // this add universe method accepts a single parameter that is a function that
            // accepts an IEnumerable<CoarseFundamental> and returns IEnumerable<Symbol>
            AddUniverse(CoarseSelectionFunction);
        }

        // sort the data by daily dollar volume and take the top 5 symbols
        public static IEnumerable<Symbol> CoarseSelectionFunction(IEnumerable<CoarseFundamental> coarse)
        {
         return (from stock in coarse
            		orderby stock.Price
            		//function that sorts by percent change
            		//where stock.Price > stock.Price - stock.Value / stock.Price ??
            		
            		where stock.Price < 5 && stock.Price > 2
            		select stock.Symbol).Take(1);
        }

        //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)
        {
        	if (Time - lastTradeTime.Date < TimeSpan.FromDays(1))
            {
                // only trade once a day at market open
                return;
            }
            lastTradeTime = Time;
        	
            // if we have no changes, do nothing
            if (_changes == SecurityChanges.None) return;

            // liquidate removed securities
            /*foreach (var security in _changes.RemovedSecurities)
            {
                if (security.Invested)
                {
                    Liquidate(security.Symbol);
                }
            }*/

            foreach (var security in _changes.AddedSecurities)
            {
            	
            	//var symbolToday = Convert.ToString(_changes.AddedSecurities);
        		//Log("" + symbolToday);
        		var equalWeightedPorfolioSize = Portfolio.TotalPortfolioValue/3;
            	var shareCount = CalculateOrderQuantity(security.Symbol, EqualWeightPercentage);
                //SetHoldings(security.Symbol, 0.25m);

                MarketOrder(security.Symbol, shareCount, tag: "Order Target Value: $" + Math.Round(equalWeightedPorfolioSize, 2));
                

                MarketOnCloseOrder(security.Symbol, -shareCount);
            
            	// submit market on close to liquidate at EOD

            }
                        	
            // you can access the settled only funds using the CashBook
            var settledCash = Portfolio.CashBook["USD"].Amount;
            // you can access the unsettled fund using the UnsettledCashBook
            var unsettledCash = Portfolio.UnsettledCashBook["USD"].Amount;

            _changes = SecurityChanges.None;
        }

        // this event fires whenever we have changes to our universe
        public override void OnSecuritiesChanged(SecurityChanges changes)
        {
            _changes = changes;
        }
        
        public override void OnEndOfDay()
        {
            // at the end of each day log the state of our settled and unsettled cashbooks
            Log(string.Empty);
            Log("-------------------"+Time.Date.ToShortDateString()+"-------------------");
            Log("SETTLED::");
            var settled = Portfolio.CashBook.ToString();
            foreach (var line in settled.Split('\n'))
            {
                Log("    " + line);
            }
            Log(string.Empty);
            Log(string.Empty);
            Log("UNSETTLED::");
            var unsettled = Portfolio.UnsettledCashBook.ToString();
            foreach (var line in unsettled.Split('\n'))
            {
                Log("    " + line);
            }
        }
    }
}