python-pour-finance/11-Quantopian-Avancé/.ipynb_checkpoints/04-Stock-Sentiment-Analysis...

129 lines
3.8 KiB
Plaintext
Raw Normal View History

2023-08-21 15:12:19 +00:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Stock Sentiment Analysis\n",
"\n",
"Check out the video for full details."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Final Code\n",
"Here is the final code:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# This section is only importable in the backtester\n",
"from quantopian.algorithm import attach_pipeline, pipeline_output\n",
"\n",
"# General pipeline imports\n",
"from quantopian.pipeline import Pipeline\n",
"from quantopian.pipeline.factors import AverageDollarVolume\n",
"\n",
"\n",
"# Using the free sample in your pipeline algo\n",
"from quantopian.pipeline.data.accern import alphaone_free\n",
"\n",
"\n",
"def initialize(context):\n",
" # Schedule our rebalance function to run at the start of each week.\n",
" schedule_function(my_rebalance, date_rules.every_day())\n",
"\n",
" \n",
" attach_pipeline(make_pipeline(), \"pipeline\")\n",
"\n",
"def make_pipeline():\n",
"\n",
" \n",
" # Screen out penny stocks and low liquidity securities.\n",
" dollar_volume = AverageDollarVolume(window_length=20)\n",
" is_liquid = dollar_volume.rank(ascending=False) < 1000\n",
" \n",
" # Add pipeline factors\n",
" impact = alphaone_free.impact_score.latest\n",
" sentiment = alphaone_free.article_sentiment.latest\n",
"\n",
" return Pipeline(columns={\n",
" 'impact': impact,\n",
" 'sentiment':sentiment,\n",
" },\n",
" screen = is_liquid)\n",
" \n",
"\n",
"\n",
" \n",
"def before_trading_start(context, data):\n",
" port = pipeline_output('pipeline')\n",
" \n",
" # Grab stocks with 100 impact and >0.5 sentiment and go long.\n",
" context.longs = port[(port['impact']==100) & (port['sentiment']>0.75)].index.tolist()\n",
" \n",
" # Grab stocks with 100 impact and <-0.5 sentiment and go long.\n",
" context.shorts = port[(port['impact']==100) & (port['sentiment']< -0.75)].index.tolist()\n",
"\n",
" context.long_weight, context.short_weight = my_compute_weights(context)\n",
"\n",
"def my_compute_weights(context):\n",
"\n",
" # Compute even target weights for our long positions and short positions.\n",
" long_weight = 0.5 / len(context.longs)\n",
" short_weight = -0.5 / len(context.shorts)\n",
"\n",
" return long_weight, short_weight\n",
"\n",
"def my_rebalance(context, data):\n",
"\n",
" for security in context.portfolio.positions:\n",
" if security not in context.longs and security not in context.shorts and data.can_trade(security):\n",
" order_target_percent(security, 0)\n",
"\n",
" for security in context.longs:\n",
" if data.can_trade(security):\n",
" order_target_percent(security, context.long_weight)\n",
"\n",
" for security in context.shorts:\n",
" if data.can_trade(security):\n",
" order_target_percent(security, context.short_weight)\n",
" \n",
" \n",
" \n",
"\n",
" \n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}