Disclaimer: I'm a beginner

Hey, I've been working on this algorithm, that trades BTC (so far) based on MFI. When the MFI for last 3 hours is greater or equal to 100 and the last candle is upward trending it should submit firstly a short order and then long after hitting the short stop loss.

The main problem I see right now is that the algorithm submits orders when the MFI is not ≥= 100. I compared the entries to the MFI data on BTC and from what I can see, the entries are completely different from the times MFI hits 100.

I tried various things, but I can't seem to figure out a solution.
Thank you!

PS: This is only a part of the code, that which I thought is the most relevant.

  1. class BTCTradingBot(QCAlgorithm):
  2. def Initialize(self):
  3. # add MFI indicator; hour resolution
  4. self.btc_money_flow = self.MFI(self.tick, 3, Resolution.Hour)
  5. # history data to check if the candle is upward or downward trending
  6. self.close_price_history = RollingWindow[float](4)
  7. self.open_price_history = RollingWindow[float](4)
  8. def OnData(self, data):
  9. self.short_order = None
  10. self.long_order = None
  11. # history data to check if the candle is upward or downward trending
  12. if data.ContainsKey(self.tick):
  13. self.close_price_history.Add(data[self.tick].Close)
  14. previous_close = self.close_price_history[0]
  15. self.open_price_history.Add(data[self.tick].Open)
  16. previous_open = self.open_price_history[0]
  17. self.Debug("// Setting rolling windows -- working")
  18. if (self.btc_money_flow.Current.Value >= 100) and (previous_close > previous_open):
  19. #SHORT ORDER
  20. self.short_order = self.StopMarketOrder(self.tick, -0.1, self.Securities[self.tick].Close - self.Securities[self.tick].Close * 0.005, "Short order")
  21. self.short_id = self.short_order.OrderId
  22. #Stop loss for short order
  23. if self.close_price_history[0] >= (self.close_price_history[1] + self.close_price_history[1] * 0.003):
  24. # self.trailing_sl_short = self.MarketOrder(self.tick, 0.1, "Short order stop loss")
  25. self.trailing_sl_short = self.LimitOrder(self.tick, 0.1, self.Securities[self.tick].Close + self.Securities[self.tick].Close * 0.003, "Short order stop loss")
  26. self.Debug("// Short stop loss -- working")
  27. #LONG ORDER
  28. if self.trailing_sl_short.Status == OrderStatus.Filled:
  29. self.long_order = self.StopMarketOrder(self.tick, 0.1, self.Securities[self.tick].Close, "Long order")
  30. self.long_id = self.long_order.OrderId
  31. #Stop loss for long order
  32. if self.close_price_history[1] >= (self.close_price_history[1] * 0.005):
  33. # self.trailing_sl_long = self.MarketOrder(self.tick, -0.1, "Long order stop loss")
  34. self.trailing_sl_long = self.LimitOrder(self.tick, -0.1, self.Securities[self.tick].Close * 0.005, "Long order stop loss")
  35. self.Debug("// Long stop loss -- working")
  36. #TIME BASED STOP LOSSES
  37. #For short orders
  38. if (self.short_order in locals()) or (self.short_order in globals()) and (self.short_order.Status == OrderStatus.Filled):
  39. self.short_fill_time = self.Time
  40. self.Debug("// Short time based stop loss -- working")
  41. #For long orders
  42. if (self.long_order in locals()) or (self.long_order in globals()) and (self.short_order.Status == OrderStatus.Filled):
  43. self.long_fill_time = self.Time
  44. self.Debug("// Long time based stop loss -- working")
+ Expand

Author

Matyáš Bureš

June 2021