Overall Statistics
Total Trades
32
Average Win
0%
Average Loss
-0.04%
Compounding Annual Return
-9.349%
Drawdown
0.600%
Expectancy
-1
Net Profit
-0.640%
Sharpe Ratio
-29.98
Loss Rate
100%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
-0.09
Beta
0
Annual Standard Deviation
0.003
Annual Variance
0
Information Ratio
-0.057
Tracking Error
0.187
Treynor Ratio
-210.546
Total Fees
$32.00
using System;
using System.Collections.Generic;
using System.Linq;
using QuantConnect.Data.Market;
using QuantConnect.Data.UniverseSelection;
using QuantConnect.Orders;
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, 2, 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)
        {
        	
        	var closedTrades = TradeBuilder.ClosedTrades;
        	
        	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)
            {
            Log("Removed " + security);
                /*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);
				if (Portfolio.Cash > 0)
				{
                MarketOrder(security.Symbol, shareCount, tag: "Order Target Value: $" + Math.Round(equalWeightedPorfolioSize, 2));
				}
				
                
			    var test = Portfolio.TotalProfit;
			    var threePercent = test * .3m;
			    
                if (test < 30 && Portfolio.HoldStock)
                {
                 Liquidate(security.Symbol);
                 Log("Total Profit " + test);
                }
				if (Portfolio.HoldStock)
				{
                MarketOnCloseOrder(security.Symbol, -shareCount);
                
				}
            }
                        	
            // 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 OnOrderEvent(OrderEvent fill)
        {
        	if (fill.Status.IsFill())
        	{
        		Log("Fill Price " + fill.FillPrice);
        	}
        }
        
        /*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);
            }
        }*/
    }
}