Overall Statistics
Total Trades
8
Average Win
0%
Average Loss
0%
Compounding Annual Return
69.677%
Drawdown
0.900%
Expectancy
0
Net Profit
0%
Sharpe Ratio
7.037
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0.763
Beta
1.005
Annual Standard Deviation
0.104
Annual Variance
0.011
Information Ratio
64.241
Tracking Error
0.012
Treynor Ratio
0.731
Total Fees
$10.26
#   QuantConnect Basic Template:
#    Fundamentals to using a QuantConnect algorithm.
#
#    You can view the QCAlgorithm base class on Github: 
'''
error at 

Runtime Error: Python.Runtime.PythonException: KeyNotFoundException : 'TMF' wasn't found in the Slice object, likely because there was no-data at this moment in time and it wasn't possible to fillforward historical data. Please check the data exists before accessing it with data.ContainsKey("TMF")

'''

import numpy as np
import pandas as pd

from datetime import datetime

from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Indicators")
AddReference("QuantConnect.Common")


from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Data import *
from QuantConnect.Indicators import *
from QuantConnect.Orders import *
from QuantConnect.Securities import *


class BasicTemplateAlgorithm(QCAlgorithm):

    def Initialize(self):
        # Set the cash we'd like to use for our backtest
        # This is ignored in live trading 
        self.SetCash(100000)
        
        # Start and end dates for the backtest.
        # These are ignored in live trading.
        self.SetStartDate(2016,7,1)
        self.SetEndDate(2017,1,1)
        
        # Add assets you'd like to see
        self.spy = self.AddEquity("SPY", Resolution.Minute).Symbol
        
                
        self.bullish_stock =  self.AddEquity("TQQQ", Resolution.Minute).Symbol 
        self.bearish_stock =  self.AddEquity("TMF", Resolution.Minute).Symbol 
        self.small_cap_stock =  self.AddEquity("TNA", Resolution.Minute).Symbol 
        self.mid_cap_stock =  self.AddEquity("IJH", Resolution.Minute).Symbol 
        self.vxx =  self.AddEquity("VXX", Resolution.Minute).Symbol 
        self.xiv=  self.AddEquity("XIV", Resolution.Minute).Symbol 
        self.spy =  self.AddEquity("TQQQ", Resolution.Minute).Symbol 
        self.shortSpy =  self.AddEquity("TLT", Resolution.Minute).Symbol 

        self.stocks = [self.bullish_stock,
                          self.bearish_stock,
                          self.small_cap_stock,
                          self.mid_cap_stock,
                          self.xiv]        

        self.skippedSecurities =  [
            self.AddEquity("AGG", Resolution.Minute).Symbol,
            self.AddEquity("TIPS", Resolution.Minute).Symbol 
            ]
            
        self.permanentStocks=  [
            self.AddEquity("AGG", Resolution.Minute).Symbol,
            self.AddEquity("TIPS", Resolution.Minute).Symbol,
            self.AddEquity("TLT", Resolution.Minute).Symbol,
            self.AddEquity("SDY", Resolution.Minute).Symbol            
            ]     

        self.enableProfitTaking = True
   
        for i in range(20, 390, 60):    # (low, high, every i minutes)
            if not self.enableProfitTaking:
                continue    # uncomment to deactivate
            self.Schedule.On(self.DateRules.EveryDay(),self.TimeRules.AfterMarketOpen("SPY"),Action(self.take_profit))   

        #error here
        '''

        

        for i in range(20, 390, 60):    # (low, high, every i minutes)
            if not self.enableProfitTaking:
                continue    # uncomment to deactivate
            self.Schedule.On(self.DateRules.EveryDay(),self.TimeRules.AfterMarketOpen("SPY"),Action(self.limit_losses))   
        #following enables trading every Monday or other day if Monday holiday  
        self.DateRules.WeekStart(),self.TimeRules.BeforeMarketOpen("SPY",10),Action(self.set_is_first_of_week)) 
        self.DateRules.WeekStart(),self.TimeRules.BeforeMarketOpen("SPY",10),Action(self.set_is_trading_day)) 
        


        '''
    # Event handler for the slice method
    def OnData(self, slice):      # Simple buy and hold template
        self.data = slice
        if not self.Portfolio.Invested:
            pLen=len(self.permanentStocks)
            for s in self.permanentStocks:
                self.SetHoldings(s, 0.5/pLen)                
            for s in self.stocks:
                self.SetHoldings(s, 0.10)


        self.Debug("numpy test >>> print numpy.pi: " + str(np.pi))




    def take_profit(self):    # Close some positions to take profit
        #pos     = self.portfolio.positions
        #if len(pos)== 0:
            #return


        for s in self.stocks:
            if not self.Portfolio.ContainsKey(s):
                continue            
            if self.Portfolio[s].Invested:
                # Get last 10 bars of any positioin held , at specific resolution.
                #history = self.PullHistory(s,10,Resolution.Daily,'close')  
                history = self.PullHistory(s,10,Resolution.Minute,'close')  

                #if self.slope(history)      > 0: continue
                #if self.slope(history[-5:]) > 0: continue
                if history[-1] > history[-2]: continue
                prc = history[-1]
                amt = self.Portfolio[s].Quantity
        ''' 
                if (amt / abs(amt)) * ((prc / pos[s].cost_basis) - 1) > self.profit_threshhold:
                    self.log("taking profit at price of  {0} above threshhold of {1} for symbol {2}".format(prc,self.profit_threshhold, s.symbol ))    
                    limitprice= pos[s].cost_basis + ( (self.profit_threshhold *0.80) * prc) 
                    #limitprice= pos[s].cost_basis + ( (self.profit_threshhold/2) * prc) 
                    order_target_percent(s, 0.0,  limit_price= limitprice)
                    self.maxprice[s.symbol] = 0
                    wait(self, s, 1)    # start wait
        '''
                



    def PullHistory(self, syl, periods, resolution, pricetype):
    
        open,close,high,low =[],[],[],[]
        history = self.History(periods, resolution)

        
        for slice in history:
            bar = slice[syl]
            open.append(bar.Open)
            close.append(bar.Close)
            high.append(bar.High)
            low.append(bar.Low)    
        histdict = {'open':open,'close':close,'high':high,'low':low}
        self.Log(str(history))
        return histdict[pricetype]

    import statsmodels.api as sm
    def slope(in_list):     # Return slope of regression line. [Make sure this list contains no nans]
        return sm.OLS(in_list, sm.add_constant(range(-len(in_list) + 1, 1))).fit().params[-1]  # slope