Overall Statistics Total Trades45Average Win10.26%Average Loss-0.45%Compounding Annual Return14.082%Drawdown12.300%Expectancy19.689Net Profit509.548%Sharpe Ratio1.359Loss Rate14%Win Rate86%Profit-Loss Ratio22.96Alpha0.08Beta2.847Annual Standard Deviation0.101Annual Variance0.01Information Ratio1.161Tracking Error0.101Treynor Ratio0.048Total Fees\$597.85
```import pandas as pd
import numpy as np
from datetime import timedelta

### <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 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.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage)
self.SetBenchmark("SPY")
self.SetStartDate(2004,12, 1)  #Set Start Date
self.SetEndDate(2018,8,18)    #Set End Date
self.SetCash(100000)           #Set Strategy Cash
self.equity = ['SPY', 'IEF']
self.months = {}
# Find more symbols here: http://quantconnect.com/data
self.file = self.file.split("\n")

i = 0
for row in self.file[1:]:
one_row = row.split(",")
i += 1

def OnData(self, data):
'''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.

Arguments:
data: Slice object keyed by symbol containing the stock data
'''
date_today = self.Time.date()
date_today = date_today.strftime(format='%Y-%m-%d')
date_today = date_today[0:7]

try:
invested = self.months[date_today]
except:
invested = "No"
if self.Time.hour == 15 and invested == "No":

if self.Portfolio[self.equity].Quantity > 0 and signal > 0:
self.Liquidate(self.equity)
if self.Portfolio[self.equity].Quantity > 0 and signal < 0:
self.Liquidate(self.equity)

if signal < 0 and self.Portfolio[self.equity].Quantity == 0:
self.SetHoldings(self.equity, 1)
self.months[date_today] = "Yes"
return
if signal > 0 and self.Portfolio[self.equity].Quantity == 0:
self.SetHoldings(self.equity, 1)
self.months[date_today] = "Yes"
return```