Back

Issues with different resolutions

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)))

Update Backtest







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

https://www.quantconnect.com/docs/algorithm-reference/rolling-windowhttps://www.quantconnect.com/docs/algorithm-reference/historical-data
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.


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"

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