Back to Community
How can I attach volume to the output of these logs? Is there a pandas method to do this?

I have this algo that computes the top 5 gainers and losers by percentage over the course of a given day.
The final output is coming through from

top_gainers = todays_gain_since_yesterday_close.nlargest(5)  
top_losers = todays_gain_since_yesterday_close.nsmallest(5)  
log.info('top gainers \n{} top losers \n{}'.format(top_gainers, top_losers))  

and the output looks like

2020-05-19 08:40 handle_data:75 INFO top gainers  
Equity(25972 [DVAX])    37.796087  
Equity(2385 [DY])       20.426344  
Equity(11120 [EXP])      8.914729  
Equity(50735 [AYX])      7.648747  
Equity(45113 [DOC])      7.509271  
dtype: float64 top losers  
Equity(52864 [GOSS])   -14.993351  
Equity(46206 [CPG])    -14.803625  
Equity(52119 [TCDA])    -9.505703  
Equity(53083 [TPTX])    -8.554688  
Equity(50807 [CVNA])    -7.919286  

I know the output is a series, and the series consists of the equity and the calculated percentage. What I would like to know is how to attach volume to the output of these logs? Is there a pandas method to do this?

I want it to look like this, where the third item is the current volume of the respective equity

2020-05-19 08:40 handle_data:75 INFO top gainers  
Equity(25972 [DVAX])    37.796087    3000000  
Equity(2385 [DY])       20.426344    373467  
Equity(11120 [EXP])      8.914729   457345257  
Equity(50735 [AYX])      7.648747   3457345645  
Equity(45113 [DOC])      7.509271    5626232  
dtype: float64 top losers  
Equity(52864 [GOSS])   -14.993351    3456345  
Equity(46206 [CPG])    -14.803625    534563456  
Equity(52119 [TCDA])    -9.505703     4563456345  
Equity(53083 [TPTX])    -8.554688     456345634  
Equity(50807 [CVNA])    -7.919286      576744357  

Anyone have any suggestions?

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
"""
Algorithm to log the top gainers and losers each minute

"""

# import pipeline methods 
from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline import Pipeline, CustomFactor

# import the built in filters and factors
from quantopian.pipeline.filters import QTradableStocksUS, Q1500US, Q500US, StaticAssets

# import any datasets we need
from quantopian.pipeline.data import USEquityPricing

# import numpy and pandas just because they rock
import numpy as np
import pandas as pd

def initialize(context):
    """
    Called once at the start of the algorithm.
    """
    # Make our pipeline and attach to the algo
    # Used to get our universe of stocks and close prices
    attach_pipeline(make_stock_pipeline(context), 'my_stocks')
    
    context.yesterdays_data = None
    context.minuteCounter = 0 
    

def make_stock_pipeline(context):
    """
    Get a list of stocks.
    This will be the universe to check for gainers and losers
    """

    # Set universe 
    stock_universe = QTradableStocksUS()
    
    pipe = Pipeline(
        columns={'yesterdays_close': USEquityPricing.close.latest},
        screen=stock_universe
    )
    return pipe
            
def before_trading_start(context, data):
    # Fetch the pipeline data
    pipe_data = pipeline_output('my_stocks')
    
    # Get the universe of stocks as a list
    context.stocks = pipe_data.index.tolist()
    
    # Get yesterdays close prices as a series
    context.yesterdays_close = pipe_data.yesterdays_close
    

def handle_data(context, data):
    context.minuteCounter += 1
 
    # counter
    if context.minuteCounter >= 10:
        context.minuteCounter = 0
    
        current_data = (data.current(assets=context.stocks, fields=['price', 'volume'])) # apparently this isn't enough
        
        # Calculate the percent gain since close yesterday for all the stocks
        todays_gain_since_yesterday_close = ((current_data['price'] / context.yesterdays_close) - 1) * 100.0
        
        # Get the top 5 gainers and losers for the day
        top_gainers = todays_gain_since_yesterday_close.nlargest(5)
        top_losers = todays_gain_since_yesterday_close.nsmallest(5)
    
        # Here we log the data but could also do more logic
        log.info('top gainers \n{} top losers \n{}'.format(top_gainers, top_losers))
There was a runtime error.