Compare Minimum to Price

Hello I am fluent in Java, but am new to Python. I have been having trouble with a simple problem and need help.

How can I take the minimum of the past say 10 days and compare it to the current price. I would appreciate code that solves this problem.

Sorry if this problem is really easy, but I just can't figure it out for the life of me!

3 responses

Hello Chris,

This is harder than it should be at present. Your options are:

(i) use 'batch_transform' in either daily or minutely mode to accumulate OHLC or just L data. But this is soon(ish) to be deprecated and I've never felt it worked properly in minutely algos.

(ii) use 'history' in a minutely algo to give you 11 closing prices (10 days plus the current minute) if you wanted the low of the closes or 11 lows if you wanted the low of the day. But this runs every minute so you may just want to look at the opening (09:31) bar and skip the rest of the day.

(iii) use another structure like a deque to accumulate minutely L data but this might get confusing with late opening/early closing days.

(iv) find a way of using talib.MIN() but if you were collecting data with 'history' anyway this seems pointless

The attached uses 'history' at 09:31, skips the rest of the day, and records the low of the 10 days excluding the current minute.

P.

94
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
 Returns 1 Month 3 Month 6 Month 12 Month
 Alpha 1 Month 3 Month 6 Month 12 Month
 Beta 1 Month 3 Month 6 Month 12 Month
 Sharpe 1 Month 3 Month 6 Month 12 Month
 Sortino 1 Month 3 Month 6 Month 12 Month
 Volatility 1 Month 3 Month 6 Month 12 Month
 Max Drawdown 1 Month 3 Month 6 Month 12 Month
def initialize(context):
context.sid = sid(2)
context.prior_datetime = None

def handle_data(context, data):
if context.prior_datetime == None or context.prior_datetime.day != get_datetime().day:
context.prior_datetime = get_datetime()
else:
context.prior_datetime = get_datetime()
return
prices = history(11, '1d', 'low')
record(low=prices[context.sid][:-1].min())
record(price=data[context.sid].close_price)
This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes.
There was a runtime error.

Hello Peter,

One fundamental question that has been nagging me is the extent to which the Quantopian data actually capture the true price extrema, in the context of high-frequency trading? It is perhaps irrelevant to the type of trading Quantopian is looking to support, but when we talk about OHLCV data, do they include the HFT activity? For example, does the daily low include every trade, down to the 1/N millisecond (N >> 1)? Or is the market somehow sampled to obtain best estimates for the OHLC values?

Grant

When you query for OHLC values in the backtester, you will receive that bar information based on the trade data. It is not the quote data (bid, ask, order book).

It will be the trade data across all US listed exchanges. If there is a trade in a dark pool, in then open or close auction etc, it will not be in our aggregated feed.

Disclaimer

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.