Back to Community
Not entering short postions

Hey All,

I'm working on this simple algorithm that enters into longs and shorts based on moving averages. It is entering into long positions but not short. I can't seem to figure out why. Any help would be appreciated.

Clone Algorithm
1
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: 5a0dbe84782583437ed04731
There was a runtime error.
1 response

'shorts' -- always False. One thing is the 'and' won't do when combining True|False values in pipeline, and grouping with parens is important. Maybe begin with:

buy   = iso & (O_sma10 < 0) & (sma10 > 0)  
short = iso & (O_sma10 > 0) & (sma10 < 0)

The debugger will be useful: https://www.google.com/search?q=debugger+site:quantopian.com
Click line number 100 and here are some example commands. (I've added some columns).

      columns={  
          'longs' : buy,  
          'shorts': short,  
          'p_diff': p_diff,  
          'iso'   : iso,  
          'sma10' : sma10,  
        'O_sma10' : O_sma10, 

len(pipe_results)  
    1498  
pipe_results.tail()  
    DataFrame:  
                               O_sma10    iso longs    p_diff shorts     sma10  
    Equity(32897 [NMX])     126.276500  False  True  0.003511  False  125.7374  
    Equity(32921 [SPR])      31.430000  False  True  0.033636  False   31.5030  
    Equity(34913 [RF])       37.309106  False  True  0.017522  False   37.4150  
    Equity(36930 [DISC_A])   16.147900  False  True  0.024425  False   16.1729  
    Equity(40069 [RBAK])     23.825200  False  True  0.269772  False   24.2650  
pipe_results[(pipe_results.O_sma10 > 0) & (pipe_results.p_diff < 0)].tail()  
    DataFrame:  
                          O_sma10    iso longs    p_diff shorts    sma10  
    Equity(32320 [JCG])   38.6089  False  True -0.010921  False  38.2609  
    Equity(32497 [HBI])   23.5190  False  True -0.014288  False  23.5490  
    Equity(32603 [WU])    22.4363  False  True -0.014521  False  22.4123  
    Equity(32618 [RVBD])  31.4018  False  True -0.000417  False  31.4498  
    Equity(32714 [LDOS])  17.9090  False  True -0.041563  False  17.8560  
pipe_results.p_diff.sort_values().head()  
    > pipe_results.p_diff.sort_values().head(): Series  
        Equity(26978 [OXPS]): -0.13726352285  
        Equity(26210 [SIRF]): -0.11939235699  
        Equity(26751 [FOXH]): -0.118967867679  
        Equity(8412 [ZOLT]): -0.118587727699  
        Equity(27026 [NDAQ]): -0.100516370232  
pipe_results.p_diff.sort_values().tail()  
    > pipe_results.p_diff.sort_values().tail(): Series  
        Equity(3353 [GROW]): 0.131512078776  
        Equity(21284 [ITMN]): 0.14046314953  
        Equity(20541 [RHT]): 0.144554289269  
        Equity(680 [AXR]): 0.180807087536  
        Equity(40069 [RBAK]): 0.269771774244