Hi, Guys! Need some help trying to realize some simple stuff: buy AAPL stock if close > open*1.01, exit - first hour minimum.
First of all, got some bug.. don't understand why it happend, because part of backtesting was good, so I got some in and out orders.
Secondly, I don't understand why opening price differs from TradingView, it should be the same, beacause DataNormalizationMode ( set to Raw);
But in Log: 2021-06-08 09:35:00 Open price: @126.6, in Tradinview: 127,;
class VirtualLightBrownAnguilline(QCAlgorithm):
# 1 stock algo
# 5 Minute resoultion
# Buy if change from the open > 1%
# Sell stop - 1 hour min
# How to make it effective?
openingBar = None
def Initialize(self):
self.SetStartDate(2021, 5, 24) # Set Start Date
self.SetCash(100000) # Set Strategy Cash
self.symbol = "AAPL"
self.aapl = self.AddEquity(self.symbol, Resolution.Minute)
self.Securities[self.symbol].SetDataNormalizationMode(DataNormalizationMode.Raw);
self.SetBenchmark("SPY")
self.fh1min = 0
# self.SetBenchmark("QQQ")
self.Consolidate(self.symbol, timedelta(minutes=5), self.OnDataConsolidated)
def OnData(self, data):
# if current price greater than open at 1% - open long position
if not self.Portfolio.Invested:
if (self.openingBar != None and data[self.symbol].Close > self.openingBar.Open*1.01):
self.SetHoldings(self.symbol, 1)
else:
# close if current price less than 1HMin.
if ((self.Time.hour==10 and self.Time.minute >=30) or
(self.Time.hour>10)):
if (data[self.symbol].Low < self.fh1min):
self.Liquidate()
# if not self.Portfolio.Invested:
# self.SetHoldings(self.aapl.Symbol, 1)
# Create a function OnDataConsolidator which saves the currentBar as bar
def OnDataConsolidated(self, bar):
if bar.Time.hour == 9 and bar.Time.minute == 30:
self.openingBar = bar
self.fh1min = bar.Low
self.Log("Open price: @" + str(self.openingBar.Open))
else:
if (bar.Time.hour == 9 and bar.Time.minute >30) or (bar.Time.hour==10 and bar.Time.minute <=30):
if (bar.Low < self.fh1min):
fh1min = bar.Low
Fred Painchaud
Hi,
Try this:
Sorry. Don't have time right now to create a project and test. So, it might not compile as-is but you should get the idea.
Fred
Ilya mart
Ilya mart
Tried to modify it, but got the same error message,
Fred Painchaud
Yes. data[self.symbol] can return None which is why I have protected the code with an if in my post. You need to check if data contains your key and if the associated value returned is not None, in that order, as I did…
Sorry for the incorrect indentation in my posted code above - this forum does not see a tab as 4 spaces in code blocks, which for Python, would be very appropriate.
Fred
Fred Painchaud
Btw. you should not use this:
You should use the Symbol object returned by a Security, as in my code.
Fred
Ilya mart
Fred, your code doesn't work
During the algorithm initialization, the following exception has occurred: AttributeError : 'VirtualLightBrownAnguilline' object has no attribute 'aapl' at Initialize self.symbol = self.aapl.Symbol === at Python.Runtime.PyObject.Invoke(PyTuple args in main.py: line 14 AttributeError : 'VirtualLightBrownAnguilline' object has no attribute 'aapl'
Fred Painchaud
Hello Ilya,
self.aapl should be created the line before that one…
The only error I got was that I wrote “ConstainsKey” instead of “ContainsKey”.
Fred
Ilya mart
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!