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.)