I am trying to record the fill price of symbols so that I can create stop losses and take profit levels from those values. To do so, I am using the OnOrderEvent function just as it is written in the documentation. I'm having trouble getting the symbol variable created in the OnOrderEvent function to be recognized as being equal to a self variable set in the initialization function which contains the same value. Here is the portion of my code involved in this issue:
class VerticalCalibratedAtmosphericScrubbers(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2020, 3, 1) # Set Start Date
self.SetCash(100000) # Set Strategy Cash
self.longetf = "JNUG"
self.shortetf = "JDST"
self.AddEquity(self.longetf, Resolution.Minute)
self.AddEquity(self.shortetf, Resolution.Minute)
self.__last = datetime.min
self.longetfentry = []
self.shortetfentry = []
def OnOrderEvent(self, orderevent):
# check if orderevent is a fill
if orderevent.Status == OrderStatus.Filled:
symbol = orderevent.Symbol
fill_price = orderevent.FillPrice
current_price = self.Securities[symbol].Price
if symbol == self.longetf:
self.longetfentry.append(fill_price)
if symbol == self.shortetf:
self.shortetfentry.append(fill_price)
While debugging the if statements at the bottom of the code, both of the symbol and self.longetf values are identical. However, the if statement does not recognize the two variables as equal and skips over the following code. Why are they not recognized as equal even though the values are identical in the debugger and how do I get the if statement to trigger when the values are equal in this case? Feel like I'm overlooking something very simple here but just can't wrap my mind around how to fix this. I imagine it may have something to do with the quotes in one variable but not the other (even though the debugger shows no quotes in either of the variables). Thanks!
Gahl Goziker
Hi Nathan,
In this algorithm, the OnOrderEvent method will never be called. OnOrderEvent is only called if securities are bought/sold. To address this, add an OnData method, and buy/sell securities in there.Also, once the OnOrderEvent method does get called, orderevent.
Symbol will return an object of the Symbol class. So the if statements will be comparing a Symbol object and a String object when they should be comparing two Symbol objects. To address this, change
self.longetf = "JNUG" self.shortetf = "JDST" self.AddEquity(self.longetf, Resolution.Minute) self.AddEquity(self.shortetf, Resolution.Minute)
to
self.longetf = self.AddEquity("JNUG", Resolution.Minute).Symbol self.shortetf = self.AddEquity("JDST", Resolution.Minute).Symbol
Best,
Gahl Goziker
Nathan Miller
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!