Skip to content

Instantly share code, notes, and snippets.

@buswedg
Created April 12, 2020 04:05
Show Gist options
  • Save buswedg/26d86255ae5f55dce8a85e2cba0b41f5 to your computer and use it in GitHub Desktop.
Save buswedg/26d86255ae5f55dce8a85e2cba0b41f5 to your computer and use it in GitHub Desktop.
reinforcement_learning_for_share_trading\run
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import datetime as dt\n",
"import pandas as pd\n",
"\n",
"from strategy_learner import *\n",
"from indicators import *\n",
"from utils import *"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"entity_symbol = \"AAPL\"\n",
"sv = 100000\n",
"comm = 0.0\n",
"imp = 0.0\n",
"\n",
"pd_prices = get_price(entity_symbol)\n",
"pd_features = get_indicator(pd_prices)\n",
"\n",
"slr = StrategyLearner(impact=imp)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# in-sample\n",
"sd = dt.datetime(2010, 1, 4)\n",
"ed = dt.datetime(2019, 12, 31)\n",
"\n",
"pd_train_trades = slr.addEvidence(entity_symbol, pd_prices, pd_features,\n",
" sd=sd, ed=ed, sv=sv)\n",
"#print(pd_train_trades)\n",
"\n",
"pd_train_bench = pd.DataFrame(data=[(pd_train_trades.index.min(), entity_symbol, 'BUY', sv / pd_prices.loc[sd, 'adj_close']),\n",
" (pd_train_trades.index.max(), entity_symbol, 'SELL', sv / pd_prices.loc[sd, 'adj_close'])],\n",
" columns=['date', 'entity_symbol', 'order', 'shares'])\n",
"\n",
"pd_train_bench = pd_train_bench.set_index('date')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# out-of-sample\n",
"sd = dt.datetime(2020, 1, 2)\n",
"ed = dt.datetime(2020, 4, 9)\n",
"\n",
"pd_test_trades = slr.testPolicy(entity_symbol, pd_prices, pd_features,\n",
" sd=sd, ed=ed, sv=sv)\n",
"#print(pd_test_trades)\n",
"\n",
"pd_test_bench = pd.DataFrame(data=[(pd_test_trades.index.min(), entity_symbol, 'BUY', sv / pd_prices.loc[sd, 'adj_close']),\n",
" (pd_test_trades.index.max(), entity_symbol, 'SELL', sv / pd_prices.loc[sd, 'adj_close'])],\n",
" columns=['date', 'entity_symbol', 'order', 'shares'])\n",
"\n",
"pd_test_bench = pd_test_bench.set_index('date')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"market_simulator(pd_train_trades, pd_train_bench, pd_prices,\n",
" sv=sv, comm=comm, imp=imp,\n",
" save_fig=True, gen_stats=True,\n",
" fig_name='AAPL_sl_train_plot.png',\n",
" stats_name='AAPL_sl_train_stats.tsv')\n",
"\n",
"market_simulator(pd_test_trades, pd_test_bench, pd_prices,\n",
" sv=sv, comm=comm, imp=imp,\n",
" save_fig=True, gen_stats=True,\n",
" fig_name='AAPL_sl_test_plot.png',\n",
" stats_name='AAPL_sl_test_stats.tsv')"
]
}
],
"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.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment