Back

Quantopian to Quantconnect -- Get current and history of Price

I am trying to transalte some Quantopian code, and am not sure of the proper C# methods to use.

Quantopian code:

 Assume that "context.XIV" is pointing to the XIV position.

 high = (data.history(context.XIV, "high", 119, "1m")).max()

 price = data.current(context.XIV, 'price')

Thanks

Update Backtest







Requesting historical data is easily implementable in QuantConnect. The History() method call is extremely flexible, and returns as an enumerable. As a result, the highest high price can be found via historyarray.max().

https://msdn.microsoft.com/en-us/library/system.linq.enumerable.max(v=vs.110).aspx

The price of a security can be called via Securities[Symbol].Price

0

Hi Gurumeher,

thanks for the input although, I'm having an issue getting the current price of a list of stocks. Here's a exerpt from my code:

I call a function everyday at 9h31 in the morning. The function tries to get the current price of a list of stocks in a list.

h2 = self.History(stock_list, 1, Resolution.Minute)

self.Debug(h2)
for sid in buyList:

self.Debug(sid.Value)
self.AddEquity(sid.Value)
self.Debug(self.Securities[sid.Value].Price)

And the code returns this:

open high low close volume
symbol time
ANY 2018-08-30 09:31:00 0.2836 0.2836 0.28 0.28 2536.0
SKLN W3D5XE441TNP 2018-08-30 09:31:00 1.2000 1.2000 1.20 1.20 5484.0
AMBO WUZNFTKLBTR9 2018-08-30 09:31:00 6.6200 6.6200 6.62 6.62 8.0
AAU TEMHQPA3FAZP 2018-08-30 09:31:00 0.6400 0.6400 0.64 0.64 9142.0
NEXM RU76HTMVKUW5 2018-08-30 09:31:00 0.3200 0.3200 0.32 0.32 7762.0
ATMR WCSV5NBCG411 2018-08-30 09:31:00 5.2200 5.2200 5.22 5.22 184.0
ARB R735QTJ8XC9X 2018-08-30 09:31:00 17.2800 17.2800 17.28 17.28 395.0
AYTU WOV3AVMS6TT1 2018-08-30 09:31:00 4.5100 4.5100 4.51 4.51 1852.0
2018-08-31 00:00:00 AYTU
2018-08-31 00:00:00 0Basically, the price of 0 is returned for many stocks.... despite the fact that it
should be (or I would expect it to be) around 4.51 according to the table above.

and I also noticed that the dates dont match either ....what am i doing wrong ?
Thanks in advance
0

Ok what I found is that I have to add the security first like so:

stock = self.AddEquity(mySym.Value).Symbol
price = float(self.Securities[stock].Price)

but still doesnt tell me why I end up with a price that is largely different than the minute data even though I call this at the same time ...

0

AddEquity() request the data for specified resolution and symbol. Therefore to get the security price data with self.Securities[stock].Price, you need to request the data in Initialize().

self.Securities[stock].Price returns the bar closing price. I did a simple test with this algorithm. The history close price equals the self.Securities[sid].Price.

class BasicTemplateAlgorithm(QCAlgorithm):
    def Initialize(self):

        self.stock_list = ["AAPL", "IBM", "AMZN"]
        for i in self.stock_list:
            self.AddEquity(i)

     
    def OnData(self, data):
        h2 = self.History(self.stock_list, 1, Resolution.Minute)
        self.Debug(h2)
        for sid in self.stock_list:
            self.Debug(sid + " "+ str(self.Securities[sid].Price))
        self.Quit()
0

Thanks a lot Jing !

Given that I pull both Minute and Daily data in my custom function:

        h1 = self.History(stock_list, 31, Resolution.Daily)
        h2 = self.History(stock_list, 1, Resolution.Minute)

 I had to put this in my initialize function:

self.UniverseSettings.Resolution = Resolution.Minute

now this:

price = float(self.Securities[stock].Price)

in my custom function return the closing of the last bar/the last minute.

Does it make sense to you too ?

Thanks !

0

With the universe resolution setting "self.UniverseSettings.Resolution = Resolution.Minute", OnData() method is called every minute. You'll get the minute data for symbols returned in universe selection. 

The resolution of self.Securities[symbol].Price in OnData() depends on your resolution setting when you request the symbol data.

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