I'm building my first algorithm with a very basic strategy.
I simply want to find all pharma companies with a market cap between [200M, 5B] with clinical result 6 month ahead of the current trading day and buy, then sell that position 1 month before the actual clinical trial result.
My current approach is to query the clinical trials data base and somehow combine that with a query to the fundamentals data base and I simply want to run through a unified dataframe to see what I should buy and what I should sell / hold.
My question would be: How to I combine my df result from the clinical trials database with that of the fundamentals and then loop through and buy / sell / hold?
I realize both have 'symbol' columns, just not sure how to leverage them.
If it is of any help here is my code so far, thank you for any input!
# for clinical trial data
from quantopian.interactive.data.eventvestor import clinical_trials_free
# libraries for manipulating datasets
from odo import odo
import pandas as pd
# For building universe of stocks for algo
from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline import Pipeline
# For getting fundamental data (primairly market cap data to filter companies)
#from
def initialize(context):
#set benchmark to XBI
set_benchmark(symbol('XBI'))
# Creates and attaches an empty pipeline
pipe = Pipeline()
pipe = attach_pipeline(pipe, name = "bio_pipeline")
# Find all sids in clinical dataset
upcoming = clinical_trials_free[clinical_trials_free.clinical_phase != "Phase I" and clinical_trials_free.clinical_phase != "Pre-Clinical" and clinical_trials_free.clinical_phase != "Phase 0" ][['symbol', 'sid','asof_date', 'clinical_phase', 'clinical_scope', 'clinical_result']]
upcoming_df = odo(upcoming, pd.DataFrame)
# Filter by market cap (<$5B USD)
lessThan5B_df = get_fundamentals(
query(
fundamentals.valuation.market_cap
fundamentals.share_class_reference.symbol
)
.filter(
fundamentals.share_class_reference.symbol in upcoming_df['symbol']
)
.filter(
fundamentals.valuation.market_cap <= 5000000000
)
.filter(fundamentals.valuation.market_cap >= 2000000
)
)
# Filter by "if event in next 6 months"
def before_trading_start(context, data):
pass
# results = pipeline_output("bio_pipeline")
# update_universe(results)
# Will be called on every trade event for the securities you specify.
def handle_data(context, data):
pass