Skip to content

Instantly share code, notes, and snippets.

@sangheestyle
Created January 1, 2017 23:44
Show Gist options
  • Save sangheestyle/3d06449d4d1420f305baa4f6a1106254 to your computer and use it in GitHub Desktop.
Save sangheestyle/3d06449d4d1420f305baa4f6a1106254 to your computer and use it in GitHub Desktop.
Practicing wash sell model
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from datetime import date, timedelta"
]
},
{
"cell_type": "code",
"execution_count": 78,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"WASHSALE_DAY_DELTA = 30\n",
"\n",
"\n",
"class StockEstimation:\n",
" \"\"\" Modeled by Sanghee\"\"\"\n",
" \n",
" def get_max_profic(self):\n",
" return 10000\n",
"\n",
"\n",
"class WashSaleFunctions:\n",
" \"\"\" Written by James\"\"\"\n",
" \n",
" def calc_wash_sale_period(self):\n",
" \"\"\"\n",
" Requirement:\n",
" \n",
" a. sell_date\n",
" b. repurchase_date\n",
" \"\"\"\n",
" before = self.get_wash_sale_before()\n",
" after = self.get_wash_sale_after()\n",
" if before < self.repurchase_date and \\\n",
" self.repurchase_date < after:\n",
" return True\n",
" else:\n",
" return False\n",
" \n",
" def get_wash_sale_before(self):\n",
" return self.sell_date - timedelta(WASHSALE_DAY_DELTA)\n",
" \n",
" def get_wash_sale_after(self):\n",
" return self.sell_date + timedelta(WASHSALE_DAY_DELTA) \n",
" \n",
"\n",
"\n",
"class TradeUnit(WashSaleFunctions, StockEstimation):\n",
" \n",
" def __init__(self, type_, name, quantity, price, \n",
" buy_date, sell_date, repurchase_date):\n",
" \n",
" assert isinstance(buy_date, date), \"Date type required\"\n",
" assert isinstance(sell_date, date), \"Date type required\"\n",
" assert isinstance(repurchase_date, date), \"Date type required\"\n",
" \n",
" self.type_ = type_\n",
" self.name = name\n",
" self.quantity = quantity\n",
" self.price = price\n",
" self.buy_date = buy_date\n",
" self.sell_date = sell_date\n",
" self.repurchase_date = repurchase_date\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 74,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"class HealthTaxUnit(WashSaleFunctions):\n",
" \n",
" def __init__(self, buy_date, sell_date, repurchase_date):\n",
" \n",
" assert isinstance(buy_date, date), \"Date type required\"\n",
" assert isinstance(sell_date, date), \"Date type required\"\n",
" assert isinstance(repurchase_date, date), \"Date type required\"\n",
" \n",
" self.buy_date = buy_date\n",
" self.sell_date = sell_date\n",
" self.repurchase_date = repurchase_date"
]
},
{
"cell_type": "code",
"execution_count": 75,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"health = HealthTaxUnit(\n",
" date(2017, 01, 01),\n",
" date(2017, 01, 02),\n",
" date(2017, 01, 15)\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 76,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 76,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"health.calc_wash_sale_period()"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"datetime.date(2017, 1, 1)"
]
},
"execution_count": 67,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"health.buy_date"
]
},
{
"cell_type": "code",
"execution_count": 88,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"items = []\n",
"for i in range(10):\n",
" item = [\"stock\", \"samsung\", i, 100.05, date(2017, 01, 01), date(2017, 01, 01), date(2017, 01, 01)]\n",
" items.append(item)"
]
},
{
"cell_type": "code",
"execution_count": 89,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"results = []\n",
"for item in items:\n",
" tu = TradeUnit(*item)\n",
" results.append(tu.calc_wash_sale_period())"
]
},
{
"cell_type": "code",
"execution_count": 90,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[True, True, True, True, True, True, True, True, True, True]"
]
},
"execution_count": 90,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"results"
]
},
{
"cell_type": "code",
"execution_count": 83,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"<__main__.TradeUnit instance at 0x10e90d680>"
]
},
"execution_count": 83,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"TradeUnit(*a)"
]
},
{
"cell_type": "code",
"execution_count": 80,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"samsung_wash_sale_true = TradeUnit(\n",
" \"stock\",\n",
" \"Samsung\",\n",
" 30,\n",
" 100.05,\n",
" date(2017, 01, 01),\n",
" date(2017, 01, 02),\n",
" date(2017, 01, 15)\n",
")\n",
"\n",
"samsung_wash_sale_false = TradeUnit(\n",
" \"stock\",\n",
" \"Samsung\",\n",
" 30,\n",
" 100.05,\n",
" date(2017, 01, 01),\n",
" date(2017, 01, 02),\n",
" date(2017, 05, 15)\n",
")\n",
"\n",
"assert samsung_wash_sale_true.calc_wash_sale_period() == True\n",
"assert samsung_wash_sale_false.calc_wash_sale_period() == False"
]
},
{
"cell_type": "code",
"execution_count": 81,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"10000"
]
},
"execution_count": 81,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"samsung_wash_sale_false.get_max_profic()"
]
},
{
"cell_type": "code",
"execution_count": 72,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"datetime.date(2017, 2, 1)"
]
},
"execution_count": 72,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"samsung_wash_sale_true.get_wash_sale_after()"
]
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 73,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"samsung_wash_sale_true.calc_wash_sale_period()"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('Before:', True)\n",
"('After:', True)\n"
]
},
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"samsung.calc_wash_sale_period()"
]
},
{
"cell_type": "code",
"execution_count": 79,
"metadata": {
"collapsed": false
},
"outputs": [
{
"ename": "TypeError",
"evalue": "__init__() takes exactly 8 arguments (7 given)",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-79-09ff50271d15>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;36m100.05\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0mdate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m2017\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m01\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m01\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 7\u001b[0;31m \u001b[0mdate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m2017\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m01\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m02\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 8\u001b[0m )\n\u001b[1;32m 9\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mTypeError\u001b[0m: __init__() takes exactly 8 arguments (7 given)"
]
}
],
"source": [
"samsung = TradeUnit(\n",
" \"stock\",\n",
" \"Samsung\",\n",
" 30,\n",
" 100.05,\n",
" date(2017, 01, 01),\n",
" date(2017, 01, 02)\n",
")\n",
"\n",
"LG = TradeUnit(\n",
" \"Stock\",\n",
" \"LG\",\n",
" 100,\n",
" 200,\n",
" date(2016, 03, 05),\n",
" date(2016, 12, 25))"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('Before:', True)\n",
"('After:', True)\n"
]
},
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"LG.calc_wash_sale_period()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment