Back to Community
Position sizing question

I'm testing a MA xover system with a stock that splits into two different share classes (GOOG/GOOGL) to see how the position sizing logic behaves. This bug is over my head. I need a resident Q guru to take a look and enlighten me on what's causing the odd lots.

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
   
def initialize(context):    
    #context.stock = sid(26578,24) WORKING
    context.stock = sid(26578)
    
    context.fast = []
    context.slow = []
    context.medium = []
    context.breakoutFilter = 0
    
    context.passedMediumLong = False
    context.passedMediumShort = False
    
    context.holdingLongPosition = False
    context.holdingShortPosition = False
    
    context.entryPrice = 0.0
               
def handle_data(context, data):    
    context.fast.append(data[context.stock].price)
    context.slow.append(data[context.stock].price)
    context.medium.append(data[context.stock].price)

    fastMovingAverage = 9.0
    mediumMovingAverage = 24.0
    slowMovingAverage = 50.0
    
    if len(context.fast) > 30:      
       context.fast.pop(0)
       fastMovingAverage = sum(context.fast)/len(context.fast)
        
    if len(context.medium) > 60 * 30:        
       context.medium.pop(0)
       mediumMovingAverage = sum(context.medium)/len(context.medium)        
        
    if len(context.slow) > 60 * 60:
       context.slow.pop(0)
       slowMovingAverage = sum(context.slow)/len(context.slow)   
       
                               
    if ( (context.holdingLongPosition == False and context.holdingShortPosition == False) and 
            ((mediumMovingAverage > 0.0 and slowMovingAverage > 0.0)        
             and (mediumMovingAverage > slowMovingAverage))):            
         context.passedMediumLong = True
            
            
    if ( (context.holdingLongPosition == False and context.holdingShortPosition == False)
             and ((mediumMovingAverage > 0.0 and slowMovingAverage > 0.0)        
             and (mediumMovingAverage < slowMovingAverage))):            
         context.passedMediumShort = True
                                       
    # Entry Strategies                               
    if (context.holdingLongPosition == False and context.holdingShortPosition == False 
             and context.passedMediumLong == True
             and ((fastMovingAverage > 0.0 and slowMovingAverage > 0.0)        
             and (fastMovingAverage > mediumMovingAverage))):     
        
         if context.breakoutFilter > 3:
             log.info("ENTERING LONG POSITION")
             order(context.stock, 500)   
                
             context.holdingLongPosition = True
             context.breakoutFilter = 0                
             context.entryPrice = data[context.stock].price        
         else:
             context.breakoutFilter += 1        
                    
    if (context.holdingShortPosition == False and context.holdingLongPosition == False 
             and context.passedMediumShort == True
             and ((fastMovingAverage > 0.0 and slowMovingAverage > 0.0)        
             and (fastMovingAverage < mediumMovingAverage))):     
        
         if context.breakoutFilter > 6:
             log.info("ENTERING SHORT POSITION")
             order(context.stock, -500)  
             context.holdingShortPosition = True
             context.breakoutFilter = 0
             context.entryPrice = data[context.stock].price           
         else:
             context.breakoutFilter += 1      
                    
    # Exit Strategies                                            
    if (context.holdingLongPosition == True and 
            ((fastMovingAverage > 0.0 and slowMovingAverage > 0.0) 
             and (fastMovingAverage < mediumMovingAverage))):   
        
        if context.breakoutFilter > 5:
            order(context.stock, -500) 
            context.holdingLongPosition = False  
            context.breakoutFilter = 0
        else:
            context.breakoutFilter += 1
                        
    if (context.holdingShortPosition == True and 
            ((fastMovingAverage > 0.0 and slowMovingAverage > 0.0) 
             and (fastMovingAverage > mediumMovingAverage))):           
        if context.breakoutFilter > 5:
            order(context.stock, 500)   
            context.holdingShortPosition = False  
            context.breakoutFilter = 0
        else:
            context.breakoutFilter += 1            

        
            
        

                                
    
There was a runtime error.