Overall Statistics
Total Trades
37
Average Win
3.97%
Average Loss
-2.22%
Compounding Annual Return
15.224%
Drawdown
9.700%
Expectancy
0.393
Net Profit
15.837%
Sharpe Ratio
1.174
Loss Rate
50%
Win Rate
50%
Profit-Loss Ratio
1.79
Alpha
0.02
Beta
0.98
Annual Standard Deviation
0.126
Annual Variance
0.016
Information Ratio
0.936
Tracking Error
0.019
Treynor Ratio
0.151
Total Fees
$37.00
import numpy as np
import math

class BasicTemplateAlgorithm(QCAlgorithm):
	
	def __init__(self):
		self.stoploss = 0.02
		self.take_profit = 0.04
		self.buy_order = None
		self.loss_order = None
		self.profit_order = None

	def Initialize(self):

		self.SetCash(25000)
		self.SetStartDate(2016,1,1)
		self.SetEndDate(2017,1,15)
		
		self.spy = self.AddEquity("SPY", Resolution.Minute).Symbol
		self.wil = self.WILR(self.spy,9,Resolution.Minute)

	def OnData(self, slice):
		if not self.wil.IsReady:
			return
		
		if not self.Securities[self.spy].Exchange.ExchangeOpen:
			return
		
		if (not self.Portfolio.HoldStock) and (float(str(self.wil.Current.Value)) < -80):
			price = float(slice[self.spy].Close)
			
			quantity = math.floor(float(self.Portfolio.Cash)/price)
			self.buy_order = self.Order(self.spy, quantity)
			self.loss_order = self.StopMarketOrder(self.spy,-quantity, price*(1-self.stoploss))
			self.profit_order = self.LimitOrder(self.spy, -quantity, price*(1+self.take_profit))
			
			self.Log('Purchased SPY on'+str(self.buy_order.OrderId))
			self.Log('Stop loss'+str(self.loss_order))
			self.Log('Take Profit'+str(self.profit_order))
			return
			
	
	def OnOrderEvent(self,event):
		
		if event.Status not in (2,3):
			return
		
		if (self.profit_order) == None or (self.loss_order) == None:
			return
		
		filled = event.OrderId
		
		if self.profit_order == filled:
			self.loss_order.Cancel()
			self.Log(str('loss order canceled'))

		elif self.loss_order.OrderId == filled:
			self.profit_order.Cancel()
			self.Log(str('profit order canceled'))
			
		else:
			return