Overall Statistics
from System.Collections.Generic import List
import numpy as np
from QuantConnect.Data.UniverseSelection import *

### <summary>
### Basic template algorithm simply initializes the date range and cash. This is a skeleton
### framework you can use for designing an algorithm.
### </summary>
class CANSLIM_ALGO(QCAlgorithm):
    def Initialize(self):
        '''Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.'''

        self.SetStartDate(2016,1,4)  #Set Start Date
        self.SetEndDate(2018,7,5)    #Set End Date
        self.SetCash(100000)           #Set Strategy Cash
        self.flag1 = 1
        self.flag2 = 0
        self.flag3 = 0
        self.UniverseSettings.Resolution = Resolution.Daily
        self.AddUniverse(self.CoarseSelectionFunction, self.FineSelectionFunction)
        self.Schedule.On(self.DateRules.MonthStart("SPY"), self.TimeRules.AfterMarketOpen("SPY"), Action(self.Rebalancing))
        # Find more symbols here: http://quantconnect.com/data
        self.__numberOfSymbols = 200
        self._changes = None
        self.Debug("numpy test >>> print numpy.pi: " + str(np.pi))
      # sort the data by daily dollar volume and take the top 'NumberOfSymbols'
    def CoarseSelectionFunction(self, coarse):
        if self.flag1:
            CoarseWithFundamental = [x for x in coarse if (x.HasFundamentalData) and (x.DollarVolume > 500000)]
            # return the symbol objects of the top entries from our sorted collection
            return [x.Symbol for x in CoarseWithFundamental]    
            return []
    def FineSelectionFunction(self, fine):
        if self.flag1:
            self.flag1 = 0
            self.flag2 = 1
            filtered_fine = [x for x in fine if x.EarningReports.BasicEPS.OneMonth != 0
                                  and x.EarningReports.BasicEPS.ThreeMonths != 0
                                  and ((x.EarningReports.BasicEPS.OneMonth - x.EarningReports.BasicEPS.ThreeMonths)/ (x.EarningReports.BasicEPS.ThreeMonths)) > 0.25]
            filtered_fine = [x for x in filtered_fine if x.OperationRatios.ROE.ThreeMonths > 0.17] 
            filtered_fine = [x for x in filtered_fine if ((x.FinancialStatements.IncomeStatement.TotalRevenue.OneMonth -x.FinancialStatements.IncomeStatement.TotalRevenue.ThreeMonths)/ (x.FinancialStatements.IncomeStatement.TotalRevenue.ThreeMonths)) >0.25]
            self.flag3 = self.flag3 + 1      
            # take the stock symbols 
            return [x.Symbol for x in filtered_fine]
            return []
    def OnData(self, data):
        '''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.

            data: Slice object keyed by symbol containing the stock data
        if self.flag3 > 0:
            if self.flag2 == 1:
                self.flag2 = 0
                # if we have no changes, do nothing
                if self._changes is None: return

                # liquidate removed securities
                for security in self._changes.RemovedSecurities:
                    if security.Invested:
                        self.Log("Sell security %s" % str(security.Symbol))
                for security in self._changes.AddedSecurities:
                    self.SetHoldings(security.Symbol, 0.8/float(len(self._changes.AddedSecurities)))
                    self.Log("Buy security %s" % str(security.Symbol))
                self._changes = None
    # this event fires whenever we have changes to our universe
    def OnSecuritiesChanged(self, changes):
        self._changes = changes
    def Rebalancing(self):
        self.flag1 = 1 

# Your New Python File