| Overall Statistics |
|
Total Trades 9 Average Win 0.39% Average Loss -0.62% Compounding Annual Return -20.439% Drawdown 3.400% Expectancy -0.187 Net Profit -1.492% Sharpe Ratio -1.801 Loss Rate 50% Win Rate 50% Profit-Loss Ratio 0.63 Alpha -0.077 Beta -0.273 Annual Standard Deviation 0.098 Annual Variance 0.01 Information Ratio -4.25 Tracking Error 0.126 Treynor Ratio 0.645 Total Fees $9.00 |
#
# 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
#
from QuantConnect.Data.Market import TradeBar
from datetime import timedelta
from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
import decimal as d
class MyAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2017, 8, 21) # Set Start Date
self.SetEndDate(2017, 9, 13)
self.SetCash(10000) # Set Strategy Cash
self.symbol = self.AddEquity("AAPL", Resolution.Second).Symbol
consolidator_daily = TradeBarConsolidator(timedelta(1))
consolidator_daily.DataConsolidated += self.OnDailyData
self.SubscriptionManager.AddConsolidator(self.symbol, consolidator_daily)
consolidator_minute = TradeBarConsolidator(60)
consolidator_minute.DataConsolidated += self.OnMinuteData
self.SubscriptionManager.AddConsolidator(self.symbol, consolidator_minute)
self.daily_rw = RollingWindow[TradeBar](2)
self.minute_rw = RollingWindow[TradeBar](2)
self.window = RollingWindow[TradeBar](2)
self.Schedule.On(self.DateRules.EveryDay(),
self.TimeRules.AfterMarketOpen(self.symbol, 5),
Action(self.one_minute_after_open_market))
self.Schedule.On(self.DateRules.EveryDay(),
self.TimeRules.BeforeMarketClose(self.symbol, 1),
Action(self.before_close_market))
# Add daily bar to daily rolling window
def OnDailyData(self, sender, bar):
self.daily_rw.Add(bar)
def OnMinuteData(self, sender, bar):
self.minute_rw.Add(bar)
def one_minute_after_open_market(self):
"""
At 9:31 check if there has been a gap at the market open from the previous day.
If so and the stock is gapping up and the first minute bar is negative, create a short selling signal.
If the stock is gapping down and the first minute bar is positive, create a buying signal.
"""
if not (self.window.IsReady and self.daily_rw.IsReady and self.minute_rw.IsReady): return
last_close = self.window[0].Close
#self.Log(last_close)
yesterday_daily_close = self.daily_rw[1].Close
first_minute_close = self.minute_rw[1].Close
first_minute_open = self.minute_rw[1].Open
gap = last_close - yesterday_daily_close
first_minute_bar = first_minute_close - first_minute_open
if not self.Portfolio[self.symbol].Invested:
# If the stock is gapping down and the first minute bar is positive, create a buying signal.
if gap < 0 and first_minute_bar > 0:
self.SetHoldings(self.symbol, 1)
self.Log('GOING LONG')
self.Log('Last bar close: {0}'.format(last_close))
self.Log('Pr day close: {0}'.format(yesterday_daily_close))
self.Log('First min close: {0}'.format(first_minute_close))
self.Log('First min open: {0}'.format(first_minute_open))
# If the stock is gapping up and the first minute bar is negative, create a short selling signal
elif gap > 0 and first_minute_bar < 0:
self.SetHoldings(self.symbol, -1)
self.Log('GOING SHORT')
def before_close_market(self):
"""
At the end of the day, if there is a short position, close it.
"""
if self.Portfolio[self.symbol].IsShort:
self.Liquidate(self.symbol)
self.Log('LIQUIDATE SHORT End of Day')
# Add second bar to window rolling window
def OnData(self, data):
if data[self.symbol] is None:
return
self.window.Add(data[self.symbol])
if not (self.window.IsReady):
return
# self.Log("haha")
factor = d.Decimal(1.01)
currBar = self.window[0].Close
# Every second, check the price and if it's higher than the price the stock was bought for times 1.01, close the position.
if self.Portfolio[self.symbol].Invested and self.Portfolio[self.symbol].AveragePrice * factor < currBar:
self.Liquidate(self.symbol)
self.Log('LIQUIDATE AT THRESHOLD REACHED.')