Not being the best at programming, is there a simple way to compare a variable at two different points in time? For example, compare the VWAP at t-1 and t in order to determine if the value is increasing or decreasing.
Not being the best at programming, is there a simple way to compare a variable at two different points in time? For example, compare the VWAP at t-1 and t in order to determine if the value is increasing or decreasing.
@ Bob
To get data from different points in time you can use the builtin history() function, this provides OHLCV history.
If you want to store a variable and then get it at a later point in time, you can always declare the variable in initialize() as context.{your variable} and set it and access it during any bar.
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 Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian 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.
From the sample algo:
vwap = data[context.qqq].vwap(3)
price = data[context.qqq].price
vwap is re-calculated every time (t, t+1,...tn) there is a new data point (price). What I want to do is compare vwap at time t, to vwap at time t +1. Then extract this value.
Bob try this on for size:
def vwap(security, length, days_back, prices, volumes):
if not days_back:
sl = slice(-(days_back + length), None)
else:
sl = slice(-(days_back + length), -days_back)
return (prices[security][sl] * volumes[security][sl]).sum() / volumes[security][sl].sum()
and use it like so...
prices = history(10, '1d', 'price')
volumes = history(10, "1d", "volume")
VWAP_LENGTH = 5
days_back = 0
print(vwap(symbol('AAPL'), VWAP_LENGTH, days_back, prices, volumes))
print(data[symbol('AAPL')].vwap(VWAP_LENGTH))
The bottom print statement is the built in VWAP transform to check the calculations. So as to you original query, if today is t = 0 and yesterday is t = -1, then to compare usages youd would set days back accordingly
# todays vwap for a given length: VWAP_LENGTH
days_back = 0
vwap(symbol('AAPL'), VWAP_LENGTH, days_back, prices, volumes)
# yesterdays vwap for a given length: VWAP_LENGTH
days_back = 1
vwap(symbol('AAPL'), VWAP_LENGTH, days_back, prices, volumes)
P.S. be sure to call history for a time range that is long enough
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 Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian 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.