I am trying to get current price in scheduled event handler:
def Initialize(self):
self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.AfterMarketOpen("SPY", 30), self.Trade)
def Trade(self):
self.CurrentSlice[symbol].Open
The "Open" value seems to repeat the "Open" value from the previous day.
How do I get the current price here?
Flame
Hi Valery
The simplest method is to use as follows:
self.Securities[symbol].Price
You also need to make sure that when adding data you include an appropriate resolution (e.g. Resolution.Minute)
Hope this helps
Xin Wei
Hi Valery,
To get "current price," as Flame has pointed out, you first need to make sure that you've asked for the data in your algorithm. You can use Resolution.Minute or Resolution.Tick based on your need and data availability.
Then, you can use self.Securities[symbol].Price to access current price. Or, you can also use self.CurrentSlice[symbol].Close which gives you the close price of last data slice pushed into your algorithm. The two methods return the same value.
I've attached a backtest. Please check out the implementation details and the logs to see how the two methods work.
Krishna posa
hi,
When i try
self.Schedule.On(self.DateRules.EveryDay(self.symbol), \
self.TimeRules.AfterMarketOpen(self.symbol, 20), \
Action(self.EveryMarketOpen))
I am getting this error
20201203 19:41:11.868 ERROR:: ScheduledEvent.Scan(): Exception was thrown in OnEventFired: Python.Runtime.PythonException: ImportError : cannot import name 'EWM' from 'pandas.core.window' (/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/window/__init__.py)
at Python.Runtime.PyObject.Invoke (Python.Runtime.PyObject[] args) [0x00035] in <6502d9c22ea54a59ae1303c418913e75>:0
at QuantConnect.Scheduling.ScheduleManager+<>c__DisplayClass15_0.<On>b__0 (System.String name, System.DateTime time) [0x00007] in /Users/kp6228/Documents/work/code/Lean/Common/Scheduling/ScheduleManager.cs:142
at QuantConnect.Scheduling.ScheduledEvent.OnEventFired (System.DateTime triggerTime) [0x00011] in /Users/kp6228/Documents/work/code/Lean/Common/Scheduling/ScheduledEvent.cs:252
20201203 19:41:11.870 ERROR:: BacktestingRealTimeHandler.ScanPastEvents(): BacktestingRealTimeHandler.Run(): There was an error in a scheduled event SPY: EveryDay: SPY: 20 min after MarketOpen. The error was ImportError : cannot import name 'EWM' from 'pandas.core.window' (/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/window/__init__.py) QuantConnect.Scheduling.ScheduledEventException: ImportError : cannot import name 'EWM' from 'pandas.core.window' (/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/window/__init__.py) ---> Python.Runtime.PythonException: ImportError : cannot import name 'EWM' from 'pandas.core.window' (/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/window/__init__.py)
at Python.Runtime.PyObject.Invoke (Python.Runtime.PyObject[] args) [0x00035] in <6502d9c22ea54a59ae1303c418913e75>:0
at QuantConnect.Scheduling.ScheduleManager+<>c__DisplayClass15_0.<On>b__0 (System.String name, System.DateTime time) [0x00007] in /Users/kp6228/Documents/work/code/Lean/Common/Scheduling/ScheduleManager.cs:142
at QuantConnect.Scheduling.ScheduledEvent.OnEventFired (System.DateTime triggerTime) [0x00011] in /Users/kp6228/Documents/work/code/Lean/Common/Scheduling/ScheduledEvent.cs:252
--- End of inner exception stack trace ---
at QuantConnect.Scheduling.ScheduledEvent.OnEventFired (System.DateTime triggerTime) [0x0006b] in /Users/kp6228/Documents/work/code/Lean/Common/Scheduling/ScheduledEvent.cs:261
at QuantConnect.Scheduling.ScheduledEvent.Scan (System.DateTime utcTime) [0x0015a] in /Users/kp6228/Documents/work/code/Lean/Common/Scheduling/ScheduledEvent.cs:176
at QuantConnect.IsolatorLimitResultProvider+<>c__DisplayClass1_0.<Consume>b__0 () [0x00000] in /Users/kp6228/Documents/work/code/Lean/Common/IsolatorLimitResultProvider.cs:48
at QuantConnect.IsolatorLimitResultProvider.Consume (QuantConnect.IIsolatorLimitResultProvider isolatorLimitProvider, QuantConnect.ITimeProvider timeProvider, System.Action code, QuantConnect.Scheduling.TimeMonitor timeMonitor) [0x0001f] in /Users/kp6228/Documents/work/code/Lean/Common/IsolatorLimitResultProvider.cs:75
at QuantConnect.IsolatorLimitResultProvider.Consume (QuantConnect.IIsolatorLimitResultProvider isolatorLimitProvider, QuantConnect.Scheduling.ScheduledEvent scheduledEvent, System.DateTime scanTimeUtc, QuantConnect.Scheduling.TimeMonitor timeMonitor) [0x00038] in /Users/kp6228/Documents/work/code/Lean/Common/IsolatorLimitResultProvider.cs:48
at QuantConnect.Lean.Engine.RealTime.BacktestingRealTimeHandler.ScanPastEvents (System.DateTime time) [0x0002b] in /Users/kp6228/Documents/work/code/Lean/Engine/RealTime/BacktestingRealTimeHandler.cs:140
20201203 19:41:11.874 Trace:: AlgorithmManager.Run(): Algorithm encountered a runtime error at 09/01/2017 14:00:00. Error: QuantConnect.Scheduling.ScheduledEventException: ImportError : cannot import name 'EWM' from 'pandas.core.window' (/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/window/__init__.py) ---> Python.Runtime.PythonException: ImportError : cannot import name 'EWM' from 'pandas.core.window' (/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/window/__init__.py)
at Python.Runtime.PyObject.Invoke (Python.Runtime.PyObject[] args) [0x00035] in <6502d9c22ea54a59ae1303c418913e75>:0
Derek Melchin
Hi Krishna,
Is this reproducible in QuantConnect Cloud? This may be an issue with the local environment.
Best,
Derek Melchin
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.
Valery T
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!