Overall Statistics
from QuantConnect.Indicators import *

class BasicTemplateAlgorithm(QCAlgorithm):
    '''Basic template algorithm simply initializes the date range and cash'''

    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(2015,9,1)  #Set Start Date
        self.SetEndDate(2018,5,15)    #Set End Date
        self.SetCash(100000)           #Set Strategy Cash
        # Find more symbols here: http://quantconnect.com/data
        self.AddEquity("PFE", Resolution.Daily)
        self.BollingerBand = self.BB("PFE",20,2,MovingAverageType.Simple,Resolution.Daily)
        self.Strength = self.RSI("PFE",14,MovingAverageType.Simple,Resolution.Daily)
        self.SetWarmUp(20)
        self.SetBenchmark("SPY")

    def OnData(self, data):
        rsi = self.Strength.Current.Value
        lower = self.BollingerBand.LowerBand.Current.Value
        upper = self.BollingerBand.UpperBand.Current.Value
        middle = self.BollingerBand.MiddleBand.Current.Value
        current = data["PFE"].Close

        
        #need to check when to go long
        if not self.Portfolio.Invested:
            if current < lower and rsi < 40:
                self.SetHoldings("PFE", 1)
            if current > upper and rsi > 60:
                self.SetHoldings("PFE", -1)
            
        if self.Portfolio.Invested:
            if self.Portfolio["PFE"].IsLong:
                if current > middle:
                    self.Liquidate("PFE")
            if self.Portfolio["PFE"].IsShort:
                if current < middle:
                    self.Liquidate("PFE")
import numpy as np
from datetime import datetime
import pandas as pd


class MovingAverage(QCAlgorithm):

 

    def Initialize(self):
      
        self.SetStartDate(2007,1,1)  #Set Start Date
        #self.SetEndDate(2018,10,29)    #Set End Date
        self.SetCash(10000)           #Set Strategy Cash
        self.spy = self.AddEquity("SPY", Resolution.Minute).Symbol
        self.ief = self.AddEquity("IEF", Resolution.Minute).Symbol
        self.SetWarmUp(2860,Resolution.Hour)
        self.previous = None
        self.position = None
        self.rsi_spy = self.RSI("SPY", 35, MovingAverageType.Simple,Resolution.Hour)
        #day--warmup 440,28
        #hour--warmup 2860,35=39,862.6
       
     
       
    def OnData(self, data):
        if self.IsWarmingUp: return         
        if data.Bars.ContainsKey("SPY") or data.Bars.ContainsKey("IEF"):
            return
        
        
        
        if self.rsi_spy.Current.Value >= 70:
                
                if self.position == None:
                    self.SetHoldings("IEF", 1)
                elif self.position == "SPY":
                    self.Liquidate("SPY")  
                    self.SetHoldings("IEF", 1)
                self.position = "IEF"
                  
                  
        if self.rsi_spy.Current.Value <= 40:
                
                if self.position == None:
                    self.SetHoldings("SPY", 1)
                elif self.position == "IEF":
                    self.Liquidate("IEF")  
                    self.SetHoldings("SPY", 1)
                self.position = "SPY"