Back

Todays close shifted in API and getting "todays" value

I am a bit confused with data I am receiving using History function.

I run my rebalancing function every day before market close:

self.Schedule.On(self.DateRules.EveryDay('SPY'),
self.TimeRules.BeforeMarketClose('SPY', 1), Action(self.Rebalance))

Then I want to get "today's" closing price. For this moment I just debug:

history = self.History(["SPY"],3, Resolution.Daily)
self.Debug("Processing:" + str(self.Time))
self.Debug("SPY price: " + str(history))

And I get:

2014-07-10 00:00:00 Processing:2014-07-09 15:59:00
2014-07-10 00:00:00 SPY :
close high low open volume
symbol time
SPY
2014-07-04 198.05 198.29 197.64 197.79 32643128.0

2014-07-08 197.53 197.98 197.22 197.82 44628828.0

2014-07-09 196.28 197.22 195.76 197.15 69544115.0

But the date in dataframe is shifted by one day to the reality.

In above dataframe the "Close" value 196.28 is stated for 2014-07-09 (it is a raw price). But in reality it is a value for 2014-07-08. If I use the History function in Research I get correct date.

Thus my questions:

Why is the date shifted in the dataframe?

How do I get "todays" close?

Update Backtest








A couple of comments.

One can't really get todays close while the market is still open. Logically, one can only retrieve the close price AFTER the market closes. One could get near the close price by simply fetching the current price shortly before the market closes.

That said, you point out another idiosyncrasy with the History method. Namely that in the research environment the timestamps allign with the correct dates (eg the close price on 2017-1-3 has a timestamp of 2017-1-3). In the algo environment however (and I assume in live trading) the timestamps are shifted by a day (eg the close price on 2017-1-3 has a timestamp of 2017-1-4). Frustrating.

This becomes obvious in the algorithm mode by simply logging a history dataframe. One never sees a Monday timestamp but sees Saturday timestamps. The dates are shifted by one.

Attached, are a notebook and an algorithm showing this divergent behavior, Check out the algorithm logs for the output of the History dataframe.

0


Thank you,

can you please help to clarify. I guess following code gives mi current price before close:

self.TimeRules.BeforeMarketClose('SPY', 1), Action(self.Rebalance))

def Rebalance(self):
self.Debug("Processing : " + str(self.Time))
self.Debug("SPY close price : " + str(self.Securities["SPY"].Price))

in the log I get for example:

2014-07-10 00:00:00 Processing : 2014-07-09 15:59:00
2014-07-10 00:00:00 SPY close price : 196.2800

self.Time is 2014-07-09, but the "actual price" is for 2014-07-08 (when comparing to Research and other datafeeds).

How can I get akctual price that aligns to self.Time? Suppose that self.Time is the "realtime" in backtest.

Thank you.

0

UPDATE and SOLUTION: 

I did not have the minute resolution (had daily). That was causing the wrong price.

After:

self.AddEquity("SPY", Resolution.Minute).SetDataNormalizationMode(DataNormalizationMode.Raw)

I get the correct price.

0

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