{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Analyse de Sentiments\n", "\n", "Regardez la vidéo pour plus de détails." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Code final\n", "Voici le code final:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Cette section est uniquement importable dans le backtester\n", "from quantopian.algorithm import attach_pipeline, pipeline_output\n", "\n", "# Importations générales de pipeline\n", "from quantopian.pipeline import Pipeline\n", "from quantopian.pipeline.factors import AverageDollarVolume\n", "\n", "\n", "# Utiliser l'échantillon gratuit dans votre algo de pipeline\n", "from quantopian.pipeline.data.sentdex import sentiment_free as sentdex\n", "\n", "# pour calculer l'impact\n", "from quantopian.pipeline.factors import CustomFactor\n", "import numpy as np\n", "\n", "\n", "def initialize(context):\n", " # Planifier notre fonction de rééquilibrage au début de chaque semaine.\n", " schedule_function(my_rebalance, date_rules.every_day())\n", "\n", " \n", " attach_pipeline(make_pipeline(), \"pipeline\")\n", "\n", "# Calcul du sentiment m sur la longueur de la fenêtre\n", "class AvgSentiment(CustomFactor):\n", " def compute(self, today, assets, out, impact):\n", " np.mean(impact, axis=0, out=out)\n", " \n", "def make_pipeline():\n", "\n", " # Éliminez les actions à un penny et les titres peu liquides.\n", " dollar_volume = AverageDollarVolume(window_length=30)\n", " is_liquid = dollar_volume.top(500)\n", " \n", " # Calcul du sentiment moyen\n", " avg_sentiment = AvgSentiment(inputs=[sentdex.sentiment_signal], window_length=10)\n", "\n", " return Pipeline(columns={\n", " 'sentiment':avg_sentiment\n", " }, screen=is_liquid)\n", " \n", "\n", " \n", "def before_trading_start(context, data):\n", " portfo = pipeline_output('pipeline')\n", " context.something = portfo['sentiment'] \n", " \n", " # Actions avec un sentiment > 0\n", " context.longs = portfo[portfo['sentiment'] > 0].index.tolist()\n", " \n", " # Actions avec un sentiment < 0\n", " context.shorts = portfo[portfo['sentiment'] < 0].index.tolist()\n", "\n", " context.long_weight, context.short_weight = my_compute_weights(context)\n", "\n", " \n", "def my_compute_weights(context):\n", "\n", " # Calculer des pondérations cibles égales pour nos positions longues et nos positions courtes.\n", " if len(context.longs)==0:\n", " long_weight = 0\n", " else:\n", " long_weight = 0.5/len(context.longs)\n", " \n", " if len(context.shorts)==0:\n", " short_weight = 0\n", " else:\n", " short_weight = -0.5/len(context.shorts)\n", " \n", " return long_weight, short_weight\n", "\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", " print(context.something)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.5", "language": "python", "name": "py35" }, "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.5.9" } }, "nbformat": 4, "nbformat_minor": 2 }