Hello,
Thanks for anyone who can help. Its really much appreciated :)
I have a Parabolic SAR algorithm. If PSAR < Price, I want it to buy with a limit order 2 cents above current price--
if self.Portfolio["TCMD"].Quantity <= 0 :
if self.psar.Current.Value < self.Securities["TCMD"].Price:
self.Log("BUY >> {0}".format(self.Securities["TCMD"].Close))
self.LimitOrder(self.forex.Symbol, 1, ( self.Securities["TCMD"].Price + .02))
This triggers Lean to send a limit order in for 1 share, once every minute, for the rest of the day. I end up with 24 shares after I realise, all orders for 1 share each.
Can someone help me so that it doesnt spam orders?
Thanks for any and all help-
Rahul Chowdhury
Hey Christian,
If a limit order is not filled right away, the algorithm needs to know that a limit order has already been placed. We can use self.ticket to keep track of our limit order. (i.e. If self.ticket is not None there is a limit order placed and if it is None, there is no limit order.) We could also have used a boolean to accomplish the same thing, but this method also lets us control the attributes of our limit order after its been placed if we need to.
class MyAlgo(QCAlgorithm): def Initialize(self): self.SetStartDate(2019, 11, 20) #Set Start Date self.SetCash(200) #Set Strategy Cash equity = self.AddEquity("TCMD") equity.FeeModel = ConstantFeeModel(.02) self.symbol = equity.Symbol self.psar = self.PSAR(self.symbol, .005, .005, .05) self.ticket = None # define a small tolerance on our checks to avoid bouncing def OnData(self, data): holdings = self.Portfolio[self.symbol]; price = holdings.Price if (self.ticket is None and holdings.Quantity <= 0 and self.psar.Current.Value < price): self.ticket = self.LimitOrder(self.symbol, 1, price + .02) if (self.ticket is None and holdings.Quantity > 0 and self.psar.Current.Value > price): self.ticket = self.LimitOrder(self.symbol, -1, price - .02) def OnOrderEvent(self, orderEvent): if orderEvent.Status == OrderStatus.Filled and orderEvent.OrderId == self.ticket.OrderId: self.ticket = None
Best,
Rahul
Christian Olsen
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!