Lean icon indicating copy to clipboard operation
Lean copied to clipboard

Futures extended market hours not supported

Open Jay-Jay-D opened this issue 7 years ago • 2 comments

Expected Behavior

Futures Market hours differs widely between securities.

  • Some have only one regular open market segment, e.g. Live Cattle LE
  • Others have multiple regular open market segments, e.g. Wheat ZW
  • Finally, there are ones with extended and regular open market hours, e.g. VIX VX

Lean should be able to reproduce all cases.

Actual Behavior

All open market hours are considered regular. This means that cases like VX cannot be handled correctly.

Potential Solution

This requires two-step:

  1. Update the market hours database from
 "monday": [{
   "start": "00:00:00",
   "end": "15:15:00",
   "state": "market"
  }, {
   "start": "15:30:00",
   "end": "1.00:00:00",
   "state": "market"
  }
 ]

To

 "monday": [{
   "start": "00:00:00",
   "end": "8:30:00",
   "state": "premarket"
  }, {
   "start": "8:30:00",
   "end": "15:15:00",
   "state": "market"
  }, {
   "start": "15:30:00",
   "end": "16:00:00",
   "state": "market"
  }, {
   "start": "16:00:00",
   "end": "1.00:00:00",
   "state": "postmarket"
  }
 ]
  1. Modify the QCAlgorithm. AddFuture method in order to change the hardcoded extendedMarketHours=false in the SecurityManager.CreateSecurity method.

Reproducing the Problem

The attached algorithm was used to test the market hours.

But, after modifying market-hours-database.json and set in extendedMarketHours=true in SecurityManager.CreateSecurity method, the data in the premarket and postmarket segments is not pumped into OnData.

public class MarketHoursTester : QCAlgorithm
{
    private bool _previousMarketStatus;
    private Symbol _symbol;

    public override void Initialize()
    {
        SetStartDate(year: 2018, month: 07, day: 01);
        SetEndDate(year: 2018, month: 07, day: 10);
        SetCash(startingCash: 100000);

        var security = AddFuture("VX");
        security.SetFilter(TimeSpan.Zero, TimeSpan.FromDays(value: 182));

        _symbol = security.Symbol;
    }

    public override void OnData(Slice slice)
    {
        var marketStatus = Securities[_symbol].Exchange.ExchangeOpen;
        if (marketStatus != _previousMarketStatus)
        {
            Log($"OnData - Market Status change at {Time.ConvertTo(TimeZone, Securities[_symbol].Exchange.TimeZone):f} ExchangeTimeZone - Is open? {marketStatus}");
            _previousMarketStatus = marketStatus;
        }

        if (!marketStatus && Time.Minute == 0)
        {
            Log($"OnData {Time.ConvertTo(TimeZone, Securities[_symbol].Exchange.TimeZone):f} ExchangeTimeZone - Fired on extended hours");
        }
    }
}

System Information

Window 7.

Checklist

  • [x] I have completely filled out this template
  • [x] I have confirmed that this issue exists on the current master branch
  • [x] I have confirmed that this is not a duplicate issue by searching issues
  • [x] I have provided detailed steps to reproduce the issue

Related with:

This issue addresses issue #2449.

Jay-Jay-D avatar Aug 31 '18 11:08 Jay-Jay-D

@Jay-Jay-D is this still an issue?

adriantorrie avatar Apr 27 '20 14:04 adriantorrie

Yes, MHBD has no extended hours declared for futures, and the extended hour parameter is still hard coded to be false at security creation.

Jay-Jay-D avatar May 04 '20 14:05 Jay-Jay-D