Overall Statistics
Total Trades
2
Average Win
0%
Average Loss
-0.01%
Compounding Annual Return
-0.029%
Drawdown
0.000%
Expectancy
-1
Net Profit
-0.014%
Sharpe Ratio
-1.414
Loss Rate
100%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
-0.001
Beta
0.012
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
-25.673
Tracking Error
0.001
Treynor Ratio
-0.024
Total Fees
$2.00
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Common")

from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Data import SubscriptionDataSource
from QuantConnect.Python import PythonData
import datetime
from System.Collections.Generic import List
from QuantConnect.Algorithm import QCAlgorithm
from QuantConnect.Data.UniverseSelection import *
import decimal as d
import numpy as np
import math
import json

class DropboxBaseDataUniverseSelectionAlgorithm(QCAlgorithm):

    def Initialize(self):
        
        self.SetCash(10000.00)
        self._changes = None
        self.UniverseSettings.Resolution = Resolution.Minute;

        
        #self.SetStartDate(2018,1,1)
        #self.SetEndDate(2018,3,31)
        
        start_date_str = '2018-01-04'
        end_date_str = '2018-01-04'
        
        start_datetime = datetime.datetime.strptime(start_date_str,'%Y-%m-%d')
        end_datetime = datetime.datetime.strptime(end_date_str,'%Y-%m-%d')
        window_size = 90
        
        start_datetime = start_datetime - datetime.timedelta(days=window_size*2+1)
        self.SetStartDate(start_datetime)
        self.SetEndDate(end_datetime)
        
        self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage, AccountType.Cash)
        
        self.AddUniverse(StockDataSource, "my-stock-data-source", self.stockDataSource)
        
        # Add SPY just to get access to the AfterMarketOpen and BeforeMarketClose calls.
        self._spy = self.AddEquity("SPY", Resolution.Minute)
        self._blacklist = [self._spy]
        
        self.Schedule.On(self.DateRules.EveryDay("SPY"), 
            self.TimeRules.AfterMarketOpen("SPY",1),
            Action(self.on_market_opened))
        self.Schedule.On(self.DateRules.EveryDay("SPY"), 
            self.TimeRules.BeforeMarketClose("SPY",16),
            Action(self.on_before_market_closes))
        
    def stockDataSource(self, data):
        list = []
        for item in data:
            for symbol in item["Symbols"]:
                list.append(symbol)
        return list

    def OnData(self, slice):
        
        #for key in slice.Keys:
        #    self.Log('{} Data for symbol: {}'.format(str(self.Time), key))

        if slice.Bars.Count == 0: return
        if self._changes == None: return
        
        # start fresh
        #self.Liquidate()

        #percentage = 1 / d.Decimal(slice.Bars.Count)
        #for tradeBar in slice.Bars.Values:
        #    self.SetHoldings(tradeBar.Symbol, percentage)
        
        # reset changes
        self._changes = None
    
    def OnSecuritiesChanged(self, changes):
        self._changes = changes
        self.Debug('{} Universe Changes: {}'.format(str(self.Time), changes))
        
    def on_market_opened(self):
        #self.Debug('{} on_market_open'.format(str(self.Time)))
        if self._changes == None: 
            return
        
        for security in self._changes.AddedSecurities:
            if self.trading_criteria_met(security):
                self.SetHoldings(security.Symbol, 0.01)
                
    def trading_criteria_met(self, security):
        if 'CMC' not in security.Symbol.Value:
            return False
        
        if security.Invested:
            return False
        
        if security in self._blacklist:
            return False
            
        if not self.criteria3(security):
            return False
        
        return True
        
    def criteria3(self, security):

        self.Debug('security.Symbol: {}'.format(security.Symbol))
        self.Debug('security.Open: {}'.format(security.Open))
        
        # Get a multi index dataframe
        history_midf = self.History( [ security.Symbol ] , 90, Resolution.Daily)
        self.Debug('{}'.format(history_midf))
        
        ### Uncomment these lines to see the error:
        
        # Select the security close and get a single index dataframe
        # close_df = history_midf[security.Symbol]['close']
        # self.Debug('{}'.format(close_df))
        
        return True
                    
    def on_before_market_closes(self):
        for holding in self.Portfolio.Values:
            if holding.HoldStock:
                #self.SetHoldings(s,0.0, tag="ScheduledEvent EOD Liquidate")
                self.MarketOnCloseOrder(holding.Symbol, -holding.Quantity)

    def OnOrderEvent(self, orderEvent):
        self.Log(str(orderEvent))
                
class StockDataSource(PythonData):
    
    def GetSource(self, config, date, isLiveMode):
        if isLiveMode:
            url = None
        else:
            url = "https://www.dropbox.com/s/e5jyy6w5rsk03cf/e0007f.csv?dl=1"
            
        return SubscriptionDataSource(url, SubscriptionTransportMedium.RemoteFile)
    
    def Reader(self, config, line, date, isLiveMode):
        if not (line.strip() and line[0].isdigit()): return None
        
        stocks = StockDataSource()
        stocks.Symbol = config.Symbol
        
        csv = line.split(',')
        if isLiveMode:
            stocks.Time = date
            stocks["Symbols"] = csv
        else:
            stocks.Time = datetime.datetime.strptime(csv[0], "%Y-%m-%d")
            stocks["Symbols"] = csv[1:]
        return stocks