Hello everyone, I'm new to Quantopian and I'm testing my first algo, it's purpose is to find stocktwits sentiment for a stock, give it a percentile value and invest a percentage of our portfolio based on that percentile.
My versión of stocktwits sentiment will be made by two factors: Bull minus bear and bull bear ratio, these are multiplied to add/lessen importance of the first ratio (which has the sentiment score). My code gives me the following error:
ValueError: cannot copy sequence with size 100 to array axis with dimension 8343
Which I know comes from the out[:] line, but don't understand why, since [stats.percentileofscore(a[x]*b[x], a[x,-1:]*b[x,-1:]) for x in range(len(a))] Works on normal Python with any given numpy array.
# This section is only importable in the backtester
from quantopian.algorithm import attach_pipeline, pipeline_output
# General pipeline imports
from quantopian.pipeline import Pipeline, CustomFilter
from quantopian.pipeline.factors import AverageDollarVolume
from quantopian.pipeline.filters import StaticAssets
from quantopian.pipeline.data.psychsignal import stocktwits_free
from quantopian.pipeline import CustomFactor, CustomFilter
import numpy as np
from scipy import stats
class ponderated(CustomFactor):
inputs = [stocktwits_free.bull_minus_bear, stocktwits_free.bull_bear_msg_ratio]
window_length = 100
def compute(self, today, assets, out, a, b):
out[:] = [stats.percentileofscore(a[x]*b[x], a[x,-1:]*b[x,-1:]) for x in range(len(a))]
def make_pipeline():
# Create our pipeline
pipe = Pipeline()
# Screen out penny stocks and low liquidity securities.
dollar_volume = AverageDollarVolume(window_length=20)
is_liquid = dollar_volume.rank(ascending=False) < 1000
#Our made class
ponderated_list = ponderated()
# Add pipeline factors
pipe.add(ponderated_list, 'ponderated')
# Set our pipeline screens
pipe.set_screen(is_liquid)
return pipe
def initialize(context):
attach_pipeline(make_pipeline(), "pipeline")
def portfolio_percentage():
results = pipeline_output('pipeline')
a = []
for each in results:
a.append(results)
return a
def handle_data(context, data):
results = portfolio_percentage()
for each in results:
order_target_percent(sid(24), float(each.iloc[-1]))