Back

How to use pandas series with time index as argument?

In QC, Is it possible to use functions with pandas series argument?

For example I would like to use function from mlfinlab packages `get_daily_vol`:

get_daily_vol(close, lookback=100)

where series is the pandas series with timestamp index and lookback is some number.

Now, I would like to calculate daily volatility for every bar (slice). Here is code that doesn't work:


import numpy as np
import mlfinlab as ml
import pandas as pd


class CalibratedResistanceAtmosphericScrubbers(QCAlgorithm):


def Initialize(self):
self.SetStartDate(2019, 1, 1) # Set Start Date
self.SetEndDate(2019, 3, 1)
self.SetCash(100000) # Set Strategy Cash
self.spy = self.AddEquity("SPY", Resolution.minute)
self.spy.SetDataNormalizationMode(DataNormalizationMode.Adjusted) # Raw, SplitAdjusted, TotalReturn
self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage, AccountType.Cash)

# init close prices
self.open = np.array([])
self.high = np.array([])
self.low = np.array([])
self.close = np.array([])
self.volume = np.array([])
self.lookback = max(self.periods)
self.SetWarmUp(self.lookback * 2)


def OnData(self, data):
'''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
Arguments:
data: Slice object keyed by symbol containing the stock data
'''
if "SPY" not in data.Bars:
return

open_ = data["SPY"].Open
high_ = data["SPY"].High
low_ = data["SPY"].Low
close_ = data["SPY"].Close
volume_ = data["SPY"].Volume
self.open = np.append(self.open, close_)[-self.lookback*2:]
self.high = np.append(self.high, close_)[-self.lookback*2:]
self.low = np.append(self.low, close_)[-self.lookback*2:]
self.close = np.append(self.close, close_)[-self.lookback*2:]
self.volume = np.append(self.volume, close_)[-self.lookback*2:]
self.time = self.Time

if self.IsWarmingUp:
return

df = pd.DataFrame({'open': self.open, 'high': self.high, 'low': self.low, 'close': self.close, 'volume': self.volume})
# HERE I SHOULD SOMEHOW CREATE INDEX VECTOR WITH FOR DF WITH ALL PASSED CLOSE PRICES

# Compute volatility - THATS THE FUNCTION I NEED TO APPLY INE EVERY STEP
daily_vol = ml.util.get_daily_vol(self.close, lookback=self.volatility_lookback)

 

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.


Hi Mislav,

To build the close vector with a timestamp index, we just need to change closes from a numpy array into a Series

self.close = pd.Series()

and append to it with

self.close = self.close.append(pd.Series([close_], index=[self.Time]))[-self.lookback*2:]

See the attached backtest for a working example. Also, consider reviewing our tutorial series. It covers pandas operations like this one.

Best,
Derek Melchin

1

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.


Derek Melchin , thank you for answer. What is the difference between yourt method and self.History . In both cases we get pandas DataFrame with OHLCV data and time index?

0

I will answer my own question. Above solution is much faster...

 

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