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.
string currentDay = data [_symbol].Time.ToString("yyyyMMdd");
if (currentDay != previousDay) {
previousDay = currentDay;
open = data[_symbol].Open;
}
Schedule.Event("Get Open Prices")
.EveryDay("SPY")
.AfterMarketOpen("SPY", minutesAfterOpen: 1)
.Run(() =>
{
foreach (var security in Securities.Values)
{
// save security.Open somewhere
}
});
Hi Michael,
Could you give one example on saving open prices? I want to save the open prices to a dictionary for the following tickers:
"AAPL",
"SPY",
"QQQ",
"IWM",
"IBM"?
Don't know how it should be coded? Thanks.
Mike
Adapting Mike's code above:
private Dictionary<Symbol, decimal> _openDict;
// In Initialize
_openDict = new Dictionary<Symbol, decimal>();
var tickers = "AAPL, SPY, QQQ, IWM, IBM".Split(',');
foreach (var ticker in tickers)
{
var symbol = AddEquity(ticker).Symbol;
_openDict.Add(symbol, 0);
}
Schedule.Event("Get Open Prices")
.EveryDay("SPY")
.AfterMarketOpen("SPY", minutesAfterOpen: 1)
.Run(() =>
{
foreach (var security in Securities.Values)
{
_openDict[security.Symbol] = security.Open;
}
});
Please note that if there is no information from one of these symbols one minute after the exchange opens, the algorithm will not catch the new opening price.
Thanks a lot, Alex! As you pointed out that there is a disadvantage for using the method above, i.e., if there is no information from one of these symbols one minute after the exchange opens, the algorithm will not catch the new opening price.
Can the logic below overcome that disadvantage?
If yes, how can I change it for multiple symbols and get the open prices for multiple symbols? Sorry, my C# skill is very rusty.
=================================================
private TradeBar open;
public override void Initialize()
{
SetStartDate(2004, 01, 1);
SetEndDate(2004, 01,3);
AddSecurity(SecurityType.Equity, "SPY", Resolution.Minute);
}
public void OnData(TradeBars data)
{
// populate our opening price variable
if (open == null || open.Time.Date != Time.Date)
{
// when TryGetValue succeeds it will populate the 'open'
// if it fails then 'open' will have a value of null
data.TryGetValue("SPY", out open);
}
}
I haven't tested your code, but the logics would overcome that issue.
In the following code, we propose a solution that uses a dictionary to save the information. We set the initial price to zero at the end of the day to know whether we has saved the new the opening price.
Hi Alex,
Thanks for sharing the program. I tried your program but it stopped working when I change from one symbol to multiple symbols. Specifically I changed from:
AddSecurity(SecurityType.Equity, "SPY", Resolution.Minute);
to:
var tickers = "AAPL, SPY, QQQ, IWM, IBM".Split(',');
foreach (var ticker in tickers)
{
AddSecurity(SecurityType.Equity, ticker, Resolution.Minute);
}
The error I got:
Runtime Error: 'SPY' wasn't found in the TradeBars object, likely because there was no-data at this moment in time and it wasn't possible to fillforward historical data. Please check the data exists before accessing it with data.ContainsKey("SPY") (Open Stacktrace)
In OnData event handler, the algorithm is looking for SPY in the TradeBars object (e.g. data["SPY"].Close) when we haven't subscribed for it in Initialize method.
We need to change the code after this part:
foreach(var item in data.Where(x => _open[x.Key] == 0))
{
_open[item.Key] = item.Value.Open;
}
for logic that will use the subscribed symbols (AAPL, SPY, QQQ, IWM, IBM) instead.
Thanks, Alex!
Finally got it.
Hi,
could anyone give a python exampe on how to get the open price ?
Thanks in advance !
Hi Stephane, there are two methods to get the open price
1) In OnData(self, data), at each time step, data is a trade bar. You can get the open price of the current trade bar with
if data.Bars.ContainsKey(symbol):
open_price = data[symbol].Open
2) You can use the "Security" object. For example
self.Securities[symbol].Open
For more properties of the Security object
https://www.quantconnect.com/lean/documentation/topic26785.html
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!