| Overall Statistics |
|
Total Trades 0 Average Win 0% Average Loss 0% Compounding Annual Return 0% Drawdown 0% Expectancy 0 Net Profit 0% Sharpe Ratio 0 Loss Rate 0% Win Rate 0% Profit-Loss Ratio 0 Alpha 0 Beta 0 Annual Standard Deviation 0 Annual Variance 0 Information Ratio 0 Tracking Error 0 Treynor Ratio 0 Total Fees $0.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.five_minutes_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 five_minutes_after_open_market(self):
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[0].Close
last_minute_close = self.minute_rw[0].Close
last_minute_open = self.minute_rw[0].Open
curr_price = self.Securities[self.symbol].Price
gap = (last_close - yesterday_daily_close)/yesterday_daily_close*100
last_minute_bar = last_minute_close - last_minute_open
stop_price06 = yesterday_daily_close*d.Decimal(0.6)
stop_price12 = yesterday_daily_close*d.Decimal(1.2)
stop_price18 = yesterday_daily_close*d.Decimal(1.8)
stop_price24 = yesterday_daily_close*d.Decimal(2.4)
stop_price30 = yesterday_daily_close*d.Decimal(3.0)
stop_price36 = yesterday_daily_close*d.Decimal(3.6)
stop_price42 = yesterday_daily_close*d.Decimal(4.2)
stop_price48 = yesterday_daily_close*d.Decimal(4.8)
if not self.Portfolio[self.symbol].Invested:
if last_close < stop_price06 and last_minute_bar > 0:
self.StopMarketOrder(self.symbol, 60, stop_price06)
self.Log('GOING LONG @ 0.6')
self.Log('Last bar close: {0}'.format(last_close))
self.Log('Pr day close: {0}'.format(yesterday_daily_close))
self.Log('Last min close: {0}'.format(last_minute_close))
self.Log('Last min open: {0}'.format(last_minute_open))
elif last_close < stop_price12 and last_minute_bar > 0:
self.StopMarketOrder(self.symbol, 60, stop_price12)
self.Log('GOING LONG @ 1.2')
#self.SetHoldings(self.symbol, -1)
#self.Log('GOING SHORT')
elif last_close < stop_price18 and last_minute_bar > 0:
self.StopMarketOrder(self.symbol, 60, stop_price18)
self.Log('GOING LONG @ 1.8')
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 and self.minute_rw.IsReady and self.daily_rw.IsReady):
return
# self.Log("haha")
factor = d.Decimal(1.006)
currBar = self.window[0].High
curr_price = data[self.symbol]
last_minute_close = self.minute_rw[0].Close
last_minute_open = self.minute_rw[0].Open
yesterday_daily_close = self.daily_rw[0].Close
last_minute_bar = last_minute_close - last_minute_open
stop_price06 = yesterday_daily_close*d.Decimal(0.6)
stop_price12 = yesterday_daily_close*d.Decimal(1.2)
stop_price18 = yesterday_daily_close*d.Decimal(1.8)
stop_price24 = yesterday_daily_close*d.Decimal(2.4)
stop_price30 = yesterday_daily_close*d.Decimal(3.0)
stop_price36 = yesterday_daily_close*d.Decimal(3.6)
stop_price42 = yesterday_daily_close*d.Decimal(4.2)
stop_price48 = yesterday_daily_close*d.Decimal(4.8)
if not self.Portfolio[self.symbol].Invested:
if curr_price == stop_price06 and last_minute_bar > 0:
self.SetHoldings(self.symbol, 1)
self.Log('GOING LONG @ 0.6')
elif curr_price == stop_price12 and last_minute_bar > 0:
self.SetHoldings(self.symbol, 1)
self.Log('GOING LONG @ 1.2')
elif curr_price == stop_price18 and last_minute_bar > 0:
self.SetHoldings(self.symbol, 1)
self.Log('GOING LONG @ 1.8')
# Every second, check the price and if it's higher than the price the stock was bought for times 1.006, close the position.
if self.Portfolio[self.symbol].Invested and self.Portfolio[self.symbol].AveragePrice * factor <= curr_price:
self.Liquidate(self.symbol)
self.Log('LIQUIDATE AT THRESHOLD REACHED.')