Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Buy/Sell Understanding {order_target_percent(a,b)}

All,

I want to clarify something on orders and how Quantopian works:
When we do order_target_percent(security, 0.1) we are ordering 10% and likewise order_target_percent(security, 0.-1) is for the short.

How do we exit the position. For example for a simple long and high sell is it appropriate to write

order_target_percent(security, 0.1)
if price_paid < current price:
order_target_percent(security, 0.0)

Will that appropriately sell the security for the profit?

Also suppose that your criteria executes:

order_target_percent(security, 0.1)

But then subsequent logic also does:

order_target_percent(security, 0.-1)
Will multiple positions be held as a long and short or does it null it out to a "0" position?
Just looking for a little clarification looking at the print out statements.

Thanks
Jon
```

3 responses

Good questions about the order_target_percent method. This method

  1. calculates the target dollar value of shares as portfolio_value * target_percent
  2. calculate the target quantity of shares by dividing this target dollar value by the last close price
  3. calculates the net quantity of shares to order as target shares minus the current shares (if any).
  4. place an order for the net quantity of shares

One thing to note in the above logic is that this method does not look at any open orders. It only uses the current portfolio and current holdings.

So, yes the appropriate way to close a position is order_target_percent(security, 0.0). There was also the question "Will that appropriately sell the security for the profit?" All this method does (as with all the order methods) is to place an order. The price at which that order fills (and therefore if it sells at a profit) is determined by the slippage model and historical minute price data. There's no guarantee, just as in real life, that an order will fill at a profit.

If one executes order_target_percent(security, 0.-1) then an order will be placed to make the final position short 10%. It doesn't cancel out a previous order order_target_percent(security, 0.1). Remember this is placing an order to meet a final target . There is a separate method order_percent which will simply place an order for the specified percent.

The final question Will multiple positions be held as a long and short or does it null it out to a "0" position? It will cancel out. One can never have both long and short positions of the same stock. This is not only true in the backtester, but also true in real life that most brokers won't allow this either.

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.

Dan:

Thanks for the insight on this one. What is the best way to determine if you are holding a security?
Would it be: security in context.portfolio.positions?

For example:
elif ((security in context.portfolio.positions) and (self.rsi_vec[0] > self.rsi_vec[1])):
pass
print("Long Pass")):

What I claim is that this would look to see if the security is in the portfolio and we have upward momentum, no need to do anything.
Is this correct?

Thanks
Jon

The keys of the dic context.portfolio.positions are all the currently held securities. So, the following will check if a single security is currently held

security_is_currently_held = security in context.portfolio.positions

Rather than doing a lot of for loops and if statements however, you may want to 'vectorize' your logic with sets and do everything all at once. Something like this

current_positions = set(context.portfolio.positions)  
upward_momentum = set(pipeline_output.query('rsi_current > rsi_previous').index)

hold_these = current_positions & upward_momentum  
close_these = current_positions - upward_momentum  

The resulting 'hold_these' and 'close_these' are sets of the securities to hold and close respectively. One can do more complicated logic in this fashion and it's often more readable. This assumes that 'rsi_current' and 'rsi_previous' are pipeline factors but something very similar could be done outside of pipeline by putting all the rsi values into a dataframe or numpy array.

One thing to be aware is any open orders will not be reflected in context.portfolio.positions. This often isn't an issue but something to think about.