Created
January 19, 2021 03:34
-
-
Save blitzblade/7498f4d181f2de536717df91678d48d1 to your computer and use it in GitHub Desktop.
#quantconnect #trading #python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import date | |
from dateutil.relativedelta import relativedelta | |
import numpy as np | |
class DynamicHorizontalContainmentField(QCAlgorithm): | |
def Initialize(self): | |
six_months_ago = date.today() + relativedelta(months=-6) | |
self.SetStartDate(six_months_ago) # Set Start Date | |
self.SetEndDate(date.today()) | |
self.SetCash(100) # Set Strategy Cash | |
# self.AddEquity("SPY", Resolution.Minute) | |
self.symbol = self.AddEquity("LTC", Resolution.Daily).Symbol | |
self.lookback = 20 | |
self.ceiling, self.floor = 30, 10 | |
self.initialStopRisk = 0.98 | |
self.tradingStopRisk = 0.9 | |
self.Schedule.On(self.DateRules.EveryDay(self.symbol), \ | |
self.TimeRules.AfterMarketOpen(self.symbol, 20), \ | |
Action(self.EveryMarketOpen)) | |
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 | |
''' | |
self.Plot("Data Price", self.symbol, self.Securities[self.symbol].Close) | |
def EveryMarketOpen(self): | |
close = self.History(self.symbol, 31, Resolution.Daily)["close"] | |
todayvol = np.std(close[1:31]) | |
yesterdayvol = np.std(close[0:30]) | |
deltavol = (todayvol - yesterdayvol) / todayvol | |
self.lookback = round(self.lookback * (1 + deltavol)) | |
if self.lookback > self.ceiling: | |
self.lookback = self.ceiling | |
elif self.lookback < self.floor: | |
self.lookback = self.floor | |
self.high = self.History(self.symbol, self.lookback, Resolution.Daily)["high"] | |
if not self.Securities[self.symbol].Invested and \ | |
self.Securities[self.symbol].Close >= max(self.high[:-1]): | |
self.SetHoldings(self.symbol, 1) | |
self.breakoutlvl = max(self.high[:-1]) | |
self.highestPrice = self.breakoutlvl | |
if not self.Securities[self.symbol].Invested: | |
if not self.Transactions.GetOpenOrders(self.symbol): | |
self.stopMarketTicket = self.StopMarketOrder(self.symbol, \ | |
-self.Portfolio[self.symbol].Quantity, \ | |
self.initialStopRisk * self.breakoutlvl) | |
if self.Securities[self.symbol].Close > self.highestPrice and \ | |
self.initialStopRisk * self.breakoutlvl < self.Securities[self.symbol].Close * self.trailingStopRisk: | |
self.highestPrice = self.Securities[self.symbol].Close | |
updateFields = UpdateOrderFields() | |
updateFields.StopPrice = self.Securities[self.symbol].Close * self.trailingStopRisk | |
self.stopMarketTicket.Update(updateFields) | |
self.Debug(updateFields.StopPrice) | |
self.Plot("Data Chart", "Stop Price", self.stopMarketTicket.Get(OrderFields.StopPrice)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment