Hi,
I'm new to QuantConnect, coming over from Quantopian. I'm trying to build a day-trading algorithm that's focused on top gainers from the top 500 dollar-volume stocks. From what I gathered from documentation and forums, I tried to start with the top-500-dollar-volume universe, check the price at open versus yesterday's close, and wittle down to the top 10 gainers to monitor for certain triggers for the rest of the day. Below is the code I have so far. The problem is that, unfortunately, the backtest is always timing out. I haven't dug deep in LEAN's code-base, and it's hard to debug since if I use VS offline I can't get data, and there's no online debugging capabilities at the moment; so my best guess is that it's timing out because it's still trying to stream data to all 500 stocks and that's obviously causing it to time out. I even tried to manually manage the equities (via RemoveSecurity, Securities.Remove, even through UniverseManager) but it still times out (I haven't included that code so as to start from a simpler code here, but I can share as well).
Any thoughts or ideas?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using QuantConnect.Data;
using QuantConnect.Data.Market;
using System.Diagnostics;
namespace QuantConnect.Algorithm.CSharp
{
class Quantithmar : QCAlgorithm
{
//private Symbol _spy = QuantConnect.Symbol.Create("SPY", SecurityType.Equity, Market.USA);
private int _universeSize = 500, _trackerCount = 10;
private IEnumerable<QuantConnect.Securities.Security> _dailyTrackers = new List<QuantConnect.Securities.Security>();
/// <summary>
/// Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.
/// </summary>
public override void Initialize()
{
SetCash(100000);
SetStartDate(2017, 08, 01);
Schedule.On(DateRules.EveryDay(), TimeRules.At(9, 31), SelectStocks);
//SetRunMode(RunMode.Parallel);
UniverseSettings.Resolution = Resolution.Minute;
AddUniverse(Universe.DollarVolume.Top(_universeSize));
}
private void SelectStocks()
{
try
{
Debug(Time.ToString());
Dictionary<QuantConnect.Securities.Security, decimal> changes = new Dictionary<QuantConnect.Securities.Security, decimal>();
IEnumerable<Slice> slices = History(TimeSpan.FromDays(1), Resolution.Daily);
foreach (var s in Securities)
{
if (slices.Count() > 0 && s.Value.Price > 0)
{
IEnumerable<decimal> closingPrices = slices.Get(s.Value.Symbol, Field.Close);
var change = (s.Value.Price - closingPrices.FirstOrDefault()) / s.Value.Price;
changes.Add(s.Value, change);
}
}
if (changes.Count > 0)
{
_dailyTrackers = changes.OrderBy(x => x.Value).Take(_trackerCount).Select(x => x.Key);
var msg = "";
foreach (var s in _dailyTrackers)
msg += s.Symbol.Value + ", ";
Debug(msg.Substring(0,msg.Length-2));
}
}
catch (Exception ex)
{
Debug(ex.ToString());
}
}
/// <summary>
/// OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
/// </summary>
/// <param name="data">Slice object keyed by symbol containing the stock data</param>
public override void OnData(Slice data)
{
if (!Portfolio.Invested)
{
}
}
}
}
Michael Manus
hi while waiting for response why you source timeout.....there are many universe select examples in the community forum that work. probably cloning one and start to work with that would be more promising....
some stuff you may have already found but it is quite a good idea to have them together if you need something:
universe.dollarvolume.top
Universe Based RSI Selection
dollar volume using coarse
sortedByDollarVolume
Can ADX be used to filter a universe
Getting Top Gainers in Universe
Sari Louis
Thanks for the reply, Michael Manus. I'll take a look at the links you shared and will update the post here if I find a good solution.
Sari 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.
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!