Hi everyone,
I am trying to use the Fill Price from a market order to create a stop. Basically what I am doing is multiplying a percentage by the Fill Price to create a stop but for some reason I can't get it to work. Does anyone have any tips on how I can get this done?
Nicholas Stern
I have attached some updated code that seems to be better.
Ian Larson
Hi Nicholas, I'm not an expert but going through your new code the following have come to my attention:
1) Instead of:
for Equity in self.Equities:
self.AddEquity("AAPL", Resolution.Minute)
Since you've already declared a list of equities to add (self.Equities = ["AAPL"]), use:
for Equity in self.Equities:
self.AddEquity(Equity, Resolution.Minute)
Â
2) I used (may be better because if there is a partial fill your code will execute):
def OnOrderEvent(self, orderEvent):
if orderEvent.Status == OrderStatus.Filled: #only handle filled orders
instead of:
if OrderEvent.FillQuantity == 0:
return
Â
3) This kind of thing could be cleaned up a bit!:
#let's separate the following into discrete conditions
if self.Window[1].Low < self.Window[2].Low and self.Window[0].Low < self.Window[1].Low and self.Securities["AAPL"].Open < self.Window[0].Low and self.Securities["AAPL"].Price > self.Window[0].Close:
self.MarkOrder = self.MarketOrder("AAPL", 100);
#for instance using a dictionary:
c = { #c is short for condition!
1:self.Window[1].Low < self.Window[2].Low,
2:self.Window[0].Low < self.Window[1].Low,
3:self.Securities["AAPL"].Open < self.Window[0].Low,
3:self.Securities["AAPL"].Price > self.Window[0].Close,
}
if c[1] and c[2] and c[3] and c[4]: #>>Be aware these are not indicies like in a list, they are dictionary keys
self.MarkOrder = self.MarketOrder("AAPL", 100);
#alternatively
if all(c[1], c[2], c[3], c[4]):
self.MarkOrder = self.MarketOrder("AAPL", 100);
## Or even more cleanly using just a list:
c = [ #c is short for condition!
self.Window[1].Low < self.Window[2].Low,
self.Window[0].Low < self.Window[1].Low,
self.Securities["AAPL"].Open < self.Window[0].Low,
self.Securities["AAPL"].Price > self.Window[0].Close,
]
if all(c):
self.MarkOrder = self.MarketOrder("AAPL", 100);
4) I can't see why your system for stop at TP orders isn't working. I personally would probably submit both an actual stop and limit order on complete fill of your original, then liquidate if either is filled:
# In initialize:
self.__openOrders = [] #the __ is just to denote we shouldn't manipulate this manually
#then in OnOrderEvent:
#after your code to calculate stop price, profit price, logging
self.Equity = "AAPL" #in this case just to make it work with my code
shares = 50 #is this correct?
#limit order
newTicket = self.LimitOrder(self.Equity, shares, ProfitPrice )
self.__opentOrders.append(newTicket)
#stop (market sell)
newTicket = self.StopMarketOrder(self.Equity, -shares, StopPrice)
self.__openOrders.append(newTicket
#then I would monitor like so:
#use protective code to liquidate on stop or take profit fill events:
if len(self.__openOrders) <2: #liquidates if the stop or limit is taken out
self.Liquidate()
self.Log("<2")
Although this may have the disadvantage of not letting you use full margin because there are 2 open orders.
(unfortunately editing this answer removed all the indentation.)
Nicholas Stern
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!