Hello, thanks to all who can help.I am trying to create a HFT-like algorithm in my spare time.I tried to acess the previous tick price using
-data['IBM'][-1].LastPrice
but that threw a error:
-'SPY' wasn't found in the Slice 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")
any ideas?
thanks for the help!
heres th code:
#
#
#
#
import clr
clr.AddReference("System")
clr.AddReference("QuantConnect.Algorithm")
clr.AddReference("QuantConnect.Indicators")
clr.AddReference("QuantConnect.Common")
from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
class MyAlgo(QCAlgorithm):
def Initialize(self):
'''Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.'''
self.SetStartDate(2019, 12, 27) #Set Start Date
# self.SetEndDate(2019, 7, ) #Set End Date
self.SetCash(200) #Set Strategy Cash
self.equity = 'SPY'
self.AddEquity(self.equity, Resolution.Tick)
self.Securities[self.equity].FeeModel = ConstantFeeModel(.00)
def OnData(self, data):
openOrders = self.Transactions.GetOpenOrders()
qty = self.Portfolio[self.equity].Quantity
last = self.History(self.equity, 1, Resolution.Tick)
price = self.Securities[self.equity].Price
data.ContainsKey(self.equity)
if self.Portfolio[self.equity].Quantity == 0 and len(openOrders) == 0 and data["SPY"][-1].LastPrice > self.Securities[self.equity].Price:
self.LimitOrder(self.equity, 1, (self.Securities[self.equity].Price -.01))
if self.Portfolio[self.equity].Quantity > 0:
self.LimitOrder(self.equity, -qty, (self.Securities[self.equity].Price ))
#####
#####
Rahul Chowdhury
Hi Christian,
The code provided is difficult to debug because of the formatting. Please provide your code in a snippet. It will help greatly in trying to understand the issue. In the meantime, I provided an example of how to access tick data.When subscribed to tick data, each second Lean provides a list of all the ticks from the past second into the current time slice.
We can access the tick data using
ticks = data.Ticks[symbol] currentTick = ticks[0] previousTick = ticks[1]
The length of the list of ticks can vary as the frequency of ticks changes.
Christian Olsen
Thanks so much rahul! Means so much... God bless!!
Christian Olsen
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!