Back

Having trouble scheduling 30 mins before a trading day

I've poured over the docs and have spent a few hours tinkering but can't seem to get a timer to working so it fires before every trading day (pre market). Is this even possible with the current system?

 

I've tried `Schedule.On(DateRules.EveryDay(), TimeRules.AfterMarketOpen("SPY", -20)` but a negative number doesn't work, it still fires at 1 min after open.

I've also tried `Schedule.On(DateRules.EveryDay(), TimeRules.At(8, 0)` and that also fires at 9:31.

Schedule.On(DateRules.EveryDay(), TimeRules.At(8,0), () =>
{
//prints 2017-03-22 09:31:00 : Pre market at 8am: 03/22/2017 08:00:00
Log("Pre market at 8am: " + Time);

// prints 2017-03-22 09:31:00 : watchlist: ["OMNT"]
fetchAndSetWatchlist();
});

If you notice, the LEAN timestamps are labeled 09:31 while the `Time` log is printing 8am when it was scheduled, however the fetch watchlist function (inside the lambda) fires at 9:31 (also confirmed since I get an error with trade bars not being available for that min).

Update Backtest








Hi Adam, Time is the algorithm time -- it is firing at 8am you just don't have any data at that moment. The log time stamp is just the next datapoint.

In live trading scheduled events are synchronized to a real clock but in backtesting we jump through space to the next datapoint for the logging.

2

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.


Oh ok thanks for the clarification Jared.

So does that mean in real trading that my 'get and set watchlist' function will run at 8am and the data will be ready by 9:31?

Currently i'm checking to see if the tradebar for that symbol exists and then return early if it doesn't, making the first tradable tim e 9:32.

If real life will be the same, is there a way to fire the code at 8am? Otherwise I guess I could use second data and be ready the 2nd second (instead of 2nd minute). 

Also just wanted to say thanks to you and your staff, the help in the community is much appreciated. Going to signup for a paid membership this weekend!

0

I tried to attach a cloned algo here but can't figure out how lol... here's a simple example if it helps:

 

using System;
using System.Net;
using Newtonsoft.Json;

namespace QuantConnect
{

public class BasicTemplateAlgorithm : QCAlgorithm
{
List<string> watchlist;

public override void Initialize()
{
SetStartDate(2017, 3, 1);
SetEndDate(2017, 3, 1);
SetCash(5000);

AddEquity("SPY"); // only used for scheduler

// populate a watchlist every morning before market open
Schedule.On(DateRules.EveryDay(), TimeRules.AfterMarketOpen("SPY", -30), () =>
{
Log("Before market open " + Time);
using (var client = new WebClient())
{
//var json = client.DownloadString("https://s3.amazonaws.com/h65d3/test.json");
var json = "[\"GOOG\"]";
watchlist = JsonConvert.DeserializeObject<List<string>>(json);

foreach (var symbol in watchlist) {
Debug("Adding symbol to watchlist: " + symbol);
AddEquity(symbol, Resolution.Minute);
}
}
});
}


public override void OnData(Slice data)
{
TradeBars bars = data.Bars;

foreach (var symbol in watchlist)
{
// uncommenting this allows trade to go through at 9:32
//if (!bars.ContainsKey(symbol)) return;

var close = bars[symbol].Close;

if (!Portfolio.HoldStock)
{
SetHoldings(symbol, 1);
Debug("Purchased on " + Time.ToShortDateString());
}
}


}
}

}

 

0

@Jared: This is one of the quirks with Lean that can be confusing (and was for me), especially due to perceived differences in algo timing when going from daily bars to higher resolution. While I don't think it urgent, it would be way more beginner friendly to have events trigger at their ordained timestamps in backtesting. The problem doesn't sound impossible to solve (but yeah, one should never underestimate codebase complexity).

1

Oh and while at it, I think you should add BeforeMarketOpen and AfterMarketClose methods to make the API less guesswork. Wanted to state this for some time, glad it was brought up as I don't remember. :)

1

Update Backtest





0

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.


Loading...

This discussion is closed