Back to Community
Why does my algo go short when I use target_percentage on daily candles?

The attached strategy is hardly perfect, but the idea is to test a very aggressive basket of stocks (in this case cybersecurity) for momentum,
and stay in those until their momentum turns negative, then jump into bonds (tlt,eif or uup) if those show positive momentum, and cash if nothing is going up. My thanks to Chris De Palma for the original elegant code.

If you look at the log files, you will see the thing goes short huge amounts of various securities. This should not happen, as I never have an
order to go short. When I want to exit a position, I am using order_target_percentage(targetSecurity,0) , which gets called once a week. So even if this were to be unnecessarily repeated it should not go short, right?

What gives?

Clone Algorithm
3
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: 567b291c5a1e5f118b7d715c
There was a runtime error.
5 responses

Hi Serge,

I tweaked your algorithm to include a check for open orders as I suspect some of your orders were taking a while to fill, and it seems to have solved the problem. There are no short positions in this backtest. Hopefully this helps!

Clone Algorithm
3
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: 567b78b15a91c71172b25bbb
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.

Profit per dollar spent is ~20 percentage points lower than the benchmark because of intraday leverage of 1.52 around Feb 24, 2014. Explanation.

Jamie,

  Thanks for the tweak. It works, but I'm not sure why. I don't understand how repeatedly calling  
    order_target(someStock,0)  
   could cause that stock to go short. I thought that was supposed to bring the stock down to the difference between actual levels and 0 , but not  
   beyond.

garyha,
thanks for your input. I will incorporate that into all my algos from here on in.

Here's the same algo incorporating those tweaks and adding 1 important fix:

I was hedging with EIF (Exchange Income Corp.) instead of desired IEF ( iShares Barclays 7-10 Year Treasury Bond Fund)

Here are results.

Clone Algorithm
3
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: 567c179b0621ed1184f9ff3e
There was a runtime error.

Serge,

Think of it like this:

Say you have 100 shares of stock XXX. If you order_target_percent(XXX, 0), you place an order for -100 shares of XXX. Before that fills, you place another order_target_percent(XXX, 0) which places another order for -100 shares of XXX (since you still technically hold 100 shares of XXX). Now you have two open orders of -100 shares in XXX. This means that when they fill, you will end up with -100 shares of XXX.

I hope this clears it up for you!