Couple of potential issues that jump out and you may want to explore:
- classified is not defined in the example, but in your actual algo when it is not invested and classified is True:
else:
if classified:
self.MarketOrder(self.stock.Symbol, 300, False,"Buy Long")
self.Plot('Stock Plot', 'Buy', barclose)
if classified:
self.MarketOrder(self.stock.Symbol, -300, False,"Sell Short")
self.Plot('Stock Plot', 'Sell', barclose)
Both of these will hit - i.e. the algorithm would Long 300 units, wait for it to fill (default 5 seconds since you have the asynchronous == False flag), then Short 300 units. Since you are using secondly resolution data (meaning OnData is supposed to be called every second), I have no idea what would happen but seems a bit messy. Did you mean to define two separate conditions, i.e. classifiedLong and classifiedShort.?
- The if statement right above this seems problematic too:
if self.stock.Invested:
if self.Portfolio[self.stock.Symbol].IsShort and classified:
self.Liquidate()
if self.Portfolio[self.stock.Symbol].IsLong and classified:
self.Liquidate()
When classified is True, and the previous order went through (ignoring the 5 second fill time/OnData issue), with the next second you would immediately liquidate the symbol. Then the next second, the cycle repeats - spreads would definitely be a big factor in this case even with Limit Orders.
Possibly, this sort of high frequency trading with weird interactions between fill times and new orders being placed could be causing the issue. Most other backtesters I've used don't model in fill times (among many other things), so maybe that is why the backtest results here are inconsistent with the other one you used?