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)