Back to Community
Fading the Gap Algo Bug?

Cannot figure out why my portfolio doubles in size with two short sales in a row.

Log output:

2013-06-07PRINTPrev Hi: 217.93  
2013-06-07PRINTOpen: 219.03  
2013-06-07PRINTBuy Short on 2013-06-07 13:31:00+00:00  
2013-06-07PRINTPortfolio value: 10214.7381812  
2013-06-07PRINTPosition shares: -46.6362515691  
2013-06-07PRINT----  
2013-06-07PRINTPortfolio value: 20314.4238187  
2013-06-07PRINTPosition shares: -46  
2013-06-07PRINTSell- cover short on 2013-06-07 13:36:00+00:00  
2013-06-07PRINTTarget: 217.93  
2013-06-07PRINTCurrent: 217.401  
2013-06-07PRINTPortfolio value: 20314.4238187  
2013-06-07PRINTPosition shares: 46  

My algo should only have one position open at a time. Looks like for some reason a position is not closing out and begins building and borrowing to do so.

What is the best way to ensure no cash is borrowed, and positions are closed out before another position is opened?

Thanks

Clone Algorithm
6
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: 51dc1ce5fd222306dd3677af
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.
2 responses

@Chuck, by definition a short position must borrow (stocks, not cash). As a result your "cash" is still there but is tied up as collateral.

I plugged in a quick test of how much capital you are using. It looks like you are borrowing $90k on your starting $10k. Not good.

I would suggest you examine your algorithm logic for how you check for existing positions. The easiest way I know of is from the portfolio object:

qty = context.portfolio.positions[context.stocks[0]].amount  
if qty > 0: print "long"  
if qty < 0: print "short"  
if qty == 0: print "no position"  

I'm wondering about your $10k starting cash. If you are planning to trade in a real account you will need $25k minimum to get a margin account. Otherwise you won't be able to take a short position at all.

Clone Algorithm
9
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: 51dc22e6fd222306dd36a2eb
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.

Thanks for the look Dennis.

I will change the initial investment to $25k as that does make more sense.

I will also add the logic you suggested.