Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Can't figure out even a simple example of using Stocktwits basic data stocktwits_limited_free

Hello,

This may sound stupid, but I don't know what else to try. Can somebody just show me an example of using the stocktwits basic data given out free from psychsignal.
I am new to this all, I had my pipeline example working though.

Simple example like printing out yesterdays stocktwits total bulls for a stock.

There are no examples on this board of how to do it using the import that is given with the data:

from quantopian.pipeline.data.psychsignal import stocktwits_limited_free  

Do you access it like you would regular pipe data? Because this isn't working for me:
today = Latest(inputs=[stocktwits_limited_free.bull_scored_messages], window_length=1)

Thanks for any help!!

2 responses

Hi Chris,

Thanks for trying out the data from PsychSignal. Its tough to tell exactly what the problem is without an error message. Generally, yes, you access it similar to other pipeline data such as pricing or fundamentals or other partner data sets like Accern's data.

I will say that we've recently had some troubles with algorithms hitting timeout errors. We're working on a fix and hope to have something out soon to solve it (if that's the error you're hitting).

Here's an algo I have laying around that should work over short time periods (pending our solution to the timeout problem). Note, it has some simplified methods for access the latest value of a data set:

from quantopian.algorithm import attach_pipeline, pipeline_output  
from quantopian.pipeline import Pipeline  
from quantopian.pipeline.data.builtin import USEquityPricing  
from quantopian.pipeline.factors import CustomFactor  
from quantopian.pipeline.data.psychsignal import stocktwits_limited_free


import pandas as pd  
import numpy as np

class AvgDailyDollarVolumeTraded(CustomFactor):  
    inputs = [USEquityPricing.close, USEquityPricing.volume]  
    window_length = 20  
    def compute(self, today, assets, out, close_price, volume):  
        out[:] = np.mean(close_price * volume, axis=0)

# Put any initialization logic here.  The context object will be passed to  
# the other methods in your algorithm.  
def initialize(context):  
    pipe = Pipeline()  
    pipe = attach_pipeline(pipe, name='sentiment_metrics')  
    # Add our AvgImpact factor to the pipeline  
    pipe.add(stocktwits_limited_free.bull_scored_messages.latest, "bull_msgs")  
    pipe.add(stocktwits_limited_free.bear_scored_messages.latest, "bear_msgs")  
    dollar_volume = AvgDailyDollarVolumeTraded()  
    # Screen out low liquidity securities.  
    pipe.set_screen(dollar_volume > 10**7)  
    context.shorts = None  
    context.longs = None  
    # context.spy = sid(8554)  
    schedule_function(rebalance, date_rules.month_start(), time_rules.market_open(hours=1))  
    set_commission(commission.PerShare(cost=0, min_trade_cost=0))  
    set_slippage(slippage.FixedSlippage(spread=0))  

def before_trading_start(context, data):  
    results = pipeline_output('sentiment_metrics').dropna()  
    bull_ranks = results["bull_msgs"].rank().order()  
    bear_ranks = results["bear_msgs"].rank().order()

    context.shorts = bull_ranks.head(5)  
    context.longs = bear_ranks.head(5)

    # The pipe character "|" is the pandas union operator  
    update_universe(context.longs.index | context.shorts.index)  


# Will be called on every trade event for the securities you specify.  
def handle_data(context, data):  
    #Log anything with open orders  
    for security in (context.shorts.index | context.longs.index):  
        if get_open_orders(security):  
            log.info("Open Order Check:")  
            log.info(security)  
    record(lever=context.account.leverage,  
           exposure=context.account.net_leverage,  
           num_pos=len(context.portfolio.positions),  
           oo=len(get_open_orders()))  

def rebalance(context, data):  
    for security in context.shorts.index:  
        if get_open_orders(security):  
            continue  
        if security in data:  
            log.info("Buying Shorts")  
            log.info(security)  
            order_target_percent(security, -1.0 / len(context.shorts))  
    for security in context.longs.index:  
        if get_open_orders(security):  
            continue  
        if security in data:  
            log.info("Buying Longs")  
            log.info(security)  
            order_target_percent(security, 1.0 / len(context.longs))  
    for security in context.portfolio.positions:  
        if get_open_orders(security):  
            continue  
        if security in data:  
            if security not in (context.longs.index | context.shorts.index):  
                log.info("Eliminating securities")  
                order_target_percent(security, 0)  
Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

This is perfect!

Thank You So Much!