| Overall Statistics |
|
Total Trades 570 Average Win 1.43% Average Loss -1.30% Compounding Annual Return 3.502% Drawdown 28.800% Expectancy 0.049 Net Profit 13.900% Sharpe Ratio 0.264 Loss Rate 50% Win Rate 50% Profit-Loss Ratio 1.11 Alpha 0.035 Beta 0.057 Annual Standard Deviation 0.148 Annual Variance 0.022 Information Ratio -0.196 Tracking Error 0.182 Treynor Ratio 0.687 Total Fees $12363.39 |
#
# QuantConnect Basic Template:
# Fundamentals to using a QuantConnect algorithm.
#
# You can view the QCAlgorithm base class on Github:
# https://github.com/QuantConnect/Lean/tree/master/Algorithm
#
import numpy as np
class BasicTemplateAlgorithm(QCAlgorithm):
def Initialize(self):
self.uso = self.AddEquity('UCO', Resolution.Minute) #United States Oil Fund (USO) etf, data from 2006-04-10
# Set the cash we'd like to use for our backtest #Proshares Ultra Bloomber Crude Oil (UCO) etf, data from 2008-11-25
# This is ignored in live trading
self.SetCash(100000)
self.SetStartDate(2013,11,27) #start one day after launch as we need one day of history #2008,11,26
self.SetEndDate(2017,9,1) #2013,11,26
#schedule opening function every day 15 minutes before market close
self.Schedule.On(self.DateRules.EveryDay('UCO'), self.TimeRules.BeforeMarketClose('UCO', 165), Action(self.trade_fn))
def trade_fn(self):
uso_hist = self.History('UCO', 1, Resolution.Daily)
for slice in uso_hist:
self.close = slice.Close
y_close = self.close
price = self.Securities['UCO'].Price
dec_change = (price - y_close) / y_close
if dec_change >= 0.035:
self.SetHoldings('UCO', 1)
holdings = self.Portfolio['UCO'].Quantity
if holdings != 0:
self.MarketOnCloseOrder('UCO', -holdings)
if dec_change <= -0.035:
self.SetHoldings('UCO', -1)
holdings = self.Portfolio['UCO'].Quantity
if holdings != 0:
self.MarketOnCloseOrder('UCO', -holdings)
def OnData(self, slice):
pass