Back to Community
Cannot convert NA to Integer

The code keeps saying that there is an error where it can't convert NA to Integers, but when I run the pipeline there is no NAs in the results. Where is the error coming from, and what part of the code should I change to make it run?

Thanks,
Taha

Clone Algorithm
1
Loading...
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
# Import Algorithm API
import quantopian.algorithm as algo
 
# Import Optimize API
import quantopian.optimize as opt
 
# Import built-in universe and Risk API method
from quantopian.pipeline.filters import QTradableStocksUS
from quantopian.pipeline.experimental import risk_loading_pipeline
 
 
def initialize(context):
    # Constraint parameters
    context.max_leverage = 1.0
    context.max_pos_size = 0.03
    context.max_turnover = 0.95
 
    # Attach data pipelines
    algo.attach_pipeline(
        make_pipeline(),
        'data_pipe'
    )
    algo.attach_pipeline(
        risk_loading_pipeline(),
        'risk_pipe'
    )
 
    # Schedule rebalance function
    algo.schedule_function(
        rebalance,
        algo.date_rules.week_start(),
        algo.time_rules.market_open(),
    )
 

# Pipeline definition
from quantopian.pipeline import Pipeline
# Form 3 Transactions
from quantopian.pipeline.data.factset.ownership import Form3AggregatedTrades
# Form 4 and 5 Transactions
from quantopian.pipeline.data.factset.ownership import Form4and5AggregatedTrades
from quantopian.pipeline.domain import US_EQUITIES


def make_pipeline():
    insider_transactions_3 = Form3AggregatedTrades.slice(False, 30)
    insider_transactions_4and5 = Form4and5AggregatedTrades.slice(False, 30)
    
    filer_3 = insider_transactions_3.num_unique_filers.latest
    buyers_4And5 = insider_transactions_4and5.num_unique_buyers.latest
    sellers_4And5 = insider_transactions_4and5.num_unique_sellers.latest
    
    all_transactions = filer_3 + buyers_4And5 + sellers_4And5
    buyers = filer_3 + buyers_4And5
    
    factor = buyers/sellers_4And5

        
    check = sellers_4And5.notnull() & sellers_4And5.notnan() & buyers.notnan() & buyers.notnull() & (sellers_4And5 != 0)
    
    return Pipeline(columns = {"factor":factor},
                    screen = check & (factor > 1),
                    domain = US_EQUITIES)

 
def rebalance(context, data):
    # Get pipeline outputs
    pipeline_data = algo.pipeline_output('data_pipe')
    risk_factor_betas = algo.pipeline_output('risk_pipe')
 
    # Retrieve alpha from pipeline output
    alpha = pipeline_data.factor
 
    if not alpha.empty:
        # Create MaximizeAlpha objective
        objective = opt.MaximizeAlpha(alpha)
 
        # Create position size constraint
        constrain_pos_size = opt.PositionConcentration.with_equal_bounds(
            -context.max_pos_size,
            context.max_pos_size
        )
 
        # Constrain target portfolio's leverage
        max_leverage = opt.MaxGrossExposure(context.max_leverage)
 
        # Ensure long and short books
        # are roughly the same size
        dollar_neutral = opt.DollarNeutral()
 
        # Constrain portfolio turnover
        max_turnover = opt.MaxTurnover(context.max_turnover)
 
        # Constrain target portfolio's risk exposure
        # By default, max sector exposure is set at
        # 0.2, and max style exposure is set at 0.4
        factor_risk_constraints = opt.experimental.RiskModelExposure(
            risk_factor_betas,
            version=opt.Newest
        )
 
        # Rebalance portfolio using objective
        # and list of constraints
        algo.order_optimal_portfolio(
            objective=objective,
            constraints=[
                constrain_pos_size,
                # max_leverage,
                # dollar_neutral,
                # max_turnover,
                # factor_risk_constraints,
            ]
        )
There was a runtime error.