Overall Statistics
Total Trades
27
Average Win
9.80%
Average Loss
-7.89%
Compounding Annual Return
7.120%
Drawdown
54.500%
Expectancy
0.897
Net Profit
128.478%
Sharpe Ratio
0.475
Loss Rate
15%
Win Rate
85%
Profit-Loss Ratio
1.24
Alpha
0.146
Beta
-3.043
Annual Standard Deviation
0.179
Annual Variance
0.032
Information Ratio
0.363
Tracking Error
0.179
Treynor Ratio
-0.028
Total Fees
$27.10
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"
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")