Back to Community
Position Object Questions

Hello,
I'm testing an algo using a single security in minute frequency and want to determine the holding time (in minutes). So, after the order has been filled, how long (in minutes) have I had the open position. Hopefully that makes sense!

For the system, I would only have one position at a time, and its long/short.

Also, if my goal was to compare cost basis to last price in minute frequency, could I use context.portfolio.positions[sid].cost_basis which provides the cost basis per share VS compare context.portfolio.positions[sid].last_sale_price? Just want to make sure i'm comparing apples to apples :)

Thanks for the help!

8 responses

Unfortunately there are no fields to determine the time a position was entered or last added to, although that would be very helpful when writing robust trailing stops. One must try to store something in context and hope you never have to restart your algo. As for cost basis, best way is to try it out!

While there isn't a native function to track the time in a specific position, you can craft it yourself in the code:

  1. Use the status attribute to detect when an order has been filled (https://www.quantopian.com/help#api-orderobj)
  2. Note the fill datetime
  3. Check the current time - fill datetime to see how long the position was held
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.

Alisa, that doesn't help the next day when all the orders are cleared, right? All we have is a position with no indication when it was entered?

I wrote some example code to get you started here. Let me know if anything isn't clear, you'll also have to modify this code for any specific application. Keep in mind that this code assumes that only one order will be placed for a given asset at a time, and that the asset position will be fully closed before any more are placed.

last_sale_price is the last observed price the underlying security is selling at. cost_basis is the price at which you filled the position, or weighted average of prices if the position is a result of many orders. You want cost_basis I believe.

Clone Algorithm
28
Loading...
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
# Backtest ID: 56fac10d047dcb0f449a78e0
There was a runtime error.
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.

Delaney,
Returning to the above example code, I am looking to represent the following logic: if i hold say AAPL for 30 minutes, I would like to exit, regardless of it being a long or short position. Could you help me formulate this in code?

Also, yes cost_basis is what I needed ..thanks!

Shoaib

Also, I did find this post, however it looks to be only set for daily mode. Perhaps this could be converted to minute mode?

exit after x days

I think the only way you can do this right now is as Alisa has already stated.

Yes, you'll need to add another function that loops though all held positions every minute, checks the time filled, and exits if the position was filled more than 30 minutes ago. You'll also need to be careful to do any data structure cleanup necessary, such as appropriately updating the fill time data.