Hi,
How would I go about getting past minute or second data for certian stocks? I keep getting errors saying minute is not defined and I was also wondering how would I define that?
import numpy as np
from datetime import datetime
import pandas as pd
import math
class VolTrading(QCAlgorithm):
def __init__(self):
self.previous = None
self.position = None
def Initialize(self):
self.SetStartDate(2007,1,1) #Set Start Date
#self.SetEndDate(2018,10,29) #Set End Date
self.SetCash(10000) #Set Strategy Cash
self.AddSecurity(SecurityType.Equity, "x", Resolution.Minute)
self.AddSecurity(SecurityType.Equity, "y", Resolution.Minute)
self.SetWarmUp(440)
self.History("x","y",-1, Minute)
def OnData(self, data):
self.xPercent = math.log((self.Identity(x").Current.Value)/
(History(x",-1, Resolution.Minute)))
self.yPercent = math.log((self.Identity("y").Current.Value)/
(History("y",-1, Resolution.Minute)))
Jing Wu
You can get the previous bar with the RollingWindow.
class BasicAlgorithm(QCAlgorithm): def Initialize(self): self.SetStartDate(2018,1,1) #Set Start Date self.SetStartDate(2018,1,4) #Set Start Date self.SetCash(10000) #Set Strategy Cash self.AddEquity("IBM", Resolution.Minute) self.window = RollingWindow[TradeBar](2) def OnData(self, data): if data.Bars.ContainsKey("IBM"): self.window.Add(data["IBM"]) if self.window.IsReady: # current bar close current_close = self.window[0].Close # previous bar close previous_close = self.window[1].Close
Alternatively, you can request the minute history.
history = self.History(["IBM"], 2, Resolution.Minute) returns a multi-index data frame with open/high/low/close instead of decimal value. To get the closing price of last minute, you need to retrieve the value with
history = self.History(["IBM"], 2, Resolution.Minute) last_minute_close = history.loc["IBM"]["close"][-2]
Please see the documentation
Ken Hampel
Thank you very much for getting back to me. I also had another question, I keep getting an error message saying VIXY is not in the index. Is there a way to go about fixing this? Thank you again.
import numpy as np
from datetime import datetime
import pandas as pd
import math
class VolTrading(QCAlgorithm):
def __init__(self):
self.previous = None
self.position = None
def Initialize(self):
self.SetStartDate(2007,1,1) #Set Start Date
#self.SetEndDate(2018,10,29) #Set End Date
self.SetCash(10000) #Set Strategy Cash
self.AddEquity("SPY", Resolution.Minute)
self.AddEquity("VIXY", Resolution.Minute)
self.SetWarmUp(440)
def OnData(self, data):
history_s = self.History(["SPY"], 2, Resolution.Minute)
last_minute_close_s = history_s.loc["SPY"]["close"][-2]
history_v = self.History(["VIXY"], 2, Resolution.Minute)
last_minute_close_v = history_v.loc["VIXY"]["close"][-2]
self.SPercent = math.log((self.Identity("SPY").Current.Value)/
(last_minute_close_s))
self.VPercent = math.log((self.Identity("VIXY").Current.Value)/
(last_minute_close_v))
if self.IsWarmingUp:
return
if SPercent <= -.01:
if self.position == None:
self.SetHoldings("VIXY", 1)
elif self.position == "SPY":
self.Liquidate("SPY")
self.SetHoldings("VIXY", 1)
self.position = "VIXY"
elif VPercent <= -.01:
if self.position == None:
self.SetHoldings("SPY", 1)
elif self.position == "VIXY":
self.Liquidate("VIXY")
self.SetHoldings("SPY", 1)
self.position = "SPY"
Jing Wu
Please add an empty data frame check before retrieving the value with the symbol index.
You can obtain the current price with self.Securities["SPY"].Price instead of "self.Identity("SPY")".
(If you have further questions, please share an algorithm with the "attach backtest" button below to demonstrate the issue instead of copying and pasting the non-formatted code. Python is structured with indentation. This procedure helps us, the community, to answer your questions quickly and effectively.)
Ken Hampel
Thank you very much for the response, I will make some changes and get back to you if anymore issues arise. Thanks.
Ken Hampel
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!