Back to Community
Limit/stop orders on futures

1) Is there a way to place limit and stop orders with futures, it seems from the posted futures tutorials that the way to order is through order_optimal_portfolio(). But I want to be able to set a target price to buy/sell at.

2) My strategy requires specific times to trigger and I am currently using data.current(context.future,'high') to track the minute to minute high over a custom time frame, however in the back tester it is giving me the current times high not the high at the time of the back test.

current_time = get_datetime('US/Eastern').time()
if (current_time >= context.startTime and current_time < datetime.time(3,15)):
context.todayHigh = max(context.todayHigh, data.current(context.future,'high'))
context.todayLow = min(context.todayLow, data.current(context.future,'low'))
if(current_time == datetime.time(8,30)):
context.todayHigh = 0
context.todayLow = 9999999999999999

2 responses

Hi Peter,

1) If you want to place limit or stop orders with futures, you can use the ordering methods that don't use Optimize. However, if your strategy was ever selected for an allocation, it would have to be converted back to using order_optimal_portfolio and retested with the Optimize API, as our internal live trading infrastructure requires that all orders go through order_optimal_portfolio.

2) Could you attach a backtest that demonstrates the problem? It's difficult to help debug with partial code that's been copied and pasted. If you attach a backtest, or email in to [email protected], we can take a look.


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.

Thank for your help, it seems I was able to resolve the time problems but I am getting errors when trying to get the previous minute bars high, even though it was working yester day. I have attached an example algorithm with the approach I am using to get the previous bar high for the ES future.

Clone Algorithm
Backtest from to with initial capital
Total Returns
Max Drawdown
Benchmark Returns
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
# Backtest ID: 5966418a5ff50552763ef758
There was a runtime error.