python-pour-finance/10-Plateforme-Quantopian/.ipynb_checkpoints/05-Trading-Algorithm-Exerci...

94 lines
3.5 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Trading Algorithm Exercise Solutions\n",
"\n",
"** This is an extremely open exercise and there are lots of ways to do it! Please feel free to just skip to the solutions to treat it as an example code along. If you attempt the exercise, do not expect the example solution to exactly match up to your solution. You may have performed much better (or much worse)! **\n",
"\n",
"## Your Task\n",
"\n",
"Your manager wants to see if [Bollinger Bands](https://en.wikipedia.org/wiki/Bollinger_Bands) are still a meaningful technical analysis strategy on their own. For this exercise, you will be testing *Johnson and Johnson* sid(4151).Specifically, your manager has decided he wants set 100% of the portfolio to go long when the stock price is below 2 times the 20 day rolling standard deviation subtracted from the 20 day moving average, and go 100% short of the portfolio on that stock when the current price is above 2 times the 20 day rolling standard deviation added on to the 20 day moving average. The check for this signal event should only happen once per day. This is probably a very unreasonable strategy, but the main point of this is to exercise your ability to write out backtest algorithms with Quantopian.\n",
"\n",
"## Time Frame\n",
"\n",
"You should use the following time frame for this exercise (so you can at least have a reasonable comparison to the solutions, but feel free to play around with this!)\n",
"\n",
"#### BACKTEST START: Jul-20-2014\n",
"#### BACKTEST END: Jul-20-2017"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Setup our variables\n",
"def initialize(context):\n",
" context.jj = sid(4151)\n",
"\n",
" schedule_function(check_bands,date_rules.every_day())\n",
" \n",
"def check_bands(context, data):\n",
" \n",
" cur_price = data.current(context.jj,'price')\n",
" \n",
" # Load historical data for the stocks\n",
" prices = data.history(context.jj,'price', 20 , '1d')\n",
" \n",
" avg = prices.mean()\n",
" std = prices.std()\n",
" lower_band = avg - 2*std\n",
" upper_band = avg + 2*std\n",
" \n",
" if cur_price <= lower_band:\n",
" order_target_percent(context.jj, 1.0)\n",
" print('Buying')\n",
" print('Current price is: ' + str(cur_price))\n",
" print(\"Lower band is: \"+str(lower_band))\n",
" \n",
" \n",
" elif cur_price >= upper_band:\n",
" order_target_percent(context.jj, -1.0)\n",
" print('Shorting')\n",
" print('Current price is: ' + str(cur_price))\n",
" print(\"Upper band is: \"+str(upper_band))\n",
" else:\n",
" pass\n",
" \n",
" record(upper=upper_band,\n",
" lower=lower_band,\n",
" mvag_20=avg,\n",
" price=cur_price)"
]
}
],
"metadata": {
"anaconda-cloud": {},
"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": 1
}