I am new to QuantConnect and have been using AI to assist me in making the algorithm I want. I want my program to filter stocks that are between 2-20 dollars, have a small market cap, and have increased by at least ten percent in the past hour. I only want my program to invest in one of these stocks if the MACD is bullish and it has a relative volume of 5 over the past fifty days. Then the program will sell when the stock grows by 5% or stops out at 98% of the original investment.
I'm not sure what is wrong with the code, but it never placed any orders. I'll include the code below and if you have any suggestions that would be greatly appreciated!
from AlgorithmImports import *
class OpeningGapStrategy(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2022, 1, 1) # Set Start Date
self.SetEndDate(2024, 1, 1) # Set End Date
self.SetCash(1000) # Set Strategy Cash
self.stocks = [] # List to hold our stocks
self.stop_loss_percent = 0.98
self.take_profit_percent = 1.05
# Define the trading hours
self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.At(9, 30), self.ScanForStocks)
self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.At(10, 30), self.CheckAndBuy) # Check one hour after market open
def ScanForStocks(self):
# Initialize the universe with filtering criteria
self.AddUniverse(self.UniverseFilter)
self.Debug("Scanning for stocks")
def UniverseFilter(self, coarse):
# Filter for small cap stocks with price between $2 and $20
filtered_symbols = [x.Symbol for x in coarse
if x.MarketCap < 2e9 and 2 < x.Price < 20]
self.Debug(f"Filtered Symbols: {filtered_symbols}")
return filtered_symbols
def CheckAndBuy(self):
if not self.ActiveSecurities:
self.Debug("No active securities.")
return
for symbol in self.ActiveSecurities.Keys:
if symbol in self.Portfolio and self.Portfolio[symbol].Invested:
continue
# Ensure the security is initialized
security = self.Securities[symbol]
if not security.HasData:
self.Debug(f"No data for {symbol}")
continue
# Fetch historical data for the first hour
try:
history = self.History(symbol, 90, Resolution.Minute) # Request more data to ensure we capture the first hour
if history.empty or len(history) < 60: # Ensure we have enough data for the first hour
self.Debug(f"Insufficient historical data for {symbol}. Data Length: {len(history)}")
continue
# Calculate growth percentage
open_price = history.iloc[0].close # Price at market open
one_hour_later_price = history.iloc[60].close # Price one hour later (adjust index if necessary)
growth_percentage = (one_hour_later_price - open_price) / open_price
# Log growth percentage
self.Debug(f"{symbol} - Open Price: {open_price}, One Hour Later Price: {one_hour_later_price}, Growth: {growth_percentage:.2%}")
# Check trading condition
if growth_percentage >= 0.10: # 10% growth
# Place order to buy the stock
quantity = self.Portfolio.Cash / self.Securities[symbol].Price # Determine quantity based on available cash
self.SetHoldings(symbol, 1.0) # Allocate 100% of the portfolio to this stock
self.Debug(f"Placed order for {symbol}")
# Set stop loss and take profit orders
stop_loss_price = one_hour_later_price * self.stop_loss_percent
take_profit_price = one_hour_later_price * self.take_profit_percent
self.Debug(f"Set Stop Loss at {stop_loss_price} and Take Profit at {take_profit_price}")
# Place stop-loss order
self.StopMarketOrder(symbol, quantity, stop_loss_price)
# Place take-profit order
self.LimitOrder(symbol, quantity, take_profit_price)
except Exception as e:
self.Debug(f"Error fetching data for {symbol}: {str(e)}")
def OnData(self, slice):
if not self.Portfolio.Invested:
self.Debug("No open positions")
return
for symbol in list(self.Portfolio.Keys):
if self.Portfolio[symbol].Invested:
price = self.Portfolio[symbol].Price
if price <= self.stop_loss_price or price >= self.take_profit_price:
self.Liquidate(symbol)
self.Debug(f"Liquidated {symbol} at {price}")
# Reset stop-loss and take-profit prices
self.stop_loss_price = None
self.take_profit_price = None
Louis Szeto
Hi Benny
The universe subscription should be only handled in the initialize method and it will run daily, but not in a scheduled function.
As per the MACD filtering, I recommend you check out Example 2: Take 10 stocks above their 200-Day EMA and have more than $1B daily trading volume as a similar example in the fundamental universe docs examples.
Best
Louis
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.
Lars Klawitter
To Yuri's earlier point of there being a difference between backtests and live trading as to when the stop orders are placed:
I've been running the strategy (using a larger universe and shorter opening range as per my previous post) on IBKR paper trading (i.e. on an actual IBKR account, not via QC paper trading) and as Yuri suggested, the stop orders are at times placed within the same minute. Mostly 20-40 seconds after the entry:
in one case the stop order was placed the same instance as the entry:
I tried second resolution, but that seems impractical given the large universe size.
So this is my attempt at an artificially delayed stop order placement:
I'm not a C# coder, so I definitely don't know what I'm doing. Backtests with this code produce by and large comparable results with the original code, so I'll try paper trading next.
Would the above code change make sense to you?
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.
Yuri Lopukhov
So far I can see two issues:
I can't fix the first issue in C#, so I guess I will switch to Python version unless somebody else fixes the C# version and share it. Not sure if fixing it will improve results as well.
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.
Jack Pizza
define: “does really well with 1 minute” ….. 2000-2002 still utterly collapses fail to see well really well fits in….
or does really well when overfitting super hard?
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.
Quant Stratege
These backtests are not representative of live performance. When adding slippage, it can be significant at the open due to volatility, small-cap stocks, and using stop orders, making the results much less appealing.
Just add this line when adding securities:
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.
Lars Klawitter
You're right. I had previously only simulated very small constant slippages, but MarketImpactSlippage has quite a savage effect…
If it looks too good to be true…
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.
AleNoc
I noticed a difference between the backtest and the selection with live data (data provider QuantConnect). What data provider do you use for live trading?
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.
Evan lightner
I don't know where to start, I'll have to write a whole post and how I'm planning to implement this strategy. But first of all thank you @derek for sharing this (and of course for everything you've done with QC -its been a game changer for me).
But first of all, I just want to comment with one simple non-coding question….
Why are a good amount of people HATING on this strategy? I understand the backtest cherry pick , but for a bare bones boiler plate ( i messed around with some variables like holdings and percent risk and still got good results) , this is the best algo I've seen in a while, especially for being shared so freely - not to mention brand new piece of research in the community.
Is there just some sort of deep skepticism in the quant community at large I guess, inherently? I suppose that fits!
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.
Kevin Patterson
Does anyone else end up with very different symbols when running the exact same algo in backtesting vs live (IBKR paper trading)? For example when it is set to choose 20 symbols, I'll see maybe only half overlap –- and of course the ones going positive for the day in the backtest are the ones not picked up live 😅. Seems the relative volume calculations aren't exactly the same and it doesn't take much to move the symbols you get. This is my first foray in to large universe algo's, is this type discrepancy common with large universe backtests or is there settings to help make it line up better with live? S
Some folks were asking about sized nodes: after trying the python version on IBKR (with a few mods) an L1-2 node will make it through one day (it crashes after close though, so likely you need the next level up if you dont want to restart daily)
Thanks for all the python related posts, even if I don't end up trading it, the algo has been super helpful for learning more about the QC code and had some good recipes in it that I think would be helpful for any algorithm.
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!